Perfect numbers in the range
Write a program to find the perfect numbers between a given range.
Input Format:
The first input contains an integer which denotes the starting number of the range
The second input contains an integer which denotes the ending number of the range
Output Format:
Print the perfect numbers in a given range
Sample Input:
1
10
Sample Output:
6
Answers
Answer:
#include <stdio.h>
int main()
{
int i, Number, Sum = 0 ;
printf("\n Please Enter starting number \n") ;
scanf("%d", &s);
printf("\n Please Enter ending number \n") ;
scanf("%d", &e);
for(i=s; s < e ; i++)
{
for(i = 1 ; i < s ; i++)
{
if(s% i == 0)
Sum = Sum + i ;
}
}
if (Sum == s)
printf("\n %d is a Perfect Number", Number) ;
else
printf("\n%d is not the Perfect Number", Number) ;
return 0 ;
}
Answer:
#include<stdio.h>
int main(){
int sum=0,p,i;
int initial,final;
scanf("%d",&initial);
scanf("%d",&final);
for(i= initial; i<= final; i++){
p=1;
while(p<=(i/2)){
if(i % p == 0)
sum=sum+p;
p++;
}
if(sum==i)
printf("%d ",i);
sum=0;
}
return 0;
}
Explanation: