Check no is Armstrong or not in java Program


import java.util.Scanner;

 class ArmstrongNumber
 {
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;
Scanner in = new Scanner(System.in);
System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt(); 
temp = n;
// Count number of digits
while (temp != 0) 
{ 
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) 
{ 
remainder = temp%10;
sum = sum + power(remainder, digits); 
temp = temp/10;
}
if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
static int power(int n, int r) 
{
int c, p = 1;
for (c = 1; c <= r; c++) 
p = p*n;
return p;

}
}

Code Breakdown:

1. Importing Scanner Class:

import java.util.Scanner;

This imports the Scanner class, which is used to take input from the user.

2. Class Definition:

class ArmstrongNumber

The class ArmstrongNumber contains the main method, where the program execution starts.

3. Main Method:

public static void main(String args[])

This is the main method, the entry point of the program.

4. Variable Declarations:

int n, sum = 0, temp, remainder, digits = 0;

n: This will hold the number input by the user.

sum: This is used to store the sum of each digit raised to the power of the number of digits.

temp: This is used as a temporary variable to store the value of n for processing.

remainder: This will store the remainder when dividing by 10, which helps in extracting each digit of the number.

digits: This will store the count of digits in the number n.

5. Creating a Scanner Object to Read Input:

Scanner in = new Scanner(System.in);

System.out.println(“Input a number to check if it is an Armstrong number”);

n = in.nextInt();

This creates a Scanner object named in to read user input from the console.

The program prompts the user to input a number, and this number is stored in the variable n.

6. Counting the Number of Digits:

temp = n;

while (temp != 0)

{

digits++;

            temp = temp / 10;

  }

  Here, the program counts the number of digits in the number n by continuously dividing temp by 10 until it becomes 0.

In each iteration, ‘digits’ is incremented, so it eventually holds the total number of digits in the number n.

7. Calculating the Armstrong Sum:

temp = n;

while (temp != 0)

{

      remainder = temp % 10;

      sum = sum + power(remainder, digits);

      temp = temp / 10;

  }

  • After counting the digits, the program resets temp back to n and begins another loop to process each digit of the number.
  • The remainder of temp % 10 gives the last digit of the number. This digit is then raised to the power of digits (using the power() method) and added to sum.
  • After processing the last digit, temp is divided by 10 to remove the last digit, and the loop continues until all digits are processed.

8. Checking if the Number is an Armstrong Number:

if (n == sum)

      System.out.println(n + ” is an Armstrong number.”);

else

      System.out.println(n + ” is not an Armstrong number.”);

  • Once the sum of the powered digits is calculated, the program compares sum with the original number n.
  • If n is equal to sum, the number is an Armstrong number, and the program prints the appropriate message. Otherwise, it prints that the number is not an Armstrong number.

9. The power() Method:

static int power(int n, int r)

{

      int c, p = 1;

      for (c = 1; c <= r; c++)

          p = p * n;

      return p;

  }

  • The power() method calculates the result of raising n to the power of r.
  • It uses a loop to multiply n by itself r times. The result is stored in p, which is returned.

Example Walkthrough:

Let’s walk through the program using the example of the number 153, which is a known Armstrong number (since \( 153 = 1^3 + 5^3 + 3^3 \)).

1. User Input: The user inputs 153.

2. Counting Digits: The number 153 has 3 digits.

3. Sum Calculation:

  • For the first digit 1: \( 1^3 = 1 \)
  • For the second digit 5: \( 5^3 = 125 \)
  • For the third digit 3: \( 3^3 = 27 \)
  • Sum = \( 1 + 125 + 27 = 153 \)

4. Comparison: Since 153 == 153, the program prints that 153 is an Armstrong number.

Sample Input and Output:

Input:

Input a number to check if it is an Armstrong number

153

Output:

153 is an Armstrong number.

Another Example:

Input:

Input a number to check if it is an Armstrong number

123

Output:

123 is not an Armstrong number.

Explanation of Output:

  • For 153, the sum of the cubes of its digits (i.e., \( 1^3 + 5^3 + 3^3 = 153 \)) equals the number itself, so it is an Armstrong number.
  • For 123, the sum of the cubes of its digits (i.e., \( 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36 \)) does not equal the number, so it is not an Armstrong number.

Summary:

1. Armstrong Number: An Armstrong number for a given number of digits is a number that is equal to the sum of its digits each raised to the power of the number of digits.

2. The program reads a number, counts its digits, calculates the sum of each digit raised to the power of the number of digits, and checks if the sum matches the original number.

3. The result is printed as either “is an Armstrong number” or “is not an Armstrong number”.

Leave a Comment

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