Program to Check Armstrong Number using Scanner Class in JAVA - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Friday, July 3, 2020

Program to Check Armstrong Number using Scanner Class in JAVA

Write a program to check weather number is Armstrong or not

Program without using user input (Scanner Class):

public class ArmstrongNumber {
    public static void main(String[] args) {
        int c=0,a,temp;
        int n=153;//It is the number to check armstrong
        temp=n;
        while(n>0)
        {
            a=n%10;
            n=n/10;
            c=c+(a*a*a);
        }
        if(temp==c)
            System.out.println("armstrong number");
        else
            System.out.println("Not armstrong number");
    }
}
Output : 

Program using user input (Scanner Class): 

public class ArmstrongNumber {
    public static void main(String[] args) {
        int c=0,a,temp;
        int n;
        System.out.println("Enter the number");
        Scanner scanner=new Scanner(System.in);
        n=scanner.nextInt();
        temp=n;
        while(n>0)
        {
            a=n%10;
            n=n/10;
            c=c+(a*a*a);
        }
        if(temp==c)
            System.out.println("this number is armstrong number");
        else
            System.out.println("this number is not armstrong number");
    }
}
Output : 







Start with your Choice
-Important Programs for Freshers
Sorting and Searching Programs
Learn Pattern Programs 
Click to learn Array Programs
Click to Learn MySQL 
for any complaint regarding my Blog please visit contact us page and write what problem you have!

Learn with us | HTML | CSS | JS | Bootstrap | JAVA | ADV JAVA | MySQL | GIT

No comments:

Pages