Hello, Im taking C in school, and i have a problem with my code, nothing happens at all when i run the program.
The task is:

A program is required that prompts the user for a number. The program will then print a series of asterisks to represent the number. If the user enters a number less than 1, the program stops. For example:

Enter a number: 5
 *****

Enter a number: 3

***
 Enter a number: 9

*********

Enter a number: 0



i have decided to use while loop since its a pretest loop
here it is

#include<stdio.h>
main()
{
int num, i;

   while(num>1)
   {
   printf("Enter a number:");
   scanf("%d", &num);

        while(i<=num)
        {
        printf("*");
        i++;
        }
   }
}

any help would be apreciated!!!

Recommended Answers

All 12 Replies

main()
{
int num, i;

while(num>1) <-----
{

What do you think the value of num is where I put the arrow?
Most likely it will be zero but you can never be certain about that.
Put your scanf function before this while, that way you give num a value.

here comes the latest version, but when i enter 1 it terminates as well as when i enter 0, it should terminate only at 0

#include<stdio.h>
main()
{
int num, i;

do
  {
printf("\n");
printf("Enter a number:");
scanf("%d", &num);

i=1;
        while(i<=num)
        {
        printf("*");
        i++;
        }
   }while(num>1);
}

Why do you make it so complicated?
You can do with one while!
And please : if you send code put it between CODE-tags

Why do you make it so complicated?
You can do with one while!
And please : if you send code put it between CODE-tags

im really sorry, im new to this website,

which CODE tags should i use?

could you please give me a hint with doing it just with while, i tried to do it like you suggested in your first reply but got infinite loop

thanx a lot.,
sorry again

>which CODE tags should i use?
Click here.

commented: Thanks for the tip it always helps +1

you should use this

code = c withing brackets;

Code tags :
Select your code.
Then click one of the buttons on top of the field where your typing or pasting text.
Hover over all of them to get their meaning. You need the #-button.

Code algorithm :
Get num
While num not=0 print *
Decrement num ----> this is essential otherwise you will never get out of the while loop! And you will loop forever and ever and ever and......

Code tags :
Select your code.
Then click one of the buttons on top of the field where your typing or pasting text.
Hover over all of them to get their meaning. You need the #-button.

Code algorithm :
Get num
While num not=0 print *
Decrement num ----> this is essential otherwise you will never get out of the while loop! And you will loop forever and ever and ever and......

Wow, Thank you sooo much, it works great!!
I have just started CPD Computer Programming course, and its a part from our first assignment,
thanx a lot!!!
cheers!!!

The trills you get when some code is working can not be described by someone who has never written some code...
Mark thisone as solved...

#include<stdio.h>
main()
{
int nu,i;
printf("enter the number");
scanf("%d",&nu);
for(i=0;i<=nu;i++)
{
printf("* \t");
}
printf("\n");
}
~

#include<stdio.h>
main()
{
int nu,i;
printf("enter the number");
scanf("%d",&nu);
for(i=0;i<=nu;i++)
{
printf("* \t");
}
printf("\n");
}
~

#include<stdio.h>
main()
{
int nu,i;
printf("enter the number");
scanf("%d",&nu);
for(i=0;i<=nu;i++)
{
printf("* \t");
}
printf("\n");
}
~

What's this? You feel inspired today? Read the rules of the forum. Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.