Program to print the perfect numbers between two intervals

Sample Input 1 :

1 20000

Sample Output 1 :

6 28 496 8128

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include<stdio.h>
int main()
{
    	int number, i,sum,start,stop;

    	printf("Enter two Numbers: ");
    	scanf("%d %d",&start,&stop);
	printf("Perfect number between %d and %d:",start,stop);

    	for(number=start;number<=stop;number++)
	{
		sum=0;
    		for(i=1; i <= number/2; ++i)
    		{
        		if (number%i == 0)
        		{
            			sum+=i;
        		}
    		}
		if(sum==number)
		{
			printf("%d ",number);
		}
		
	}
	

    return 0;
}
			
				
			

Program Explanation

refer video tutorial

Comments