Header-C
Header-C

 

 

Introduction:

A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number cannot be divided evenly by any other number except for 1 and the number itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.

Objective:

The objective of this program is to determine whether a given number is prime or not. The program will take an integer as input, check if it has any divisors other than 1 and itself, and then output whether the number is prime or not.

Code:

#include 

// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) {
        return 0; // Numbers less than or equal to 1 are not prime
    }
    
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return 0; // If the number is divisible by i, it is not prime
        }
    }
    return 1; // If no divisors were found, the number is prime
}

int main() {
    int num;

    // Ask the user to input a number
    printf("Enter a number to check if it is prime: ");
    scanf("%d", &num);

    // Check if the number is prime and display the result
    if (isPrime(num)) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }

    return 0;
}

Explanation of the Program:

The program is structured into two main parts:

  1. Function to check prime: The isPrime function takes an integer as input and returns 1 if the number is prime and 0 otherwise. It uses a loop to check if the number is divisible by any number from 2 to the square root of the number. If it finds any divisor, it returns 0, indicating the number is not prime. If no divisors are found, it returns 1, indicating the number is prime.
  2. Main function: In the main function, the program prompts the user to enter a number. This number is then passed to the isPrime function. Based on the result, the program prints whether the number is prime or not.

How to Run the Program:

Follow these steps to run the program:

    • Write the code in a file with a .c extension, for example, prime_check.c.
    • Compile the program using a C compiler like gcc. Open your terminal or command prompt and type:
gcc prime_check.c -o prime_check
    • Run the compiled program by typing:
./prime_check
  • The program will prompt you to enter a number. After entering the number, it will check and display whether the number is prime or not.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)