You are currently viewing Check Given Number Is Prime Or Not Using While Loop in C

Check Given Number Is Prime Or Not Using While Loop in C

In this tutorial, you will learn how to write a C program to check whether a given number is a prime number or not using a while loop conditional statement. Here, we will go through examples, algorithms, logic, and program explanation for a better understanding of this problem in C programming language.

Required Knowledge

Problem Statement

You have to Write a C program that takes a number as input from the user. After taking the integer input our program should check whether it is a prime number or not using a while loop. The program provides a solution to determine the primality of a given number using a while loop in the C programming language.

To understand what we have to achieve, you can see the below example.

Example

Input:
Enter a number: 17

Output:
17 is a prime number.

In the above example, you can see that the user input is 17. And we know that by the definition of prime number, 17 is a Prime number because it is divided by 1 and 17 only.

Algorithm

  1. Start the program.
  2. Declare the variable num to store the input number, isPrime to store the prime status, and i as a counter.
  3. Read the value of num from the user.
  4. Initialize isPrime to 1 (true).
  5. Initialize i to 2.
  6. Enter the while loop with the condition i <= sqrt(num).
    • Check if num is divisible by i without a remainder.
      • If true, set isPrime to 0 (false) and break out of the loop.
      • If false, continue to the next iteration.
    • Increment i by 1.
  7. Exit the while loop.
  8. Check the value of isPrime.
    • If isPrime is 1, print that num is a prime number.
    • If isPrime is 0, print that num is not a prime number.
  9. End the program.

Logic used to solve this Problem

  • Declare a variable to hold the input number.
  • Prompt the user to enter a number.
  • Read and store the entered number in the variable.
  • Check if the number is less than 2. If true, it is not a prime number.
  • Check if the number is exactly 2. If true, it is a prime number.
  • Initialize a divisor variable to 2.
  • Use a while loop to iterate while the divisor is less than the square root of the number.
  • Inside the loop, check if the number is divisible evenly by the divisor. If true, it is not a prime number.
  • Increment the divisor by 1.
  • If the loop completes without finding any divisors, the number is a prime number.
  • Print the result indicating whether the number is prime or not.
  • Finish the program.
  • Use a while loop to iterate while the divisor is less than the square root of the number.

C Program to Check whether a given number is Prime or not

#include <stdio.h>
#include <math.h>
int main() {
    int num, isPrime = 1, i = 2;
    printf("Enter a number: ");
    scanf("%d", &num);
    while (i <= sqrt(num)) {
        if (num % i == 0) {
            isPrime = 0;
            break;
        }
        i++;
    }
    if (isPrime == 1) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }
    return 0;
}

Output 1

Enter a number: 54
54 is not a prime number

Output 2

Enter a number: 5 
5 is a prime number

Program Explanation

  • The program takes input for the number to be checked by the user.
  • It initializes a variable isPrime to 1, which represents the initial assumption that the number is prime.
  • It initializes a variable divisor to 2, which will be used to check divisibility.
  • The program enters a while loop with the condition divisor <= number/2 to iterate through possible divisors.
  • Inside the while loop, it checks if the number is divisible by the current value of divisor.
  • If the number is divisible, it sets the value of isPrime to 0, indicating that it is not a prime number.
  • It then increments the value of divisor by 1 to move on to the next possible divisor.
  • The loop continues until divisor exceeds half of the given number.
  • After the while loop, it checks the value of isPrime.
  • If isPrime is 1, it prints that the given number is prime.
  • Otherwise, it prints that the given number is not prime.
  • The program ends.

Conclusion

The C program successfully checks whether a given number is prime or not using a while loop. By iterating through the numbers from 2 to the square root of the number and checking for any divisors, it determines the prime status and displays the result on the screen.