Java program to print upto N Armstrong numbers


Program

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