0% found this document useful (0 votes)
39 views7 pages

C++ Practice Codes

The document describes a C++ program to format the output of variables of different data types like double, boolean etc. by setting formatting flags like fixed, scientific, width, fill character etc. It displays the values of pi, boolean variable with different formatting and width settings.

Uploaded by

Andrea Shiks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views7 pages

C++ Practice Codes

The document describes a C++ program to format the output of variables of different data types like double, boolean etc. by setting formatting flags like fixed, scientific, width, fill character etc. It displays the values of pi, boolean variable with different formatting and width settings.

Uploaded by

Andrea Shiks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a C++ program to add two numbers and accept them from the keyboard.

Visual Presentation:

C++ Exercises: Add two numbers accept through keyboard


Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
int num1, num2, sum; // Declaring integer variables num1, num2, and sum

cout << "\n Sum of two numbers :\n"; // Outputting a message indicating sum of
two numbers
cout << "-------------------------\n"; // Outputting a separator line

cout << " Input 1st number : "; // Prompting the user to input the first number
cin >> num1 ; // Taking input for the first number from the user

cout << " Input 2nd number : "; // Prompting the user to input the second
number
cin >> num2; // Taking input for the second number from the user

sum = num1 + num2; // Calculating the sum of the two numbers

cout <<" The sum of the numbers is : " << sum << endl; // Displaying the sum
of the numbers
cout << endl; // Outputting a blank line for better readability

return 0; // Returning 0 to indicate successful program execution


} // End of the main function

Sample Output:

Sum of two numbers :


-------------------------
Input 1st number : 25
Input 2nd number : 39
The sum of the numbers is : 64

#include <iostream>
using namespace std;
int main()
{
int n = 4, k = 2;
cout << ++n << endl;
cout << n << endl;
cout << n++ << endl;
cout << n << endl;
cout << -n << endl;
cout << n << endl;

cout << --n << endl;


cout << n << endl;

cout << n-- << endl;


cout << n << endl;

cout << n + k << endl;


cout << n << endl;
cout << k << endl;
cout << n << k << endl;
cout << n << endl;
cout << " " << n << endl;
cout << " n" << endl;
cout << "\n" << endl;
cout << " n * n = "; //CAREFUL!
cout << n * n << endl;
cout << 'n' << endl;

return 0;
}
2. What is the output of the program below?
#include <iostream>
using namespace std;
int main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}
3. What is the output of the program below?
#include <iostream>
using namespace std;
int main()
{
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;
return 0;
}
4. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
enum color_type {red, orange, yellow, green, blue, violet};
color_type shirt, pants;
shirt = red;
pants = blue;
cout << shirt << " " << pants << endl;
return 0;
}

Write a program in C++ to print the sum of two numbers using variables.

Pictorial Presentation:

C++ Exercises: Print the sum of two numbers


Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
cout << "\n\n Print the sum of two numbers :\n"; // Outputting a message to
print the sum of two numbers
cout << "-----------------------------------\n"; // Outputting a separator
line

int a; // Declaration of integer variable 'a'


int b; // Declaration of integer variable 'b'
int sum; // Declaration of integer variable 'sum'

a = 29; // Assigning value 29 to variable 'a'


b = 30; // Assigning value 30 to variable 'b'
sum = a + b; // Calculating the sum of 'a' and 'b' and assigning it to 'sum'

cout << " The sum of "<< a << " and "<< b <<" is : "<< sum <<"\n\n" ; //
Outputting the calculated sum

return 0; // Returning 0 to indicate successful program execution


} // End of the main function

Sample Output:

Print the sum of two numbers :


-----------------------------------
The sum of 29 and 30 is : 59

Write a C++ program that checks whether primitive values cross the limit.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
cout << "\n\n Check whether the primitive values crossing the limits or not :\
n"; // Outputting a message to check primitive values
cout <<
"--------------------------------------------------------------------\n"; //
Outputting a separator line

// Declaring and initializing various primitive data type variables


char gender = 'F'; // char is single-quoted
bool isEmployed = true; // true(non-zero) or false(0)
unsigned short numOfsons = 2; // [0, 255]
short yearOfAppt = 2009; // [-32767, 32768]
unsigned int YearlyPackage = 1500000; // [0, 4294967295]
double height = 79.48; // With fractional part
float gpa = 4.69f; // Need suffix 'f' for float
long totalDrawan = 12047235L; // Suffix 'L' for long
long long balance = 995324987LL;// Need suffix 'LL' for long long int

// Outputting the values of the variables


cout << " The Gender is : " << gender << endl;
cout << " Is she married? : " << isEmployed << endl;
cout << " Number of sons she has : " << numOfsons << endl;
cout << " Year of her appointment : " << yearOfAppt << endl;
cout << " Salary for a year : " << YearlyPackage << endl;
cout << " Height is : " << height << endl;
cout << " GPA is " << gpa << endl;
cout << " Salary drawn up to : " << totalDrawan << endl;
cout << " Balance till : " << balance << endl;

return 0; // Returning 0 to indicate successful program execution


} // End of the main function
Sample Output:

Check whether the primitive values crossing the limits or not :


--------------------------------------------------------------------
The Gender is : F
Is she married? : 1
Number of sons she has : 2
Year of her appointment : 2009
Salary for a year : 1500000
Height is : 79.48
GPA is 4.69
Salary drawn upto : 12047235
Balance till : 995324987

Write a C++ program to print the results of the specified operations.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
cout << "\n\n Print the result of some specific operation :\n"; // Outputting a
message indicating specific operations
cout << "--------------------------------------------------\n"; // Outputting
a separator line

// Calculating and displaying results of specific mathematical expressions


cout << " Result of 1st expression is : "<< (-1+4*6) <<"\n" ; // Calculation: -
1 + (4*6) = -1 + 24 = 23
cout << " Result of 2nd expression is : "<< ((35+5)%7) <<"\n" ; // Calculation:
(35+5) % 7 = 40 % 7 = 5 (remainder of 40/7)
cout << " Result of 3rd expression is : "<< (14+-4*6/11) <<"\n" ; //
Calculation: 14 - (4*6)/11 = 14 - 24/11 = 14 - 2 = 12
cout << " Result of 4th expression is : "<< (2+15/6*1-7%2) <<"\n\n" ; //
Calculation: 2 + (15/6)*1 - (7%2) = 2 + 2 - 1 = 4 - 1 = 3

return 0; // Returning 0 to indicate successful program execution


} // End of the main function
Sample Output:

Print the result of some specific operation :


--------------------------------------------------
Result of 1st expression is : 23
Result of 2nd expression is : 5
Result of 3rd expression is : 12
Result of 4th expression is : 3

Write a C++ program to format the output.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file


#include <iomanip> // Including the header file for formatted I/O

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
cout << "\n\n Formatting the output :\n"; // Outputting a message to indicate
formatted output
cout << "----------------------------\n"; // Outputting a separator line

double pi = 3.14159265; // Initializing a double variable 'pi' with the value


of pi
cout << fixed << setprecision(4); // Setting the output format to fixed with 4
decimal places
cout <<" The value of pi : " << pi << endl; // Displaying 'pi' with 4 decimal
places

cout << " The value of pi 4 decimal place of total width 8 : |" << setw(8) <<
pi << "|" << endl; // Displaying 'pi' with 4 decimal places and a width of 8
cout << " The value of pi 4 decimal place of total width 10 : |" << setw(10)
<< pi << "|"<< endl; // Displaying 'pi' with 4 decimal places and a width of 10

cout << setfill('-'); // Setting the fill character to '-'


cout << " The value of pi 4 decimal place of total width 8 : |" << setw(8) <<
pi << "|" << endl; // Displaying 'pi' with 4 decimal places and a width of 8,
filled with '-'
cout << " The value of pi 4 decimal place of total width 10 : |" << setw(10)
<< pi << "|"<< endl; // Displaying 'pi' with 4 decimal places and a width of 10,
filled with '-'

cout << scientific; // Setting the output format to scientific notation


cout <<" The value of pi in scientific format is : " << pi << endl; //
Displaying 'pi' in scientific notation

bool done = false; // Initializing a boolean variable 'done' with the value
false
cout <<" Status in number : " << done << endl; // Displaying the boolean
variable 'done' as a number (0 for false, 1 for true)

cout << boolalpha; // Setting the output format to display true or false as
words
cout <<" Status in alphabet : " << done << endl; // Displaying the boolean
variable 'done' as true or false

cout << endl; // Outputting a blank line for better readability


return 0; // Returning 0 to indicate successful program execution
} // End of the main function
Sample Output:

Formatting the output :


----------------------------
The value of pi : 3.1416
The value of pi 4 decimal place of total width 8 : | 3.1416|
The value of pi 4 decimal place of total width 10 : | 3.1416|
The value of pi 4 decimal place of total width 8 : |--3.1416|
The value of pi 4 decimal place of total width 10 : |----3.1416|
The value of pi in scientific format is : 3.1416e+00
Status in mumber : 0
Status in alphabet : false

Write a C++ program that swaps two numbers.


Visual Presentation:

C++ Exercises: Swap two numbers


Sample Solution :-

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function


{
cout << "\n\n Swap two numbers :\n"; // Outputting a message indicating
swapping of two numbers
cout << "-----------------------\n"; // Outputting a separator line

int num1, num2, temp; // Declaring integer variables num1, num2, and temp for
swapping
cout << " Input 1st number : "; // Prompting the user to input the first
number
cin >> num1 ; // Taking input for the first number from the user

cout << " Input 2nd number : "; // Prompting the user to input the second
number
cin >> num2; // Taking input for the second number from the user

temp = num2; // Storing the value of num2 in a temporary variable temp


num2 = num1; // Assigning the value of num1 to num2
num1 = temp; // Assigning the value of temp (initially num2) to num1 to
complete the swap

cout << " After swapping the 1st number is : "<< num1 <<"\n" ; // Displaying
the value of num1 after swapping
cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ; // Displaying
the value of num2 after swapping

return 0; // Returning 0 to indicate successful program execution


} // End of the main function
Sample Output:

Swap two numbers :


-----------------------
Input 1st number : 25
Input 2nd number : 39
After swapping the 1st number is : 39
After swapping the 2nd number is : 25

You might also like