Remainder Code

Hello!
Okay so here is my code, everything is working dine except the while loop.
The purpose of this program is to input a number "40235" and then dividing it by 10, taking it's remainder (5), subtracting it from main number, and then dividing it by 10, to get it it's perfect quotient i.e 4023. I did managed to run the code, but while loop is not iterating.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
#include <iostream>
using namespace std;

int main()
{
    int num, remainder_1, a, subtract;
    cin>>num;
    
    
    while(num!=0)
    {
        remainder_1 =num%10;
        num=num-remainder_1;
        a =num/10;
        cout<<a<<endl;
    }
    return 0;
}
Last edited on
Read lines 8 and 11 physically out loud to yourself (or, if you prefer, to a rubber duck) and explain what they do.
I bolded it because I'm not joking. It helps.
https://en.wikipedia.org/wiki/Rubber_duck_debugging

(edit: Original code had while(num==0) as the condition)

Once you solve that, you might also have an infinite loop. To fix this, focus on two things:
1. How should the loop initially start?
2. How should the loop be able to end?
Last edited on
Ah Yes, Ganado, I sort of forgot to write it here. I rectified my mistake xD
The main point is to keep dividing it by 10 and subtracting it, until we get zero. i.e 4/10, whose remainder (4) is again subtracted from 4/10, making it zero. Getting our final answer. and in the end we have to sum all teh remainder (but that's another talk because first I need to figure out the while loop working)
Don't modify your first post, it just makes it confusing to others reading this after the fact.
https://stackoverflow.com/questions/59587919/how-to-run-while-loop-here
Last edited on
Oh alright, sorry I didn't thouht about it n that way.
It's fine, but a question: How will num ever be 0 if num is initially, say, 100?
Thank You so much,
Figured it out



#include <iostream>
using namespace std;

int main()
{
cout<<"Program to yield exact quotient and sum of remainders: "<<endl;
int num, remainder_1, a, subtract;
cout<<"Enter a number: "<<endl;
cin>>num;
int sum;

while(num!=0)
{
remainder_1 =num%10;
num/=10;
cout<<num<<endl;
sum+=remainder_1;

}
cout<<"The Sum is: \t"<<sum<<endl;
return 0;
}
Well, I got confused, that's all I can say xD
... and with a small tidy up and code tags we get ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int main()
{
    cout << "Program to yield exact quotient and sum of remainders\n"; // <--
    int num = 0, remainder_1 = 0; // <--
    cout << "Enter a number: "; // <--
    cin >> num;
    
    int sum = 0; // <--
    
    while(num != 0)
    {
        remainder_1 = num % 10;
        num /= 10;
        cout << num << '\n';
        sum += remainder_1;
        
    }
    cout<< "The Sum is: " << sum << '\n'; // <--
    
    return 0;
}


Suggest:
- '/n' is better than endl
- delete unused variables
- whitespace helps readability
- always initialise variables
- answers to prompts appear on same line as prompt

:)
Topic archived. No new replies allowed.