473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C++ program to display triangle using while/for loop

Baka otaku
I need to display the following triangle ~
Expand|Select|Wrap|Line Numbers
  1. 1 2 3 4 5
  2.   1 2 3 4
  3.     1 2 3
  4.       1 2
  5.         1
  6.       1 2
  7.     1 2 3
  8.   1 2 3 4
  9. 1 2 3 4 5
  10.  
I got the first part sort of right...but I cant figure out how to go about the lower half....I tried using switch but got messed up @__@

heres what I have :

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. { int row=5;
  7. int x=0;
  8. while (row > 0)
  9. {for(int a=1; a<=x; a++)
  10. {cout<<" ";               
  11. }
  12. for (int b=1; b<=row; b++)
  13. {cout << b;
  14. }
  15. x++;
  16. row = row - 1;
  17. cout << endl;
  18. }
  19.  
  20. system("pause"); //end while         
  21. return 0;
  22. }
  23.  
Any kind of help or suggestion is appreciated ^_^
Feb 6 '07 #1
2 17114
Banfa
9,065 Expert Mod 8TB
Your want to draw 9 rows not five, for the first 4 rows x increases, for the second 4 rows x decreases for the last row it doesn't matter because you aren't going to use x again.
Feb 6 '07 #2
hmm ok so I use 9 rows...and I should include the latter part in the same..with the conditions for x ? I'll try it....thanks !
Feb 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: monomaniac21 | last post by:
hi all im querying a db for two rows which are always returned. how can i reference each row and output its contents without using a while loop. ive tried: $row = mysql_fetch_array($result);...
3
by: coolindienc | last post by:
If I want to use while loop instead for loop in below program, how would I do that? Andy import random heads = 0 for i in range(100): heads+=random.randrange(2)
13
by: inexploration | last post by:
Hello Sirs/madam i have 5 picture store in mysql , and now i want to display these picture on the page ,using while loop ., in php. please let me know through very very simple code. thanking you....
15
Markus
by: Markus | last post by:
What i want to do: Get urls from the database and echo them out into a multiple columned table i.e. 4 pictures per row (recently uploaded table) MY problem is: I have, in my MySQL database,...
4
by: shane3673 | last post by:
i need to write a basic c++ cmd program that uses the while loop to display the odd numbers and sum of the even numbers in between 2 numbers that the users types in. Im not sure how to go about this....
2
by: Peter9588 | last post by:
I am trying to read in every line from a given file and check (amongst other things) that the 3rd character of every line is a ';' (a semi colon). The files i make to go into my database needs to...
4
by: Haris Radoncic | last post by:
I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.