Program to delete duplicates from a link list

Program to check that number is perfect number or not

Program to check that number is perfect number or not

Given a number , write a Program to check that number is perfect number or not.

Sample input:- 

6

Sample output:-

6 is a perfect number

What is perfect number: –

A number is said to be perfect if sum of it’s all divisors excepts it is equals to the number itself.

For example : –  6 is a perfect number because divisors of 6 are 1 , 2 ,3  and sum of all these are 6 which is equal to the number 6 .

Code :-

C language:-

#include<stdio.h>
int main()
{
    int n,sumd=0;
    printf("Enter a number\n");
    scanf("%d",&n);
    // logic for finding the divisors and their sum
    for(int i=1;i<n;i++)
    {
        if(n%i==0)
          sumd+=i;
    }
    
    if(sumd==n)
      printf("%d is a perfect number",n);
    else
      printf("%d is not a perfect number",n);
    return 0;
}

 

C++ code:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,sumd=0;
    cout<<"Enter a number\n";
    cin>>n;
    
    // finding diviors and their sum
    for(int i=1;i<n;i++)
    {
        if(n%i==0)
          sumd+=i;
    }
    
    // if sum equal to number
    if(sumd==n)
       cout<<n<<" is a perfect number"<<endl;
    else
      cout<<n<<" is not a perfect number"<<endl;
     return 0;
}

Output:- 

Enter a number
6
6 is a perfect number

python code:-

n=int(input())
sum=0
for i in range(1,n):
    if n%i==0:
        sum=sum+i 
if n==sum:
    print(n,"is a perfect number ")
else:
    print(n,"is not a perfect number")

output:-

28
28 is a perfect number

 

 

 

Also check this:-

codechef problems:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-

Leave a Reply

Your email address will not be published.