Find out all Perfect numbers present within a given range

Write a C program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range in C.

Program in C

Code:

/*Write a C program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range Using C*/

#include<stdio.h>
int main()
{
    int j,i,range1,range2;
    printf("Enter a range:");
    scanf("%d %d",&range1,&range2);
    printf("Perfect numbers between %d and %d are:",range1,range2);
    for(j=range1;j<=range2;j++)
    {
    int num=j;
    int sum=0;
    for(i=1;i<j;i++)
    {
        if(j%i==0)
        {
            sum=sum+i;
        }
    }

   if(sum==num)
     printf("%d ",j);
}
}

Input/Output:
Enter a range:
2
200
Perfect numbers between 2 and 200 are:6 28

Write a C++ program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range in C++.

Program in C++

Code:

/*Write a C++ program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range using C++*/

#include<iostream>
using namespace std;
int main()
{
    int range1,range2;
    cout<<"Enter a range:";
    cin>>range1>>range2;
    cout<<"Perfect numbers between "<<range1<<" and "<<range2<<" are:";
    for(int j=range1;j<=range2;j++)
    {
    int num=j;
    int sum=0;
    for(int i=1;i<j;i++)
    {
        if(j%i==0)
        {
            sum=sum+i;
        }
    }

   if(sum==num)
     cout<<j<<" ";
}
}

Input/Output:
Enter a range:
201
500
Perfect numbers between 201 and 500 are:496

Write a JAVA program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range in Java.

Program in Java

Code:

/*Write a JAVA program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range using Java*/

import java.util.Scanner;
public class PerfectNumberInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int firstnumber, endnumber, i;
       System.out.println("Enter the First number:");
       firstnumber=cs.nextInt();
       System.out.println("Enter the Last number:");
       endnumber=cs.nextInt();
System.out.println("Perfect numbers between "+firstnumber+" and "+endnumber+" are:");
  for(i=firstnumber;i<=endnumber;i++)
    {
  int num=i;
    int sum=0;
    for(int j=1;j<i;j++)
    {
        if(i%j==0)
        {
            sum=sum+j;
        }
    }

   if(sum==num)
     System.out.print(i+" ");
    }
cs.close();
}}

Input/Output:
Enter the First number:
501
Enter the Last number:
1000000
Perfect numbers between 501 and 1000000 are:
8128 

Write a PYTHON to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range in Python.

Program in Python

Code:

'''Write a Python program to find out all Perfect numbers present within a given range. or Write a program to find out all Perfect numbers present within a given range using Python '''

print("Enter a range:")
range1=int(input())
range2=int(input())
print("Perfect numbers between ",range1," and ",range2," are: ")
for j in range(range1,range2+1):
    sum=0
    num=j
    for i in range(1,j):
        if(j%i==0):
            sum=sum+i
    if sum==num:
      print(j,end=" ") 

Input/Output:
Enter a range:
20
10000
Perfect numbers between  20  and  10000  are: 
28 496 8128 


Post a Comment

0 Comments