Perfect Number | single number | 1 to 100 | in c# with source code | output with explanation
In this program i have shown how to check whether entered number is perfect number or not.
Logic i have developed is so simple .Anyone can learn easily.
so first i will show you how to check perfect number logic for single number.
Definition of perfect number:- Perfect number is number which is equal to its factor's addition.
example 6 has 3 factor 3 and 2 and 1 now if i will add 1 +2+ 3=6 which is number itself.so , we can say
6 is perfect number.
now check for 8.
1,2,2,2 here if i will add 4 number i will get 1+2+2+2=7 which is not equal to 8.so, 8 is not perfect number.
Here is source code in c# .similar code we can write in c and other language just change the
printing syntax.
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("</br>");
Response.Write("How to check The given number is Perfect number or not");
Response.Write("</br>");
Response.Write("----------------------------------------------------------");
Response.Write("</br>");
int num = 10;
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0) // finding its factor
{
sum = sum + i;
}
}
if(sum==num)
{
Response.Write (num+ "is Perfect Number ");
}
else
{
Response.Write (num+ " is Not Perfect Number ");
}
}
output:-
How to check The given number is Perfect number or not
----------------------------------------------------------
10 is Not Perfect Number
Now i want to print list of perfect number from 1 to 100 .
logic will be same just include one more for loop.
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("</br>");
Response.Write ("Print List oF perfect Number from 1 to 100");
Response.Write("</br>");
Response.Write("----------------------------------------------");
Response.Write("</br>");
int num = 100;
int sum = 0;
for (int j = num; j > 0;)
{
for (int i = 1; i < j; i++)
{
if (j % i == 0)
{
sum = sum + i;
}
}
if (j == sum)
{
Response.Write(j + " is Perfect Number");
Response.Write("</br>");
}
j--;
sum = 0;
}
}
output:-
Print List oF perfect Number from 1 to 100
----------------------------------------------
28 is Perfect Number
6 is Perfect Number