Find out the perfect number using c program






Code 1:
1. C program to check perfect number

#include<stdio.h>
int main(){
  int n,i=1,sum=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

  return 0;
}

Sample output:
Enter a number: 6
6 is a perfect number

Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language

#include<stdio.h>
int main(){
  int n,i,sum;
  int min,max;

  printf("Enter the minimum range: ");
  scanf("%d",&min);

  printf("Enter the maximum range: ");
  scanf("%d",&max);

  printf("Perfect numbers in given range is: ");
  for(n=min;n<=max;n++){
    i=1;
    sum = 0;

    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }

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

  return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6

Code 3:
3. C program to print perfect numbers from 1 to 100

#include<stdio.h>
int main(){
  int n,i,sum;
 
  printf("Perfect numbers are: ");
  for(n=1;n<=100;n++){
    i=1;
    sum = 0;

    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }

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

  return 0;
}

Output:
Perfect numbers are: 6 28





Definition of perfect number or What is perfect number? 

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128








10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
15. Write a c program to solve quadratic equation.
18. Write a c program which passes structure to function.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.

18 comments:

Anonymous said...

thank u for ur program.

vernika said...

This site is really good I visit this site eneryday.Good questions.The best part is it provides explanation to every question.Best site.
Thanks alot

Anonymous said...

this site is undoubtedly a super duper excellent site to let anyone quench his thirst for c programming logics and concepts! hats offff!

Anonymous said...

IT IS BETTER TO PUT OUTPUT OF PARTICULAR PROGRAM....

Anonymous said...

this is a great site had much information for beginners to experienced .....

vigneshwaran said...

super,best site for all new learners

Raj said...

lets work out this with example now suppose
for n=1
1<1 does not satisfy
for n=2
1<2 satisfies
2%1==0 satisfies
sum=0+1=1
i++(so i is 2 now)
2<2 does not satisfy
for n=3
while 2<3 satisfies
3%2==0 does not satisfy
for n=4
while 2<4 satisfies
2%4==0 satisfies
sum=1+2=3
i=3(i++ i.e i=2+1=3)
for n=5
while 3<5 satisfies
5%3==0 does not satisfy
for n=6
while 3<6 satisfies
6%3==0 satisfies
sum=3+3=6
So here Sum==n satisfies so 6 is a perfect number...
Rocked it everyone can easily understand now

Anonymous said...

It would be better to initialize Sum with 1 and i with 2.

Arpan Patel said...

Thankx. . .for more description on Prefect No. . .just wiki on: http://en.wikipedia.org/wiki/Perfect_number

DonKiller said...

brilliant

sandeep said...

thanks

Priyanka said...

luv dis site...\m/\m/\m/

Anonymous said...

nice blog for studentsthx agian

clover said...

give the answer for question --------------10. Write a c program to add two numbers without using addition operator.???

LBEF SMU DE's IT student Zone said...

Thank you very much. This tutorial have helped me complete my university Assignments and Lab reports in time.

Anonymous said...

Thank u.....

Anonymous said...

Best site for a computer engineering student like me. Thanks a lot.....
Parveen Arora

(( * sofi *)) said...

really i am very much thankful to u...its very much helpful to me..thanks a lot.

C compiler: gcc 4.1.2