Informatics - Triangles (week 18)
Hi everyone!
In this week I just want to show you some codes of how to make a standing up and also upside-down triangle. There will be some types of triangles here so stay tuned!
1. Right Triangle
#include <iostream>
int main (){
for (int i = 1; i <= 5; i++){
for (int j = 1; j <= i; j++){
std :: cout << "*";
}
std :: cout << std :: endl;
}
return 0;
}
This program prints a right-angled triangle pattern using asterisks (*). It uses a nested for loop, where the outer loop controls the number of rows (from 1 to 5), and the inner loop controls how many * are printed in each row. In each iteration of the outer loop, the inner loop runs from 1 up to the current row number (i), printing that many stars. After the inner loop finishes, a newline (endl) is printed to move to the next row. As a result, the output looks like a growing triangle with one * in the first row, two in the second, and so on until five * in the last row.
output:
*
**
***
****
*****
2. Standing up triangle
#include <iostream>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
std::cout << " ";
}
for (int k = 1; k <= (2 * i - 1); k++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
This program prints a centered pyramid made of asterisks (*). It uses a nested loop structure, where the outer loop (i) controls the number of rows (5 in this case). The first inner loop (j) prints spaces to align the stars properly, decreasing as the rows increase to center the pyramid. The second inner loop (k) prints an increasing number of stars in each row, following the formula (2 * i - 1), which ensures an odd number of stars per row, forming a symmetrical shape. Finally, std::cout << std::endl; moves to the next line after printing each row, creating the pyramid structure.
output:
*
***
*****
*******
*********
3. Upside down triangle
#include <iostream>
int main() {
int rows = 5; // Number of rows
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
std::cout << " ";
}
for (int k = 1; k <= (2 * i - 1); k++) {
std::cout << "*";
}
std::cout << std::endl;
}
return 0;
}
This program prints an upside-down triangle made of stars. It starts with 5 rows, where the first row has the most stars, and each row below has fewer stars until the last row has just one. The first loop controls how many rows there are. The second loop adds spaces before the stars to keep the triangle centered. The third loop prints the stars, starting with the most at the top and reducing the count as it goes down. After printing each row, the program moves to the next line to continue the pattern until the triangle is complete.
Output:
*********
*******
*****
***
*
4. Right triangle with users input
#include <iostream>
using namespace std;
int main (){
int n;
cout << "Enter the height that you want: ";
cin >> n;
for (int i = n; i >=1; i--){
for (int k = n; k <= i - 1; k++){
cout << " ";
}
for (int j=1; j <= i; j++){
cout << i << " ";
}
cout << endl;
}
}
This program creates a triangle pattern using numbers. First, it asks the user to enter a height (n), which determines how many rows the triangle will have. The outer loop (i) starts from n and counts down to 1, controlling the rows. The first inner loop (k) is supposed to print spaces for alignment, but it has a mistake, so it doesn't work as intended. The second inner loop (j) prints the current row number (i) multiple times in that row. As the rows decrease, the numbers go from n down to 1, creating a descending triangle shape where each row repeats the same number.
Output:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
5. Standing up and Upside down triangle with users input
//StandingnUpsidedownTriangle.cpp
#include <iostream>
using namespace std;
void standingtriangle (int n){
for (int i = 1; i <=n; i++){
for (int k = 1; k <= n-i; k++){
cout << " ";
}
for (int j=1; j <= i; j++){
cout << i << " ";
}
cout << endl;
}
}
void upsidedowntriangle (int n){
for (int i = n; i >=1; i--){
for (int k = n; k >= i - 1; k--){
cout << " ";
}
for (int j=1; j <= i; j++){
cout << i << " ";
}
cout << endl;
}
}
int main (){
int height;
int choice;
cout << "Enter the heights of your triangle: ";
cin >> height;
cout << "How do you want your triangle? \n";
cout << "1. Standing up triangle \n";
cout << "2. Upside down triangle \n";
cout << "Choose between 1 or 2! \n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
standingtriangle(height);
break;
case 2:
upsidedowntriangle(height);
break;
default:
cout << "!!!YOU INSERT THE WRONG INPUT!!!" << endl;
cout << " !!ERROR!!ERROR!!ERROR!! " << endl;
}
}
This program allows the user to generate either a standing-up triangle or an upside-down triangle using numbers. It starts by asking the user for the height of the triangle and then prompts them to choose between two options: a normal upright triangle or an inverted one. Depending on the user’s choice, the program calls either the standingtriangle function or the upsidedowntriangle function. The standingtriangle function prints a right-angled triangle where each row contains the row number, starting from 1 and increasing up to the height entered by the user, with proper spacing for alignment.
The upsidedowntriangle function does the opposite by printing a downward-facing triangle, starting with the largest row number at the top and decreasing down to 1. The for loops inside these functions ensure proper alignment by adding spaces before printing the numbers, creating a visually structured output. Finally, the program uses a switch statement to execute the correct function based on user input. If the user enters an invalid choice, an error message is displayed.
Komentar
Posting Komentar