473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Oct 14 '08 #1
20 1822
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
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
"@$|-|. 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
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
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
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
"@$|-|. 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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
5
by: outofmymind | last post by:
Hi every1, im trying to solve this question, i did some of it but i dont think that its correct or complete: this is the question: Write the definition of a class called Product. A Product...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
2
by: mia23 | last post by:
Hello, I am a java beginner and I need series help in solving this program; my assignment is due after 2 days and I can't understand this program . This is THE PROGRAM:  The ABC medical clinic...
2
Sl1ver
by: Sl1ver | last post by:
Create my app.exe.config file e.g <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MobileDBPath" value="\Program Files\database.sdf"/> <add...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.