Problem Exercises Chapter 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.

Write a program that prints the numbers from 1 to 4 on the same line with each
pair of adjacent numbers separated by one space. Write your program using the
following three methods:
a. Using one output statement with one stream insertion operator.
b. Using one output statement with four stream insertion operators.
c. Using four output statements.

#include <iostream>
using namespace std;

int main ()
{
// Part A: Using one output statement
// with one stream insertion operator
cout << "1 2 3 4 \n";
// Part B: Using one output statement
// with four stream insertion operators
cout << "1 " << "2 " << "3 " << "4 \n";
// Part C: Using four output statements
cout << "1 ";
cout << "2 ";
cout << "3 ";
cout << "4" << endl;
return 0;
}

A sample screen output look like:


2. Write a program that prints a box, an oval, an arrow and a diamond.
#include <iostream>
using namespace std;

int main()
{
// output results
cout << "********* *** * * \n"
<< "* * * * *** * * \n"
<< "* * * * ***** * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "********* *** * * " << endl;
return 0;
}

A sample screen output look like:


3. Write a program that asks the user to enter two numbers, obtains the two numbers
from the user and prints their sum, product, difference, and quotient in tabular
format.

#include <iostream>
using namespace std;

int main()
{
int num1, num2; // declare variables
cout << "Enter two integers: \t"; // prompt user
cin >> num1 >> num2; // read values from keyboard
// output results
cout << "The sum is: \t\t" << num1 + num2 << "\n"
<< "The product is: \t" << num1 * num2 << "\n"
<< "The difference is: \t" << num1 - num2 << "\n"
<< "The quotient is: \t" << num1 / num2 << endl;
return 0;
}

A sample screen output look like:


4. Write a program that asks the user to enter two integers, obtains the numbers from
the user, and 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>
using namespace std;

int main()
{
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 == num2 ) // compare if "num1" is = to "num2"
cout << "These numbers are equal." << endl;
if ( num1 > num2 ) { // compare if "num1" is > than "num2"
cout << num1 << " is larger." << endl;
}
if ( num2 > num1 ) // compare if "num2" is > than "num1"
cout << num2 << " is larger." << endl;
return 0;
}

Two samples screen output look like:


5. Write a program that inputs three integers from the keyboard and prints the
average, smallest and largest of these 3 numbers.

#include <iostream>
using namespace std;

int main()
{
int num1, num2, num3, smallest, largest;
cout << "Input three different integers: ";
cin >> num1 >> num2 >> num3;
largest = num1; // assume "num1" is the largest
if ( num2 > largest ) // if "num2" is > than largest
largest = num2; // then "num2" is the largest
if ( num3 > largest ) // if "num3" is > than largest
largest = num3; // then "num3" is the largest
smallest = num1; // assume "num1" is the smallest
if ( num2 < smallest ) // if "num2" is < than smallest
smallest = num2; // then "num2" is the smallest
if ( num3 < smallest ) // if "num3" is < than smallest
smallest = num3; // then "num3" is the smallest
// output results
cout <<"Average is: \t" << (num1 + num2 + num3) / 3 << "\n"
<<"Smallest is: \t" << smallest << "\n"
<<"Largest is: \t" << largest << endl;
return 0;
}

A sample screen output look like:


6. Write a program that reads an integer and determines and prints whether it is odd
or even. (Hint: Use the modulus operator. An even number is a multiple of two.
Any multiple of two leaves a remainder of zero when divided by 2.)

#include <iostream>
using namespace std;

int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
// If "num" is even, then
if ( (num % 2) == 0 )
cout << "The number " << num << " is even." << endl;
// If "num" is odd, then
if ( (num % 2) != 0 )
cout << "The number " << num << " is odd." << endl;
return 0;
}

A sample screen output look like:


7. Write a program that reads in two integers and determines and prints if the first is
a multiple of the second. (Hint: Use the modulus operator.)

#include <iostream>
using namespace std;

int main()
{
int n1, n2;
cout << "Enter two integers: ";
cin >> n1 >> n2;
// If "n1" is a multiple of "n2", then
if ( (n1 % n2) == 0 )
cout << n1 << " is a multiple of " << n2 << endl;
// If "n1" is not a multiple of "n2", then
if ( (n1 % n2) != 0 )
cout << n1 << " is not a multiple of " << n2 << endl;
return 0;
}

A sample screen output look like:


8. Write a program that inputs a five-digit number, separates the number into its
individual digits and prints the digits separated from one another by three spaces
each. Hint: Use the integer division and modulus operators.
For example, if the user types in 42339 the program should print: 4 2 3 3 9

#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter a five-digit number: ";
cin >> n;
cout << n / 10000 << " "; // integer division yield
// an integer result
n = n % 10000; // modulus return the remainder
// after an integer division
cout << n / 1000 << " ";
n = n % 1000;
cout << n / 100 << " ";
n = n % 100;
cout << n / 10 << " ";
n = n % 10;
cout << n << endl;
return 0;
}

A sample screen output look like:


9. Using only the techniques you learned in this chapter, write a program that
calculates and prints the squares and cubes of numbers from 1 to 5. Use tabs to
print the following table of values:
number square cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125

#include <iostream>
using namespace std;

int main()
{
int n = 1; // Declare and initialize "n" to 1
// Print the table header
cout << "number" <<'\t'<< "square" <<'\t'<< "cube" << "\n";
// Print the square and cube of "n"
cout << n << '\t' << n * n << '\t' << n * n * n << "\n";
n = n + 1; // Increment "n" by 1
cout << n << '\t' << n * n << '\t' << n * n * n << "\n";
n = n + 1;
cout << n << '\t' << n * n << '\t' << n * n * n << "\n";
n = n + 1;
cout << n << '\t' << n * n << '\t' << n * n * n << "\n";
n = n + 1;
cout << n << '\t' << n * n << '\t' << n * n * n << "\n";
return 0;
}
A sample screen output look like:

You might also like