By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

help solving addition problem in C

P: n/a
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
Oct 14 '08 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On Oct 14, 11:30*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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..,
Oct 14 '08 #2

P: n/a
On Oct 14, 11:39*am, Pranav <pranav...@gmail.comwrote:
On Oct 14, 11:30*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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..,


can u explain with code...
i didn't understand.....
Oct 14 '08 #3

P: n/a
"@$|-|. DUBEY" <du***********@gmail.comwrites:
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
Hint:

val % 10 is the rightmost digit of val.
val / 10 shifts all digits to the right.
Oct 14 '08 #4

P: n/a
On 14 Oct, 07:48, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
On Oct 14, 11:39*am, Pranav <pranav...@gmail.comwrote:
On Oct 14, 11:30*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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...

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..,

can u explain with code...
i didn't understand.....
not only do you want help with your homework but you want the code as
well!
Another approach would be to use sprintf()

--
Nick Keighley
****Can anyone find the error
Yes:
Error on line 0: Lazy programmer.
(comp.lang.c++)
Oct 14 '08 #5

P: n/a
On Oct 14, 11:55*am, Nate Eldredge <n...@vulcan.lanwrote:
"@$|-|. DUBEY" <dubey.ashw...@gmail.comwrites:
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

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
Oct 14 '08 #6

P: n/a
On Oct 14, 12:05*pm, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
On Oct 14, 11:55*am, Nate Eldredge <n...@vulcan.lanwrote:
"@$|-|. DUBEY" <dubey.ashw...@gmail.comwrites:
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
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
Thats good Always try to solve on your own or by following an
algorithm never expect a valid code.., It will help you a lot,
Oct 14 '08 #7

P: n/a
"@$|-|. DUBEY" <du***********@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 14 '08 #8

P: n/a
On 14 Oct, 08:33, Keith Thompson <ks...@mib.orgwrote:
"@$|-|. DUBEY" <dubey.ashw...@gmail.comwrites:
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". *
perhaps he's a number theorist or something. Tho' a 5
digit number seems kind of small. Ah! perhaps "interger"
isn't meant to be "integer" but indicates some other
mathematical object with more complex properties
(tho' Plain Old Integers a pretty complicated).
We need more info about these inergers of which you
speak.

Unless, of course,
you're asking us to do your homework for you.
good grief!

--
Nick Keighley

Many astrologers think that this concentration on [the sun-sign
column] has
done untold damage to serious astrology.
The Independent
Oct 14 '08 #9

P: n/a
On Oct 14, 2:30*pm, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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
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;
}
Oct 14 '08 #10

P: n/a
Keith Thompson said:
"@$|-|. DUBEY" <du***********@gmail.comwrites:
>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".
<entire Star Trek episode snipped before posting, with extreme prejudice>

The above seemed like a good idea at the time, but didn't turn out all that
well (I haven't had much practice writing ST scripts!), and I consigned it
to the bit bucket for the benefit of humanity. Suffice to say that they're
hiding in a ventilation shaft in an extremely expensive set^W^W alien city
on an SG-4 planet[1] and, for excellent reasons which never become readily
apparent, if they don't find the digital root of this astoundingly
enormous number *really* quickly, the Klingons will get the victory, the
universe, and the girl. The hero of the hour is a young programmer
named... well, that would be giving too much away, but anyway, although he
doesn't know how to calculate digital roots (this knowledge having long
since been lost to the human race), he *does* know how to open a vortex in
space-time and hack into 21st century Usenet.

[1] A planet's SG-rating is like closures. If you know, you know. If you
don't know, you don't want to.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 14 '08 #11

P: n/a
On Mon, 13 Oct 2008 23:30:50 -0700 (PDT), "@$|-|. DUBEY"
<du***********@gmail.comwrote:
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
I think that if you are adding the digits you need an intrager rather
than an interger.

Tony
Oct 14 '08 #12

P: n/a
In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.orgwrote:
>"@$|-|. DUBEY" <du***********@gmail.comwrites:
>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.
... and it's due today.
Oct 14 '08 #13

P: n/a
On Oct 14, 11:30*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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();
}
Oct 14 '08 #14

P: n/a
On Oct 14, 7:03*am, ssksakthi -image processing <ssksak...@gmail.com>
wrote:
On Oct 14, 11:30*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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();

}
Doesn't compile on my system:

