Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Prime Number Or Not

Prime number or not:-

Prime number program in C programming is to test whether the inserted number by the user is prime or not. A prime number is a natural number that is divisible by just 1 and itself. In order to test whether the number inserted by the user is prime or not, the C program runs an if-else statement for the same.

Algorithm to check whether the inserted number is prime or not:

Step 1: Start

Step 2: The user is asked to enter a number

Step 3: Check whether the number is divisible by the natural numbers starting from 2.

Step 4: If the number is divisible, then it is not a prime number, if it is not, printf “the number is prime”.

Step 5: Exit

Read Also: Program for Prime Number in a given Range

Program to check whether the inserted number is prime or not:

/*
 * C program to check whether a given number is prime or not
 * and output the inserted digit with appropriate message.
 */
#include <stdio.h>
#include <stdlib.h>
void main()
{
    int no, j, flag;
    printf("Insert a no \n");
    scanf("%d", &no);
    if (num <= 1)
    {
        printf("%d is not a prime numbers \n", no);
        exit(1);
    }
    flag = 0;
    for (j = 2; j <= no / 2; j++)
    {
        if ((no % j) == 0)
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("%d is a prime num \n", no);
     else
        printf("%d is not a prime num \n", no);
}

Output

Insert a no: 23
23 is a prime num
Insert a no. 15 15 is not a prime num