C Programming language

adplus-dvertising
C Program example: Accept a number and check, is it prime number or not
Previous Home Next
Accept a number and check is it prime number or not a prime number

Here we are taking number from keyboard .Then iterate for loop from 1 to t (number). In for loop check modulo of t with i if it is zero then increase c by one. When iteration is completed then check c==2 then the number is prime else it is not prime. Here we are checking so because a prime number is divisible by 1 or itself. So c=2.If it is greater than two then it is not prime.

/* Accept a number and check it  is prime number or not a prime number */

#include<stdio.h>
#include<conio.h>
void main()
{
	int t,c=0,i;
	clrscr();
	printf("Enter a number\t");
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
		if(t%i==0)
		c++;
	}
	if(c==2)
		printf("\nNumber is prime");
	else
		printf("\nNumber is not prime");
	getch();
}
Output:

Enter a number 23

Number is prime

Previous Home Next