Perfect Number in C Programming. || Learning Duniya.
What will be the programming for Perfect Number ?
Today topic is Perfect Number. We discuss about Perfect Number and its logic how to find perfect number by programming and we will also write a program to find Perfect Number and also see the output that is it number is Perfect Number or not.
What is Perfect Number?
Perfect Number are those number whose sum of its postie divisors except that number is equal to that number. Means that a number having its all factor except that number and we add all factor then the number must be equal to that number then its is called Perfect Number.
Example:--
For example, let we take a number 6 and we find it factor then all factor are:- 1,2,3,6. and we have to add all factor except that number then sum will be:- 1+2+3 = 6 , which is equal to original number. Then 6 is Perfect Number.
Programming :-
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i, newNum = 0; //Data type deceleration.
printf(" Enter teh number to check either it is Perfect Number or not");
scanf("%d",&num); //User input.
for(i=1; i<num; i++) //For finding divisor.
{
if(num%i == 0) //Condition for divisor
{
newNum = newNum + i; //Adding divisor
}
}
if(newNum == num) //Comparing the new number to original Number.
{
printf(" %d is a Perfect Number",num);
}
else
{
printf(" %d is not a Perfect Number",num);
}
getch();
return 0;
}
Output:-
Logic:-
For finding Perfect Number we have see the definition of the Perfect Number. As definition of Perfect Number we have to find the divisor of that number or factor of that number. After that we will add the all the factor except that number and compare with that number. For finding factor or divisor we have use for loop and if condition for finding factor. After that, by using If - Else we are comparing with the new number to original number, if both number are equal then number is Perfect Number other wise not.
If you face any problem then comment in comment section or visit my YouTube Channel to understand the logic of different type of programming. All videos are in Hindi language.
Bhot badhiya h 👍👍👌👌
ReplyDeletenice
ReplyDeletegreat
ReplyDelete