Uploaded by Yash Verma

Assignment 5 - Nested Loop(CPP KANHA)

advertisement
Assignment 5 – Nested Loop
Pattern programs are nothing but patterns consisting of numbers, alphabets, or
symbols in a particular form. These kinds of pattern programs can be solved easily
using for loop condition.
#1 - Rectangle Pattern Programs in C++
Ans -Hollow rectangle
Code
#include <iostream>
using namespace std;
int main() {
int rows, columns;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
if (i == 1 || i == rows || j == 1 || j == columns) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
Output
Ans-Solid rectangle
Code
#include <iostream>
using namespace std;
int main() {
int rows, columns;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
#2 - Pyramid Pattern Programs in C++ using stars
Ans-half pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
ans-Inverted Half Pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the inverted half pyramid: ";
cin >> n;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
Ans- Hollow inverted Hatf Pyramid
Code#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the inverted hollow right-angled half
pyramid: ";
cin >> n;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
Output
Ans-full solid pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the full pyramid: ";
cin >> n;
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print left asterisks
for (int k = 1; k <= i; k++) {
cout << "*";
}
// Print right asterisks
for (int k = 2; k <= i; k++) {
cout << "*";
}
// Move to the next line
cout << endl;
}
return 0;
}
Output
Ans-solid full invert pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the solid inverted full pyramid: ";
cin >> n;
for (int i = n; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
// Move to the next line
cout << endl;
}
return 0;
}
Output
Ans-full hollow pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the hollow inverted full pyramid: ";
cin >> n;
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j < i; j++) {
cout << " ";
}
// Print the left asterisk for each row
cout << "*";
// Print the spaces between the left and right asterisks
for (int j = 1; j <= 2 * (n - i) - 1; j++) {
if (i == 1 || j == 2 * (n - i) - 1) {
cout << "*";
} else {
cout << " ";
}
}
// Print the right asterisk
if (i != 1) {
cout << "*";
}
// Move to the next line
cout << endl;
}
return 0;
}
Output
Assignment 5 – Nested Loop
#3 - Pyramid Pattern Programs in C++, using numbers
USING NUMBERS
Ans- half pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the half pyramid: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output
Ans - half invert pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the inverted half pyramid: ";
cin >> n;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output
Ans- half invert hollow pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the half hollow pyramid: ";
cin >> n;
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print numbers and spaces
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == n) {
cout << j;
} else {
cout << " ";
}
}
// Move to the next line
cout << endl;
}
return 0;
}
Ans-full hollow pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the half hollow pyramid: ";
cin >> n;
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print numbers and spaces
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == n) {
cout << j;
} else {
cout << " ";
}
}
// Move to the next line
cout << endl;
}
return 0;
}
Output
Ans- full solid pyramid
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the full solid pyramid: ";
cin >> n;
int num = 1;
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print numbers
for (int k = 1; k <= 2 * i - 1; k++) {
cout << num << " ";
num++;
}
// Move to the next line
cout << endl;
}
return 0;
}
Output
#5 - Diamond Pattern Programs in C++, using stars
Ans - solid diamond
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the diamond pattern: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Output
Ans-hollow diamond
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the hollow diamond pattern: ";
cin >> n;
// Print the top half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
// Print the bottom half of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
Output
Ans-half solid diamond
Code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the diamond pattern: ";
cin >> n;
// Print the top half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= i; k++) {
cout << "*";
}
cout << endl;
}
// Print the bottom half of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= i; k++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Output
Download