Thread: Another simple program using for loop

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Another simple program using for loop

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main()
    {
        int space, row, stars, number_of_stars;
    
    
    
    
        printf("How many stars you want?");
        scanf(" %d", &number_of_stars);
        for (row = 1; row <= number_of_stars; row++) {
            for (space = 1; space <= number_of_stars; space++) {
                printf(" ");
            }
            for (stars = 1; stars <= row; stars++) {
                printf(" *");
            }
            printf("\n");
            number_of_stars--;
        }
        return 0;
    }
    The output is a simple pyramid but it skips one row/line. I want the user to type how many rows you want for example I type '12'. It only shows half of it.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    You are using the number_of_stars variable for both the number of rows you want and the number of spaces to start each line with. In order to have the correct number of spaces at the start of each line (in addition to the space which is included before each star) you are decrementing number_of_stars but this will have the effect of reducing the number of rows to be printed since the same variable is used. Instead of decrementing, change line 17 to
    Code:
    for (space = 1; space <= number_of_stars - row; space++) {
    I would also suggest renaming number_of_stars to number_of_rows since you are asking the user for how many rows they want, not how many stars in total.
    Last edited by Golf7; 09-10-2015 at 02:30 PM.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    1
    A slight modification of your code and it is working well.Just carefully go through the program and see how it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        int space, row, stars, i,number_of_stars;
    
    
        printf("How many rows you want?");
        scanf(" %d", &row);
    
    
        for (i = 1; i <= row; i++)
    	{
            number_of_stars=i; 
    		
            for (space = 1; space <=row-i; space++) 
    			{
                             printf(" ");
                            }
    
    
            for (stars = 1; stars <= number_of_stars; stars++) 
    			{
                             printf(" *");
                            }
    
    
            printf("\n");
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Thank you guys. Love you all <3

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Quote Originally Posted by manishfzr1995 View Post
    A slight modification of your code and it is working well.Just carefully go through the program and see how it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        int space, row, stars, i,number_of_stars;
    
    
        printf("How many rows you want?");
        scanf(" %d", &row);
    
    
        for (i = 1; i <= row; i++)
        {
            number_of_stars=i; 
            
            for (space = 1; space <=row-i; space++) 
                {
                             printf(" ");
                            }
    
    
            for (stars = 1; stars <= number_of_stars; stars++) 
                {
                             printf(" *");
                            }
    
    
            printf("\n");
        }
        return 0;
    }
    Ohh I just noticed why did you put another variable there? I could just use number_of_stars if it's equal to 'i' anyway? or if I used number_of_stars to be scanned?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Loop Program
    By sgk1498 in forum C Programming
    Replies: 5
    Last Post: 02-27-2014, 08:09 PM
  2. Replies: 4
    Last Post: 11-13-2013, 08:13 PM
  3. Simple program, stopping invalid character infinite loop
    By samwillc in forum C++ Programming
    Replies: 16
    Last Post: 01-06-2013, 03:18 PM
  4. Simple while loop
    By aaronbsk in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2010, 02:41 PM
  5. Very simple loop program for payroll
    By alexpos in forum C Programming
    Replies: 17
    Last Post: 10-19-2005, 06:19 PM

Tags for this Thread