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...
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...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT