Thread: A simple query

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Question A simple query

    I found this puzzle in some book and i dont know the solution. please tell me how to write this program.

    The following pyramid should be printed using WHILE LOOP.


    http://img813.imageshack.us/img813/9789/36177637.jpg

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    First, you'll need to figure out how to describe the shape mathematically. Try coming up with a formula relating the line number to the number of stars on that line.
    Then it's just a matter of putting that formula into the while loop, which will be a breeze.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Exclamation

    Code:
    #include <stdio.h>
    void main()
    {
    	int i,j,k,l;
    	i=4;
    	l=1;
    
    	while(i>=0)
    	{
    		j=i;
    		while(j>0)
    		{
    			printf(" ");
    			j--;
    		}
            
    		k=l;
    		while(k>0)
    		{
    			printf("*");
    			k--;
    		}
    		l=l+2;
    		i--;
    		printf("\n");
    	}
    }
    I am sure the answer cant be this complicated, if anyone has a better code please reply...

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    That is C. This is the C++ board. Which are you looking for?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    I am sorry for posing in wrong board, i am just looking for correct algorithm

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Goodness me, that’s way too complicated, lol.

    It can be done with 3 integers.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    is there any chance of reducing number of while loops

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Yes, with two, if you’re happy to use a simple if else statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  2. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  3. Simple string query
    By ozzy34 in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2004, 11:40 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM