A Platform to LEARN | CODE | ACHIEVE



Start Coding
Take Challenge
Subscribe us !

Connect :





TO CHECK WHETHER A GIVEN NUMBER IS PRIME OR NOT IN C

Code

#include<stdio.h>
int main()
{
 int n;
 int i;
 int count=0;
 printf("ENTER THE NUMBER TO CHECK :\n");
 scanf("%d",&n);
 for(i=2;i<=n/2;i++)
 {
 if(n%i==0)
 {	
   count=1;
 } 		
 }
 if(count==0)
 {
   printf("IT IS PRIME NUMBER.\n");
 }
 else
 {
   printf("IT IS NOT A PRIME NUMBER.\n");
 }	
 return 0; 
}



Output


ENTER THE NUMBER TO CHECK : 39
IT IS NOT A PRIME NUMBER.


check a number a prime number or not


What we did - Our Approach


In this problem we have to check whether a given number is prime or not. A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole numbers that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.

For checking a prime number,we ran a for loop from i:2 to n/2,now if our number gets divided by any value of 'i' we set our counter to 1. After the end of the loop , if counter=0 then our number is a prime number else not.


Join us :





CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



SHARE:



For any Queries,suggestions,feel free to Mail us at :
codeofgeeks@gmail.com

For reporting any kind of copyright voilation,mail us at : cogcopyclaim@gmail.com

© copyright CODE OF GEEKS