FOR - skip first and last

Good day,

This little code outputs numbers which ends with number 7.
Code works just fine but there's one task which still needs to be accomplished.
From interval (a;b) I enter a = 1 and b = 30. it outputs 7, 17, 27. Which is good. But now the thing, i have to skip,ignore first(a) and last number(b) so program would only output number 17. i hope you did catch what i'm trying to say.
Can someone please lead me on how it can be done?

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
#include <iostream>
using namespace std;

int main()
{

	int a, b;
	int s = 0;

	cout << "Enter number 1: ";
	cin >> a;
	cout << "Enter number 2: ";
	cin >> b;
	cout << endl;
	
for (a; a <= b; a++)
{
	if (a % 10 == 7)
	{
		cout << a << endl;
		s++;
	}
}
cout << endl;
cout << "Count of sevens  = " << s;
cout << endl << endl;
	
return 0;

}


Thank you, Edvards.
Fourth effing try. Apparently you don't want a solution.

You'd like to work with the keyword continue which skips the current iteration of the loop and goes to the next one.

In your if statement you can include another if statement that checks for the first and last fitting number. The first is quite easy and can be accomplished by another variable. For the second one you'd have to do some math, I guess. Something like this: if(variableIsTrue? || someMath) {...}
Last edited on
After the user enters a and b, you should:
- increment a until it ends with 7.
- decrement b until it ends with 7.
- print the numbers ending with 7 that are between the new values of a and b.
Hi again,

im stuck here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (a; a <= b; a++)
{
	for (a; a != 7; a++)
	for (b; b > 7; b--)
	{
		
		if (a % 10 == 7)
		{
			cout << a << endl;
			s++;
		}

	}
}


did i wrote it correctly incrementing and decrementing a and b?
You lack the brackets for your second loop.
Also you adjust a and b then you find all the numbers between.
Basically you are altering a and b, then running a search for numbers.

So try this order of steps,
Open function/program
Create variables
Get user to give values for a and b
Make a the lesser and b the larger (in case user enters them backwards)
Adjust a to next number ending in 8
Adjust b to previous number ending in 6
Use loops to find all numbers between a and b ending with 7
Print results
End function/program

Setting a and b to end in 8 and 6 means you don't have to worry about excluding a and b from checks later. A simplicity thing rather than a necessity.
I meant like this. After you enter a and b:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    // Increment a until it ends in 7
    while (a%10 != 7) {
        ++a;
    }

    // decrement b until it ends in 7
    while (b%10 != 7) {
        --b;
    }

    // Now print the numbers ending in 7 that are between
    // a and b (exclusive)
    for (++a; a < b; ++a) {
        if (a % 10 == 7) {
            cout << a << endl;
            s++;
        }
    }
1
2
3
4
std::cin >> a >> b ;
a += 10 ; // 7 ;
b -= 10 ; // 7 ;
// the loop as in your original program 
Last edited on
[insert face palm]. JLBorges is right. That's much easier. The only caveat is that you need to increment/decrement by 10, not 7 since you want to skip base 10 numbers that end in 7.
Yes, thanks. Corrected now. (Misread the problem as print multiples of 7)
If dhayden inserts a facepalm, then I'm inserting a quadruple facepalm. Amazing how easy it can be solved actually :)
Did use the same loop for (a = a + 1; a < b; a++) added a = a + 1, but this works only if you enter specific 7 and 57, then it will print numbers in middle.

But i will go for JLBorges code, that's more likely what im trying to accomplish.

That looks so simple :( Lesson learned, very big thanks to all of you guys.

Topic archived. No new replies allowed.