Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Check if a Number is Prime using a For Loop.

#include <stdio.h>

int main() {
int num, isPrime = 1; // Assume the number is prime initially

// Taking input for the number
printf(“Enter a positive integer: “);
scanf(“%d”, &num);

// Checking if the number is prime using a for loop
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // Set to 0 if the number is divisible by any other number
break;
}
}

if (isPrime)
printf(“%d is a prime number.n”, num);
else
printf(“%d is not a prime number.n”, num);

return 0;
}