Program to check number is prime or not using do-while-loop

Write a program to check whether the entered number is prime or not using do-while-loop.

C Program

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

    return 0;
}

Input:

Enter a number : 5

Output:

Number is prime 


No comments:

Post a Comment