Write a program to find the Armstrong number using control structures.

0

Assignment: Module 03- Part 01

Write a program to find the Armstrong number using control structures.  e.g (1*1*1)+(5*5*5*)+(3*3*3)   = 153
Assignment: Module 03- Part 01

05. Write a program to find the Armstrong number using control structures.

e.g (1*1*1)+(5*5*5*)+(3*3*3)   = 153

Input : 153         

Output: It is a Armstrong number   

Input: 210 

Output: It is not a Armstrong number

Here is a Java program that finds whether a given number is an Armstrong number or not:

Copy code:

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 num = sc.nextInt();
        int originalNum = num;
        int result = 0;
        int n = 0;

        // Counting number of digits
        for (; num != 0; n++) {
            num /= 10;
        }

        num = originalNum;
        for (; num != 0; num /= 10) {
            int digit = num % 10;
            result += Math.pow(digit, n);
        }

        if (result == originalNum) {
            System.out.println(originalNum + " is an Armstrong number.");
        } else {
            System.out.println(originalNum + " is not an Armstrong number.");
        }
    }
} 

You can run this program and input the number you want to check. The program will then output whether the number is an Armstrong number or not.

👉Write a class SwitchCaseExample to display the color using switch case Read the character from the user.👈

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates