Teen Programmers Unite  
 

 

Return to forum top

Simple C++ problem...

Posted by Kalkulas [send private reply] at March 07, 2002, 12:51:52 PM

I am a begining C++ programmer, and cant figure how one could use the modulas operator to solve this problem:

Input a 5 digit number, seperate the number into its indvidual digits and print the digits with each digit 3 spaces apart from each other, using divsion operator and modulas operator. Its a question from a Deitel & Deitel book.

Posted by taubz [send private reply] at March 07, 2002, 04:02:49 PM

Here's a hint. Remember that we're using the decimal system, so a five digit number ABCDE is actually

10000A + 1000B + 100C + 10D + E

A second hint is to start with just getting one of the end digits first (A or E, I won't tell you which).

- taubz

Posted by PavalDaniel [send private reply] at March 12, 2002, 01:19:57 PM

First what's that a modulas operator? If it means modulus (or remainder) then the problem would be like this (the algoritm works for any number no matter the lenght):

#include <iostream.h>
main()
{
long nr; //the number
long dgt; //the digit
do
{
dgt = nr % 10;//find the last digit. In taubz's eg it is
//E. eg: 345 % 10 = 5
cout<<dgt<<"___"; //the lines are actually spaces!
nr = nr / 10; //345 / 10 = 34.
}
while (nr); //loops while the number is not 0.
}
//the output for my eg is "5___4___3___".

Posted by Psion [send private reply] at March 12, 2002, 02:54:01 PM

Giving direct answers is generally not encouraged. It's better to let people figure things out themselves. Luckily, I'd assume that Kalkulas has gotten it by now. =)

Posted by PavalDaniel [send private reply] at March 13, 2002, 01:24:30 PM

OK Psion.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.