Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Java » Programs » Basic » Java program to check whether the number is an armstrong number or not
Suryateja Pericherla Categories: Basic. No Comments on Java program to check whether the number is an armstrong number or not
0
(0)

In this article we will learn to implement a Java program to check whether the number is an armstrong number or not.

 

Armstrong number is one in which the sum of the cubes of individual digits is same as the given number. For example if you take the number 153, (1^3) + (5^3) + (3^3) = 153.

 

Following program reads a number from the user and finds out whether it is an Armstrong number or not:

import java.util.Scanner;

public class Driver
{	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter a number: ");
		int n = input.nextInt();
		int dup = n;
		int sum = 0, rem;
		while(n != 0)
		{
			rem = n % 10;
			sum = sum + (rem * rem * rem);
			n = n / 10;
		}
		if(sum == dup)
			System.out.println("Given number is an Armstrong number");
		else
			System.out.println("Given number is not an Armstrong number");
		input.close();
	}
}

 

Input and output for the above program are as follows:

Enter a number: 
153
Given number is an Armstrong number

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory