Slip No 3 Q A

Q.Write a ‘java’ program to check whether given number is Armstrong or not.(Use static keyword)[15 M]

import java.util.Scanner;

public class ArmstrongStatic {

    // Static method to check Armstrong number
    static boolean isArmstrong(int num) {
        int original = num;
        int sum = 0, rem;

        while(num > 0) {
            rem = num % 10;
            sum = sum + (rem * rem * rem);   // cube of digit
            num = num / 10;
        }

        return sum == original;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        if(isArmstrong(n))
            System.out.println(n + " is an Armstrong number.");
        else
            System.out.println(n + " is not an Armstrong number.");

        sc.close();
    }
}
Spread the love

Leave a Comment

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

Scroll to Top