English

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

Advertisements
Advertisements

Question

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
Code Writing
Advertisements

Solution

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
  Is there an error in this question or solution?
Chapter 5: Library Classes - Exercises [Page 315]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 5 Library Classes
Exercises | Q 6. | Page 315
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×