Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
13.4k views
in Computer by (49.0k points)
closed by

Design a class ArmNum to check if a given number is an Armstrong number or not. [A number is said to be Armstrong if 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 

1: to store the length of the number Methods/Member functions:

ArmNum (int nn): parameterized constructor to initialize the data member n = nn int sum_pow(int i): returns the sum of each digit raised to the power of the length of the number using recursive technique eg., 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 sum_pow () and displays the result with an appropriate 

message.

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

1 Answer

+1 vote
by (49.9k points)
selected by
 
Best answer

import java.io.*; 

import javautil.*; 

class ArmNum 

private int n; 

private int 1; 

public ArmNum(int num) 

n = num; 

i = 0; 

for(int i = n; i! = 0; i/= 10) i++; 

public int sumPow(int i) 

{ if(i < 10) 

return (int)Math.pow(i, 1); 

return (int)Math.pow(i % 10, 1) + 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{ 

Scanner sc = new Scanner(System.in); 

System.out.print("N ="); 

int num = sc.nextInt(); 

ArmNum obj = new ArmNum(num); 

obj.isArmstrong(); 

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...