Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


bubaiii
java
2 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


Write a program ‚??checkHighestArmstrong‚?? to find the highest Armstrong number in a given array of integers. If there is no Armstrong number in the input array, then return 0. Armstrong numbers are those numbers whose sum of their own digits to the power of the number of digits is equal to the digit itself. Example: 1634 = 1^4 + 6^4 + 3^4 + 4^4 (i.e. sum of 1 to the power of 4, 6 to the power of 4,3 to the power of 4 and 4 to the power of 4 is 1634) Hence 1634 is an Armstrong number. The input array should contain only 5 positive integers between 100 to 99999999 (both inclusive). Contract: checkHighestArmstrong (int[] inputArray) -> int Test case: int[] inputArray = {1631,1634,1599,1789,14784}; checkHighestArmstrong (inputArray) -> 1634 For any error in business validations (i.e., if the input array does not contain 5 elements or if any element of the array is not between 100 to 99999999), return message should be -1.


Advertisement
View Answers

February 25, 2011 at 11:15 AM


 import java.lang.Math;

    class arm{
    public static void main(String atgs[]){
    int array[]={1631,1634,1599,1789,14784};
    int a[]=new int[5];
    for(int i=0;i<5;i++){
    int num=array[i];
    int n=num;
    int no=num;
    int sum=0;
    int count=0;

    while(num>=1){
    num/=10;
    count++;
    }
    while(no>=1){
    int rem=no%10;
    no/=10;
     sum+=Math.pow(rem,count);
    }
    if(n==sum){
    System.out.println("yes");
    a[i]=n;
    }
    else
        a[i]=0;
    }
    int max=a[0];
    for(int i=1;i<5;i++){
        if(a[i]>max)
        max=a[i];


    System.out.println("max="+max);
    }

    }


February 25, 2011 at 11:27 AM


Java check highest Armstrong Number

import java.util.*;
class CheckHighestArmstrong
{
    public static void main(String[] args) 
    {
        int max = Integer.MIN_VALUE;
     Scanner input=new Scanner(System.in);
     System.out.println("Enter Array Elements between 100 and 99999999: ");
     int arr[]=new int[5];
     for(int i=0;i<arr.length;i++){
         int num=input.nextInt();
         if((num<100)||(num>99999999)){
             System.out.print("Re-enter Numbers.It is out of range.");
             num=input.nextInt();
             arr[i]=num;
         }
         else{
             arr[i]=num;
         }
     }
     for(int i=0;i<arr.length;i++){
         int k=arr[i];
     int n=k;
int d=0,s=0;
while(n>0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(k==s){
if (k > max) {
        max = k;
               }
}
else{
}
}
System.out.println(max);
    }
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  why to set classpath in java
Advertisements
 

 

 

DMCA.com