C Online Compiler
Example: Check Prime or Armstrong Number in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Check Prime or Armstrong Number #include <stdio.h> #include <math.h> // Required for sqrt() and pow() // Function to check if a number is prime (from Approach 1) int isPrime(int num) { if (num <= 1) { return 0; } for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) { return 0; } } return 1; } // Function to check if a number is an Armstrong number (from Approach 2) int isArmstrong(int num) { int originalNum, remainder, n = 0; double result = 0.0; originalNum = num; int tempNum = num; if (num == 0) { // Handle 0 separately for digit count n = 1; } else { while (tempNum != 0) { tempNum /= 10; n++; } } tempNum = num; // Reset tempNum for sum of powers calculation while (tempNum != 0) { remainder = tempNum % 10; result += pow(remainder, n); tempNum /= 10; } if ((int)result == originalNum) { return 1; } else { return 0; } } int main() { int number; // Step 1: Prompt user for input printf("Enter a positive integer: "); scanf("%d", &number); // Step 2: Check if the number is prime using the isPrime function if (isPrime(number)) { printf("%d is a Prime number.\n", number); } else { printf("%d is not a Prime number.\n", number); } // Step 3: Check if the number is an Armstrong number using the isArmstrong function if (isArmstrong(number)) { printf("%d is an Armstrong number.\n", number); } else { printf("%d is not an Armstrong number.\n", number); } return 0; }
Output
Clear
ADVERTISEMENTS