any one help explain how dd this can recieve and answer of 28 than 36 pleas been try half of the day

Suppose the input value for type is the character‘/’. What is the value of value after the following C++ code has been executed? (2)
int value = 5;
char type;
cin >> type;
switch (type)
{
case '+': value += 5;
case '/': value = 100/value;
case '-': value = value/2;
case '%': value += value % 2 + 4;
default: value += value / 3 + 10;
}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) You have neglected to put break statements in your cases. This means that code execution will continue from one block to the next, so that all of the following lines will be executed:

7
8
9
10
value = 100/value;
value = value/2;
value += value % 2 + 4;
value += value / 3 + 10;


We often describe this as "falling through" from one case to the next.
Last edited on
this is how the code was provided on the question and their answer is 28 so im struggking to understand how they got to 28
What's confusing you?

line 7: 100/5 = 20
line 8: 20/2 = 10
line 9: 10 + (10 % 2) + 4 = 14
line 10: 14 + 14/3 + 10 = 28
Last edited on
Thank you so much very clear
You're welcome!
one more help I am Biginner pls bear with me, I am struggling to understand how this arrive at 13 as an answer when u subtitute 2 for a and count will be zero if Im following well then it will return a value of 3 then do I use the 3 again which count will be1 this time

#include <iostream>

using namespace std;

int result(int valueP)
{
int count = 0;
int a = 2;
while (count < valueP)
{
a += count + a / 2;
count += 2;
}
return a;
}

int main ()
{
cout << result(6) << '\n';
return 0;
}
I've already asked you to use code tags when posting code. Please edit your post to do so.

EDIT: Also, duplicate post:

http://www.cplusplus.com/forum/general/223146/

Don't do that. It waste's people's time.
Last edited on
Topic archived. No new replies allowed.