C Online Compiler
Example: Prime Number or Not in C using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Prime Number or Not in C using For loop #include <stdio.h> // Function to check if it is a prime or not int is_prime(int num) { // If the input number is divisible by i, then the input number is not a Prime for (int i = 2; i < num; i++) { // If the input number is divisible by i, It's not a prime number if (num % i == 0) return 0; } // If there are no divisors, It's a prime number return 1; } // It's the main function of the program int main() { int num; // Step-1 Take input from the User printf("INPUT A NUMBER: "); scanf("%d", &num); // Step-2 Check if the input number is less than or equal to 1 if (num <= 1) printf("%d IS NOT A PRIME NUMBER.\n", num); else { // Step-3 Call the is_prime() function to check whether the input number is prime or not if (is_prime(num)) printf("%d IS A PRIME NUMBER.\n", num); else printf("%d IS NOT A PRIME NUMBER.\n", num); } return 0; }
40
Output
Clear
ADVERTISEMENTS