# C++ program

1. Sep 3, 2015

### Raghav Gupta

Write a “C++” program to read any integer number and print each digit into word.
I went like this

# include <iostream.h>
#include <conio.h>
void main(){
int a,b,c,d;
clrscr();
b=a
cout<<"Enter any number"<<endl;
cin>>a;
c=a%10;
d=a/10;
I am not getting further logic.
someone was saying for loop we also have to apply.

2. Sep 3, 2015

### DEvens

This is clearly homework. So I will only give hints.

Do not start by writing C++ code. That comes only very near the end.

Can you solve the problem "on paper?" I mean, can you work out the steps that you would take to get the results you want?

If you can't then it is NOT time to start writing computer code. You need to stop and be sure you understand what it is you are meant to accomplish.

Work out each step in detail. Work out how you would know when you were done. Work out how you would get each next digit, and what you would do to determine the next digit. Work out whether you will print out the digits as you get them or if you will store them and print them out all at once. Work out which order you will print them, meaning if the integer is 146 do you print 1 then 4 then 6, or do you print 6 then 4 then 1?

Work through your steps with several example integers. Be sure to use different length integers. Be sure to include cases like 4000 with several trailing 0's. Be sure to include cases like -45 with a negative sign. Be sure to include 5, one digit, 37, two digits, 389484, a bunch of digits.

Be sure to think about cases where the user types in letters or punctuation marks instead of integers, and figure out what your program should do about that. Maybe the spec of the problem does not include this, so the behaviour of your program satisfies the spec no matter what it does in this case.

Once you have all the rules and steps worked out, only then is it time to start writing computer code.

C++ is based on certain standard structures. Using "void main" is non-standard, but many compilers let you do it. The routine clscr() isn't standard either, but again, many compilers let you away with it.

You have not got a complete program. When you are coding, always be sure you have a complete program at each step. For example, make sure you balance all { and } as soon as you type them. So when you put in {, immediately go put in the balancing }.

Use white space to your advantage. Line up { with } in this kind of fashion.
{
.... stuff
}

Note that the { and } should be in the same column on different lines. Don't jam it all on one line like this {...stuff} because that is hard to read. If you have another layer of {} inside the first layer, then indent the inside layer.
{
<tab here> {
<tab here> ... stuff
<tab here> }
}

The forum editor does not like leading white space, so where it says <tab here> you should put in a tab.

Your understanding of the problem will have some kind of thing like this: Determine how long the integer is. Figure out the next digit. Keep outputting digits for that long. You need to figure out how to do that. It will probably be either a for loop or a while loop. You need to look in your text and find those and figure out how to enter them.

3. Sep 3, 2015

### Staff: Mentor

Tip: when posting code in this forum, enclose it in "code tags" like this:

[code=C]
#include <iostream>

using namespace std;

int main ()
{
cout << "Hello, world!" << endl;
return 0;
}
[/code]

Then it will preserve any spaces that you use at the beginning of a line to indent your code. In the code above, I actually indented two lines. The forum software strips out the extra spaces in "normal" text, but not in "code" text.

Code (C):

#include <iostream>

using namespace std;

int main ()
{
cout << "Hello, world!" << endl;
return 0;
}

Also, if you put the language name in the code tag, the software can colorize certain features of your code to make it easier to read. (I used "C" instead of "C++" because this trick apparently treats them as the same)