C++

C++ Code to Display all prime numbers in an interval

C++ Code to Display all prime numbers in an interval

In this article, we will discuss the concept of C++ Code to Display all prime numbers in an interval

In this program, we are going to learn how to write the code to display all prime numbers between two intervals using different methods in C++ language.

This is done using for loop , while loop , do-while loop and function in C++ language

Display prime numbers between two intervals

Display prime numbers between two intervals using for loops

In this program, we will print prime numbers between two intervals  using a for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,min,max;
    cout<<"Enter two number with intevals\n";
    cin>>min>>max;

    cout<<"Prime numbers"<<min<<"to"<<max<<" are\n";

   for(i=min; i<=max;  i++){
        int counter=0;

        for( j=1; j<=i; j++){
        if(i%j==0){
            counter++;
        }

    }
    if(counter==2){
        cout<<i<<" ";
    }

    }
getch();
    return 0;
}



When the above code is executed.it produces the following result

Enter two number with intervals
30
70
Prime numbers 30 to 70 are:
31 33 37  41 43 47 53 59 61 67

 

Display prime numbers between two intervals using while loop

In this program, we will print prime numbers between two intervals  using a while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,min,max;
    cout<<"Enter two number with intevals\n";
    cin>>min>>max;

    cout<<"Prime numbers "<<min<<" to "<<max<<" are\n";
    i=min;
   while(i<=max){
        int counter=0;
        j=1;
        while(j<=i){
        if(i%j==0){
            counter++;
        }
         j++;
    }
    if(counter==2){
        cout<<i<<" ";
    }
     i++;
    }
getch();
    return 0;
}



When the above code is executed.it produces the following result

Enter two number with intervals
35
55
Prime numbers 35 to 55 are:
37 41 43 47 53

 

Display prime numbers between two intervals using do-while loop

In this program, we will print prime numbers between two intervals  using a do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,min,max;
    cout<<"Enter two number with intevals\n";
    cin>>min>>max;

    cout<<"Prime numbers"<<min<<"to"<<max<<" are\n";
    i=min;
   do{
        int counter=0;
        j=1;
        do{
        if(i%j==0){
            counter++;
        }
         j++;
    }while(j<=i);
    if(counter==2){
        cout<<i<<" ";
    }
     i++;
    } while(i<=max);
getch();
    return 0;
}


When the above code is executed.it produces the following result

Enter two number with intervals
40
80
Prime numbers 40 to 80 are:
41 43 47 53 59 61 67 71 73 79

 

Display prime numbers between two intervals using function

In this program, we will print prime numbers between two intervals  using a function in C++ language

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int printPrime(int lower, int upper){//function definition
    int i,flag;
    while(lower<=upper){
    flag=0;
    for(i=2; i<=lower/2; i++){
            if(lower%i==0){
                flag=1;
                break;
            }
    }

    if(flag==0)
        cout<<lower<<" ";
   lower++;
}
    }
int main(){
    int lValue,uValue;
    cout<<"Enter two number with intervals\n";
    cin>>lValue>>uValue;

    cout<<"Prime numbers between"<<lValue<<" to "<<uValue<<" are: \n";

   printPrime(lValue,uValue);//function call
   getch();
return 0;
}


When the above code is executed.it produces the following result

Enter two number with intervals
25
70
Prime numbers 25 to 70 are:
29 31 37 41 43 47 53 59 61 67


Suggested post

Operator in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.