C++
Please and thank you. I will upvote
Read the following problem, and answer questions.
#include<iostream>
using namespace std;
void shownumbers(int, int);
int main()
{
int x, y;
cout << "Enter first number : ";
cin >> x;
cout << "Enter second number : ";
cin >> y;
shownumbers(x, y);
return 0;
}
void shownumbers(int a, int b)
{
bool flag;
for (int i = a + 1; i <= b; i++)
{
flag = false;
for (int j = 2; j<i; j++)
{
if (i%j == 0)
{
flag = true;
break;
}
}
if (flag == false && i>1)
cout << i << endl;
}
}
1. What does the function shownumbers implement?
2. If the inputs for x and y are 3 and 25 respectively, what are the outputs if running the program?
Solution
1)
in this program the range of numbers is taken as input and stored in the variables ‘x’ and ‘y’.
in shownumbers function using for-loop, the numbers between the interval of "a" &"b" are traversed.
For each number in the for loop, it is checked if this number is prime or not.
If found prime, print the number.
Then the next number in the loop is checked, till all numbers are checked.
---
2)
It will print prime numbers between 3 and 25
Enter first number : 3
Enter second number : 25
5
7
11
13
17
19
23
---
if you need any furthe help, let me know, love to help
all the best
Get Answers For Free
Most questions answered within 1 hours.