Logic:
- Perfect Number A number is perfect if sum of its divisors other than the number is the number itself.
- Enter the number n
- Example 1: n=6
- We will check whether number is divisible by any number from 1 to n-1 here 5.
- 6 is divisibly by 1,2,3 (Don’t include number itself ie. 6 in its divisors)
- sum=1 + 2 + 3 =6=number itself
- So It is a perfect number
- Example 2: n=7
- We will check whether number is divisible by any number from 2 to n-1 here 6.
- 7 is divisibly by 1 only (Don’t include number itself ie. 7 in its divisors)
- sum=1 which is not equal to 7
- So It is not a perfect number
Algorithm
- Enter the number as n
- for (i = 1; i <= n – 1; i++)
- if (n % i == 0)
- s = s + i;
- if (s == n)
- printf(“It is perfect number”);
- else
- printf(“It is not perfect number”);
Program
#include <stdio.h> int main() { printf("****************************************************"); printf("\n****************************************************"); printf("\n** WAP to check a number for perfect **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************"); printf("\n******************************************************\n"); int n, i, s = 0; printf("enter number"); scanf("%d", &n); for (i = 1; i <= n - 1; i++) if (n % i == 0) s = s + i; if (s == n) printf("It is perfect number"); else printf("It is not perfect number"); return 0; }
Output 1
*************************************************************************** *************************************************************************** ** WAP to check a number for perfect ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************************** *************************************************************************** enter number 6 It is perfect number
Output 2
*************************************************************************** *************************************************************************** ** WAP to check a number for perfect ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************************** *************************************************************************** enter number 7 It is not perfect number