Introduction to C++ Programming Practice Exercises with Correct Answers, Exams of Computer Programming

This study material for Introduction to C++ Programming provides practice exercises with correct answers designed to support beginner programming learning and exam preparation. Topics include variables, data types, operators, input and output, control structures, loops, functions, arrays, and basic object-oriented programming concepts. Designed to help learners strengthen programming fundamentals, improve problem-solving skills, and build confidence in writing and understanding C++ code.

Typology: Exams

2025/2026

Available from 05/27/2026

StudySphereHub
StudySphereHub 🇺🇸

647 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(num2Wk usingknamespacekstd;k intkmain()k {k intknum;k//kdeclarekvariablek coutk<>knum;k//kreadkvaluekfromkkeyboardk ifk((numk%k2)k==k0)k//kIfk"num"kiskeven,kthenk coutk<