Friday, 31 May 2013

    C program to check a number is perfect number or not

A number which is equal to the sum of its divisor is called perfect number.For eg,divisors of 6 are 1,2 and 3.The sum of these divisors is 6.So 6 is called as perfect number.
#include<stdio.h>
#include<conio.h>


//perfect number checking
void main()
{
    int n,d,sum;
    clrscr();
    printf("\n Enter the number : ");
    scanf("%d",&n);
    sum = 0;

    d = 1;
    while ( d<= n/2)
    {
        if ( n % d == 0 )
        {
            sum = sum + d;
        }
        d++;
    }
    if ( sum == n )
        printf("\n %d is perfect number",n);
    else
        printf("\n %d is not perfect number",n);

  and the output is like:
                                    




 

 

0 Responses to “ ”

Post a Comment

Recent Comments

All Rights Reserved Assignments on C language