Professional Documents
Culture Documents
Visual Presentation:
C++ Code :
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
cout <<" The sum of the numbers is : " << sum << endl; // Displaying the sum
of the numbers
cout << endl; // Outputting a blank line for better readability
Sample Output:
#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;
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++ Code :
cout << " The sum of "<< a << " and "<< b <<" is : "<< sum <<"\n\n" ; //
Outputting the calculated sum
Sample Output:
Write a C++ program that checks whether primitive values cross the limit.
Sample Solution:
C++ Code :
Sample Solution:
C++ Code :
Sample Solution:
C++ Code :
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
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
C++ Code :
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
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