0% found this document useful (0 votes)
2 views2 pages

Prime and Perfect Number Program

The document contains Java programs to check if a number is prime or perfect. A prime number is defined as one that has only two factors: 1 and itself, while a perfect number is one where the sum of its proper divisors equals the number itself. Example implementations for both checks are provided in the form of two classes: 'prime_number' and 'perfect_number'.

Uploaded by

utkarshakshat3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Prime and Perfect Number Program

The document contains Java programs to check if a number is prime or perfect. A prime number is defined as one that has only two factors: 1 and itself, while a perfect number is one where the sum of its proper divisors equals the number itself. Example implementations for both checks are provided in the form of two classes: 'prime_number' and 'perfect_number'.

Uploaded by

utkarshakshat3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Program

Ques1:- Input a number and check Input number is prime number or not.
Ans:- Prime Number:- Prime number is a number that is divisible by 1 and itself only.
Whose factor is only two such number is called prime number.
Eg:- 5 factor is 1 and 5

//Printing prime number


class prime_number
{
int no;
void input_value(int n)
{
no=n;
int di=0,fctr=0;
for(di=1;di<=no;di++)
{
if(no%di==0)
{
fctr++;
}
}
if(fctr==2)
{
System.out.print(no+" is a prime number");
}
else
{
System.out.print(no+"is not a prime number");
}
}
}

Ques2:- Input a number and check Input number is Perfect number or not.
Ans:- Perfect number:-A number is said to be a perfect number if the sum of its proper divisors ( i.e.
all positive divisors excluding the number itself )is equal to that number itself.
Eg= 6 factor is 1,2,3 and 6
6=1+2+3=6

//printing perfect number


class perfect_number
{
int no;
void input_value(int n)
{
no=n;
int di=0,factsum=0;
for(di=1;di<no;di++)
{
if(no%di==0)
{
factsum+=di;
}
}
if(factsum==no)
{
System.out.print(no+"is a perfect number");
}
else
{
System.out.print(no+" is not a perfect number");
}
}
}

You might also like