C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
for loops Posted by Blangash on 20 Apr 2007 at 2:38 AM
Hi!
i recently started learning c++, i have been writting code to print Asterisks '*' in shape forms but i just cannot write code to print them in an Equilateral triagle(size depending on the user's input number). i.e. if user inputs 3, it should print:
*
* *
* * *

Help!!!

Report
Re: for loops Posted by bilderbikkel on 20 Apr 2007 at 3:58 AM
: Hi!
: i recently started learning c++, i have been writting code to print Asterisks '*' in shape forms but i just cannot write code to print them in an Equilateral triagle(size depending on the user's input number). i.e. if user inputs 3, it should print:
: *
: * *
: * * *
:
: Help!!!
:
Please post your attempt...

bilderbikkel

Report
Re: for loops Posted by somnathde on 23 Apr 2007 at 2:06 AM
This message was edited by somnathde at 2007-4-23 2:7:39

#include<iostream.h>
#include<conio.h>
void main()
{int i,j,n;
clrscr();
cout<<"Enter the no of stars in the base of the triangle";
cin>>n;
for(i=1;i<n;i++)
{for(j=1;j<i;j++)
cout<<'*';
}
cout<<"\n";
getch();
}


Report
Re: for loops Posted by Lundin on 23 Apr 2007 at 2:31 AM
: This message was edited by somnathde at 2007-4-23 2:7:39

: #include<iostream.h>
: #include<conio.h>
: void main()
: {int i,j,n;
: clrscr();
: cout<<"Enter the no of stars in the base of the triangle";
: cin>>n;
: for(i=1;i<n;i++)
: {for(j=1;j<i;j++)
: cout<<'*';
: }
: cout<<"\n";
: getch();
: }
:
:


Please don't respond to code beggars since that will only encourage them to continue to pester us. Don't respond with code unless they can prove that they have made some effort on their part. Thanks.

Also please respond in ANSI C/C++ to avoid learning out bad habits to the newbies. The posted program will not work on any ANSI C/C++ compiler.
Report
Working Code Posted by Mohammad Rast on 23 Apr 2007 at 3:01 AM
Hi,

There is some spaces in the beginning of each line and won't be shown in the posted message, but shown in the reply screen.

#include <iostream>
using namespace std;

#include <stdlib.h> // system()


int main()
{

	int rows_num; // height of triangle (number of rows)
	int i, j; // counters

	cout << "\nPlease enter the height of triangle (number of rows) : ";
	cin  >> rows_num;
	cout << endl;

	int beg_sp = (rows_num-1)*2; // number of beginning spaces
	
	for (i=1; i<=rows_num; i++) // print each row
	{
		for (j=0; j<beg_sp; j++) // print beginning spaces
			cout << ' ';
		for (j=0; j<i; j++) // print stars
			cout << "*   ";
				
		cout << endl; // goto next row

		beg_sp -= 2;
	}
	
	system("pause");
	
	return 0;
}

--------------------------------------
- I don't have Time To waste The Time!

Report
Re: Working Code Posted by Lundin on 23 Apr 2007 at 4:23 AM
: Hi,
:
: There is some spaces in the beginning of each line and won't be shown in the posted message, but shown in the reply screen.
:
:
: #include <iostream>
: using namespace std;
: 
: #include <stdlib.h> // system()
: 
: 
: int main()
: {
: 
: 	int rows_num; // height of triangle (number of rows)
: 	int i, j; // counters
: 
: 	cout << "\nPlease enter the height of triangle (number of rows) : ";
: 	cin  >> rows_num;
: 	cout << endl;
: 
: 	int beg_sp = (rows_num-1)*2; // number of beginning spaces
: 	
: 	for (i=1; i<=rows_num; i++) // print each row
: 	{
: 		for (j=0; j<beg_sp; j++) // print beginning spaces
: 			cout << ' ';
: 		for (j=0; j<i; j++) // print stars
: 			cout << "*   ";
: 				
: 		cout << endl; // goto next row
: 
: 		beg_sp -= 2;
: 	}
: 	
: 	system("pause");
: 	
: 	return 0;
: }
: 

: --------------------------------------
: - I don't have Time To waste The Time!
:
:


This goes for you as well:
http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=358144

And this wasn't the first time you did it either.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.