~$ gcc -Wall sumdigits.c -o sumdigits
sumdigits.c:2: warning: return type defaults to ‘int’
sumdigits.c: In function ‘main’:
sumdigits.c:4: warning: implicit declaration of function ‘printf’
sumdigits.c:4: warning: incompatible implicit declaration of built-in
function ‘printf’
sumdigits.c:5: warning: implicit declaration of function ‘scanf’
sumdigits.c:5: warning: incompatible implicit declaration of built-in
function ‘scanf’
sumdigits.c:15: warning: implicit declaration of function ‘getch’
sumdigits.c:3: warning: unused variable ‘b’
sumdigits.c:16: warning: control reaches end of non-void function
/tmp/ccalL9CH.o: In function `main':
sumdigits.c:(.text+0xb1): undefined reference to `getch'
collect2: ld returned 1 exit status

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?

Hyuga
Oct 14 '08 #15

P: n/a
Keith Thompson wrote:
"@$|-|. DUBEY" <du***********@gmail.comwrites:
>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.

--
Er*********@sun.com
Oct 14 '08 #16

P: n/a
Hyuga <hy**********@gmail.comwrites:
On Oct 14, 7:03Â*am, ssksakthi -image processing <ssksak...@gmail.com>
wrote:
>On Oct 14, 11:30Â*am, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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();

}

Doesn't compile on my system:
<snip>
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?
There are so many way this program is wrong (not least that the
algorithm is all wrong) that I'd like to believe it was all intended.

--
Ben.
Oct 14 '08 #17

P: n/a
Eric Sosman <Er*********@sun.comwrites:
Keith Thompson wrote:
>"@$|-|. DUBEY" <du***********@gmail.comwrites:
>>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.
You, sir, are a very bad person.
Oct 14 '08 #18

P: n/a
Keith Thompson wrote:
"@$|-|. DUBEY" <du***********@gmail.comwrites:
>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.
Another possibility - he is trying to develop a check-digit (or a
check-digit checking) system. Not likely, but feasible.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 14 '08 #19

P: n/a
On Oct 14, 3:17*pm, CBFalconer <cbfalco...@yahoo.comwrote:
Keith Thompson wrote:
"@$|-|. DUBEY" <dubey.ashw...@gmail.comwrites:
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.

Another possibility - he is trying to develop a check-digit (or a
check-digit checking) system. *Not likely, but feasible.
Or even pure-math research:
The distribution of the sum-of-digits function (1998)
by Michael Drmota, Johannes Gajdosik
J. Theor. Nombres Bordx
http://www.geometrie.tuwien.ac.at/drmota/distdig.ps.gz
Oct 16 '08 #20

P: n/a
On Oct 14, 1:14*am, "lovecreatesbea...@gmail.c0m"
<lovecreatesbea...@gmail.comwrote:
On Oct 14, 2:30*pm, "@$|-|. DUBEY" <dubey.ashw...@gmail.comwrote:
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

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;

}
Since you have so neatly destroyed any hope of education for the O.P.,
let's complete the job:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *revstr(char *s)
{
const char *start = s;
char *end = s;
char c;

while (*s++) {;
}
s -= 2;
while (end < s) {
c = *end;
*end++ = *s;
*s-- = c;
}

return (start);
}
// The digit sum of N can be defined as the sum of the digits of N
// See: http://mathworld.wolfram.com/DigitSum.html
unsigned long digit_sum(unsigned long long n)
{
unsigned long sum;
for (sum = 0; n; n /= 10)
sum += n % 10;
return sum;
}

// The digit root of N is the digit sum repeated until we have a
single digit
unsigned long digit_root(unsigned long long n)
{
while (n 9) {
n = (unsigned long long) digit_sum(n);
}
return (unsigned long) n;
}

// What strange magic is this?
unsigned long dr(unsigned long long n)
{
if (n)
return 1 + (n - 1) % 9;
return 0;
}

// What is so special about these?
unsigned long long magic_numbers[] =
{1, 81, 1458, 1729};

int main(void)
{
const size_t s = sizeof magic_numbers / sizeof
magic_numbers[0];
size_t index;
for (index = 0; index < s; index++) {
const unsigned long l = digit_sum(magic_numbers[index]);
unsigned long lrev;
char revnum[12];
sprintf(revnum, "%lu", l);
lrev = (unsigned long) atol(revstr(revnum));
printf("%lu * %lu = %lu\n", l, lrev, l * lrev);
}
for (index = 9999999; index <= 10000099; index++) {
printf("digit root %lu is %lu\n", (unsigned long) index,
digit_root(index));
printf("dr %lu is %lu\n", (unsigned long) index,
dr(index));
}
return 0;
}

Oct 16 '08 #21

This discussion thread is closed

Replies have been disabled for this discussion.