Program for perfect numbers

Thursday, December 4th, 2008

Here is a program to find whether the entered number is a perfect or not. A perfect number is a one, whose sum of devisors is equals the number itself.

Logic: Here in the program, user need to enter the number to check if it is perfect. The for loop in the program traces the iteration till the number, while in each of iteration it checks the present number is the divisor of the entered or not. If it is, it adds it to the flag, which holds the sum of all the divisors. Finally, it checks if the sum equals the given number.

The same algorithm is modified slightly, and developed a program to print all the perfect number till the user defined range.

Program to find whether a number is a perfect or not

#include<stdio.h>
#include<math.h>
void main()
{
int i ,n, sum = 0;
clrscr();
printf(“nnt ENTER A NUMBER…: “);
scanf(“%d”, &n);
for(i=1; i<n; i++)
if(n%i == 0)
sum = sum + i;
if(sum == n)
printf(“nnt THE NUMBER %d IS A PERFECT NUMBER”, n);
else
printf(“nnt THE NUMBER %d IS NOT A PERFECT NUMBER”, n);
getch();
}

Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 2 Comments


2 Responses to “Program for perfect numbers”

  1. fin says:

    hye. can you help me. i’ve been searching through out the internet but there is no tutorial about my problem.i want to take out prime numbers from a sequence of numbers. i already did the command for producing 100 sequence of random numbers. the next task is to take out prime numbers from the sequence of numbers. do know how to do it? i saw there’s many tutorial to find out prime numbers by given number, but mine is from sequence number. can you please help me……..TT

  2. fin says:

    im sorry. i’m suppose to comment on “program for prime numbers”…

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup