Write a program in JAVA to enter a number and check and print whether the number is Armstrong or not.
★ A number is said to be Armstrong, if the sum of the cubes of the digits is equal to the original number.
Answers
Answer:-
★ Program to check for Armstrong number in Java:-
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
import java.util.*;
public class Armstrong
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,num,a,s=0;
System.out.println("Enter a Number:");
n=in.nextInt();
num=n;
while(n>0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}
if(num==s)
System.out.println("It is an Armstrong Number");
else
System.out.println("It is NOT an Armstrong Number");
}
}
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
★ Refer to the attachment for Output.
★ Input value in the above Output is 153.
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Explore More:-
✯ Here, is the list of all 3 digit Armstrong numbers.
- 153
- 370
- 371
- 407
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
✯ In 4 digit Armstrong numbers like 1634, 8208 and 9474 the sum of the power of four of the digits is equal to the original number.
Answer:
//Java Program to check if a number is Armstrong number or not.
import java.util.*;
public class Armstrong {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
long n=sc.nextInt(); //Enter a number
int c=0,s=0;long n1=n;
while (n!=0)
{
c++;
n/=10;
}
n=n1;
while (n1!=0)
{
s+=Math.pow(n1%10,c);
n1/=10;
}
if(s==n)
System.out.println("Armstrong");
}
}
Variable Description:-
- n:- to Accept input
- c:- counter variable
- s:- sum variable
- n1:- to make the copy of the original number.