Showing posts with label prime or composite. Show all posts
Showing posts with label prime or composite. Show all posts

Wednesday, May 16, 2018

C program to find whether the given number is prime or composite


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int question=0, i=2;
clrscr();
printf(“Please input a positive integer to find out whether it is a prime number or composite number.\n”);
scanf(“%d”,&question);
if (question==1)
{
printf(“%d is neither prime nor composite.\n”, question);
goto stop;
}
if (question==2)
{
printf(“%d is a prime number.\n%d is the only even prime number.\n”,question,question);
goto stop;
}
do
{
if (question%i==0)
{
printf(“%d is a composite number.\n”,question);
goto stop;
}
i++;
}
while(i<question);
printf(“%d is a prime number.\n”,question);
stop:
getch();
}

Example of output
Please input a positive integer to find out whether it is a prime number or composite number.
3
3 is a prime number.