You're online now.

Hurray! you are online now.

Write a c program to check the given number prime or not

Prime Number is a number which divisible by 1 and own so we called the number is prime. The condition is number is greater than 1. A number is divisible by itself or 1 so called the prime number.

// Write a c program to check the given number prime or not.
    #include<stdio.h>
    #include<stdbool.h>
    // boolean return type function to isPrime()
    bool isPrime(int n){
        if(n < 2){
            return false;
        }
        for(int i = 2; i < n; i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
    // main function
    int main(){
        int n;
        printf("Enter the number : ");
        scanf("%d", &n)
        if(isPrime(n)){
            printf("%d is a prime number", n);
        }else{
            printf("%d is not a prime number", n);
        }
        return 0;
    }
#include<stdio.h>

This line include the standard libary function to used for input/output function in program.

#include<stdbool.h>

This line include the statndard library to used for bool type method/keywords like true or false and return type also of boolean function.

// check prime function
    bool isPrime(int n){
         if(n < 2){
        return false
    } 
        for(int i = 2; i < n; i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }

Above the function called as isPrime() is used for to check the given number is prime or not, this function return type bool which return only true or false. Inside the function for loop start from 2 to n - 1 because we know about the prime number very well that if number is devided 1 or itself then then the number is prime otherwise number is not prime so we start the for loop from 2 to n - 1, if n % i == 0 then return false (means given number is not prime) otherwise after complete the loop then return true (means given number is prime). if n < 2 then return false suppose anybody give the input 1 so 1 is not a prime number the first prime number is 2.

// main function
    int main(){
        int n;
        printf("Enter the number : ");
        scanf("%d", &n);
    
        if(isPrime(n)){
            printf("%d is a prime number", n);
        }else{
            printf("%d is not a prime number", n);
        }
        return 0;
    }

In the main function which known as the entry point of the program, declared the variable named n as integer. Prompt to user enter the number and read the input from the user. Check using the if-else statement, if(isPrime(n)) then print "%d is a prime number" otherwise print "is not a prime number" using the printf() function, after this the program return 0 which indicates that program is successfully executed.

Algorithm

1. Start
2. Declared an variable AS INTEGER
3. Read the input from user
4. Check given number is prime or not
5. isPrime() true then print " %d is a prime number", n
6. otherwise print "%d is not a prime number", n
7. End

Pseudocode

// Pseudocde of prime number
    1. INCLUDE stdio.h
    2. INCLUDE stdbool.h
    3. FUNCTION isPrime(int n)
        1. FOR i = 2 to n - 1
            1. IF n % i == 0
                RETURN false
        2. RETURN true
    4. FUNCTION main()
        1. DECLARED n AS INTEGER
        2. PROMPT to the user enter the number : 
        3. READ the input and store the value in n variable
        4. IF isPrime(n)
            PRINT "%d is a prime number", n
        3. ELSE
            PRINT "%d is not a prime number", n
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...