Armstrong Number

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

 import java.util.*;

class armstrong

{//class armstrong opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no

        int k=n,r=0,s=0;//variable k to store the no,varible r to extract the digits,variable s to store the sum

        while(n>0)

        {

            r=n%10;

            s=s+(r*r*r);

            n=n/10;

        }

        if(k==s)

        System.out.println(k+" is an armstrong no");

        else

        System.out.println(k+" is not an armstrong no");

    }//method closes

}//class closes

        


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...