Lets write a C program to check whether user input number is prime number or not, using for loop.
Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.
Related Read:
For Loop In C Programming Language
if else statement in C
break Statement In C Programming Language
In this video tutorial we’re illustrating 3 methods to find if the user entered number is prime number or not.
Page Contents
All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2. So our c program starts checking for divisibility from number 2.
Video Tutorial: C Program To Find Prime Number or Not using For Loop
#include<stdio.h> int main() { int num, count, prime = 1; printf("Enter a positive number\n"); scanf("%d", &num); for(count = 2; count < num; count++) { if(num % count == 0) { prime = 0; break; } } if(prime) printf("%d is a Prime Number\n", num); else printf("%d is a not Prime Number\n", num); return 0; }
Output 1:
Enter a number
7
7 is prime number
Output 2:
Enter a number
10
10 is not prime number
We ask the user to enter a positive number and store it in variable num. Using for loop we start dividing the user entered number from 2 to num-1 times. If any number from 2 to num-1 perfectly divide the user entered number, then it’s not a prime number. We assign value 0 to variable prime and break out of the loop and print the message to the user.
#include<stdio.h> int main() { int num, count, prime = 1, inum; printf("Enter a positive number\n"); scanf("%d", &num); inum = num / 2; for(count = 2; count <= inum; count++) { if(num % count == 0) { prime = 0; break; } } if(prime) printf("%d is a Prime Number\n", num); else printf("%d is a not Prime Number\n", num); return 0; }
Output 1:
Enter a number
41
41 is prime number
Output 2:
Enter a number
15
15 is not prime number
Please read the logic for method 1 above before proceeding.
In this method, we divide the user entered number by 2. This reduces the number of iterations of for loop.
If num = 41;
inum = num / 2;
inum = 41 / 2;
inum = 20;
So its enough if we iterate through the for loop 19(num/2) times to check if number 41 is perfectly divisible by any number from 2 to 20.
#include<stdio.h> #include<math.h> int main() { int num, count, prime = 1, inum; printf("Enter a positive number\n"); scanf("%d", &num); inum = sqrt(num); for(count = 2; count <= inum; count++) { if(num % count == 0) { prime = 0; break; } } if(prime) printf("%d is a Prime Number\n", num); else printf("%d is a not Prime Number\n", num); return 0; }
Output 1:
Enter a number
50
50 is not prime number
Output 2:
Enter a number
53
53 is prime number
Please read the logic for method 1 above before proceeding.
In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of for loop even further.
If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;
So its enough if we iterate through the while loop 5( sqrt(num) ) times to check if number 41 is perfectly divisible by any number from 2 to 6.
Note: We are not using curly braces around if and else because we only have 1 line of code after if and else – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.
You can also watch video for C Program To Find Prime Number or Not using While Loop
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert