C + + learning notes 4: programming exercise 1

Keywords: C++ html

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

//myPractice201.cpp -- programming practice, displaying your name and address
#include <iostream>

using namespace std;

int main()
{
	cout << "full name: Tom\n Address: beautiful country" << 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 codes).

//myPractice202.cpp -- enter a long value and convert it to a code
#include <iostream>

using namespace std;
int LongToMa(int);

int main()
{
	cout << "Please enter long Value:";
	int long_num;
	cin >> long_num;
	//Direct calculation method
    int ma_num = long_num * 220;
	cout << long_num << " long = "
		<< ma_num << " Code." << endl;
    //Call function method
    cout << long_num << " long = "
		<< LongToMa(long_num) << " Code." << endl;
	return 0;
}

int LongToMa(int ln)
{
    return 220 * ln;
}

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 remainder output.

//myPractice203.cpp -- custom function for content output
#include <iostream>

using namespace std;

void MessageOutput1();
void MessageOutput2();

int main()
{
	MessageOutput1();
	MessageOutput1();
	MessageOutput2();
	MessageOutput2();
	return 0;
}

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

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

4. Write a program to let users enter their age, and then display how many months the age contains.

//myPractice204.cpp -- enter the age and display the number of months
#include <iostream>

using namespace std;
int YearToMonth(int);

int main()
{
	cout << "Please enter your age:";
	int year_num;
	cin >> year_num;
	//Direct calculation method
	cout << year_num << "The number of months corresponding to age is" << year_num*12 << "Months." << endl;
	cout << year_num << "The number of months corresponding to age is" << YearToMonth(year_num) << "Months." << endl;
	return 0;
}

int YearToMonth(int yn)
{
	return 12 * yn;
}

5. Write a program in which main() calls a user-defined function (take the Celsius temperature value as the parameter and return 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.
Conversion formula: Fahrenheit temperature = 1.8 × Celsius temperature + 32.0

//myPractice205.cpp -- input Celsius temperature value and convert it to Fahrenheit temperature value for output
#include <iostream>

using namespace std;

double CelsiusToFahrenheit(double);

int main()
{
	cout << "Please enter the Celsius temperature value:";
	double celsius_num;
	cin >> celsius_num;
	double fahrenheit_num = CelsiusToFahrenheit(celsius_num);
	cout << celsius_num << "Celsius equals" << fahrenheit_num << "Fahrenheit degree." << endl;
	return 0;
}

double CelsiusToFahrenheit(double cn)
{
	return 1.8*cn + 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 astronomical 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 for the sun, the nearest star is about 4.2 light years away from the earth). Please use double type, conversion formula: 1 light year = 63240 astronomical units

//myPractice206.cpp -- input the Lightyear value, convert it into Lightyear units and output it		
#include <iostream>

using namespace std;

double convert(double);

int main()
{
	cout << "Please enter the Lightyear value:";
	double light_year,astro_unit;
	cin >> light_year;
	astro_unit = convert(light_year);
	cout << light_year << "Light years equals" << astro_unit << "Astronomical unit" << endl;
	return 0;
}

double convert(double ly)
{
	return 63240 * ly;
}

7. Write a program that requires the user 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

//myPractice207.cpp -- the user enters the hours and minutes, and the system displays the time
#include <iostream>

using namespace std;

void DisplayTime(int h, int m);

int main()
{
	int hour,minute;
	cout << "Please enter the number of hours:";
	cin >> hour;
	cout << "Please enter the number of minutes:";
	cin >> minute;
	DisplayTime(hour, minute);
	return 0;
}

void DisplayTime(int h, int m)
{
	cout << "Time:" << h << ":" << m << endl;
}

Posted by shyish on Sun, 19 Sep 2021 02:24:09 -0700