Loops

Iv'e managed to complete part of the code I need.

int main() {

int n, digit;

printf("Number? ");
scanf("%d", &n);

while(n > 0)
{
digit = n % 10;
n /= 10;

printf("\n%d\n", digit);
}
return 0;
}

This code will take the input number and spit them out individually. For example, if i input '2334' over the next few lines it will output:

'4
3
3
2'

What I am trying to do is set up a system which makes a number of asterisks follow each number (eg. the number 7 would have seven asterisks, 5 would have five). Like the previous answer, the programme would read:

'4 ****
3***
3***
2**'

I know this requires another loop but i have no clue how to make it.
Don't let nested loops scare you. Think what the purpose of each loop is. The outer loop that you have already written loops n from the user inputted value down to 1. Now you should narrow your mind and think what you should do for each value of n. Well you should print n stars. This should be as easy to add as if you where asked to write a program that given a number n, outputs n stars.
I have a little difficulty interpreting how things are written sometimes xD i think i get what you said. i added in an additional printf after the previous.

while(n > 0)
{
digit = n % 10;
n /= 10;

printf("\n%d ", digit);
printf("*" );

}

im not sure if this is the right way of doing it. but if it is, i dont know what i put or whether you can put anything in these brackets to multiply the number of * by n?

i forgot to add the next loop xD would i need to include the next printf within a new loop?
Last edited on
I think you should use a loop yes. Can you think of a way to write a loop that calls printf("*"); n times?
Ive thought that but exactly how to do it is completely eluding me >.< i know you're trying to help assist me to find out what i need to do. the thing is i know what to do, i just have no idea how to do it :/ im not trying to get other people to do my code but i really have no clue how to do this.


Well i finished it. put a similar loop inside the previous which read while(digit > 0)
{
printf("*");
digit--;
}
so yep, DONE!
Last edited on
Topic archived. No new replies allowed.