CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Pyramids in C++

  1. #1
    Join Date
    Feb 2009
    Posts
    11

    Exclamation Pyramids in C++

    Hi Friends,
    I am a beginner in C++ I have to make a programms using loops to get the out put as
    1
    2 3
    4 5 6
    7 8 9 10
    11 12 13 14 15

    also I have to write a program having output as
    1
    121
    12321
    1234321
    123454321
    The pyramid mentioned above should look like a pyramid and not like a right triangle.

    please help me as I am not very well aware about the concepts of loops.
    Last edited by siddharth_1202; February 6th, 2009 at 06:54 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pyramids in C++

    What have you tried?

    You could use [code][/code] tags to help format your expected output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Pyramids in C++

    I have an example here: http://mariusbancila.ro/blog/?p=143.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Pyramids in C++

    Quote Originally Posted by cilu View Post
    I have an example here: http://mariusbancila.ro/blog/?p=143.
    Hello cilu!
    How are you?
    Haha that looks so cool!

  5. #5
    Join Date
    Feb 2009
    Posts
    11

    Re: Pyramids in C++

    Quote Originally Posted by cilu View Post
    I have an example here: http://mariusbancila.ro/blog/?p=143.
    Thanks for your reply, it helped me, but I am still not able to get
    1
    23
    456
    78910

    can you please give me some more tips.
    I also made another program for printing
    ....1
    ...121
    ..12321
    . 1234321
    1 2 3 4 5 4 3 2 1
    Last edited by siddharth_1202; February 7th, 2009 at 08:51 AM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pyramids in C++

    Quote Originally Posted by siddharth_1202 View Post
    Thanks for your reply, it helped me, but I am still not able to get
    1
    23
    456
    78910

    can you please give me some more tips.
    You've shown no code, so what "tips" can we give you? Maybe "work harder".
    I also made another program for printing
    ....1
    ...121
    ..12321
    . 1234321
    1 2 3 4 5 4 3 2 1
    So far, you've shown no code that you came up with. You need to show us what you've done.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Feb 2009
    Posts
    11

    Re: Pyramids in C++

    Quote Originally Posted by Paul McKenzie View Post
    You've shown no code, so what "tips" can we give you? Maybe "work harder".
    So far, you've shown no code that you came up with. You need to show us what you've done.

    Regards,

    Paul McKenzie
    //WAP to display
    // 1
    // 1 2 1
    // 1 2 3 2 1
    // 1 2 3 4 3 2 1
    // 1 2 3 4 5 4 3 2 1
    // achived:
    // 1
    // 1 2 1
    // 1 2 3 2 1 0
    // 1 2 3 4 3 2 1 0 -1
    // 1 2 3 4 5 4 3 2 1 0 -1 -2

    #include<iostream>
    class hill
    {
    int row,space,col,a;
    public:
    void display()
    {
    for (row=1;row<=5;row++)
    {
    for (space=18;space>=row*2-1;space--)
    {
    cout<<" ";
    }
    for (col=1;col<=row;col++)
    cout<<col<<" ";
    // cout<<"\n";
    for (a=col-2;a>=col*-1+4;a--)
    // cout<<"\n";
    cout<<a<<" ";
    cout<<"\n";
    }
    }
    };
    void main()
    {
    hill formation;
    formation.display();
    return 0;
    }

  8. #8
    Join Date
    Feb 2009
    Posts
    11

    Re: Pyramids in C++

    There is another program for which I have written a code but there is problem with it as the display is showing 51 infinite times.

    //WAP to display prime numbers between 1 to 50.

    #include<iostream>
    class prime
    {
    int a,b,c;
    public:
    void display()
    {
    for (a=2;a<=50;a++)
    // cout<<"\nEnter a number:";cin>>a;
    b=2,c=0;
    while (b<=a/2)
    {
    if (a%b==0)
    {
    b++;
    }
    else
    {cout<<a;
    cout<<", ";
    b++;}
    }

    // if (c==1)
    // {cout<<"\nNumber is not a prime number";}
    // else
    // {cout<<"\nNumber is a prime number";}
    }
    };
    void main()
    {
    prime num;
    num.display();
    return 0;
    }

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pyramids in C++

    Quote Originally Posted by siddharth_1202 View Post
    //WAP to display
    // 1
    // 1 2 1
    // 1 2 3 2 1
    // 1 2 3 4 3 2 1
    // 1 2 3 4 5 4 3 2 1
    // achived:
    // 1
    // 1 2 1
    // 1 2 3 2 1 0
    // 1 2 3 4 3 2 1 0 -1
    // 1 2 3 4 5 4 3 2 1 0 -1 -2
    Please use code tags when posting code.
    Code:
    #include<iostream>
    class hill
    {
    	int row,space,col,a;
    	public:
    	void display()
    	{
    		for (row=1;row<=5;row++)
    		{
    				for (space=18;space>=row*2-1;space--)
    				{	
    					cout<<" ";
    				}
    				for (col=1;col<=row;col++)
    				cout<<col<<" ";
    //				cout<<"\n";
    				for (a=col-2;a>=col*-1+4;a--)
    //				cout<<"\n";
    				cout<<a<<" ";
    				cout<<"\n";
    		}
    	}
    };
    void main()
    {
    	hill formation;
    	formation.display();
    	return 0;
    }
    It's int main(), not void main().
    Code:
    int main()
    Second, look at what the problem is in your output. You are looping too many times in the second loop. The easiest way to fix this is to print only if a is greater or equal to 1.
    Code:
    for (a=col-2; a>=1;a--)
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2009
    Posts
    11

    Re: Pyramids in C++

    Thanks a lot Paul, it works. I am very happy, thanks a lot for the help.
    Can you please also suggest me what mistake am I making in the below mentioned program?

    Code:
    //WAP to display prime numbers between 1 to 50.
    
    #include<iostream>
    class prime
    {
    	int a,b,c;
    	public:
    	void display()
    	{
    		for (a=2;a<=50;)
    		b=2,c=0;
    		while (b<=a/2)
    		{
    			if (a%b==0)
    			{
    				b++;
    			}
    			else
    			{
    				cout<<a;
    				cout<<", ";
    				b++;
    			}
    		}	
    		a++;
    	}
    };
    int main()
    {
    	prime num;
    	num.display();
    	return 0;
    }

  11. #11
    Join Date
    Feb 2009
    Posts
    11

    Re: Pyramids in C++

    Dear Friends,
    Can you please help me with the following program?

    Code:
    //WAP to print 1 to 15 in different lines in incrementing manner.
    // 1
    // 2 3
    // 4 5 6
    // 7 8 9 10
    // 11 12 13 14 15
    
    #include<iostream>
    class one
    {
    	int x,a;
    	public:
    	void display()
    	{
    		for (x=1;x<6;x++)
    		{
    			for (a=1;a<=x;a++)
    			cout<<a;
    			cout<<"\n";
    		}
    	}
    };
    void main()
    {
    one z;
    z.display();
    return 0;
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured