Check given number is prime number or not using c program

Definition of prime number:

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, it only has two divisors: 1 and the number itself.

For example, with the number 5, as you mentioned, the only divisors are 1 and 5. This property of having exactly two distinct positive divisors holds true for all prime numbers.

All other even numbers are divisible by 2, making them composite (having more than two divisors).



Logic for prime number in c

We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.

Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.

Code 1:
1. C program to determine prime number
2. Determining if a number is prime in c
3. C program to find given number is prime or not

#include<stdio.h>

int main(){

    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Sample output:
Enter a number: 5
5 is a prime number

Code 2:
1. C program for prime numbers between 1 to 100
2. How to find prime numbers from 1 to 100 in c
3. How to print prime numbers from 1 to 100 in c

#include<stdio.h>

int main(){
    int num,i,count;
  
    for(num = 1;num<=100;num++){
         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}

Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Code 3:
1. C program for prime numbers between 1 to n
2. C program to find prime numbers up to n
3. C program to list prime numbers
4. Write a c program to generate n prime numbers
5. C program to find n prime numbers

#include<stdio.h>

int main(){

    int num,i,count,n;
    printf("Enter max range: ");
    scanf("%d",&n);

    for(num = 1;num<=n;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}

Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
  
Code 4:
1. C program to find prime numbers using while loop
2. Wap to find prime numbers in c
3. Write a c program to generate prime number
4. How to get prime numbers in c

#include<stdio.h>

int main(){

   int num,i,count,min,max;

printf("Enter min range: ");
    scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    num = min;
    while(num<=max){

         count = 0;
         i=2;

         while(i<=num/2){
             if(num%i==0){
                 count++;
                 break;
             }
             i++;
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);

         num++;
    }
  
   return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100
53 59 61 67 71 73 79 83 89 97

Code 5:
1. How to find out prime numbers in c programming
2. Display prime numbers in c
3. C program to find prime numbers between two numbers
4. C code to display prime numbers within a range

#include<stdio.h>

int main(){

    int num,i,count,min,max;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}

Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47

Code 6:
1. Sum of prime numbers from 1 to 100 in c

#include<stdio.h>

int main(){

    int num,i,count,sum=0;

    for(num = 1;num<=100;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             sum = sum + num;
    }

    printf("Sum of prime numbers is: %d ",sum);
  
   return 0;
}
  
Output:
Sum of prime numbers is: 1060

Code 7:
1. C program to find sum of prime numbers

#include<stdio.h>

int main(){

    int num,i,count,min,max,sum=0;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             sum = sum + num;
    }

    printf("Sum of prime numbers is: %d ",sum);
  
   return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100

1. Write a c program to check given number is perfect number or not.
Sum of prime numbers is: 732

10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
15. Write a c program to solve quadratic equation.
18. Write a c program which passes structure to function.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.

56 comments:

Unknown said...

nice ^_^ thanks

raghav said...

awesme practise for the beginners.........full fledge programs......:)))

Unknown said...

hello sir,

suppose i want to search prime number between 1 to 200 and want to find largest prime number between result, how i can achieve this result?

example
min value=1
max valu=10
prime numbers between 1 to 10=2,5,7
large prime=7

Anonymous said...

please tell me the use of cont variable in this program and if possible then please explain the first program.

Anonymous said...

Excellent website for beginners

Atiq said...

no need to divide the num by 2 just take sqrt of num and increase only check odd values (except 2) much efficient program Sir....

jagadeesh said...

use this method to easily to understand
#include
#include
void main()
{
int i,j,a,n;
printf("enter the number");
scanf("%d",&a);
for(i=2;i<=n;i++)
{
}
if(a%i==0)
{
printf("the number is not prime");
}
else
{
printf("the number is prime");
}
getch();
}

Anonymous said...

realy helpfl......

Unknown said...

Superb.....Thanx a lot.

Unknown said...

what about 2?? for that the above logic will not work.. because 2%2=0, so it will not show as a prime number...

Unknown said...

Q 4) Write a C++ program to print all prime numbers between two limits by using do while loop.
Q 5) Write a C++ program to search an element by using binary search.
Q 6) Write a C++ program to interchange two numbers by using function.

kavita said...

Thanks its realy helps

Anonymous said...

What is the value of n?

Unknown said...

hello sir.
how can i print prime number by using if else condition. with out using any loop or other condition.
only in if else condition.......

Anonymous said...

can anyone please show the dry run of the second program please??

Unknown said...

How to use strcmp() in c-language , please send a one example

Anand said...
This comment has been removed by the author.
Anand said...

Program to print prime number from 1 to n.
One solution may be like this :

# include
main()
{
int n, i, j, isPrime = 0;
scanf("%d\n",&n );
for(i = 2; i <= n; i++)
{
isPrime = 1;
for(j = 2; j <= i/2; j++)
{
if(i % j == 0)
{
isPrime = 0;
break;
}
}
if(isPrime)
printf("%d ",i);
}
}

Anand said...

#include
#include
int main(){
char str1[30],str2[30];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Both strings are equal");
else
printf("Strings are unequal");
return 0;
}

Anand said...

You can further more reduce the number of iterations.
As, for a number to be prime or not, it is not necessary to check upto 'number/2'.
Check only upto sqrt(number).
e.g.
TO check 23, check '23 % i == 0' only upto 'i <= sqrt(number)' i.e. {i <= 4}.
This will always work.


Anonymous said...

#include
#include
int main()
{
int i,j,n=100 ;
for(i=2;i<=n;i++)

{

for(j=2;j<i;j++)

{

if(i%j==0)

break;
}



if(i==j)

printf("\n%d",i);
}
system("PAUSE");
}
In this program why 4 is not printed?when 14 and j=4 then it should be print 4 as prime number(program works correctly :)).i dont understand nested loops.Can anybody please explain me the working of this program for say,n=10.i reach at 3 but 4 makes me in trouble.

ankan sengupta said...

In a global Mathematics contest, the contestants are told to invent some special numbers which can be built by adding the squares of its digits. Doing this perpetually, the numbers will end up to 1 or 4.
If a positive integer ends with 1, then it is called the Number of Game.
An example from above is:
13 = 1^2 + 3^2 = 1+9 = 10 (Step:1)
10 = 1^2 + 0^2 = 1+0 = 1 (Step:2), iteration ends in Step 2 since number ends with 1

Then in next round, the contestants are asked to combine their newly invented number, i.e. Number of Game with prime number.
Now, being a smart programmer, write a program to help the contestants to find out the Nth combined number within any given range, where N can be any integer.
Input Format:

Input consists of 3 integers X, Y, N, one on each line. X and Y are upper and lower limits of the range. The range is inclusive of both X and Y. Find Nth number in range [X,Y].

Line 1

X,where X is the upper limit of the range
Line 2

Y,where Y is the lower limit of the range
Line 3

N,where Nth element of the series is required

Constraints:

X <= Y
X > 0
N > 0

Output Format:

Output will show the Nth element of the combined series lying in the range between X and Y.

Line 1
For Valid Input,print

U,where U is the Nth element of the combined number series lying in the range between X and Y.

Or

No number is present at this index

For Invalid Input,print

Invalid Input



Sample Input and Output

SNo. Input Output
1 1,30,3 19



2 12,33,5 No number is present at this index



3 -5,@,4 Invalid Input



ankan sengupta said...

please answer how to solve this?

Unknown said...

wap to find out the sum of all numbers between 1 to 100 which are divisible by 7 can plz any i can solve this?

Anonymous said...

if n=1
1
1
if n=2
1
2*2
2*2
1
like this for n=1 to 100

Anonymous said...

Exlnt

Anonymous said...

can anyone please say how count works in the first program??

Prini Raj said...

#include
#include
#include

int main()
{
int num,i,count=0;
clrscr();
printf("Enter a number:\n");
scanf("%d",&num);
for(i=2;i<=sqrt(num);i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count>0)
printf("Not a prime");
else
printf("%d is a prime number",num);
getch();
return 0;
}

Anonymous said...

can you please explain working

Unknown said...

Write a program that checks if a given number is prime but does not belong to the fibonaci series. The program should return 1 if the numer is prime and does not belong to the fibonaic series or 0 otherwise

Anonymous said...

#include
#include

void main (void)
{
int score1,score2,score3,average,rating1,rating2,call=0,reject=0,offer=0,choice=1;
char name;

while(choice==1)
{
printf("Enter name of Candidate");
scanf("%c",&name);
printf("\nscore of test1:");
scanf("%d",&score1);
printf("\nscore of test2:");
scanf("%d",&score2);
printf("\nscore of test3:");
scanf("%d",&score3);
printf("Enter Rating 1 of candidate :");
printf("and enter Rating 2 of candidate :");
scanf("%d%d",&rating1,&rating2);
average=((score1+score2+score3)/3);
if(score1>75 && score2>75 && score3>75 && average>80)
{
printf("the candidate selected for interviw");
call++;
}
else
{
printf("rejection letter is sent");
reject++;
printf("press 1 if you want to continue Else press any number\n");
scanf("%d",&choice);
continue;
}
printf("Enter round1 marks");
scanf("%d",&rating1);
printf("Enter round2 marks");
scanf("%d",&rating2);

if (rating1>5 && rating2>7)
{
printf("Offer letter has been sent\n");
offer++;

}
else
{
printf("rejection letter has been sent\n");
reject++;
continue;
}
printf("\nPress 1 if you want to continue else press any other number\n");
scanf("%d",&choice);

printf("the number of candidates whom offer letter has been sent:%d\n",offer);
printf("the number of candidates called for interview:%d\n",call);
printf("the number of candidates whom rejection letter has been sent:%d\n",reject);
getch();
}

Unknown said...

program o genrate sequnce like

1111
1112
1121
1211
1213
....

Unknown said...

HI can anyone tell me how to reverse a string. also wap to reverse a string in C?

Unknown said...
This comment has been removed by the author.
Anonymous said...

In for loop when 2/2 gives the answer 1 it will false the for loop condition [for(i=2;i<num/2;i++)] so the compiler will check the first if condition which is satisfied in this case and as a result it will show 2 a prime number.

Ankit said...

hey...
sir plz tell me how to make account to chat in your chat box i wana ask questions...

Unknown said...

#include
#include
int main()
{
for(int i=1;i<=100;i++)
{
if(i%7==0)
cout<<i<<endl;
}
getch();
}


Anonymous said...

Q. how to find out given number is prime number or not?
A.
Note :
1. The number must divisible itself or divisible by 1.
2. 0 and 1 is not prime number
3. So less than 2 is invalid number to find out prime number.
4. Prime number starts with 2 ,3 ……………………..

Prime number Algorithm:
1. Check given number is less than 2 (invalid number to find prime number)
2. Check given number is less than or equal to three (if given number is 2 or 3 it is prime number)
3. Initialize array with 2,3,5,7 array size is 4.
4. Check given number is divisible by any one number from array values.
5. If it is divisible than it is not a prime number.
6. If not divisible than it is prime number.


main()
{
int arr[4]={2,3,5,7}
int given_numbr=49;
int i=0;
if(given_number <2){
printf(“Invalid number - pls choose must be number >=2 \n\n”);
exit(0);
}
If (given_number <=3){
printf(“given number is prime number \n”);
exit(0);
}
While (i<=3){
If( given_number %(arr[i]) == 0){
Printf(“Number is prime =%d \n\n”,given_number);
i++;
Break;
}
i++;
}
If(i==4){
Printf(“%d - > not prime number \n\n”,given_number);
}
}

Mahesh vk

Digital marketing kottapelli said...

include
include
main()
{
int n,
frintf("enter n value");
scanf("%d",&n);
{
if(n==0,n/2)
}
printf("primary number")
{
else
printf(" odd number")
}
getch();
}

Unknown said...

1. Write a program to find the largest sequence of numbers in an array which sums to any given number.
Example:
Consider an array: [1,2,3,5,6,7]
Given a number say 8.
Possible sequences: 3+5=8, 1+2+5=8, 2+6=8, 1+7=8, etc.
Output: (1,2,5)

Unknown said...

Write efficient code to figure out the duplicates in an array of 1 to 100?

Unknown said...

Write a program to achieve the following:
I/P: a-1-b-2-c-3(Linked list)
O/P should be: a-b-c-1-3-2
Logic should reverse every alternate element and append it to end of list.

Unknown said...

I am very thankful to this program

karkki said...

#include

void main()
{
int num;
printf("enter the value");
scanf("%d",&num);
if(num==2||num==3)
{
printf("%d is a prime number ",num);
}
else if(num%2==0||num%3==0)
{
printf("%d is not a prime number ",num);
}
else
{
printf("%d is a prime number ",num);
}
}

MEGAmind said...

char str[n];
cin >> str;
for( int i=0, j=n-1; i<j; i++, j-- )
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << str;

MEGAmind said...

I don't understand why every published code checks for prime from 2 to n/2 although it can be solved by checking divisibility of n by numbers in range [2, sqrt(n)].


#include
int main()
{
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=sqrt(num);i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}

Unknown said...

awesome

Unknown said...
This comment has been removed by the author.
Megha Jayakumar said...

34,18,10,6,4...
can anyone help with a program to generate dis sequence?

Unknown said...

program that checks if a given number is prime but does not belong to the Fibonacci series

Unknown said...

hey
why is the for loop till n/2 in the prime number question?

Unknown said...

I want tht code of prime no if i enter a range like 10 or 20 it prints 2,3,5,7 & 11,13,17,19 as output

Unknown said...

I want the flowchart for identifying prime number in c。。。。。。。please help

Unknown said...

Write a ‘C’ function to generate the following figure for n = 7.

1
1 3
1 3 5
1 3 5 7
1 3 5
1 3
1
The value of n is passed to the function as an argument. Print the triangle only if n is odd otherwise print an error message.

Unknown said...

hello sir,
I want one program code
input is an only integer value and output will be displayed only one digit in c
e g
input int a=8999
8+9+9+9=35
3+5=8

Anonymous said...

Hi sir,
Que. Write a program to print sum of the pairs of prime number is equal to the even no.
OR
Accept an even int and print the addition of the pairs of the prime number .
for Eg. Enter any Even No. : 20
Then Output will be 20 = 3 + 17
20 = 13 + 7
and so on as like....
Plz tell me the code for this question or mail me vkkumbhar291096@gmail.com or vkk291096@gmail.com