Thursday, 16 November 2017

C Program to Check if a given Number is Prime number


Code:

#include   stdio.h
#include   stdlib.h

void main()
{
    int num, j, flag;

    printf("Enter a number \n");
    scanf("%d", &num);

    if (num <= 1)
    {
        printf("%d is not a prime numbers \n", num);
        exit(1);
    }
    flag = 0;
    for (j = 2; j <= num / 2; j++)
    {
        if ((num % j) == 0)
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("%d is a prime number \n", num);
     else
        printf("%d is not a prime number \n", num);
}


Output:

Enter a number
23
23 is a prime number


More C Programs:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...