0% found this document useful (0 votes)
6 views11 pages

PP 1 Sol

Uploaded by

ans78hu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

PP 1 Sol

Uploaded by

ans78hu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ Assignment Solutions | Pattern Printing - 1 | Week 3

Print the following pattern


1 1 1 1

2 2 2 2
3 3 3 3

4 4 4 4

Solution:

#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 1; i <= n; ++i) {


for(int j = 1; j <= n; ++j) {
cout << i << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern

Input: n = 4
Output:
1 2 3 4
1 2 3

1 2

1
Solution:

#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 1; i <= n; ++i) {


for(int j = 1; j <= n-i+1; ++j) {
cout << j << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern

Input: n = 4
Output:
A

A B
A B C

A B C D

Solution:
#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 1; i <= n; ++i) {


for(int j = 1; j <= i; ++j) {
cout << (char)('A'+j-1) << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern

Input: n = 4
Output:
1

A B
1 2 3

A B C D

1 2 3 4 5

Solution:
#include <bits/stdc++.h>

int main() {

int n;
cin >> n;

for(int i = 1; i <= n; ++i) {


if(i % 2 == 1)
for(int j = 1; j <= i; ++j) {
cout << j << " ";
}
else
for(int j = 1; j <= i; ++j) {
cout << (char) ('A' + j - 1) << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern


Input n = 4
Output:
*
**

***
****

***

**

Solution:
#include <bits/stdc++.h>

int main() {

int n;
cin >> n;

// Printing upper triangle


for(int i = 0; i < n; ++i) {
for(int j = 0; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}

// Printing lower triangle


for(int i = 0; i < n-1; ++i) {
for(int j = 0; j < n - i - 1; ++j) {
cout << "* ";
}
cout << endl;
}

Print the following pattern


Sample Input : m = 4, n = 6

Sample Output :

******

* *

* *

******

Solution:
#include <iostream>
using namespace std;

int main() {

int m, n;
cin >> m >> n;

for(int i = 0; i < m; ++i) {


for(int j = 0; j < n; ++j) {
if(i == 0 || j == 0 || i == m-1 || j == n-1) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}

return 0;
}

Print the following pattern


Sample Input : n = 4
Output :
****

****
****

****

Solution:
#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 0; i < n; ++i) {


for(int j = 0; j < n-i-1; ++j) {
cout << " ";
}
for(int j = 0; j < n; ++j) {
cout << "* ";
}
cout << endl;
}

return 0;
}

Print the following pattern


Sample Input : n= 4

Output :
1

1 2

1 2 3
1 2 3 4

Solution:
#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 0; i < n; ++i) {


for(int j = 0; j < n-i-1; ++j) {
cout << " ";
}
for(int j = 1; j <= i+1; ++j) {
cout << j << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern


Input : n = 4
Output :
A

A B
A B C

A B C D

Solution:
#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 0; i < n; ++i) {


for(int j = 0; j < n-i-1; ++j) {
cout << " ";
}
for(int j = 1; j <= i+1; ++j) {
cout << (char)('A' + j - 1) << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern


Input: n = 4

Output:
1
2 1

3 2 1
4 3 2 1

Solution:
#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

for(int i = 0; i < n; ++i) {


for(int j = 0; j < n-i-1; ++j) {
cout << " ";
}
for(int j = i+1; j >= 1; --j) {
cout << j << " ";
}
cout << endl;
}

return 0;
}

Print the following pattern

Input: n = 4

Output:
*

**

***
****

***

**

Solution:
#include <bits/stdc++.h>

int main() {

int n;
cin >> n;

// Printing upper triangle


for(int i = 0; i < n; ++i) {
for(int j = 0; j < n-i-1; ++j) {
cout << " ";
}
for(int j = 0; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}

// Printing lower triangle


for(int i = 0; i < n-1; ++i) {
for(int j = 0; j <= i; ++j) {
cout << " ";
}

Note:- Please try to invest time doing the assignments which are necessary to build a strong
foundation. Do not directly Copy Paste using Google or ChatGPT. Please use your brain .

You might also like