i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
take a temporary variable to store sum and initialize it to zero,
Now by recursively taking modulus and dividing the number you can each
digit separated and you can go on adding the digit..,
@$|-|. DUBEY said:i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
right...
can u explain with code...
i didn't understand.....
Yes:****Can anyone find the error
Hint:
val % 10 is the rightmost digit of val.
val / 10 shifts all digits to the right.
i got it you saved my day........
this group rocks.!!!!!!!!!!!!!!!1
@$|-|. DUBEY said:i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
It's difficult to imagine how adding the digits of the decimal
representation of an integer could be "urgent".
Unless, of course,
you're asking us to do your homework for you.
i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
... and it's due today.It's difficult to imagine how adding the digits of the decimal
representation of an integer could be "urgent". Unless, of course,
you're asking us to do your homework for you.
i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
main()
{
int a,b,sum=0;
printf("enter the number\n");
scanf("%d", &a);
while(a>0)
{
a=a%10;
sum=sum+a;
a=a/10;}
printf("%d", sum);
getch();
}
Hyuga said:Doesn't compile on my system:
Which I suppose is fine, because the OP can hardly learn anything if
you do his homework for him. Or maybe that was your point?
Eric Sosman said:Keith said:@$|-|. DUBEY said:i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
It's difficult to imagine how adding the digits of the decimal
representation of an integer could be "urgent". [...]
The O.P. has been interviewing for jobs in Frankfurt, and has
met with nothing but rejections. He's trying to overcome them by
casting out neins.
Keith said:It's difficult to imagine how adding the digits of the decimal
representation of an integer could be "urgent". Unless, of
course, you're asking us to do your homework for you.
Another possibility - he is trying to develop a check-digit (or a
check-digit checking) system. Not likely, but feasible.
Hope it doesn't invoke any UB
/* given eg 54321, return 1 + 2 + 3 + 4 + 5. */
int my_sum(int n)
{
int sum = 0;
for (; n; n /= 10)
sum += n % 10;
return sum;
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.