HomeC++C++ Program to Display Prime Numbers Between Two Intervals

C++ Program to Display Prime Numbers Between Two Intervals

In this post, you will learn how to Display Prime Numbers Between Two Intervals in C++.

This lesson will teach you how to display Prime Numbers between two intervals, with a for loop, logical operator and decision making statement using the C++ Language. Letâ€™s look at the below source code.

Source Code

```#include <iostream>
using namespace std;
int main()
{
int a, b, i, j, flag;
cin >> a;
cout << "Enter lower bound of the interval: "<<a;
cin >> b;
cout << "\nEnter upper bound of the interval: "<<b<<endl;
cout << "\nPrime numbers between "
<< a << " and " << b << " are: ";
for (i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
cout << i << " ";
}
return 0;
}```

```10
15```

Output

```Enter lower bound of the interval: 10
Enter upper bound of the interval: 15
Prime numbers between 10 and 15 are: 11 13```

The statementsÂ #include<iostream>, using namespace std, int mainÂ are the main factors that support the function of the source code. Now we can look into the working and layout of the codeâ€™s function.

1. Declare the variablesÂ a, b, i, j, flagÂ anÂ integersÂ and collect the initial and final number; the upeer bound and lower bound from the user and store it in theÂ a and b respectivelyÂ using function `cin>>`Â  and display the value using `cout<<`Â and the Insertion Operators'<<â€˜ , â€˜>>â€™.
2. Create aÂ for loop with the condition `(i = a; i <= b; i++)` , and then we create the loop statement.
3. Using the decision making if else statements we declare the condition to be verified. In the condition theÂ logical operator ORÂ is used. When both or any one of the sides of the conditions is true then the if condition is true, or it is false.
4. TheÂ continueÂ statement is used to force the loop to continue,Â  if the if condition is true, the next iteration is executed if not it moves to the next statement.
5. Create aÂ for loopÂ with the condition `(j = 2; j <= i / 2; ++j)` , and then we create the loop statement which includes anÂ if statementÂ with a condition to be verified.
6. When the condition is verified the body of the if statement is executed. Finally we add anotherÂ if statementÂ with a condition to be verified and an output statement.

Note: The â€˜ << endl â€˜ in the code is used to end the current line and move to the next line and â€˜\nâ€™ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

You May Also Like

C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022