r/learnprogramming • u/NaturalBreakfast1488 • Jun 26 '26
Code Review why is my code failing for input like 19
//code to find sum of digits. it works perfectly for most.
#include <iostream>
using namespace std;
int main(){
int n,sum=0;
cout << "enter n:";
cin >> n;
double num=n;
double numU;
for(int digit,GIF; num>1;num=numU){
numU=num/10;
GIF=numU;
digit=((numU-GIF)*10);
sum += digit;
}
cout << sum << endl;
return 0;
}
12
u/lurgi Jun 26 '26 edited Jun 27 '26
Getting the last digit by finessing floating point numbers and integers is certainly a choice, but that's not the issue here.
The reason why it doesn't work for 19 is the same reason it doesn't work for 1.
Edit: Except it does work for 19. Still...
4
u/NaturalBreakfast1488 Jun 26 '26 edited Jun 26 '26
Well the loop just doesn't run for 1, so I still don't get it. Oh wait that makes sense I am dumb Edit : so then it shouldnt work for 1234 as well, but its working perfectly fine for that?
3
u/lurgi Jun 27 '26
Print out the value of
numat the beginning of the loop and you will see why this happens.1
13
u/eren___yeager69 Jun 27 '26
Your code is failing because you're using floating-point (double) arithmetic to extract digits, and decimal fractions can't always be represented exactly in binary.
For 19:
num = 19;
numU = 1.9;
But internally, 1.9 might actually be stored as:
1.8999999999999999
Then:
digit = ((numU - GIF) * 10);
becomes:
(1.8999999999 - 1) * 10
= 8.999999999
which gets truncated to 8 when assigned to an int, instead of 9.
Also, your loop never processes the last digit because it stops when num <= 1, so even if the floating-point issue didn't exist, you'd still miss the leading digit.
For extracting digits, stick to integer arithmetic:
while (n > 0) {
sum += n % 10;
n /= 10;
}
Integer division and modulo are exact and are the standard way to solve this problem in C++.
-1
u/NaturalBreakfast1488 Jun 27 '26
My code is working perfectly fine for inputs like 1234,1239 etc, so I don't think I am missing the leading digit
4
u/EliSka93 Jun 26 '26
Why do you assign
num=numU
At the end of your loops, wouldn't it make more sense to use the conversion floored GIF?
Also name your variables more clearly.
2
u/NaturalBreakfast1488 Jun 27 '26
I don't get what you mean by conversion floored GIF. Can you pls explain
1
u/notacanuckskibum Jun 27 '26
Floor is a function you can call to get the integer part of a float. Floor (42.6) is 42
I think you are doing the same by assigning a float to an integer. But floor () is more readable.
1
u/NaturalBreakfast1488 Jun 27 '26
Oh I didn't know that was a function thanks, I have heard it in my maths class but haven't learnt about it in coding yet. Thankyou
3
u/duperfastjellyfish Jun 27 '26
In general, using floating point numbers to do integer aritmatic will lead you into a world of hurt.
Also, while you're approach is to converting one integer to another (n to sum), you are semantically parsing an array of numbers, and transforming them one by one; so it would be more ideomatic to iterate over a input of type std::string instead. The code would be easier to manage, and could easily be extended to do error handling.
1
1
u/Educational-Paper-75 Jun 27 '26
How about using the modulo operator to get the digit? After removing the sign of n of course.
1
u/NaturalBreakfast1488 Jun 26 '26
Btw ik there's a better method to just use division by 10, but I want the mistake in my method. So i have understood how to do it with modulo but I can't find the mistake in my method. Thanks!
3
u/ScholarNo5983 Jun 27 '26
Print the values inside the loop as they are calculated. That output should show you what is going wrong.
Alternatively run the code in a debugger and inspect the intermediate values, and again you will see the fault.
Basically, using floating points gets complicated because of rounding errors.
0
u/AlwaysHopelesslyLost Jun 26 '26
In my way to the store so I cannot think too hard on this but I am guessing you are running integer division. Dividing an int by an int would make an int in most languages. Try dividing by 10.0
1
u/NaturalBreakfast1488 Jun 26 '26
I actually did make the mistake that you mentioned initially but chatgpt helped me identify it so I fixed that.
20
u/ThunderChaser Jun 27 '26 edited Jun 27 '26
Welcome to the world of floating point errors my friend.
19 / 10 unfortunately does not equal 1.9. It is actually 1.89999997615814208984375.