INTRODUCTION TO C++ PROGRAMMING PRACTICE EXERCISES WITH CORRECT ANSWERS, Exams of Computer Programming

This study guide for Introduction to C++ Programming contains beginner-level programming practice exercises with correct solutions. It covers fundamental C++ concepts such as input and output handling using cin and cout, variable declaration, arithmetic operations (addition, subtraction, multiplication, and division), and conditional logic using if statements for comparisons. The exercises include programs that calculate and display mathematical results between two user inputs, as well as logic to determine the larger number or check if two numbers are equal. The material is structured in a clear code-and-explanation format to support learning core programming logic, strengthen problem-solving skills, and build a strong foundation in C++ syntax and structure.

Typology: Exams

2025/2026

Available from 05/30/2026

TheScholarVault
TheScholarVault 🇺🇸

603 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INTRODUCTION To C++ PROGRAMING
PRACTICE EXERCISES WITH CORRECT ANSWERS
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>
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
}
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: "; // 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 INTRODUCTION TO C++ PROGRAMMING PRACTICE EXERCISES WITH CORRECT ANSWERS and more Exams Computer Programming in PDF only on Docsity!

INTRODUCTION To C++ PROGRAMING

PRACTICE EXERCISES WITH CORRECT ANSWERS

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 using namespace std; int main() { int num1, num2; // declare variables cout << "Enter two integers: \t"; // prompt user cin >> num >> 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 }

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: "; // 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.WriteWaWprogramWthatWinputsWthreeWintegersWfromWtheWkeyboardWandWprintsWtheWs

um,Waverage,Wproduct,WsmallestWandWlargestWofWtheseWnumbers.

#includeWWusi ngWnamespaceWstd;WintWmai n()W{ intWnum1,Wnum2,Wnum3,Wsmallest,Wlargest;W//declareWvariablesWcoutW <>Wnum1W>>Wnum2W>>Wnum3;W//readWvaluesWfromWkeyboardWlargestW=Wn um1;W//WassumeW"num1"WisWtheWlargest ifW(num2W>Wlargest)W//isW"num2"WlargerWthanW"largest"?WlargestW=Wnu m2;W//WthenW"num2"WisWtheWlargest ifW(num3W>Wlargest)W//isW"num3"WlargerWthanW"largest"?WlargestW=Wnu m3;W//WthenW"num3"WisWtheWlargest smallestW=Wnum1;W//WassumeW"num1"WisWtheWsmallest ifW(num2Wv usingvnamespacevstd;v intvmain()v {v intvnum;v//vdeclarevvariablev coutv<>vnum;v//vreadvvaluevfromvkeyboardv ifv((numv%v2)v==v0)v//vIfv"num"visveven,vthenv coutv<