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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.