Program to find the perfect numbers between a given range

The program is to find all the perfect numbers inside the user defined range. A perfect number is one, whose sum of devisors is equals the number itself.

Logic: This is advanced version of the previous program. Here, user need to enter two numbers as the lower and upper limits for the iteration loop to find the perfect number between that. The outer for loop traces the iteration till the limit, wherein each of iteration inner for loop checks the present number is perfect or not, with the perfect number’s logic. If it is, it prints out the present number.

In this program, both the lower limit and the upper limit are variables, and so is flexible. These three programs show, how we can upgrade the logic to make the code flexible one.

The similar algorithm can be used to find the prime numbers inside a given range.

Program to find the perfect numbers between a given range

#include<stdio.h>
#include<math.h>
void main()
{
int i, n, sum, lim_low, lim_up;
clrscr();
printf(“\n\n\t ENTER THE LOWER LIMIT…: “);
scanf(“%d”, &lim_low);
printf(“\n\n\t ENTER THE UPPER LIMIT…: “);
scanf(“%d”, &lim_up);
printf(“\n\n\t THE PERFECT NUMBER ARE..:”);
for(n=lim_low+1; n<lim_up; n++)
{
sum = 0;
for(i=1; i<n; i++)
if(n%i == 0)
sum = sum + i;
if (sum == n)
printf(“\n\n\t\t\t%d”,n);
}
getch();
}

Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

2 thoughts on “Program to find the perfect numbers between a given range

  1. import java.util.*;
    public class Perfect
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int num[] = new int[n];
    int x=0,i=0;
    String line = sc.next();
    String numbers[] = new String[n+(n-1)];
    numbers = line.split(“,”);
    int sum;
    for(String y:numbers)
    {
    x = Integer.parseInt(y);
    sum=0;
    for(int j=1;j<=x;j++)
    {
    if(x%j==0)
    sum=sum+j;
    }
    if(sum/2==x && x!=0)
    System.out.print(x);
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in