C Program to find Perfect Numbers between intervals given by user - wordsclank.in

Friday, 13 April 2018

C Program to find Perfect Numbers between intervals given by user

What is Perfect Number? 


www.wordsclank.inperfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).


Also, a perfect number is that number whose sum of its proper positive divisor(including the number itself) is equals to the twice the number.


For example 28,
divisor of 28:1,2,4,7,14,28

sum of divisors(excluding the number itself):1+2+4+7+14=28 i.e, equals to the number 28.

or, sum of divisors(including the number itself):1+2+4+7+14+28=56 i.e, equals to twice the number(2 * 28 = 56).


Logic behind the program:

  • Take the two intervals from the user.
  • Run a for loop from i to last interval, where i is first interval and increment  i by 1 in each iteration.
  • Withing that for loop run another for loop which will find the perfect numbers and print it.

Similar Problem:


Problem Statement:

Write a program in C which will find all the perfect numbers between two intervals given by the user.






Source Code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include

void main()

 {

   int i, j, beg, end, sum;

   printf("Enter the first interval:");

   scanf("%d", &beg);
   
   printf("Enter the last interval:");

   scanf("%d", &end);
   

   for(i = beg; i<=end; i++)

   {
     sum = 0;
     
     for(j=1; j<i; j++)
     
     {

   if(i % j == 0)

                 sum = sum + j;
          
             }

     if( sum == i) 

             {

                  printf("%d \t",i);

             }
}


 }

Output:



More Topics on C program


No comments:

Post a Comment