Program to display Armstrong number between two intervals in java

 

In this article, you’ll learn to print Armstrong number programming in java language. You may also learn the following programming i.e Armstrong number program between two number in c, cpp and java language or up to N terms with more example in detail.

Q: what is Armstrong number

Ans: A number is Armstrong number if only if “The digit number equal to the sum of the powers of their digit. For example 1,153,1634 etc.

  • 1^3+5^3+3^3=153 is an Armstrong number.
  • 1^4+6^4+3^4+4^4=1634 is an Armstrong number.

Q: Write a program in java to display all Armstrong number between 1 to 500 on the screen.

Solution:

public class Armnum {

public static void main(String args[]) {

 

int n,r,x,s;

System.out.println(“Armstrong number are: “);

for(n=1;n<=500;n++){

 

s=0;

x=n;

while(x!=0){

r=x%10;

s=s+r*r*r;

x=x/10;

}

if(s==n)

System.out.println(n);

}}}

Note: s=s+r*r*r; you may also write this statement in better way like s+=r*r*r;

Output of this program:

Armstrong number are:

1

153

370

371

407

 

Q: Write a program in java language to display all Armstrong number between 1 to 1000 on the screen.

Solution:

public class Armnum {

public static void main(String args[]) {

 

int n,r,x,s;

System.out.println(“Armstrong number are: “);

for(n=1;n<=1000;n++){

 

s=0;

x=n;

while(x!=0){

r=x%10;

s+=r*r*r;

x=x/10;

}

if(s==n)

System.out.println(n);

}}}

 

The output of this program:

Armstrong number are:

1

153

370

371

407

 

Q: Write a program in java language to display all Armstrong number between 1000 to 10000 on the screen.

Solution:

public class Armnum {

public static void main(String args[]) {

 

int n,r,x,s;

System.out.println(“Armstrong number are: “);

for(n=1000;n<=10000;n++){

 

s=0;

x=n;

while(x!=0){

r=x%10;

s+=r*r*r*r;

x=x/10;

}

if(s==n)

System.out.println(n);

}}}

 

The output of this program:

Armstrong number are:

1634

8208

9474

 

Q: Write a program in java language to display all Armstrong number between 10000 to 100000 on the screen.

Solution:

public class Armnum  {

public static void main(String args[]) {

 

int n,r,x,s;

System.out.println(“Armstrong number are: “);

for(n=10000;n<=100000;n++){

 

s=0;

x=n;

while(x!=0){

r=x%10;

s+=r*r*r*r*r;

x=x/10;

}

if(s==n)

System.out.println(n);

}}}

 

The output of this program:

Armstrong number are:

54748

92727

93084

 

You may also learn the following programming:

 

Leave a Reply

Your email address will not be published. Required fields are marked *