Write a program to identify if the number is Perfect number or not

14 May 2022

Write a program to identify if the number is Perfect number or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get the input from the user and check whether that number is a perfect number or not.

E.g. Let number is 28, factors of 28 are 1,2,4,7,14. Now the sum of all these factors are 28 itself.

Input

28

Output

Perfect Number

Input

4

Output

Not a perfect number

C Program

Method 1

#include <stdio.h>

int main()

{

    int i,num,sum=0;

    printf("Enter a number: ");

    scanf("%d",&num);

    for(i=1;i<num;i++)

    {

        if(num%i==0)

        sum=sum+i;

    }

    if(num==sum)

    printf("Perfect number");

    else

    printf("Not a perfect number");

    return 0;

}

Method 2

#include <stdio.h>

#include<math.h>

int main()

{

    int i,num,sum=0;

    printf("Enter a number: ");

    scanf("%d",&num);

    for(i=1;i<=sqrt(num);i++)

    {

        if (num%i==0)

        {

            if(i==1)

                sum=sum+i;

            else if(i==num/i)

                sum=sum+i;

            else

                sum=sum+i+num/i;

        }

    }

    if(num==sum)

    printf("Perfect number");

    else

    printf("Not a perfect number");

    return 0;

}

C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int i,num,sum=0;

    cout<<"Enter a number: ";

    cin>>num;

    for(i=1;i<num;i++)

    {

        if(num%i==0)

        sum=sum+i;

    }

    if(num==sum)

    cout<<"Perfect number";

    else

    cout<<"Not a perfect number";

    return 0;

}

Method 2

#include <iostream>

#include<math.h>

using namespace std;

int main()

{

    int i,num,sum=0;

    cout<<"Enter a number: ";

    cin>>num;

    for(i=1;i<=sqrt(num);i++)

    {

        if (num%i==0)

        {

            if(i==1)

                sum=sum+i;

            else if(i==num/i)

                sum=sum+i;

            else

                sum=sum+i+num/i;

        }

    }

    if(num==sum)

    cout<<"Perfect number";

    else

    cout<<"Not a perfect number";

    return 0;

}

Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.print("Enter a number: ");

int num=sc.nextInt();

int sum=0;

for(int i=1;i<num;i++)

{

if(num%i==0)

sum=sum+i;

}

if(sum==num)

System.out.println("Perfect Number");

else

System.out.println("Not an Perfect Number");

}

}

Python

num=int(input("Enter a number: "))

Sum=0

for i in range(1,num):

    if num%i==0:

        Sum=Sum+i

if(Sum==num):

    print("Perfect Number")

else:

    print("Not a perfect number")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !