Advertisements
Advertisements
प्रश्न
Define a class to accept a 4-digit number and check whether it is an Armstrong number or not.
Note: A number is an Armstrong number if the sum of all its digits to the power of the total number of digits in that number equals the number itself.
| Example 1: | |
| Input: 154 Output: Invali |
|
| Example 2: | |
| Input: 1634 Output: Armstrong number |
कोड लेखन
Advertisements
उत्तर
import java.util.Scanner;
class Armstrong
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a 4-digit number");
int n=sc.nextInt();
if(n>=1000 &&n<=9999)
{
int sum=0;
int t=n;
while(t>0)
{
sum+=(int)Math.pow(t%10,4);
t=t/10;
{
if(sum==n)
System.out.println("Armstrong no.");
else
System.out.println("Non armstron no.");
}
else
System.out.println("Invalid");
}
}shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
