help solving addition problem in C

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
 
P

Pranav

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

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

Nate Eldredge

@$|-|. 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

Hint:

val % 10 is the rightmost digit of val.
val / 10 shifts all digits to the right.
 
N

Nick Keighley

right...



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++)
 
P

Pranav

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

Keith Thompson

@$|-|. 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.
 
N

Nick Keighley

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
 
L

lovecreatesbeauty

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;
}
 
D

Doug Miller

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

ssksakthi -image processing

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();
}
 
H

Hyuga

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
 
B

Ben Bacarisse

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?

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

Nate Eldredge

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.

You, sir, are a very bad person.
 
C

CBFalconer

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

user923005

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;
}
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top