Home >Documents >Write a program that asks the user to enter two numbers ... int digits{0}; // number of digits in...

# Write a program that asks the user to enter two numbers ... int digits{0}; // number of digits in...

Date post:15-Mar-2018
Category:
View:218 times
Transcript:
• 1

Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum,

product, difference, and quotient of the two numbers.

#include // allows program to perform input and output using namespace std; // program uses names from the std namespace int main() { int number1{0}; // first integer read from user int number2{0}; // second integer read from user cout > number1 >> number2; // read values from user // output the results cout

• 2

Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows:

#include // allows program to perform input and output using namespace std; // program uses names from the std namespace int main() { int number1{0}; // first integer read from user int number2{0}; // second integer read from user int number3{0}; // third integer read from user int smallest{0}; // smallest integer read from user int largest{0}; // largest integer read from user cout > number1 >> number2 >> number3; // read three integers largest = number1; // assume first integer is largest if (number2 > largest) { // is number2 larger? largest = number2; // number2 is now the largest } if (number3 > largest) { // is number3 larger? largest = number3; // number3 is now the largest } smallest = number1; // assume first integer is smallest if (number2 < smallest) { // is number2 smaller? smallest = number2; // number2 is now the smallest } if (number3 < smallest) { // is number3 smaller? smallest = number3; // number3 is now the smallest } cout

• 3

Write a program that reads an integer and determines and prints whether its odd or even. #include // allows program to perform input and output using namespace std; // program uses names from the std namespace int main() { int number{0}; // integer read from user cout > number; // read integer from user if (number % 2 == 0) { cout

• 4

Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each. #include // allows program to perform input and output using namespace std; // program uses names from the std namespace int main() { int number{0}; // integer read from user cout > number; // read integer from user cout

• 5

A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether its a palindrome. #include using namespace std; int main() { unsigned int number{0}; // user input number unsigned int digits{0}; // number of digits in input // ask for a number until it is five digits while (digits != 5) { cout > number; // get number // verify if number has 5 digits if (number < 100000) { if (number > 9999) { digits = 5; } else cout

• 6

Write a program that displays the following checkerboard pattern. Your program must use only three output statements, one of each of the following forms: cout

• 7

Write a program that prints the powers of the integer 2, namely 2, 4, 8, 16, 32, 64, etc. Your while loop should not terminate (i.e., you should create an infinite loop). #include using namespace std; int main() { unsigned int x{1}; while (true) {// infinite loop x *= 2; cout

• 8

Write a program that computes and prints the factorial of the specified value. The factorial of a nonnegative integer n is written n! (pronounced n factorial) and is defined as follows: n! = n (n 1) (n 2) 1 (for values of n greaterthan1) and n! = 1 (for n = 0 or n = 1). For example, 5! = 5 4 3 2 1, which is 120. #include using namespace std; int main() { int number; // user input unsigned int factorial{1}; // factorial of input value // get input cout > number; cout

• 9

Write a program that uses a for statement to find the smallest of several integers. Assume that the first value read specifies the number of values remaining. #include using namespace std; int main() { unsigned int number{0}; // number of values int value{0}; // current value int smallest{0}; // smallest value so far cout > smallest; // loop (number -1) times for (unsigned int i{2}; i > value; // read in next value // if current value less than smallest, update smallest if (value < smallest) { smallest = value; } } // display smallest integer cout

• 10

Write a program that uses for statements to print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout

• 11

// (d) fourth triangle for (unsigned int row{10}; row >= 1; --row) { for (unsigned int space{1}; space < row; ++space) { cout = row; --column) { cout

• 12

Write a program that uses for statements to print the following patterns separately. Use for loops to generate the patterns.

1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7

#include using namespace std; int main() { for (unsigned int row = 1; row

• 13

Write a program that uses for statements to print the following patterns separately. Use for loops to generate the patterns.

1 2 3 3 4 5 4 5 6 7

#include using namespace std; int main() { for (unsigned int i = 1; i

• 14

Write a program that uses for statements to print the following patterns separately. Use for loops to generate the patterns.

1 2 3 4 1 2 3 1 2 1

#include using namespace std; int main() { for (unsigned int i = 4; i >= 1; i--) { for (unsigned int j = 1; j

• 15

Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single blank or a single newline. Maximize your use of repetition (with nested for statements) and minimize the number of output statements.

#include using namespace std; int main() { // top half for (unsigned int row{1}; row Embed Size (px)
Recommended

Documents

Documents

Documents

Documents

Documents

Documents

Documents

Documents