Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

C++ program

I am a beginner programmer and am struggling writing a program that I have for homework. Here is the scenario: write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. Then it gives a hint: use the integer division and modulus operators. Here is what I wrote so far:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()

//variable declarations

Here is the start of the confusion for me. On this scenario, do you only declare 1-5 digit number or do you have to declare each individual number? Then I don't know how to write the rest. I am very new to this and am totally lost. Thanks for any help you can give!
[821 byte] By [mcquetr00] at [2007-11-11 10:14:09]
# 1 Re: C++ program
As he asked, five integers, and I guess u don't know arrays yet, so u need a five int variables:
int a0, a1, a2, a3, a4; //we prefere to start with 0 in programming but u r free to use what u want just any name for variables.
And this (if u don't know) exactly equivalent to :
int a0;
int a1;
int a2;
int a3;
int a4; //five integers.
I think that what u need correct ?
Amahdy at 2007-11-11 20:59:15 >
# 2 Re: C++ program
5 digits is 100000 range, which will fit in a standard "int" on a 32 bit or larger system. You probably are on this type of system. So you cin your integer, and use the mod operator on it. Mod is basically the remainder, so 11 mod 10 is 1, and 120 mod 100 is 20. You can construct your digits from this by subtracting mod 10 before mod 100 etc. That operator is the % by the way, so its x%100 for example.
jonnin at 2007-11-11 21:00:10 >
# 3 Re: C++ program
well another way is here but maybe too unprofessional. i'm also a kid in c++ tbh.
need only one int for input. e.g: x
but coud have another five int for results. e.g : a, b, c, d and e
still need int array or another 4 int for modulus. e.g: mod[4] or mod1, mod2,..etc
now say... input x = 34567 which is 10 base rite.
i.e ( 3*10^4 + 4*10^3 +...7*10^0) ok?

u can use looping if you want.

a = x / 10^4; // so u'll get division i.e '3'
mod[0] = x % 10^4 // which determines the rest digits i.e '4567'

b = mod[0] / 10^3 // which gives u '4'
mod[1] = mod[0] % 10^3 // i.e '567'

......untill

e = mod[3] / 10^0 // so this's last digit 7

/*there u go... :)
!out of topic: i've got c++ programming exam tmw, but it's gonna be horrible :( !
anyway, enjoy yr beginning level and practice a lot in array and pointers problems as a friendly advice ;) */
BurmanP at 2007-11-11 21:01:19 >
# 4 Re: C++ program
So this is basically what jonnin has said
Let's take one 5 digit number e.g. 34522. Now you divide it with the smallest 5 digit number (10000) . So 34522/10000 = 3 and the remainder is 4522 (34522%10000). Now divide 4522 with the smallest 4 digit number which is 1000. Now we have 4522/1000 = 4 and the remainder is 522 (4522%1000). So 522/100 = 5 and the remainder is 22 (522%100). Now 22/10 = 2 and the remainder is 2. The individual digits are 3, 4 , 5, 2, 2.
Ivan** at 2007-11-11 21:02:16 >
# 5 Re: C++ program
Thanks for all the help. It finally worked!
mcquetr00 at 2007-11-11 21:03:20 >
# 6 Re: C++ program
Good , I just wanted if it could be too easy to be done, and using the part he asked in the original question : "separates the integer into its individual digits and prints the digits separated from one another by three spaces each" ; so at this question level I don't think we know loop yet , what about arrays and pointers , and tow many mathematical operations ; just those too lines do it :

cin >> a0>>a1>>a2>>a3>>a4;
cout<<a0<<" "<<a1<<" "<<a2<<" "<<a3<<" "<a4;

the user will seperate input by space as wanyed above like that : 1 2 3 5 6
and the output will be : 1 2 3 5 6

of course it's not perfect solution , but u can say the worest solution too, but the better to learn the c++ basis instead of math operation .
Amahdy at 2007-11-11 21:04:19 >