C Online Compiler
Example: Perfect Number or Not in C using While loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Perfect Number or Not in C using While loop #include <stdio.h> // It's the main function of the program int main() { int num, result = 0; // Step-1 Take input from the User printf("INPUT A NUMBER: "); scanf("%d", &num); // Step-2 Check input number should be positive if (num > 0) { // Step-3 Use a loop to find the perfect number int i = 1; while (i <= num) { if (num % i == 0) result += i; i++; } // Step-3 Check the Result(sum of factors) == 2*Input Number if (result == 2 * num) printf("%d IS A PERFECT NUMBER.\n", num); else printf("%d IS NOT A PERFECT NUMBER.\n", num); } else printf("Enter a positive number!\n"); return 0; }
28
Output
Clear
ADVERTISEMENTS