here's my question:
write a program that asks the user to type the value of N and writes this picture :
N=1
*
N=2
**
*
N=3
***
**
*

here's my work:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
	int count = 0; 
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
	  
	  if (numOfStars != 9999)
	  {
         for (count < numOfStars; ++count;)
             printf("*");               
      }
      printf("endl");
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

MY problem is that when run, and enter a number it become endless loop
any help please

thanks

>> printf("endl");

Huh?? endl is c++, not c, and its not in quotes. I think what you want is printf("\n");

> for (count < numOfStars; ++count;)
The general syntax for a for loop is
for ( initialisation ; comparison ; step )

Which part is your "comparison" ?
Which part should be the initialisation?

thanks you very much for your help

but when i write it like this

for (int count = 0;count < numOfStars; ++count;)

it says error
thanks

well, your code gets in an endless loop because if numofstars!=9999 is the condition for your loop. your program will never reach that value if numofstars is left unchanged. Like Salem said.. your loop in the if statement is not declared properly. if you want to use count as the variable, you should at least add a semicolon before your condition, so you just skip the definition, like this:
for ('count < numOfStars; ++count;)

thanks you very much for your help

but when i write it like this

for (int count = 0;count < numOfStars; ++count;)

it says error
thanks

you cannot do that. if you have int before the variable name you declare it. you cannot declare a static variable more than once. so here you could just set its value.
for (count = 0;count < numOfStars; ++count)
this should work.

> it says error
How many ; should a loop have?

it should have 2 semicolons. one after the initialisation, one after the condition.

> it says error
How many ; should a loop have?

i dont know :(
its only 4 days am learning C

for (count = 0;count < numOfStars; ++count)
this should work.

its not working

ok its working,but not like i wanted
can any1 help me with my code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
	  
	  if (numOfStars != 9999)
	  {
         for (count = 0;count < numOfStars; ++count)
             printf("*");               
      }
      printf("\n");
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

so the problem is, when i enter 3 it gives me 3 star
N=3
***
but i wanted

N=3
***
**
*

any helps please

try putting it in a loop where n decreases till it reaches 1 and doing the same thing like this. dont forget a new line at the end of the loop

try ( ;count < numOfStars; ++count)

this is working now
see above problem not yet solved:(

when enter 3 got 3 stars
N=3
***
but i want this
N=3
***
**
*

any help abt how to modify my code?

thank you very much for your help
thank you

this is working now
see above problem not yet solved:(

when enter 3 got 3 stars
N=3
***
but i want this
N=3
***
**
*

any help abt how to modify my code?

thank you very much for your help
thank you

as i said. add a loop.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
          for(int i=nimOfStars; i>0; i--)
{
	  if (numOfStars != 9999)
	  {
         for (count = 0;count < numOfStars; ++count)
             printf("*");               
      }
      printf("\n");
}
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

try this and see what you get.

as i said. add a loop.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
          for(int i=nimOfStars; i>0; i--)
{
	  if (numOfStars != 9999)
	  {
         for (count = 0;count < numOfStars; ++count)
             printf("*");               
      }
      printf("\n");
}
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

try this and see what you get.

when put 3 i got
N=3
***
***
***

for 2

N=2
**
**

and this not working

for(int i=nimOfStars; i>0; i--)

i need to define int i; first and remove the INT in the for loop

thanks

my bad.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
          for( ;numOfStars>0; numOfStars--)
{
	  if (numOfStars != 9999)
	  {
         for (count = 0;count < numOfStars; ++count)
             printf("*");               
      }
      printf("\n");
}
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

my bad.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);
          for( ;numOfStars>0; numOfStars--)
{
	  if (numOfStars != 9999)
	  {
         for (count = 0;count < numOfStars; ++count)
             printf("*");               
      }
      printf("\n");
}
     }
      while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

WAOW thanks man
its working
i'll study this well

thank you

a last question
suppose i want it to display like this:
N=3
***
..**
....*
(exclude the dots)

than the for loop i added would have to move from 1 to numOfStars, not from numofstars to 1. rep points are always welcomed if you want to say thanks :)

than the for loop i added would have to move from 1 to numOfStars, not from numofstars to 1. rep points are always welcomed if you want to say thanks :)

can u just show me in the code plzz
AND Where to say Thanks?
cant see any button

can u just show me in the code plzz
AND Where to say Thanks?
cant see any button

1.
      #include <stdio.h>
   2.
      #include <stdlib.h>
   3.
       
   4.
      int main(int argc, char *argv[])
   5.
      {
   6.
      int numOfStars = 0, nos;
   7.
      int count;
   8.
       
   9.
      do
  10.
      {
  11.
      printf("N=");
  12.
      scanf ("%d",&nos);
  13.
      for( ; numOfStars<=nos; numOfStars++)
  14.
      {
  15.
      if (numOfStars != 9999)
  16.
      {
  17.
      for (count = 0;count < numOfStars; ++count)
  18.
      printf("*");
  19.
      }
  20.
      printf("\n");
  21.
      }
  22.
      }
  23.
      while (numOfStars != 9999);
  24.
       
  25.
      system("PAUSE");
  26.
      return 0;
  27.
      }

So you take the nos and move it from 0 to nos. the nos is what your final number of stars should be.

simply use the field width specifiers

WAOW thanks man
its working
i'll study this well

thank you

a last question
suppose i want it to display like this:
N=3
***
..**
....*
(exclude the dots)

simply use the field width specifiers

what u mean by this: field width specifiers
any example??
thanks in advance

what u mean by this: field width specifiers
any example??
thanks in advance

