Write a program to input a number and print if it's Armstrong or not.
Java
Answers
Answer:
import java.util.Scanner ;
public class Armstrong
{
public static void main (String args[ ] )
{
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter a 3digit number to be checked:") ;
int num= sc.nextInt( ) ;
int check = 0, remainder;
do {
remainder = n % 10 ;
check = check + (int)Math.pow(remainder,3) ;
n = n / 10 ;
} while (n != 0) ;
if (check = = num )
System.out.println(num + "is an Armstrong Number") ;
else
System.out.println(num + "is not a Armstrong Number") ;
}
}
Example output:
Enter a 3 digit number to be checked : 345
345 is not a Armstrong number
import java.util.Scanner ;
public class Armstrong
{
public static void main (String args[ ] )
{
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter a 3digit number to be checked:") ;
int num= sc.nextInt( ) ;
int check = 0, remainder;
do {
remainder = n % 10 ;
check = check + (int)Math.pow(remainder,3) ;
n = n / 10 ;
} while (n != 0) ;
if (check = = num )
System.out.println(num + "is an Armstrong Number") ;
else
System.out.println(num + "is not a Armstrong Number") ;
}
}