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
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply