Q:

Write a C Program to find whether number is perfect or not using recursion

0

Write a C Program to find whether number is perfect or not using recursion. Here’s simple Program to find check whether entered number is perfect or not using recursion in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Perfect number


A perfect number is a positive number that equals the sum of its divisors, excluding itself. This is also known as its aliquot sum. At this time, it is unknown how many perfect numbers truly exist in our number system.

 
 

While we have discovered 48 perfect numbers, the fact that there are an infinite number of prime numbers leads us to believe that there could be an infinite number of perfect numbers.


Perfect number are positive integers n such that

 n=s(n),  

where s(n) is the restricted divisor function (i.e., the sum of proper divisors of n), or equivalently

 
 sigma(n)=2n,  

where sigma(n) is the divisor function (i.e., the sum of divisors of n including n itself). For example, the first few perfect numbers are 6, 28, 496, 8128, … (OEIS A000396), since

6 = 1+2+3  
28 = 1+2+4+7+14  
496 = 1+2+4+8+16+31+62+124+248,  

etc.

The nth perfect number is implemented in the Wolfram Language as PerfectNumber[n] and checking to see if k is a perfect number as PerfectNumberQ[k].

The first few perfect number Pn are summarized in the following table together with their corresponding indices p (see below).

n p_n P_n
1 2 6
2 3 28
3 5 496
4 7 8128
5 13 33550336
6 17 8589869056
7 19 137438691328
8 31 2305843008139952128

Below is the source code for C Program to find whether number is perfect or not using recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program to find whether number is perfect or not using recursion */


#include<stdio.h>
int sumdiv(int num, int x);

int main()
{
        int num;
        printf("Enter a number :");
        scanf("%d",&num);
        if(sumdiv(num, num/2) == num)
                printf("\nPerfect Number\n");
        else
                printf("\nNot Perfect Number\n");
}

sumdiv(int num, int x)
{
        if(x==1)
                return 1;
        if(num%x==0)/*if x is a proper divisor*/
                return x + sumdiv(num,x-1);
        else
                return sumdiv(num,x-1);
}

OUTPUT : :


*************** OUTPUT **************


**************** FIRST RUN : ********

Enter a number :200

Not Perfect Number



**************** SECOND RUN : ********


Enter a number :6

Perfect Number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Recursion Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to Find sum of proper divisors o... >>
<< Write a C Program to count prime numbers and displ...