C++ PROGRAMMING QUESTIONS The following functions can be written and tested in o
ID: 3803151 • Letter: C
Question
C++ PROGRAMMING QUESTIONS
The following functions can be written and tested in one program.
Each function should be tested with at least TWO different function calls.
1. Write and test the following function that prints the date as follows:
6/28/2011
void PrintDate(int, int, int);
2. Write and test the following function that returns the area of a circle with given radius.
float CircleArea(float);
3. Write and test the following function that converts a temperature from degrees Celsius to degrees Fahrenheit.
(F = 9/5C + 32)
float Fahrenheit(float);
4. Write and test the following function that returns the value of any number n factorial (n!).
Eg. 5! = 5 * 4 * 3 * 2 * 1 = 120
int Factorial(int);
5. Write and test the following function that determines whether a given number is a perfect square.
bool IsSquare(int);
Explanation / Answer
// C++ code
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include <math.h>
using namespace std;
void PrintDate(int, int, int);
float CircleArea(float);
float Fahrenheit(float);
int Factorial(int);
bool IsSquare(int);
int main()
{
int day, month, year;
cout << "Enter day month and year: ";
cin >> day >> month >> year;
PrintDate(day,month,year);
cout << "Enter day month and year: ";
cin >> day >> month >> year;
PrintDate(day,month,year);
float radius;
cout << " Enter radius ";
cin >> radius;
cout << "Area: " << CircleArea(radius) << endl;
cout << "Enter radius ";
cin >> radius;
cout << "Area: " << CircleArea(radius) << endl;
float celsius;
cout << " Enter temperature in celsius: ";
cin >> celsius;
cout << "Temperature in Fahrenheit: " << Fahrenheit(celsius) << endl;
cout << "Enter temperature in celsius: ";
cin >> celsius;
cout << "Temperature in Fahrenheit: " << Fahrenheit(celsius) << endl;
int n;
cout << " Enter n: ";
cin >> n;
cout << n << "! is " << Factorial(n) << endl;
cout << "Enter n: ";
cin >> n;
cout << n << "! is " << Factorial(n) << endl;
cout << " Enter n: ";
cin >> n;
if(IsSquare(n) == true)
cout << n << " is a perfect square ";
else
cout << n << " is not a perfect square ";
cout << "Enter n: ";
cin >> n;
if(IsSquare(n) == true)
cout << n << " is a perfect square ";
else
cout << n < " is not a perfect square ";
return 0;
}
void PrintDate(int day , int month, int year)
{
cout << "Date: " << day << "/" << month << "/" << year << endl << endl;
}
float CircleArea(float radius)
{
return 3.14*radius*radius;
}
float Fahrenheit(float celsius)
{
return (9*(celsius/5) + 32);
}
int Factorial(int n)
{
int f = 1;
for (int i = 2; i <= n; ++i)
{
f = f*i;
}
return f;
}
bool IsSquare(int n)
{
if(sqrt(n)*sqrt(n) == n)
return true;
else
return false;
}
/*
output:
Enter day month and year: 2 3 1993
Date: 2/3/1993
Enter day month and year: 12 12 1994
Date: 12/12/1994
Enter radius 3
Area: 28.26
Enter radius 4
Area: 50.24
Enter temperature in celsius: 32
Temperature in Fahrenheit: 89.6
Enter temperature in celsius: 12
Temperature in Fahrenheit: 53.6
Enter n: 4
4! is 24
Enter n: 8
8! is 40320
Enter n: 10
10 is not a perfect square
Enter n: 16
16 is a perfect square
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.