Advertisement
Guest User

c++ ladder

a guest
Feb 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int x, y, z, num;
  7. char c;
  8.  
  9. cout<<"Insert a number: ";
  10. cin<<num;
  11. cout<<"Insert a letter (l, r): ";
  12. cin<<c;
  13.  
  14. if (c == 'l')
  15. {
  16. for (x=1; x<=num; x++) //tracks the number of lines
  17. {
  18. for (y=1; y<=x; y++) //tracks number of Xs already printed
  19. {
  20. cout<<"X";
  21. }
  22. cout<<"\n"; //prints newline character in order to start printing next line in the ladder
  23. }
  24. }
  25. else //when c == 'r'
  26. {
  27. z = num; //z holds the number of total characters (Xs and whitespace) in a line
  28. for (x=1; x<=num; x++) //tracks the number of lines
  29. {
  30. while (z > x) //i.e while total number of characters in the line is greater than the number of Xs needed
  31. {
  32. cout<<" "; //prints spaces in front of the Xs in order to right-align the ladder
  33. z--;
  34. }
  35.  
  36. for (y=1; y<=x; y++) //tracks number of Xs already printed
  37. {
  38. cout<<"X";
  39. }
  40. cout<<"\n"; //prints newline character in order to start printing next line in the ladder
  41. }
  42. }
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement