Armstrong Number ISC 2019 Theory

Design a class ArmNum to check if a given number is an Armstrong number or not. A number is said to be Armstrong if the sum of its digits raised to the power of length of the number is equal to the number.

Example:
371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.

Some of the members of the class are given below:

Class name: ArmNum
Data members/instance variables:
n: to store the number.
l: to store the length of the number.
Methods/Member Functions:
ArmNum(int num): parameterized constructor to initialize the data member n = num.
int sumPow(int i): returns the sum of each digit raised to the power of the length of the number using recursive technique. E.g. 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong(): checks whether the given number is an Armstrong number by invoking the function sumPow() and displays the result with an appropriate message.

Specify the class ArmNum giving details of the constructor, int sumPow(int) and void isArmstrong(). Define a main() function to create an object and call the functions accordingly to enable the task.

import java.io.*;
class ArmNum{
    private int n;
    private int l;
    public ArmNum(int num){
        n = num;
        l = 0;
        for(int i = n; i != 0; i /= 10)
            l++;
    }
    public int sumPow(int i){
        if(i < 10)
            return (int)Math.pow(i, l);
        return (int)Math.pow(i % 10, l) + sumPow(i / 10);
    }
    public void isArmstrong(){
        if(n == sumPow(n))
            System.out.println(n + " is an Armstrong number.");
        else
            System.out.println(n + " is not an Armstrong number.");
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("N = ");
        int num = Integer.parseInt(br.readLine());
        ArmNum obj = new ArmNum(num);
        obj.isArmstrong();
    }
}

9 thoughts on “Armstrong Number ISC 2019 Theory”

  1. Sir,can we do the prime number using recursive function this way?
    import java.io.*;
    class Prime
    {
    public static void main(String args[])throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print(“N = “);
    int n = Integer.parseInt(br.readLine());

    if(isPrime(n,2)==1)
    System.out.println(n+” is prime”);
    else
    System.out.println(n+” is not prime”);
    }
    public static int isPrime(int num,int f)
    {
    if(num==2)
    return 1;
    else if(num % f == 0 && num > f)
    return 0;
    else if(num == f)
    return 1;
    else if(num<2)
    return 0;
    else{
    f++;
    return isPrime(num,f);
    }
    }
    }

  2. Sir,can we do the prime number using recursive function this way?
    import java.io.*;
    class Prime
    {
    public static void main(String args[])throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print(“N = “);
    int n = Integer.parseInt(br.readLine());
    if(n f)
    return 0;
    else if(num == f)
    return 1;
    else{
    f++;
    return isPrime(num,f);
    }
    }
    }

  3. Sir,2 programs is given which states that :
    1)WAP in Java to accept a string & print all the consecutive and repeated characters present in the string.
    Sample Input: understanding computer science
    Sample Output:
    Consecutive characters:
    d e r s s t
    Repeated characters:
    u n e r s t i c

    2) WAP in Java to accept a number and check whether the number is prime or not by using recursive function

  4. Sir,a program is given which states that
    WAP in JAVA to accept a word in lower case and display the new word after removing all the repeated letters.
    Sample Input : applications
    Sample Output : aplictons

Leave a Reply

%d bloggers like this: