[C/C++] Program to Find Prime Number Between Given Range

What is a Prime Number?
” A Natural number greater than 1 which has only two divisor 1 and itself is called prime number “.
For Example:
5 is prime, because it has only two divisors 1 and itself.

C Code

#include <stdio.h>
#include <math.h>
#include <conio.h>

void main()
{
 int startNum,endNum;
 int total=0;
 printf("Enter Number START of Range: ");
 scanf("%d", startNum);
 printf("Enter Number END of Range: ");
 scanf("%d", endNum);
 for(int i=startNum;i<=endNum;i++)
 {
 int count=0;
 for(int j=2;j<=sqrt(i);j++)
 {
 if(i%j==0)
 { count++; break; }
 
 }
 if(count==0 && i>1)
 { 
 total++;
 printf("Prime Number -> %d\n", i);
 }
 }

 printf("Total Prime Number Between %d to %d = %d\n", startNum, endNum,total);
 
 getch();
}

C++ Code

#include<iostream>
#include<math.h>

using namespace std;

int main()
{
 int startNum,endNum;
 int total=0;
 cout<<"Enter Number START of Range: ";
 cin>>startNum;
 cout<<"Enter Number END of Range: ";
 cin>>endNum;
 for(int i=startNum;i<=endNum;i++)
 {
 int count=0;
 for(int j=2;j<=sqrt(i);j++)
 {
 if(i%j==0)
 { count++; break; }
 
 }
 if(count==0 && i>1)
 { 
 total++;
 cout << "Prime Number -> " << i << endl;
 }
 }

 cout<<"Total Prime Number Between " << startNum 
 << " to " << endNum << " = " << total << endl;
 
 return 0;
}

Output:

program-to-find-prime-number-in-given-range

Other Related Search Terms

  1. Code For Prime Number In C++
  2. Code For Prime Number In C
  3. C++ Programs to find Prime number
  4. C Programs to find Prime number
  5. Find all Prime numbers between 1 to 100

Categories: C Language,C Programs,Computer Science

Tags: ,,

2 Comments

  1. #include
    #include
    void main(void){
    clrscr();
    int flag=0,num;
    printf(“Enter range = “);
    scanf(“%d”,&num);
    for(int i=2;i<=num;i++)
    {
    for(int j=2;j<=i/2;j++)
    {
    if(i%j==0){
    flag++;break;}
    }
    if(flag==0){
    printf(" %d ",i);
    }
    flag=0;

    }
    getch();
    }

    Reply
  2. Arslan Majeed

    #include
    #include
    main ()
    {
    int up,low,i,j,sum,flag,pnum;
    pnum=0;
    sum=0;
    flag=0;
    cout<>low;
    cout<>up;
    //for inputting the vlues again and again till user input all the values true..
    while(lowup)
    {
    if(low<2)
    {
    cout<<"THE lower value must be grater then 1\n";
    cout<>low;
    cout<>up;
    }
    else
    {
    cout<<"THe Uper limit must be grater than the lower\n";
    cout<>low;
    cout<>up;
    }
    }
    cout<<"\n THe prime numbers B/W the Range are: \n\n ";
    //findin the prime number…
    for(i=low; i<=up; i++)
    {
    for(j=2; j<=i/2; j++)
    {
    if(i%j==0)
    {
    flag++;
    break;
    }
    }
    if(flag==0)
    {
    cout<<i<<"\t";
    //counting the total prime numbers within the range…
    pnum++;
    // calculatting the sum of all prime numbers within the range..
    sum += i;
    }
    flag=0;
    }
    cout<<"\n\n\nTotal number of Prime number B/W the range are: "<<pnum;
    cout<<"\n\n\nTHe sum of all Prime numbers B/W the range is: "<<sum;
    getche();
    }

    Reply

Leave A Reply

Your email address will not be published.

×