Share
Explore BrainMass

How to use cout in C++ statements ?

15. What is the output of the following statements? Suppose a and b are int variables, c is a double variable, and a = 13, b = 5, and c = 17.5.

a. cout <<a + b - c << endl; = 0.5
b. cout << 15 / 2 + c << endl; = 25
c. cout << a / static_cast<double>(b) + 2 * c << endl; = 37
d. cout << 14 % 3 + 6.3 + b / a << endl; = 8.6846153
e. cout << static_cast<int>(c) % 5 + a - b << endl; = 11
f. cout << 13.5 / 2 + 4.0 * 3.5 + 18 << endl; = 33.75

Would you please check my work to see if I did this correct. Thanks a lot!

Solution Preview

#include <iostream.h>
int main(){
int a,b;
double c;
a=13;
b=5;
c=17.5;
cout << a<< endl;// != 0.5
// a = 13, if you didn't miss something

cout << 15 / 2 + c << endl;// != 25
//15/2 = 7 (integer division) + 17.5 = 24.5

/*
lets look at an example using static_cast<>()

#include <iostream>
using namespace std;

int main() {
int j = 41;
int v = 4;
float m = j/v;
float d = ...

Solution Summary

Discusses several short examples of correct use of cout in different c++ statements.

$2.19