C Online Compiler
Example: Perfect Number or Not in C using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Perfect Number or Not in C using For 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 for (int i = 1; i <= num; i++) { if (num % i == 0) result += 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; }
496
Output
Clear
ADVERTISEMENTS