C Online Compiler
Example: Prime Number or Not in C using while loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Prime Number or Not in C using While loop #include <stdio.h> // It's the main function of the program int main() { int num; int is_prime = 0; // 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) is_prime = 1; // Step-3 Use a loop to check whether the input number is prime or not int i = 2; while (i <= num / 2) { // If the input number is divisible by i, then the input number is not a Prime if (num % i == 0) { is_prime = 1; break; } i++; } // Step-4 If `is_prime == 0` INPUT number is prime // Final output of the program if (is_prime == 0) printf("%d IS A PRIME NUMBER.\n", num); else printf("%d IS NOT A PRIME NUMBER.\n", num); return 0; }
37
Output
Clear
ADVERTISEMENTS