Java program to print upto N Armstrong numbers



How to print Armstrong number between 1 to n numbers

            An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Refer - Data structure to print Armstrong number

Refer - Java program to find Armstrong number

Java program to print Armstrong number between 1 to 1000

package com.candidjava;

import java.util.Scanner;

public class ArmstrongNumbers
{
	public static void main(String[] args)
	{
		int n, count = 0, a, b, c, sum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the Amstrong No N:");
		int no = sc.nextInt();
		System.out.print("Armstrong numbers from 1 to N:");
		for (int i = 1; i <= no; i++)
		{
			n = i;
			while (n > 0)
			{
				b = n % 10;
				sum = sum + (b * b * b);
				n = n / 10;
			}
			if (sum == i)
			{
				System.out.print(i + " ");
			}
			sum = 0;
		}
	}
}

Output

Enter the Amstrong No N:

500

Armstrong numbers from 1 to N:1 153 370 371 407 



Related Post

Comments


©candidjava.com