Problem: Program to check whether a number is prime or not.
A prime number is a positive number and which is divisible by 1 and itself.
For Example:Input1: num = 9
Output1: 9 is not a Prime number.
Explanation: 9 has divisors other than 1 and 9 (i.e., 3). Hence, 9 is not a prime number.
Input2: num = 7
Output2: 7 is a Prime number.
Explanation: 7 has no divisors other than 1 and 7. Hence, 7 is a prime number.
#include<stdio.h>
int main() {
int num,i;
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
break;
}
}
if(i>num/2){
printf("%d is a Prime number");
}else{
printf("%d is not a Prime number");
}
return 0;
}
int main() {
int num,i;
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
break;
}
}
if(i>num/2){
printf("%d is a Prime number");
}else{
printf("%d is not a Prime number");
}
return 0;
}
Sample Input1
5
Sample Output1
5 is a Prime number
Sample Input2
9
Sample Output2
9 is not a Prime number
5
Sample Output1
5 is a Prime number
Sample Input2
9
Sample Output2
9 is not a Prime number