KEMBAR78
Program 16 AMSTRONG NUMBER | PDF
0% found this document useful (0 votes)
44 views5 pages

Program 16 AMSTRONG NUMBER

Uploaded by

aliasgarop123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Program 16 AMSTRONG NUMBER

Uploaded by

aliasgarop123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 16 : AMSTRONG

_____________________________________________
import java.util.Scanner;

public class ArmstrongNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();

int originalNumber = number;


int sum = 0;
int digits = Integer.toString(number).length();

while (number > 0) {


int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}

if (sum == originalNumber) {
System.out.println(originalNumber + " is an
Armstrong number.");
} else {
System.out.println(originalNumber + " is not an
Armstrong number.");
}
}
}
ALGORITHM
_____________________________________________

 Input the number.


 Initialize sum to 0.
 Calculate the number of digits in the number.
 Extract each digit and raise it to the power equal to
the number of digits.
 Add the result to sum.
 Repeat steps 5-6 until all digits are processed.
 If sum equals the original number, it is an Armstrong
number; otherwise, it is not.
OUTPUT
_____________________________________________
Variable Table
_____________________________________________

You might also like