C Program to Print Perfect Numbers From 1 to N and Given Range on Command Line

Can I teach the possibilities to understand the concept of “C Program to Print Perfect Numbers From 1 to N and Given Range on Command Line” using this tutorial? Come on! Without wasting our time, Move on to the tutorial directly.

Source Code

/**
* C program to print all Perfect numbers between 1 to n
*/

#include <stdio.h>

int main()
{
int i, j, end, sum;

/* Input upper limit to print perfect number */
printf("Enter upper limit: ");
scanf("%d", &end);

printf("All Perfect numbers between 1 to %d:n", end);

/* Iterate from 1 to end */
for(i=1; i<=end; i++)
{
sum = 0;

/* Check whether the current number i is Perfect number or not */
for(j=1; j<i; j++)
{
if(i % j == 0)
{
sum += j;
}
}

/* If the current number i is Perfect number */
if(sum == i)
{
printf("%d, ", i);
}
}

return 0;
}
/**
* C program to print all Perfect numbers between 1 to n
*/

#include <stdio.h>

int main()
{
int i, j, start, end, sum;

/* Input lower and upper limit from user */
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);

printf("All Perfect numbers between %d to %d:n", start, end);

/* Iterate from start to end */
for(i=start; i<=end; i++)
{
sum = 0;

/* Check whether the current number i is Perfect number or not */
for(j=1; j<i; j++)
{
if(i % j == 0)
{
sum += j;
}
}

/* If the current number i is Perfect number */
if(sum == i)
{
printf("%d, ", i);
}
}

return 0;
}

Conclusion

I hope this article helps you to learn “C Program to Print Perfect Numbers From 1 to N and Given Range on Command Line”. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks.

Share on: Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.