Docsity

C++ introduction exercises, Exercises of Computer Science

Toggle sidebar
bg1
Introduction to C++ Programing.
Practice exercises.
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.
2. 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; // declare variables
cout << "Enter two integers: \t"; // prompt user
cin >> num1 >> num2; // read values from keyboard
// output the 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; //indicate successful termination
}
#include <iostream>
using namespace std;
int main()
{
int num1, num2; // declare variables
cout << "Enter two integers: "; // prompt user
cin >> num1 >> num2; // read values from keyboard
// Compares if values of "num1" and "num2" are equal
if (num1 == num2) // If condition is true, then
cout << "These numbers are equal." << endl;
if (num1 > num2) // If condition is true, then
cout << num1 << " is larger." << endl;
if (num2 > num1) // If condition is true, then
cout << num2 << " is larger." << endl;
return 0; //indicate successful termination
}
pf3

Partial preview of the text

Download C++ introduction exercises and more Exercises Computer Science in PDF only on Docsity!

Introduction to C++ Programing.

Practice exercises.

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.

2. 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 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 the 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; //indicate successful termination } #include using namespace std; int main() { int num1, num2; // declare variables cout << "Enter two integers: "; // prompt user cin >> num1 >> num2; // read values from keyboard // Compares if values of "num1" and "num2" are equal if (num1 == num2) // If condition is true, then cout << "These numbers are equal." << endl; if (num1 > num2) // If condition is true, then cout << num1 << " is larger." << endl; if (num2 > num1) // If condition is true, then cout << num2 << " is larger." << endl; return 0; //indicate successful termination

3. Write a program that inputs three integers from the keyboard and prints the sum,

average, product, smallest and largest of these numbers.

4. Write a program that reads an integer and determines and prints whether it is odd

or even.

#include using namespace std; int main() { int num1, num2, num3, smallest, largest; //declare variables cout << "Input three different integers: ";//prompt user cin >> num1 >> num2 >> num3; //read values from keyboard largest = num1; // assume "num1" is the largest if (num2 > largest) //is "num2" larger than "largest"? largest = num2; // then "num2" is the largest if (num3 > largest) //is "num3" larger than "largest"? largest = num3; // then "num3" is the largest smallest = num1; // assume "num1" is the smallest if (num2 < smallest) //is "num2" smaller than smallest? smallest = num2; // then "num2" is the smallest if (num3 < smallest) //is "num3" smaller than smallest? smallest = num3; // then "num3" is the smallest // output the results cout << "Sum is: \t" << num1 + num2 + num3 << "\n" << "Average is: \t" << (num1 + num2 + num3) / 3 << "\n" << "Product is: \t" << num1 * num2 * num3 << "\n" << "Smallest is:\t" << smallest << "\n" << "Largest is: \t" << largest << endl; return 0; //indicate successful termination } #include using namespace std; int main() { int num; // declare variable cout << "Enter a number: "; // prompt user cin >> num; // read value from keyboard if ((num % 2) == 0) // If "num" is even, then cout << "The number " << num << " is even." << endl; if ((num % 2) != 0) // If "num" is odd, then cout << "The number " << num << " is odd." << endl; return 0;