if you want a field to be 5 characters, then printf("%5s", "Hello");. If you want the text to be right justified then its the same with a - symbol printf("%-10s", "Hello"); Write yourself a little program to test that out for yourself so that you can see how it works.

if you want a field to be 5 characters, then printf("%5s", "Hello");. If you want the text to be right justified then its the same with a - symbol printf("%-10s", "Hello"); Write yourself a little program to test that out for yourself so that you can see how it works.

hello,
ok i understand the piece of code u written, i tried it and see what it mean,
but cant get any idea to put it in a loop to get the result i wanted

the question is in another thread : http://www.daniweb.com/forums/thread276781.html

please take a look

thank you very much for helping me

if you want a field to be 5 characters, then printf("%5s", "Hello");. If you want the text to be right justified then its the same with a - symbol printf("%-10s", "Hello"); Write yourself a little program to test that out for yourself so that you can see how it works.

hello i do this code, but not getting the right result any help please??
here's the code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int numOfStars = 0;
    int count, counter;

    do
    { 
      printf("N=");
      scanf ("%d",&numOfStars);

for( ; counter<=numOfStars; counter++)
{
       for( ;numOfStars>0; numOfStars--)
       {
         if (numOfStars != 9999)
          {
            for (count = 0;count < numOfStars; count++)
             {
               printf("*");  
             }
            printf("\n");
         }               
       }
       printf(" ");
}
     }

     while (numOfStars != 9999);

  system("PAUSE");  
  return 0;
}

thanks

here's my question:
write a program that asks the user to type the value of N and writes this picture :
N=1
*
N=2
**
*
N=3
***
**
*

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
	
	do
	{ 
         // it is bad place for print and scan.
         // move these 2 lines somewhere else
         // think only - where ? (and why ?)
         printf("N=");
	 scanf ("%d",&numOfStars);
	  
         // think do you really need this branch ?
         // or you can live without it ?
	 // if (numOfStars != 9999)
	 // {
         for (int count=0; count < numOfStars; ++count)
             printf("*");               
         //}
      printf("\n");
      // something is missing here. Think - What is missing ?
      // maybe single operation with numOfStars variable ??
     }
      while (numOfStars != /* bad bad bad number 9999, think of any other - more useful number*/);

  system("PAUSE");	
  return 0;
}

I made useful hints in your example. The rest - you should find out yourself.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
	
	do
	{ 
         // it is bad place for print and scan.
         // move these 2 lines somewhere else
         // think only - where ? (and why ?)
         printf("N=");
	 scanf ("%d",&numOfStars);
	  
         // think do you really need this branch ?
         // or you can live without it ?
	 // if (numOfStars != 9999)
	 // {
         for (int count=0; count < numOfStars; ++count)
             printf("*");               
         //}
      printf("\n");
      // something is missing here. Think - What is missing ?
      // maybe single operation with numOfStars variable ??
     }
      while (numOfStars != /* bad bad bad number 9999, think of any other - more useful number*/);

  system("PAUSE");	
  return 0;
}

I made useful hints in your example. The rest - you should find out yourself.

hello, thanks you for helping me,
but for wat question u answering??

i wanted to modify my code to get this result:

N=5
*****
 ****
  ***
   **
    *
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
	
	do
	{ 
         // it is bad place for print and scan.
         // move these 2 lines somewhere else
         // think only - where ? (and why ?)
         printf("N=");
	 scanf ("%d",&numOfStars);
	  
         // think do you really need this branch ?
         // or you can live without it ?
	 // if (numOfStars != 9999)
	 // {
         for (int count=0; count < numOfStars; ++count)
             printf("*");               
         //}
      printf("\n");
      // something is missing here. Think - What is missing ?
      // maybe single operation with numOfStars variable ??
     }
      while (numOfStars != /* bad bad bad number 9999, think of any other - more useful number*/);

  system("PAUSE");	
  return 0;
}

I made useful hints in your example. The rest - you should find out yourself.

Hello,
i know exactly how to do it i tries it but not working.
i need to input a number
and get the result

N=5
*****
****
***
**
*
Type Y if want to continue or N to stop
//i need to ask the user

here's my code:

#include <stdio.h>
#include <stdlib.h>

//procedure for star

void star_workout (void)
 {
    int numOfStars = 0;
    int count;
    
      printf("N=");
	  scanf ("%d",&numOfStars);

       for( ;numOfStars>0; numOfStars--)
        {
            for (count = 0;count < numOfStars; count++)
             {
                printf("*");
             }
             
            printf("\n");
        }
     }
     
//procedure to continue     
void cont_end (void)
{
     char ch;
     printf("Do you want to continue, Type Y or N ");
     scanf("%C",&ch);
 
     if (ch=="Y")
     {
      star_workout();        
     } 
     else
     {
         system("PAUSE");
         return 0;
         }
}

//The main program
int main(int argc, char *argv[])
{
    
    star_workout ();
    cont_end();
 
 system("PAUSE");
 return 0;
 }

thanks

hey do you want to ask the user to restart a program on pressing Y ?
simply call the main()

if (ch=="Y" || ch=='y')
     {
      main();       
     } 
     else
     {
         exit();
     }

hey do you want to ask the user to restart a program on pressing Y ?
simply call the main()

if (ch=="Y" || ch=='y')
     {
      main();       
     } 
     else
     {
         exit();
     }

its not working
i tried this one

int main(int argc, char *argv[])
{
    int ch;
    
    printf("Enter 1 to continue or 0 to stop ");
    scanf("%c",&ch);
    if (ch==1)
    {
    star_workout ();
    }
    else
 system("PAUSE");
 return 0;
 }

when i enter 1
nothing happen, it just displayed

press any key to continue

it just dont enter in the if

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.