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 = 3^{3} + 7^{3} + 1^{3}

1634 = 1^{4} + 6^{4} + 3^{4} + 4^{4}

54748 = 5^{5} + 4^{5} + 7^{5} + 4^{5} + 8^{5}

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 3^{2} + 4^{2} (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();
}
}
```

## 13 replies on “Armstrong Number ISC 2019 Theory”

Sir there is a problem in compilation of program in psvmain block

Hi Muskan, I have checked the program. It is executing without any errors. You can copy the code and execute.

Sir,2 programs is given which states that:

1) A financial Institution has only two kinds of accounts,Simple & Compound.

The class Account has two protected attributes accountNumber (+ve int) and principal ( a double precision real number to store the money deposited initially).The interest for a Simple account is calculated by using the simple interest formula:

SI=PRT/100

The interest for a Compound account is calculated by using the compound interest formula:

CI=P[1+/100]^T-P

Rate & Time for two kinds of account may vary.

Model the above situation by defining classes Account,Simple & Compound where Simple & Compound are derived classes from class Account.

Write appropriate constructors for passing various attributes for the three classes.

Write the functions double interest() that calculate appropriate interest and return interest for the class Simple & Compound.

Also write the functions display() for the three classes which printout all the attribute values as

account number=……………..

principal=……………….

rate=………………

time=………………

interest=………………

with each attribute on a single line.Make sure that each display() function is correctly placed in the appropriate class.NO NEED of writing Main().

2) A class Personal contains employee details and another class Retire calculates the employee’s Provident Fund and Gratuity.

The details of the 2 classes are given below:

Class Name : Personal

Name : stores the employee name

Pan : stores the employee pan number

basic pay : stores the employee basic salary(in decimals)

acc no : stores the employee bank account number

Member Functions :

Personal(…) : parameterized constructor to assign value to data members

void display() : to display the employee details

Class Name : Retire

Yrs : stores the employee years of service

Pf : stores the employee provident fund amount(in decimals)

Grat : stores the employee gratuity amount(in decimals)

Member Functions :

Retire(…) : parameterized constructor to assign value to data members of

both the classes

void provident() : calculates the PF as (2% of the basic pay) * years of service

void gratuity() : calculates the gratuity as 12 months salary,if the years of service

is more than or equal to 10 years else the gratuity amount is nil.

void display1() : Displays the employee details along with the PF(Provident Fund)

and gratuity amount.

Specify the class Personal giving details of the constructor and member functions

void display().Using the concept of Inheritance,specify the class Retire giving details of a constructor,and the member functions void provident(),void gratuity() and the void display1().The main() NEED NOT be written.

Send only those questions that are not in your project.

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);

}

}

}

Yes, looks correct. Try with some sample inputs to verify.

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);

}

}

}

The first “if” condition is wrong: if(n f)

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

Here is the link to your program that displays consecutive and repeated characters in a string.

Here is a program to check for Emirp numbers that has a recursive mehod to check for prime numbers. Refer to that program to get access to that recursive method.

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

Here is the program to remove repeated characters from a given string:

https://www.happycompiler.com/remove-repeated-letters-string/