473,408 Members | 2,009 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,408 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 17115
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.