473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Baka otaku
4 New Member
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 17127
Banfa
9,065 Recognized Expert Moderator Expert
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
Baka otaku
4 New Member
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
7445
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); extract $row; echo $var1; //var 1 of row 0 echo $var1; //var 1 of row 1
3
3307
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
11277
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. best regards. ikram
15
5974
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, the urls to the pictures that have been uploaded. I'm not really sure how i would get the urls to go into multiple rows... i can do it as a single row...
4
2229
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. if someone could show me how to get it to list of all numbers inbetween the numbers i think i could figure it out from there
2
2310
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 have every line with a two letter identifier followed by a semi colon. The issue I am having is that the while loop goes through every line fine and...
4
3084
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 newlines entered into the program, using a while loop to control when the program performs the outputs. I know that EOF is a negative value (-1...
0
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7483
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.