+ All Categories
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 <iostream> // 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 << "Enter two integers: "; // prompt user for data cin >> number1 >> number2; // read values from user // output the results cout << "The sum is " << number1 + number2 << "\nThe product is " << number1 * number2 << "\nThe difference is " << number1 - number2 << "\nThe quotient is " << number1 / number2 << endl; return 0; } Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal." #include <iostream> // 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 << "Enter two integers: "; // prompt user for data cin >> number1 >> number2; // read two integers from user if (number1 == number2) { cout << "These numbers are equal." << endl; } if (number1 > number2) { cout << number1 << " is larger." << endl; } if (number2 > number1) { cout << number2 << " is larger." << endl; } return 0; }

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 <iostream> // 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 << "Input three different integers: "; // prompt cin >> 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 << "Sum is " << number1 + number2 + number3 << "\nAverage is " << (number1 + number2 + number3) / 3 << "\nProduct is " << number1 * number2 * number3 << "\nSmallest is " << smallest << "\nLargest is " << largest << endl; return 0; }

3

Write a program that reads an integer and determines and prints whether it’s odd or even. #include <iostream> // 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 << "Enter an integer: "; // prompt cin >> number; // read integer from user if (number % 2 == 0) { cout << "The integer " << number << " is even." << endl; } if (number % 2 != 0) { cout << "The integer " << number << " is odd." << endl; } return 0; } Write a program that reads in two integers and determines and prints if the first is a multiple of the second #include <iostream> // 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 << "Enter two integers: "; // prompt cin >> number1 >> number2; // read two integers from user // using modulus operator if (number1 % number2 == 0) { cout << number1 << " is a multiple of " << number2 << endl; } if (number1 % number2 != 0) { cout << number1 << " is not a multiple of " << number2 << endl; } return 0; }

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 <iostream> // 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 << "Enter a five-digit integer: "; // prompt cin >> number; // read integer from user cout << number / 10000 << " "; number = number % 10000; cout << number / 1000 << " "; number = number % 1000; cout << number / 100 << " "; number = number % 100; cout << number / 10 << " "; number = number % 10; cout << number << endl; return 0; }

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 it’s a palindrome. #include <iostream> 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 << "Enter a 5-digit number: "; // prompt for a number cin >> number; // get number // verify if number has 5 digits if (number < 100000) { if (number > 9999) { digits = 5; } else cout << "Number must be 5 digits" << endl; } else cout << "Number must be 5 digits" << endl; } // get the digits unsigned int digit1{number / 10000}; unsigned int digit2{number % 10000 / 1000}; unsigned int digit4{number % 10000 % 1000 % 100 / 10}; unsigned int digit5{number % 10000 % 1000 % 100 % 10}; // print whether the number is a palindrome if (digit1 == digit5) { if (digit2 == digit4) { cout << number << " is a palindrome!!!" << endl; } else { cout << number << " is not a palindrome." << endl; } } else { cout << number << " is not a palindrome." << endl; } return 0; }

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 << "* "; cout << ' '; cout << endl;

#include <iostream> using namespace std; int main() { int row{8}; // row counter while (row-- > 0) // loop 8 times { int column{8}; // reset column counter // if even row, begin with a space if (row % 2 == 0) { cout << ' '; } while (column-- > 0) { // loop 8 times cout << "* "; } cout << endl; // go to next line } retun 0; } Or using for loop:

for (int row = 8; row >= 1; row--) { if (row % 2 == 0) cout << " "; for (int column = 8; column >= 1 ; column--) { cout << "* "; } cout << endl; }

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 <iostream> using namespace std; int main() { unsigned int x{1}; while (true) {// infinite loop x *= 2; cout << x << endl; if(x == 0) //why this? break; } return 0; } Write a program that reads the radius of a circle (as a double value) and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for π. The program will exit if the user enters a value of 0 for radius. #include <iostream> using namespace std; int main() {

double radius{ 0 }; // input radius const double PI{ 3.14159 }; // value for pi // get radius value cout << "Enter the radius (0 to exit): "; cin >> radius; while (radius != 0) { // compute and display diameter cout << "The diameter is " << radius * 2.0; // compute and display circumference cout << "\nThe circumference is " << 2.0 * PI * radius; // compute and display area cout << "\nThe area is " << PI * radius * radius << endl; cout << "Enter the radius (0 to exit): "; cin >> radius; } return 0; }

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 <iostream> using namespace std; int main() { int number; // user input unsigned int factorial{1}; // factorial of input value // get input cout << "Enter a positive Integer: "; cin >> number; cout << number << "! is "; while (number > 0) { // calculate factorial factorial *= number; --number; } cout << factorial << endl; return 0; }

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 <iostream> 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 << "Enter the number of integers to be processed "; cout << "followed by the integers: " << endl; cin >> number >> smallest; // loop (number -1) times for (unsigned int i{2}; i <= number; ++i) { cin >> value; // read in next value // if current value less than smallest, update smallest if (value < smallest) { smallest = value; } } // display smallest integer cout << "\nThe smallest integer is: " << smallest << endl; return 0; }

Write a program that uses a for statement to calculate and print the product of the odd integers from 1 to 15. #include <iostream> using namespace std; int main() { unsigned int product{1}; // calculate product // increment counter i by 2 for odd numbers for (unsigned int i{3}; i <= 15; i += 2) { product *= i; } // display resulting product cout << "Product of the odd integers from 1 to 15 is: " << product << endl; return 0; }

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 << '*';

#include <iostream> using namespace std; int main() { // (a) first triangle for (unsigned int row{1}; row <= 10; ++row) { for (unsigned int column{1}; column <= row; ++column) { cout << "*"; } cout << endl; } cout << endl; // (b) second triangle for (unsigned int row{10}; row >= 1; --row) { for (unsigned int column{1}; column <= row; ++column) { cout << "*"; } cout << endl; } cout << endl; // (c ) third triangle for (unsigned int row{10}; row >= 1; --row) { for (unsigned int space{10}; space > row; --space) { cout << " "; } for (unsigned int column{1}; column <= row; ++column) { cout << "*"; } cout << endl; } cout << endl;

11

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

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<iostream> using namespace std; int main() { for (unsigned int row = 1; row <= 4; row++) { for (unsigned int column = row; column <= row + 3; column++) cout << column << " "; cout << endl; } return 0; } OR #include<iostream> using namespace std; int main() { for (unsigned int i = 1; i <= 4; i++) { for (unsigned int j = 1; j <= 4; j++) cout << j + i - 1 << " "; cout << endl; } return 0; } OR #include<iostream> using namespace std; int main() { for (unsigned int i = 1; i <= 4; i++) { for (unsigned int j = 1; j <= 4; j++) cout << j + i + 1 << " "; cout << endl; } return 0; }

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<iostream> using namespace std; int main() { for (unsigned int i = 1; i <= 4; i++) { for (unsigned int j = 1; j <= i; j++) cout << j + i - 1 << " "; cout << endl; } cout << endl; //OR using the following: for (unsigned int i = 1; i <= 4; i++) { for (unsigned int j = 1; j <= 4; j++) if (i >= j) cout << j + i - 1 << " "; cout << endl; } return 0; }

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<iostream> using namespace std; int main() { for (unsigned int i = 4; i >= 1; i--) { for (unsigned int j = 1; j <= i; j++) cout << j << " "; cout << endl; } cout << endl; //OR using the following: for (unsigned int i = 4; i >= 1; i--) { for (unsigned int j = 1; j <= 4; j++) if (i >= j) cout << j << " "; cout << endl; } return 0; }

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 <iostream> using namespace std; int main() { // top half for (unsigned int row{1}; row <= 5; ++row) { // print preceding spaces for (unsigned int space{1}; space <= 5 - row; ++space) { cout << ' '; } // print asterisks for (unsigned int asterisk{1}; asterisk <= 2 * row - 1; ++asterisk) { cout << '*'; } cout << '\n'; } // bottom half for (unsigned int row{4}; row >= 1; --row) { // print preceding spaces for (unsigned int space{1}; space <= 5 - row; ++space) { cout << ' '; } // print asterisks for (unsigned int asterisk{1}; asterisk <= 2 * row - 1; ++asterisk) { cout << '*'; } cout << '\n'; } cout << endl; return 0; }


Top Related