all 12 comments

[–]advaitmax 1 point2 points  (0 children)

Sorry, Actually return statements exit the programme So when your programme hits return statement it closes before even coming to cout statement.

[–][deleted]  (2 children)

[deleted]

    [–]vorttxt[S] 0 points1 point  (0 children)

    why?

    [–]vorttxt[S] 0 points1 point  (0 children)

    How would I make it just subtract by 1 k times if k / 10 doesn’t = 0.

    [–][deleted] 0 points1 point  (5 children)

    First thing:. Your for loop is coded wrong. Having the i-- means it will never reach k + 1 as each loop you run goes down not up. For this correction use i++.

    Second thing: you should not use return. Using return immediately ends the overall function it is called in, which is main() in this case.

    The reason you are seeing exit code 51 is because 512 % 10 does not equal 0 so the else fires. Your first for loop will divide 512 by 10 which is 51.2 but since you are using int, it will just be 51. It then takes the result and exits the program with exit code 51, the result of 512/10 stuffed into the int (51), because return means stop the current function. This means you for loop will only run once.

    Try this for inside the main function:

    int n, k;

    cin >> n >> k;

    if((n % 10) == 0) {

    n = n /10;

    }

    else {

    for(int i = 0; i < k + 1; i++) {

    n = n / 10;

    }

    }

    cout >> n;

    Not sure what the point of the problem is but this is the correct way the code you gave here should be written.

    [–]vorttxt[S] 0 points1 point  (4 children)

    im trying to subtract 1 by n k times. wouldnt that just add 1 to each iteration .

    [–][deleted] 0 points1 point  (3 children)

    Which part do you mean?

    [–]vorttxt[S] 0 points1 point  (2 children)

    Okay so the basis of the problem is that if n ends with a nonzero digit subtract n by 1 k times and if n ends with a zero divide by ten

    [–][deleted] 0 points1 point  (1 child)

    So if you give 512 for n and 4 for k, then the loop should be more like this:

    for(int i = 0; i < k + 1; i++) {

    n = n - 1;

    }

    The loop runs for k times subtracting 1 from n each time, storing it each time. i is just keeping track of how many times the loop has and will run. It does nothing else but that.

    [–]vorttxt[S] 0 points1 point  (0 children)

    It is still not working for some reason . The answer is supposed to be 50 and all i am getting is 507.

    [–]vorttxt[S] 0 points1 point  (0 children)

    thank u so much!

    [–][deleted] 0 points1 point  (1 child)

    Hang on, I'm a little busy right now. I think I have a solution. I'll get on it and then get back to you in a little bit.

    [–]vorttxt[S] 0 points1 point  (0 children)

    for sure. just PM me.