Multiples of 4

Hey guys, here is the basis of my program:

float x; //start number
float y; //end number

cout<< "Hello, please enter a whole number that will be the starting number. \n" <<endl;
cin>>x;
cout<< "Please enter a whole number that will be the ending number. \n" <<endl;
cin>>y;

for ( ; x<y; x+=4) //counts in multiples of 4
{
cout<<"\n"<< x << "\n"; //displays the multiples
}

if(x>=y) //ends at the ending variable
{
cout<<y; //need to figure out how to get it to stop before y if y isn't a multiple of 4
}

How would I be able to get the end number to not show if it is not a multiple of 4, but show if it is a multiple of 4?
if((x % 4) == 0) cout << x << " is a multiple of 4\n";
Last edited on
This helps greatly, but how would I be able to display the end number if it is a multiple of 4? Where would I need to add cout<<y?
My code shows exactly how to display a number if it is a multiple of 4.
It works but doesn't show the end # if it is a multiple of 4

i.e.
14 and 54

shows all the way to 50, but not 54. See what I'm saying?
So, you're wanting to see if the last (ones) digit of a number is a multiple of 4?
Only numbers which end in 4 or 8 *have end numbers which are multiples of 4. You want n = 4 and then to output n and then n + 4 = n (which is 8) and then n = n + 16 (which is 24) and then n = n + 4 (28), n = n + 16 (44) etc...

This is easily accomplished in a while loop.

Edit: *Clarified
Last edited on
Only numbers which end in 4 or 8 are multiples of 4

This is false. 12, multiple of 4, does not end in 4 or 8.

Anyways, OP needs to figure out how to get his question out in a clearer manner. I don't think either of us understand what he's asking.
I meant of course, only numbers which end in 4 or 8 have last digits which are multiples of 4.

But yes, the OP does need to clarify, if their question is not yet answered.
Last edited on
OP's question is pretty straight-forward. He even gave examples.

First, don't use floats -- they'll goof up. Use integers (int or unsigned or something like that).

Second, your basic loop continues while x < y, meaning that it does NOT continue even if x == y. To change the end condition, change the comparison to include equality:

1
2
3
4
for (; x<=y ; x+=4)
{
  cout << x << "\n";
}


Hope this helps.
Yeah, this fixed it Duoas. Thanks!
Topic archived. No new replies allowed.