Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    int sock = n;
    int count = 0;

   while (sock > 0) {
        count+=1;

        if (sock % m == 0) {
            sock++;
        }
            sock--;
    }

    cout << count << endl;

    return 0;
}


What I have tried:

actually i was workink with while loops but there is some problem in my code there is no printing anything i have checked it with different approaches but no solution is coming
Posted
Comments
0x01AA 8-Jan-24 11:55am    
If I enter '4' and '6' then it prints '4'.
If I enter '6' and '4' then it loops endless.
So, think about your target and use the debugger

I think the problem is here:
C++
if (sock % m == 0) {
    sock++;
}
    sock--; // If you add 1 to sock and then subtract 1 it will go on for ever

but I am not sure what those lines of code are supposed to do.
 
Share this answer
 
Comments
CPallini 9-Jan-24 2:15am    
5.
As soon as sock is divisible by m, it is incremented and immediately decremented again - so the next loop iteration has the same value of sock and the loop never exits.
Since the loop never exits, the code never reaches the cout and you never see any output.

To be honest, 2 minutes with the debugger would have shown you that for yourself: get used to testing your code in the debugger as it's the best development friend you have!
 
Share this answer
 
v2
Comments
CPallini 9-Jan-24 2:15am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900