Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
6.7k views
in Computer by (79.3k points)

Design a class Perfect to check if a given number is a perfect number or not. [A number is said to be perfect if sum of the factors of the number excluding itself is equal to the original number]

Example : 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself) Some of the members of the class are given below :

Class name                   :                                  Perfect

Data members/instance variables num  :  to store the number

Methods/Member functions  :   Perfect (int nn) parameterized constructor to initialize the data member num=nn

int sum_of_factors (int i)   :  returns the sum of the factors of the number(num), excluding itself, using recursive technique

void check ( )  :   checks whether the given number is perfect by invoking the function sum_of_factors ( ) and displays the result with an appropriate message

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

1 Answer

+2 votes
by (76.4k points)
selected by
 
Best answer

import java. io. *;

class Perfect

{

int num;

Perfect (int nn)

{

num = nn;

} int sum_of_factors (int i)

{

if (num = = i)

return (sum_of_factors (i/2));

else if (i = = 1) return (1);

else if (num % i = = 0)

return (i + sum_of_factors (i – 1));

else return (sum_of_factors (i – 1));

}

void check ( )

{

if (num = = sum_of_factors (num))

System. out. println (" Perfect Number"); else System. out. println ("Not Perfect Number");

}

public static void main (String args [ ] )

{

system. out. println ("Enter one number");

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

int n = Integer parseInt (br. readLine( )); Perfect ob = new Perfect (n);

ob. check ( );

}

}

by (15 points)
edited
Thank you very much aditi.
It is very helpful
by (15 points)
Can you please send me this on my insta id
Official_ankit_63 ( https://instagram.com/officials_ankit_63?igshid=YmMyMTA2M2Y= )

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

...