java program to display armstrong numbers within a range - t4t

t4t

we give you solutions

Home Top Ad

Responsive Ads Here

Saturday, 28 September 2019

java program to display armstrong numbers within a range


Learn java


Armstrong number is a number that is equal to the sum of cubes of its digits.For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
    
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153


Let's see the java program to find Armstrong Number.

import java.util.Scanner;

public class Amstrong
{
public static void main(String args[])
{
int num, start, end, i, rem, temp, counter=0;

Scanner scanner = new Scanner(System.in);
System.out.print("Enter the start number: ");
start = scanner.nextInt();
System.out.print("Enter the end number: ");
end = scanner.nextInt();
scanner.close();

//generate Armstrong numbers between start and end
for(i=start+1; i < end; i++)
{
temp = i;
num = 0;
while(temp != 0)
{
rem = temp%10;
num = num + rem*rem*rem;
temp = temp/10;
}
if(i == num)
{
if(counter == 0)
{
System.out.print("Armstrong Numbers Between "+start+" and "+end+": ");
}
System.out.print(i + " ");
counter++;
}
}
// if no Armstrong number is found
if(counter == 0)
{
System.out.print("There is no Armstrong number Between "+start+" and "+end);
}
}
}

Output


if you guys want us to post more about programming or codes please let us know either in the comment box or just mail us, so that we can start right away if you are interested.

No comments:

Post a Comment