Question

In: Computer Science

Write a program in C++ to display the following patterns: * ** *** **** 1 12...

Write a program in C++ to display the following patterns:
*
**
***
****

1
12
123
1234

1
22
333
4444

1
2 3
4 5 6
7 8 9 10

1
2 3
4 5 6
7 8 9 10

*
***
*****
*******
*********
*******
*****
***
*

Solutions

Expert Solution

Typed Code:

#include <iostream>

using namespace std;

int main()
{
int i = 0 , j =0;
for(int i = 0;i<4;i++){
for(int j = 0;j<=i;j++){ //Logic to print stars
cout<<"*";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///SECOND PATTERN//\n";
for(int i = 1;i<=4;i++){
for(int j = 1;j<=i;j++){ //in this logic we are printing j ,with i starting from 1 to 4
cout<<j;
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///THIRD PATTERN//\n";
for(int i = 1;i<=4;i++){ //in this logic we are printing i, with i starting from 1 to 4
for(int j = 1;j<=i;j++){
cout<<i;
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///FOURTH PATTERN//\n";
int k = 1;
for(int i = 1;i<=4;i++){
for(int j = 1;j<=i;j++){//in this logic we are printing a number from 1 with incrementing
//, with i starting from 1 to 4
cout<<k++<<" ";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///FIFTH PATTERN//\n";
for(int i = 1,k = 1;i<=4;i++){//in this logic we are printing a number from 1 with incrementing
//, with i starting from 1 to 4
for(int j = 1;j<=i;j++){
cout<<k++<<" ";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///SIXTH PATTERN//\n";
int n = 5;
int m = 2*n; // doubling the number bcz we need to iterate 2*n -1 times
for(int i = 0;i<m;i++){
int x = m/2; //taking half of the number
if(i < x) { //from 1 to less than half of number , it is in inceasing order
for(int j = 0;j<(2*i + 1);j++){ //here 2i +1 gives odd number of stars
cout<<"*";
}
} else { // after half of number we need to print decreasing order
int z = (2*n - (2*i -6)); //logic to print odd number of stars
for(int j = z ;j>=0;j--){ //printing stars with decreasing order
cout<<"*";
}
}

cout<<"\n"; //after every j loop we need new line
}
return 0;
}
SCREEN SHOT :

OUTPUT:


Related Solutions

Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
Write a C++ program to display toy name
Write a C++ program to display toy name
Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
Write a C++ main program that has the following: 9. Display all printable characters of the...
Write a C++ main program that has the following: 9. Display all printable characters of the ASCII table in 3 columns: first column: 32-63, second column: 64-95, third column: 96-127. Each column must include the numeric value and the corresponding character. Following is an example of one of 32 rows in the ASCII table: 33 ! 65 A 97 a
Write a program in C++ to display the multipliaction table vertically from 1 to n (use...
Write a program in C++ to display the multipliaction table vertically from 1 to n (use setw to format the output). 1x1 = 1, 2x1 = 2, 3x1 = 3, 4x1 = 4, 5x1 = 5, 6x1 = 6, 7x1 = 7, 8x1 = 8 ... 1x10 = 10, 2x10 = 20, 3x10 = 30, 4x10 = 40, 5x10 = 50, 6x10 = 60, 7x10 = 70, 8x10 = 80
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT