How to find Armstrong number in Java using for loop?

Armstrong Number Program in Java. We are going to write the code in for loop in Java to print Armstrong numbers and with sample outputs.

What is Armstrong Number in Java?

A: An Armstrong number of three digits is an integer, where the sum of the cubes of its digits is equal to the number itself. Let us consider a simple example to understand an Armstrong number in Java. So, consider an example: 371

A: 3^3 + 7^3 + 1^3 = 371 ( If you add all these cubed numbers, the final summed value must be obtained the same as the given number ).

Armstrong Number in Java using for loop
Armstrong Number in Java using for loop

Armstrong Number Simple and Easy Program Code in Java

Following is the Java program for Armstrong numbers between 1 to n. We generate the Armstrong number in Java, we simply create a new class in Java. Then, we write the main method. Inside the public static void main(String[] arg we perform all the calculations of logic like for loop.

import java.util.scanner;
public class Armstrong {
public static void main(String[] arg){
    int a,arm=0,n,temp;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a number");
    n=sc.nextInt();
    temp=n;
    for( ;n!=0;n/=10 ){
    a=n%10;
    arm=arm+(a*a*a);
  }
     if(arm==temp)
     System.out.println(temp+" is a armstrong number ");
     else
     System.out.println(temp+" is not a armstrong number ");
    }
}

This is the sample program to print Armstrong numbers using for loop in Java. You can also check out this program along with the sample output is given below:


The output of Armstrong Number in Java:

Armstrong numbers between 1 to 1000
1
153
370
371

407

Puskar Nath
Puskar Nathhttps://topschoolmba.com
Puskar Nath, a 19-year-old science stream graduate from Himalayan Whitehouse International College, is a budding author with a passion for sharing his thoughts and ideas through writing.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular