Monday 8 February 2016

Write a program that has main() call a user-defined function that takes a distance in light years as an argument and then returns the distance in astronomical units. The program should request the light year value as input from the user and display the result, as shown in the following code

Leave a Comment
Questions:

Write a program that has main() call a user-defined function that takes a distance in light years as an argument and then returns the distance in astronomical units. The program should request the light year value as input from the user and display the result, as shown in the following code:

Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.

An astronomical unit is the average distance from the earth to the sun (about 150,000,000 km or 93,000,000 miles), and a light year is the distance light travels in a year (about 10 trillion kilometers or 6 trillion miles). (The nearest star after the sun is about 4.2 light years away.) Use type double (as in Listing 2.4) and this conversion factor: 1 light year = 63,240 astronomical units

Explanation:

This problem is simple and just demonstration of basic console out operation and user defined function.  Below mention code is compiled in Visual Studio 2015 and output snap is attached.. If any problem you feel and want explanation feel free to contact.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/


#include<iostream>
using namespace std;
//function prototype
double convert(double);
void main() {
       double lightyears;
       cout << "Enter the number of light years == ";
       cin >> lightyears;
       cout << lightyears << " light years = " << convert(lightyears) << " astronomical units" << endl;
      
}

//function defination
double convert(double lightyears) {
       double astronomicalunits = lightyears * 63240;
       return astronomicalunits;
}



Output:
Write a program that has main() call a user-defined function that takes a distance in light years as an argument and then returns the distance in astronomical units. The program should request the light year value as input from the user and display the result, as shown in the following code


Related Articles:

C++ Books Solution

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: