Introduction to C++ Programming Practice Exercises with Answers Study Guide, Exams of C programming

This Introduction to C++ Programming practice guide is designed to help students build a strong foundation in programming concepts through structured exercises with correct answers. It covers essential topics such as variables, data types, control structures, loops, functions, arrays, and basic object-oriented programming principles. Each exercise is designed to reinforce understanding and improve coding logic and problem-solving skills. Ideal for beginners and students preparing for exams, this guide simplifies complex programming concepts into clear, step-by-step explanations to support learning, revision, and practical coding success in C++.

Typology: Exams

2025/2026

Available from 04/18/2026

PROF.GUIDE
PROF.GUIDE ๐Ÿ‡ท๐Ÿ‡ด

5

(1)

1.6K 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 progr m th t sks the user to enter two numbers, obt ins the two numbers ๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž
from the user, nd prints the sum, product, difference, nd quotient of the two ๐‘Ž ๐‘Ž
numbers.
#include <iostre m>๐‘Ž
using n mesp ce ๐‘Ž ๐‘Ž std;
int m in()๐‘Ž
{
int num1, num2; // decl re v ri bles๐‘Ž ๐‘Ž ๐‘Ž
cout << "Enter two integers: \t"; // prompt user cin >> num1 >> num2;
// re d v lues from keybo rd // 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; //indic te successful termin tion๐‘Ž ๐‘Ž
}
2.Write progr m th t sks the user to enter two integers, obt ins the numbers from๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž
the user, nd then prints the l rger number followed by the words "is l rger." If๐‘Ž ๐‘Ž ๐‘Ž
the numbers re equ l, print the mess ge "These numbers re equ l."๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž ๐‘Ž
#include <iostre m>๐‘Ž
using n mesp ce ๐‘Ž ๐‘Ž std;
int m in()๐‘Ž
{
int num1, num2; // decl re v ri bles๐‘Ž ๐‘Ž ๐‘Ž
cout << "Enter two integers: "; // prompt user cin >> num1
>> num2; // re d v lues from keybo rd ๐‘Ž ๐‘Ž ๐‘Ž // Comp res if v lues of ๐‘Ž ๐‘Ž
"num1" nd "num2" re equ l ๐‘Ž ๐‘Ž ๐‘Ž if (num1 == num2) // If condition is
true, then cout << "These numbers re equ l." ๐‘Ž ๐‘Ž << endl; if
(num1 > num2) // If condition is true, then
cout << num1 << " is l rger." ๐‘Ž<< endl;
if (num2 > num1) // If condition is true, then
cout << num2 << " is l rger." ๐‘Ž<< endl;
return 0; //indic te successful termin tion๐‘Ž ๐‘Ž
}
pf3

Partial preview of the text

Download Introduction to C++ Programming Practice Exercises with Answers Study Guide and more Exams C programming in PDF only on Docsity!

INTRODUCTION To C++ PROGRAMING

PRACTICE EXERCISES WITH CORRECT ANSWERS

1.Write ๐‘Ž progr m th t ๐‘Ž ๐‘Ž ๐‘Ž sks the user to enter two numbers, obt ins the two numbers ๐‘Ž

from the user, ๐‘Ž nd prints the sum, product, difference, ๐‘Ž nd quotient of the two

numbers.

#include ๐‘Ž using n mesp ce๐‘Ž ๐‘Ž std; int m in()๐‘Ž { int num1, num2; // decl re๐‘Ž v ri bles๐‘Ž ๐‘Ž cout << "Enter two integers: \t"; // prompt user cin >> num1 >> num2; // re d๐‘Ž v lues๐‘Ž from keybo rd๐‘Ž // output the results cout << "The sum is: \t\t" << num1 + num2 << "\n" << "The product is: \t" << num1 * num2 << "\n" << "The difference is: \t" << num

  • num2 << "\n" << "The quotient is: \t" << num1 / num2 << endl; return 0; //indic te๐‘Ž successful termin tion๐‘Ž }

2.Write ๐‘Ž progr m th t ๐‘Ž ๐‘Ž ๐‘Ž sks the user to enter two integers, obt ins the numbers from ๐‘Ž

the user, ๐‘Ž nd then prints the l rger number followed by the words "is l rger." If ๐‘Ž ๐‘Ž

the numbers ๐‘Ž re equ l, print the mess ge "These numbers ๐‘Ž ๐‘Ž ๐‘Ž re equ l." ๐‘Ž

#include ๐‘Ž using n mesp ce๐‘Ž ๐‘Ž std; int m in()๐‘Ž { int num1, num2; // decl re๐‘Ž v ri bles๐‘Ž ๐‘Ž cout << "Enter two integers: "; // prompt user cin >> num >> num2; // re d๐‘Ž v lues๐‘Ž from keybo rd๐‘Ž // Comp res๐‘Ž if v lues๐‘Ž of "num1" ๐‘Žnd "num2" ๐‘Žre equ l๐‘Ž if (num1 == num2) // If condition is true, then cout << "These numbers ๐‘Žre equ l."๐‘Ž << endl; if (num1 > num2) // If condition is true, then cout << num1 << " is l rger."๐‘Ž << endl; if (num2 > num1) // If condition is true, then cout << num2 << " is l rger."๐‘Ž << endl; return 0; //indic te๐‘Ž successful termin tion๐‘Ž

3.WriteW ๐‘Ž Wprogr m ๐‘Ž Wth t ๐‘Ž WinputsWthreeWintegersWfromWtheWkeybo rd ๐‘Ž W ๐‘Ž nd WprintsWtheWs

um,W ๐‘Ž ver ge, ๐‘Ž Wproduct,Wsm llest ๐‘Ž W ๐‘Ž nd Wl rgest ๐‘Ž WofWtheseWnumbers.

#includeW๐‘Ž Wusi ngWn mesp ce๐‘Ž ๐‘Ž Wstd;WintWm i๐‘Ž n()W{ intWnum1,Wnum2,Wnum3,Wsm llest,๐‘Ž Wl rgest;๐‘Ž W//decl re๐‘Ž Wv ri bles๐‘Ž ๐‘Ž WcoutW <>Wnum1W>>Wnum2W>>Wnum3;W//re d๐‘Ž Wv lues๐‘Ž WfromWkeybo rd๐‘Ž Wl rgest๐‘Ž W=Wn um1;W//W ๐‘Žssume W"num1"WisWtheWl rgest๐‘Ž ifW(num2W>Wl rgest)๐‘Ž W//isW"num2"Wl rger๐‘Ž Wth n๐‘Ž W"l rgest"?๐‘Ž Wl rgest๐‘Ž W=Wnu m2;W//WthenW"num2"WisWtheWl rgest๐‘Ž ifW(num3W>Wl rgest)๐‘Ž W//isW"num3"Wl rger๐‘Ž Wth n๐‘Ž W"l rgest"?๐‘Ž Wl rgest๐‘Ž W=Wnu m3;W//WthenW"num3"WisWtheWl rgest๐‘Ž sm llest๐‘Ž W=Wnum1;W//W ๐‘Žssume W"num1"WisWtheWsm llest๐‘Ž ifW(num2W๐‘Ž using n mesp ce๐‘Ž ๐‘Ž std; int m in()๐‘Ž { int num; // decl re๐‘Ž v ri ble๐‘Ž ๐‘Ž cout << "Enter ๐‘Ž number: "; // prompt user cin >> num; // re d๐‘Ž v lue๐‘Ž from keybo rd๐‘Ž 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;