C-PROGRAM TO CHECK/DISPALY PERFECT NUMBERS-WITH ALGORITHM AND FLOWCHART

BACKGROUND

According to the number theory,perfect numbers are the numbers that are equal to the sum of it's divisors excluding the number itself.
For example 6 is a perfect number. The divisors of six are 1,2,3 and 6. So, 1+2+3=6.
The sum of the number excluding itself is called aliquot sum. The numbers whch are equal to their aliquot sum are perfect numbers. Also, if we add the number itself in the sum of divisors, the for the perfect numbers the sum become double that numbers.
1+2+3+4+5+6=12=6X2.

FOR LOOP IN C

Since we are going to use for loop in this program, itis necessary for you to know about for loop in C.

Syntax:
 for (initialization;condition;operation)
{
           statements;
}

FLOWCHART
flowchart of for loop in c

ALGORITHM

Step 1: Start
Step 2:  For each numbers find the sum of  the factorials excluding the number itself one by one.
Step 3: Display the number if its is equal to its aliquot sum.
Step 4: Stop

FLOWCHART

flowchart for perfect numbers

SOURCE CODE

#To display perfect numbers between 1 and 100.


#include<stdio.h>
#include<conio.h>
main()
{ 
int i,n,s;

for (n=1;n<=500;n++){    
    s=0;
        for(i=1;i<n;i++)
        {
         if (n%i==0)          
            s=s+i;
            }

if (n==s)
   printf("%d\n",n);
}

getch();
}

OUTPUT
6
28
496

#TO check if given number is perfect or not.


#include<stdio.h>
#include<conio.h>
main()
{ 
int i,n,s=0;
printf("Enter a number");
scanf("%d",&n);
        for(i=1;i<n;i++)
        {
         if (n%i==0)          
            s=s+i;
            }

if (n==s)
   printf("%d is perfect number",n);
else
   printf("%d isn's perfect number");
getch();
}


COMMENT DOWN YOUR DOUBTS 

Post a Comment

0 Comments