Question

In: Computer Science

A. Write a program 1. Prompt the user to enter a positive integer n and read...

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. 

Solutions

Expert Solution

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

  • In this program, we have to print a cross with '*' characters
  • First, get an odd integer n from user. After reading, check if n is even, if yes then terminate the program
  • loop for i, j = 0 to n.
  • Inside the loop, check if (i,j) are diagonal elements or secondary diagonal elements. If yes, then print '*' . otherwise, print a white space

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 |

  • In this program, we have to print a grid of 'X' and '|'
  • get the values of a and b from user.
  • loop for i = 0 to a+b and j = 0 to a*b.
  • Inside the loop, print 'X'. if j+1 is divisible by b, then print '|'

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:


Related Solutions

Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT