Adsense 728 * 15

Search

Custom Search

Print Perfect numbers between 1 to n in C, C++

Write a C, C++ program to print perfect numbers between 1 to 100. Write a program to print perfect number between 1 to 1000 or 1 to n.

In my previous post, i have explained what is perfect number and how to check whether entered number is perfect or not.

Program to check whether input number is perfect or not

Print factors of a number

Programming questions on strings

C Program to Print Perfect numbers from 1 to 100


#include <stdio.h>

int main() {

 int k;
 
 /* Run loop from 1 to 100. */

 for(k = 1;k <= 100; k++){

  /* Initialize value. */
 
   int i=1,sum=0;
    
   while(i < k){
        
      if(k % i == 0) {
          sum = sum + i;
      }
      
      i++;
    }

           
    if(sum == k){
     
      printf("\n %d ",i);
    
    } 
 }  

 return 0;
}


Output : 6  28

C Program to Print Perfect numbers from 1 to 1000

#include <stdio.h>

int main() {

 int k;
 
 /* Run loop from 1 to 1000. */

 for(k = 1; k <= 1000; k++){

  /* Initialize value. */
 
   int i=1,sum=0;
    
   while(i < k){
        
      if(k % i==0){
          sum = sum + i;
      }
      
      i++;
    }

           
    if(sum == k){
     
      printf("\n %d ",i);
    
    } 
 }  

 return 0;
}


Output : 6 28 496

Similarly to print perfect numbers between 1 to n. Take a value of n as input from user. Put the same logic which we have written in above code.

No comments:

Post a Comment