package org.interview.test;
import java.util.Scanner;
public class ArmstrongNumber {
/*
* An Armstrong number of three digits is an integer such that the sum of the cubes of its
* digits is equal to the number itself.
* For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
*/
public static void main (String [] args ) throws Exception {
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;
System.out.println("temp:"+temp);
int c =1;
while( temp != 0 ) {
System.out.println(c+".temp:"+temp);
r = temp%10;
System.out.println(c+".r:"+r);
sum = sum + r*r*r;
System.out.println(c+".sum:"+sum);
temp = temp/10;
System.out.println(c+".temp:"+temp);
c++;
}
if ( n == sum )
System.out.println("Entered number is an armstrong number.");
else
System.out.println("Entered number is not an armstrong number.");
}
}
import java.util.Scanner;
public class ArmstrongNumber {
/*
* An Armstrong number of three digits is an integer such that the sum of the cubes of its
* digits is equal to the number itself.
* For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
*/
public static void main (String [] args ) throws Exception {
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;
System.out.println("temp:"+temp);
int c =1;
while( temp != 0 ) {
System.out.println(c+".temp:"+temp);
r = temp%10;
System.out.println(c+".r:"+r);
sum = sum + r*r*r;
System.out.println(c+".sum:"+sum);
temp = temp/10;
System.out.println(c+".temp:"+temp);
c++;
}
if ( n == sum )
System.out.println("Entered number is an armstrong number.");
else
System.out.println("Entered number is not an armstrong number.");
}
}
No comments:
Post a Comment