473,397 Members | 2,084 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,397 software developers and data experts.

How to write a program which sums the digits of a 4-digit number?

Hi. How do I write a program that sums the digits of a 4-digit number? I have a program that works, but you have to input the digits separately. Can anyone help or possibly explain what I'm doing wrong? My professor told us as a hint to use the modulus sign (%), but i don't know how I would use that here. My code so far is below:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7.     int iA = 0;
  8.     int iB = 0;
  9.     int iC = 0;
  10.     int iD = 0;
  11.     int iTotal = 0;
  12.  
  13.     cout << "Please enter a four-digit number." << endl;
  14.     cin >> iA >> iB >> iC >> iD;
  15.  
  16.     iTotal = iA + iB + iC + iD;
  17.     cout << "The sum of the digits is " << iTotal << endl;
  18.  
  19.     return 1;
  20. }
Mar 11 '10 #1
11 8963
Banfa
9,065 Expert Mod 8TB
Do you know what the modulous operator does? How would it help you isolate the digits in a four digit number?

By the way that is not the only operator you will need you will need 1 or more of the normal arithmetic operators too.
Mar 11 '10 #2
jkmyoung
2,057 Expert 2GB
One of the problems you will have is that you are reading in ints, which will read in the whole 4 digit number, not just each character. If you want to do it the way you are, you'd have to read it character by character, and subtract '0' from each digit to get the numeric value.

Unfortunately, this is a more precarious method, as a negative sign or a space in front will not be handled by default.

Oddly enough there was a similar post in the Java forum, which might help in terms of logic.
Mar 11 '10 #3
mshmyob
904 Expert 512MB
I won't do it for you but the basics are:

get the mod 10 of each digit and add each together.

cheers,
Mar 11 '10 #4
I know the modulus operator finds the remainder when you divide one number by another, but I don't really understand what that has to do with adding the digits. And what is mod 10?
Mar 11 '10 #5
mshmyob
904 Expert 512MB
Think about it logically:

Say you have a number for example 4679

If you divide the number by 10 and keep the remainder you get (4679/10=9) 9.
You then take the number (4679) again and divide by 10 and drop the remainder your will get 467.

Repeat the cycle until no digits are left and keep adding the remainders together and you get the sum of all the digits in the number. This can be done in a simple loop until statement.

I'll let you figure out what the MOD (%) function gives you.

cheers,
Mar 11 '10 #6
I got it to work. I don't know what a loop statement is, because we just learned operators, but this works so it should be acceptable. not pretty, but it gets the job done. What do you think?

<Code Snipped>


Thank you so much for all your help!
Mar 11 '10 #7
mshmyob
904 Expert 512MB
Like you said - it's not pretty but it works. And working code is the best thing you can have.

Congrats,. When you learn about looping try changing the code down to just a few lines.

cheers,
Mar 12 '10 #8
Banfa
9,065 Expert Mod 8TB
Jordan218 , we do not allow the posting of full code solutions to homework questions on this forum.

The reasons are
  • Preventing this becoming a forum where people come to get their homework done form them, not that you are in this category, but we do get plenty of requests for "someone give me the codez for ..." and we firmly believe that you tend to learn more by doing your own work, with a few pointers. Not to mention that a student getting their work done by someone else is basically cheating.
  • Preventing your tutors accusing you of copying your solution from the internet, remember they can easily find us but have no hard proof that you are the poster of this question. You could end up being accused of copying your own work.
  • Preventing anyone else on your (or similar) courses finding and using your solution.

I am glad you found your solution but I have had to remove it to comply with our posting guidelines. Now might be a good time for you to look them over.
Mar 12 '10 #9
My apologies. It was just a practice problem, but like you said, someone else might use it as a homework solution. I did not realize that until just now. I'll look over the guidelines to make sure it doesn't happen again. Thank you again!
Mar 12 '10 #10
Agh, I just got pretty much the same assignment with no info aside from a vague hint from the prof and was totally lost on it. Thanks everyone here for helping me to get the idea. I felt like I was trying to solve a riddle. I hate riddles.
Sep 27 '10 #11
accept the 4 digit number and take mod10 it will give the last digit of ur 4digit number u please store it in array or some variable .again take the mod 10 and it will give the third digit of your 4 digit no and store it some where. continue the process
Oct 1 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
My program can, when directed, check a floppy for updates, verify the MD5 sums from another source, then copy the new Java classes to its own directory tree. Once it does this, I'd like to restart...
16
by: engsol | last post by:
There is a number puzzle which appears in the daily paper. Because I'm between Python projects, I thought it might be fun to write a program to solve it....20 minute job, max. On closer...
0
by: Jaeho Kim | last post by:
Hi,,, I am very interested about the following Python program, but I am very beginner on Python.. I do not understand this algorithm,, I would appreciated if you give me this algorithm to some...
9
by: Eric Lilja | last post by:
Hello, consider the following two functions: /* function foo() */ void foo() { float y = 0.0f; float sum = 0.0f; for(int i = 0; i < num; ++i) {
5
by: MJK | last post by:
Suppose I have the following function in my program: void ExtractData(Ind *AM) { int i,j; char str; char c; FILE *ext=fopen("test.out","r"); //suppose I have N line each with M digits (in...
8
KoreyAusTex
by: KoreyAusTex | last post by:
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong: import...
39
by: mike3 | last post by:
Hi. I was writing a program in C++ that generates fractals. I got this weird bug though right now that's holding it up and was wondering if you could help. Anyway, it seems that when this...
4
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
3
by: madsornomads | last post by:
Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on...
26
by: Prime Mover | last post by:
Hello all, I have got the pseudo-code below that I would like to convert to c language. The algorithm calculates Pi value. I am somewhat familiar with C language, but I am just starting to learn...
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: 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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.