Problem Exercises Chapter 2
Problem Exercises Chapter 2
Problem Exercises Chapter 2
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;
}
int main()
{
// output results
cout << "********* *** * * \n"
<< "* * * * *** * * \n"
<< "* * * * ***** * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "* * * * * * * \n"
<< "********* *** * * " << endl;
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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: