Write a Java program to find whether given number is Armstrong or not
Armstrong numbers are: 153, 370, 371, 407
Definition: We call a given number as Armstrong if it is equal to the sum of the powers of its own digits.
Ex: abcd...n => pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... =abcd...n
Answer:
package seleniumrepo;
import java.util.Scanner;
class ArmstrongNumber{
public static void main(String args[]){
int n, sum = 0, temp, r;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check if it is an armstrong number");
n = in.nextInt();
temp = n;
while( n!= 0 ){
r = n%10;
sum = sum + r*r*r;
n= n/10;
}
if ( temp == sum )
System.out.println("Entered number is an armstrong number.");
else
System.out.println("Entered number is not an armstrong number.");
in.close();
}
}
Armstrong numbers are: 153, 370, 371, 407
Definition: We call a given number as Armstrong if it is equal to the sum of the powers of its own digits.
Ex: abcd...n => pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... =abcd...n
Answer:
package seleniumrepo;
import java.util.Scanner;
class ArmstrongNumber{
public static void main(String args[]){
int n, sum = 0, temp, r;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check if it is an armstrong number");
n = in.nextInt();
temp = n;
while( n!= 0 ){
r = n%10;
sum = sum + r*r*r;
n= n/10;
}
if ( temp == sum )
System.out.println("Entered number is an armstrong number.");
else
System.out.println("Entered number is not an armstrong number.");
in.close();
}
}
0 comments:
Post a Comment