Write a program to find the given number is a perfect number or not. it is the sum of factors ,excluding itself or equal to itself. Ex:- 6 - 1+2+3 =6
Answers
Answered by
0
Answer:
6-6
=0
or
5+5=10 ihad write two types yu choose the right answer
Answered by
5
Question:-
- Write a program to find whether a given number is perfect number or not.
Code:-
A number is said to be a perfect number if the sum of its proper factors is equal to the number itself.
Here is the code given,
import java.util.*;
class Perfect_Number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
int s=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
System.out.println("Given number is a perfect number.");
else
System.out.println("Given number is not a perfect number. ");
}
}
Similar questions