### beginner for loop question

Hi, I recently started for loop in C++ and I am stuck on a question. I'm hoping that someone will be able to help me with this.

Last edited on
This is more easily done with a while loop than a for loop. If anything, the for loop is excessive.
Yes that also what I thought, but since i'm learning for loops right now, my teacher want us to do it using for loops, so it's quite annoying
That is unfortunate, learning how to use a pizza cutter by using it to remove a splinter.

What code have you written so far?
Why is it done easier with a while loop then a for loop?

I thought for loops are better for algorithms because they execute faster?

Also show us the code you have written so far in order for us to help you :P
Last edited on
 ``123456789101112131415161718`` ``````#include int main() { std::cout << "Enter an integer number: "; int x = 0; std::cin >> x; const int base = 10; int y; for ( y = x; y > base; y = y / base - y % base ) { /* empty */ } if ( y == 0 ) std::cout << x << " is divisible by 11." << std::endl; else std::cout << x << " is not divisible by 11." << std::endl; return 0; }``````
Last edited on

Thats using modulus. Im pretty sure he wants to use that algorithm.
I suppose you could cram the whole reduction into an empty for loop:
 ``123`` `````` for(numToTest = userInput; numToTest has more than 2 digits; numToTest = numToTestShortened - whatWasChoppedOff) {}``````

This is most of the work. The rest is getting the user input and determining whether or not 11 evenly divides the value after the loop executes.

EDIT: It was practically a giveaway if I left valid C++ code.
Last edited on
By the way my code contains a bug. I did not take into account that x can be a negative number. So instead of the condition y > base in the loop there should be

y < -base || y > base

or maybe it will be simpler to write

y / base
Last edited on
@Anmol444 there is no difference in execution speed between for and while loops ;)
@LB

There isnt? Then why are their two? Cant the exact same thing be done in both?
closed account (3CXz8vqX)

For loops are typically used for things which we know are of a fixed limit.

While loops are used for things that could continue forever.

...To be honest, they are exactly the same, but it's how the programmer 'reads' it that is important in this case. Both can be made to do exactly the same thing. An example is that a for loop is used to cycle through an array, typically a while loop is not. Mostly because an array should have a fixed number... ....until you get to dynamic memory allocation in which case....
Oh ok thank you
hi everyone, sorry for the late reply. This is not due until next week, but i tried it one my own and i made something like this:

#include <iostream>
using namespace std;

int main()
{
int x;
cout << "Enter an integer number: ";
cin >> x ;

int y ;
for ( y = x ; y > 99; y = y / 10 - y % 10 )
;

cout << x << " reduced to " << y << "\n" ;

if ( y % 11 == 0 )
{
cout <<y << " is evenly divisible by 11" ;

}
else
{
cout << y << " is not evenly divisible by 11 " ;

}

return 0;
}
}

I not sure if it is right since I want to make it look like this when i input 12345678901234567900:

12345678901234567900
1234567890123456790
123456789012345679
12345678901234558
1234567890123447
123456789012337
12345678901226
1234567890116
123456789005
12345678995
1234567884
123456784
12345674
1234563
123453
12342
1232
121
11

Last edited on
closed account (3CXz8vqX)
and does it?
no i dont think so because i think i just deleted a number once and i need it to delete the last digit until it reaches 11
I think that this value 12345678901234567900 exceeds the maximum exceptable value for type int .
You should use an integer value that is not greater than INT_MAX (#include <climits>) or std::numeric_limits<int>::max (#include <limits>).
Last edited on
ok thanks for the suggestment, but i was wondering that someone was able to suggested a way to fix my code to delete every last digit until it divides into 11