Armstrong Program in Java


Prev Tutorial Next Tutorial

Armstrong number Program in Java

Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself. In java programming you can easily write Armstrong Program in Java just follow same concept of C programming, only need to use syntax of java programming language.

For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634

armstrong number program in java

Armstrong Program in Java

import java.util.Scanner;
class Armstrong 
{
 public static void main(String[] args)
{
int arm=0,a,b,c,d,no; 
Scanner s=new Scanner(System.in);
System.out.println("Enter any num :");
no=s.nextInt();
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
System.out.println("Armstrong :");
}
else
{
System.out.println("not Armstrong");
}
}
}

Output

Enter any num :
153
Armstrong

Syntax to compile and run java program

Syntax

for compile -> c:/>javac Armstrong
for run -> c:/>java Armstrong

Explanation of Code

  • Scanner s=new Scanner(System.in): are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Prev Tutorial Next Tutorial

Advertisements
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826

Google Adsense Advertisements