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?
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) {...}
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.
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.
// 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++;
}
}
[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.
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.