Tuesday, January 29, 2019

Write a Java program to find whether given number is Armstrong or not
Armstrong numbers are: 153, 370, 371, 407
Definition: We call a given number as Armstrong if it is equal to the sum of the powers of its own digits.
Ex: abcd...n => pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... =abcd...n
Answer:
package seleniumrepo;

import java.util.Scanner;
class ArmstrongNumber{
          public static void main(String args[]){
                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;
                while( n!= 0 ){
                       r = n%10;
                       sum = sum + r*r*r;
                       n= n/10;
                 }
                 if ( temp == sum )
                       System.out.println("Entered number is an armstrong number.");
                  else
                       System.out.println("Entered number is not an armstrong number."); 
   
                 in.close();
         }
}


0 comments:

Post a Comment

selenium-repo by venu

Blog helps to a student or IT employee to develop or improve skills in Software Testing.
For Online Classes Email us: gadiparthi122@mail.com

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts