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.

25 comments:

  1. thank u for ur program.

    ReplyDelete
  2. 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

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

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

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

    ReplyDelete
  6. super,best site for all new learners

    ReplyDelete
  7. 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

    ReplyDelete
    Replies
    1. Plz tell me how to use this site???????

      Delete
    2. Nice one raj

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

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

    ReplyDelete
  10. luv dis site...\m/\m/\m/

    ReplyDelete
  11. nice blog for studentsthx agian

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

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

    ReplyDelete
  14. Thank u.....

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

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

    ReplyDelete
  17. sir should we have to take the sum of values of i for the perfect no...

    ReplyDelete
  18. This site is really good.Good questions.The best part is it provides explanation to every question. site had much information for beginners

    ReplyDelete
  19. /* reverse of the value in using the program*/
    #include
    #include
    void main()
    {
    int a[15][15],n,i,j;
    printf("Entr the number of the n=>");
    scanf("%d",&n);
    printf("Enter the number you want to insert=>");
    for(i=0;i0;i--)
    {
    for(j=n;j>0;j--)
    {
    printf("%d",a[i][j]);
    }
    getch();
    }
    }
    ____________________________________________________________________________________________________________________
    output
    _______
    inter the value of n=2
    value= 42,43,44,46;
    output is=46,44,43,42 is reverse order;

    ReplyDelete
  20. these are the question in c asked by the interviewer's so friends utilize this site.
    this is a great job

    ReplyDelete

Share It