C++PrimerPlus Chapter 2 start learning C + + (programming exercises with answers)

Posted by MattWeet on Wed, 05 Jan 2022 23:23:16 +0100

1. Write a C + + program that displays your name and address.

#include<iostream>
using namespace std;

int main() {
	cout << "name:" << "Hank" << endl;
	cout << "Address:" << "Shanghai" << endl;
	return 0;
}

2. Write a C + + program, which requires the user to enter a distance in long, and then convert it into code (a long is equal to 220 code).

#include<iostream>
using namespace std;

int main() {
	cout << "Please enter a to long Distance in:" ;
	double dis1;
	cin >> dis1;
	double dis2 = dis1 * 220;
	cout << dis1 << "long Unit distance is" << dis2 << "Code distance" << endl;
	return 0;
}

3. Write a C + + program that uses three user-defined functions (including main()) and generates the following output:
Three blind mice
Three blind mice
See how they run
See how they run
One of the functions needs to be called twice, and the function generates the first two lines; The other function is also called twice and generates the rest of the output.

#include<iostream>
using namespace std;

void Print_1();
void Print_2();

int main() {
	Print_1();
	Print_1();
	Print_2();
	Print_2();
	return 0;
}

void Print_1() {
	cout << "Three blind mice" << endl;
}

void Print_2() {
	cout << "See how they run" << endl;
}

4. Write a program to let users enter their age, and then display the number of months included in the age, as shown below:
Enter your age: 29

#include<iostream>
using namespace std;

int main() {
	cout << "Please enter your age:";
	int age;
	cin >> age;
	cout << "Your age includes:" << age * 12 << "Months" << endl;
	return 0;
}

5. Write a program in which main() calls a user-defined function (takes the Celsius temperature value as the parameter and returns the corresponding Fahrenheit temperature value). The program requires the user to input the Celsius temperature value according to the following format and displays the results:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
The following is the conversion formula:
Fahrenheit = 1.8 * Celsius + 32.0

#include<iostream>
using namespace std;

double CelsiusToFahreheit(double celsius);

int main() {
	cout << "Please enter a Celsius value: ";
	double celsius;
	cin >> celsius;
	double fahrenheit;
	fahrenheit = CelsiusToFahreheit(celsius);
	cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit." << endl;
	return 0;
}

double CelsiusToFahreheit(double celsius) {
	return celsius * 1.8 + 32.0;
}

6. Write a program whose main() calls a user-defined function (take the light-year value as the parameter and return the value of the corresponding astronomical unit). The program requires the user to input the light year value in the following format and displays the results:
Enter the number of light years: 4.2
4.2 light years = 265608 astronmical units.
The astronomical unit is the average distance from the earth to the sun (about 150 million kilometers or 93 million miles), and the light year is the distance light travels in a year (about 10 trillion kilometers or 6 trillion miles) (except the sun, the nearest star is about 4.2 light-years away from the earth). Please use the double type (participant listing 2.4), and the conversion formula is:
1 light year = 63240 Au

#include <iostream>
using namespace std;

double LightToUnit(double lightYears);

int main()
{
	cout << "Enter the number of light years:";
	double lightYears;
	cin >> lightYears;
	double lightUnit;
	lightUnit = LightToUnit(lightYears);
	cout << lightYears << " light years = " << lightUnit << " astronomical units." << endl;
	return 0;
}

double LightToUnit(double lightYears) {
	return 63240 * lightYears;
}

7. Write a program that requires users to enter hours and minutes. In the main() function, pass these two values to a void function, which displays them in the following format:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28

#include <iostream>
using namespace std;

void display(int hours, int minutes);

int main()
{
	cout << "Enter the number of hours: ";
	int hours;
	cin >> hours;
	cout << "Enter the number ouf minutes: ";
	int minutes;
	cin >> minutes;
	display(hours, minutes);
	return 0;
}

void display(int hours, int minutes)
{
	cout << "Time: " << hours << ":" << minutes << endl;

}

Topics: C++