dealing with some logic errors pt 2

Nov 5, 2016 at 10:54am
Hello again. This time I had done a prog that will print out prime number, even number and also odd number from specific intervals. My problems were the output for prime number and even number were not printed out at all. Hope to see some good comments about my problems. Thanks in advance!

[code]
#include <iostream>
#include <cmath>
using namespace std;

int main()
{ int int1,int2,i,j,k;
cout << " Please enter two integers ";
cin >> int1 >> int2;
cout << " the odd numbers between the integers are : ";
for(i=int1; int1<int2; int1++){
if(int1%2!=0){
cout << int1 << " ";
}
if(int1%2==0)continue;
}
cout << endl;
cout << " The even number between the integers are : ";
for(j=int1; int1<int2; int1++){
if(int1%2==0){
cout << int1 << " ";
}
if(int1%2!=0)continue;
}
cout << endl;
cout << " The prime number between the integers are : ";
for(k=int1; int1<int2; int1++){
int1%(int1-1);
if(int1%(int1-1)==0)continue;
if(int1%(int1-1)!=0){
cout << int1 << " ";
}
}

return 0;
}
Nov 7, 2016 at 2:53am
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
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

bool isprime(int a)
{
	int i;
	int nFactors = 0;
	for(i = 1; i <= a; i++) if(a % i == 0) nFactors++;

	return (nFactors == 2);
}

int main()
{
	int i;
	int a, b;
	cout << "Please enter a and b (a < b) : " << endl;
	cout << "- a : "; cin >> a;
	cout << "- b : "; cin >> b;

	cout << endl;
	cout << "+ Odd numbers between " << a << " and " << b << " : ";
	for(i = a; i <= b; i++) 
	{
		if(i % 2 == 1) cout << i << ' ';
	}
	cout << endl;

	cout << "+ Even numbers between " << a << " and " << b << " : ";
	for(i = a; i <= b; i++) 
	{
		if(i % 2 == 0) cout << i << ' ';
	}
	cout << endl;

	cout << "+ Prime numbers between " << a << " and " << b << " : ";
	for(i = a; i <= b; i++) 
	{
		if(isprime(i)) cout << i << ' ';
	}
	cout << endl;

	return 0;
}


Please enter a and b (a < b) :
- a : 2
- b : 25

+ Odd numbers between 2 and 25 : 3 5 7 9 11 13 15 17 19 21 23 25
+ Even numbers between 2 and 25 : 2 4 6 8 10 12 14 16 18 20 22 24
+ Prime numbers between 2 and 25 : 2 3 5 7 11 13 17 19 23

http://cpp.sh/4l2yq
Nov 7, 2016 at 3:14am
for(i=int1; int1<int2; int1++){
Here you are comparing and increasing int1 when you should be comparing and increasing i. When you reach the other loops, int1 < int2 is already true, so the body of the loops do not execute.
Nov 8, 2016 at 11:07am
Got it. Thanks a lot!
Topic archived. No new replies allowed.