0% found this document useful (0 votes)
22 views4 pages

C++ Program Input and Output Examples

The document contains multiple C++ programs demonstrating various functionalities such as reading user input, performing calculations, and displaying results. It includes examples of calculating the area of a rectangle, handling different data types, and using the getline function for string input. Additionally, it explores integer division and type casting in calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

C++ Program Input and Output Examples

The document contains multiple C++ programs demonstrating various functionalities such as reading user input, performing calculations, and displaying results. It includes examples of calculating the area of a rectangle, handling different data types, and using the getline function for string input. Additionally, it explores integer division and type casting in calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Study the following program and complete the table.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double value1, value2, value3;
cout << "Enter a number: ";
cin >> value1;
value2 = 2 * pow(value1, 2.0);
value3 = 3 + value2 / 2 - 1;
cout << value3 << endl;
return 0;
}
If the User Enters…
The Program Will Display What Number
(Stored in value3 )?
2
5
4.3
6

// This program demonstrates how cin can read multiple values


2 // of different data types.
3 #include <iostream>
4 using namespace std;
6 int main()
7{
8 int whole;
9 double fractional;
10 char letter;
12 cout << "Enter an integer, a double, and a character: ";
13 cin >> whole >> fractional >> letter;
14 cout << "Whole: " << whole << endl;
15 cout << "Fractional: " << fractional << endl;
16 cout << "Letter: " << letter << endl;
17 return 0;
18 }

// This program asks the user to enter the length and width of
2 // a rectangle. It calculates the rectangle's area and displays
3 // the value on the screen.
4 #include <iostream>
5 using namespace std;
7 int main()
8{
9 int length, width, area;
11 cout << "This program calculates the area of a ";
12 cout << "rectangle.\n";
13 cout << "What is the length of the rectangle? ";
14 cin >> length;
15 cout << "What is the width of the rectangle? ";
16 cin >> width;
17 area = length * width;
18 cout << "The area of the rectangle is " << area << ".\n";
19 return 0;
20 }

What will the following program display?


#include <iostream>
using namespace std;
int main()
{
int integer1, integer2;
double result;
integer1 = 19;
integer2 = 2;
result = integer1 / integer2;
cout << result << endl;
result = static_cast<double>(integer1) / integer2;
cout << result << endl;
result = static_cast<double>(integer1 / integer2);
cout << result << endl;
return 0;
// This program demonstrates using the getline function
2 // to read character data into a string object.
3 #include <iostream>
4 #include <string>
5 using namespace std;
7 int main()
8{
9 string name;
10 string city;
12 cout << "Please enter your name: ";
13 getline(cin, name);
14 cout << "Enter the city you live in: ";
15 getline(cin, city);
17 cout << "Hello, " << name << endl;
18 cout << "You live in " << city << endl;
19 return 0;
20 }

You might also like