programa Java para imprimir todos los números entre Armstrong rango dado

En este programa Java, vamos a aprender cómo comprobar e imprimir todos los números entre Armstrong rango dado (de serie a partir del número final)?

Teniendo en cuenta una serie de números y tenemos que imprimir todos los números Armstrong entre la gama que utilizan programa Java.

Ejemplo 1:

Enter Starting Number :  100
Enter Ending Number : 1500
Armstrong Numbers Between the Given Interval are :
153 370 371 407

Ejemplo 2:

Enter Starting Number :  1
Enter Ending Number : 150
Armstrong Number not Found between the Given Interval.

programa para imprimir números de Armstrong entre un rango en Java

import java.util.Scanner;
public class GenerateArmstrongNumber
{
public static void main(String args[])
{
int n, n1, n2, i, rem, temp, count=0;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Interval :n");
System.out.print("Enter Starting Number : ");
n1 = scan.nextInt();
System.out.print("Enter Ending Number : ");
n2 = scan.nextInt();
// read numbers one-by-one and generate armstrong.
for(i=n1+1; i<n2; i++)
{
temp = i;
n = 0;
while(temp != 0)
{
rem = temp%10;
n = n + rem*rem*rem;
temp = temp/10;
}
if(i == n)
{
// print all the armstrong number between given interval.
if(count == 0)
{
System.out.print("Armstrong Numbers Between the Given Interval are : n");
}
System.out.print(i + " ");
count++;
}
}
// print if no number found.
if(count == 0)
{
System.out.print("Armstrong Number not Found between the Given Interval.");
}
}
}

salida

First run:
Enter the Interval :
Enter Starting Number : 100
Enter Ending Number : 500
Armstrong Numbers Between the Given Interval are :
153 370 371 407
Second run:
Enter the Interval :
Enter Starting Number : 100
Enter Ending Number : 1500
Armstrong Numbers Between the Given Interval are :
153 370 371 407
Third run:
Enter the Interval :
Enter Starting Number : 1
Enter Ending Number : 150
Armstrong Number not Found between the Given Interval.


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *