In: Computer Science
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *.
B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n made up of *, where the * is in the mid row and mid column
C. Write a C++ program that 1. Prompt user to enter two integer a and b. 2. Then prints a + b rows each of which contains a ∗ b columns of Xs, but after each group of b complete columns the program prints a | symbol.
A.) printing a Z
In this program we have to print a pattern that looks like a Z, with '*' characters
First , get the value of n from user
Then, loop for i , j = 0 to n
Inside, the loop, if i is 0 ot n-1, print '*'. If (i,j) is a secondary diagonal element , print '*'. Otherwise print a white space
program:
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Enter n: "; cin>>n;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(i==0)
cout<<"*";
else
if(i==n-1)
cout<<"*";
else if(i+j ==
n-1)
cout<<"*";
else
cout<<" ";
}
cout<<"\n";
}
}
sample input and output:
B.) Printing a cross
program:
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Enter a odd integer: ";
cin>>n;
if(n%2==0){
cout<<n<<" is not
odd\n";
return 0;
}
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(i==j)
cout<<"*";
else
if(i+j==n-1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
sample input and output:
C.) Grid of X and |
program:
#include <iostream>
using namespace std;
int main(){
int a, b;
cout<<"Enter two integers: ";
cin>>a>>b;
for(int i = 0; i<a+b; i++){
for(int j = 0; j < a*b;
j++){
cout<<"X";
if((j+1)%b==0)
cout<<"|";
}
cout<<endl;
}
}
sample input and output: