Change the code to output with the min
Nov 4, 2014 at 2:42pm UTC
My desired output:
Enter lower limit: 3
Enter upper limit: 10
3
5
7
My output in my current program:
Enter lower limit: 3
Enter upper limit: 10
2
3
4
7
How to fix this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
using namespace std;
void prime_num(int );
int main()
{
cout << "Enter lower limit: " ;
int num1 = 0;
cin >> num1;
cout << "Enter upper limit: " ;
int num = 0;
cin >> num;
prime_num(num);
system("pause>0" );
return 0;
}
void prime_num(int num)
{
bool isPrime = true ;
for (int i = 2; i <= num; i++)
{
for (int j = 2; j <i; j++)
{
if (i % j == 0)
{
isPrime = false ;
break ;
}
}
if (isPrime)
{
cout << i << endl;
}
isPrime = true ;
}
}
Nov 4, 2014 at 2:53pm UTC
woot!~ I figured it out!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
using namespace std;
void prime_num(int , int );
int main()
{
cout << "Enter lower limit: " ;
int num1 = 0;
cin >> num1;
cout << "Enter upper limit: " ;
int num = 0;
cin >> num;
prime_num(num1, num);
system("pause>0" );
return 0;
}
void prime_num(int num1,int num)
{
bool isPrime = true ;
for (int i = num1; i <= num; i++)
{
for (int j = 2; j <i; j++)
{
if (i % j == 0)
{
isPrime = false ;
break ;
}
}
if (isPrime)
{
cout << i << endl;
}
isPrime = true ;
}
}
Topic archived. No new replies allowed.