Functions Practice 1
Today, We will start practicing questions on function. We may have covered the concepts behind the questions in previous articles but our main aim in these questions will be to practice writing functions. As we know just doing the theory in a practical course is not a good idea. It's recommended to practice questions on these basic concepts when we are starting our programming journey to understand the concepts properly and build intuition for advanced concepts. Trust me this will help you in the long run.
Suggestion : First try to do the questions on your own. Give yourself some time and resist the temptation to look at the solution quickly. After giving your best shot, you can definitely go through the solution.✌️
Now, Let's start !!
Note: If you read the previous articles, I expect that while practicing questions you will follow good coding practices such as meaningful variable and function names.
Problem 1
Write a C++ program to input any two numbers(integers) from the user and find the sum of the given numbers using a function.
Input
Enter number 1 : 10
Enter number 2 : 5
Output
Sum of numbers is 15
Approach
Simple question, right !! Please try on your own.
Intuition
The Sum of two numbers num1 and num2 is sum = num1+num2. This is easy, but we need to write a separate function for this simple statement.
- First, assign a meaningful name to the function, say FindSum( ).
- The function should accept a numbers whose sum is to be calculated. Hence, the function definition is FindSum(int num1, int num2).
- Finally, the function should return the sum of the numbers passed. Hence, the return type of the function should be an integer. Why the return type will be integer? Because the sum of two integers gives an integer only.
After observing the above points function looks like int FindSum(int num1, int num2)
Now, Are we ready to write our code? 🙌
Solution
#include <iostream>
using namespace std;
/**
* Function to find the sum of two numbers
*/
int FindSum(int num1, int num2){
return num1 + num2;
}
int main(){
int num1;
int num2;
int sum;
/* Input numbers to find sum from user */
cout << "Enter number 1: ";
cin >> num1;
cout << "Enter number 2: ";
cin >> num2;
sum = FindSum(num1,num2);
cout << "Sum of numbers is " << sum;
return 0;
}
Problem 2
Write a C++ program to input any number from the user and find the cube of the given number using a function.
Input
Enter the number : 5
Output
Cube of number is 125
Approach
Simple question, right !! Please try it on your own.
Intuition
The Cube of a number num is cube = num * num * num. This is easy, but we need to write a separate function for this simple statement.
- First, assign a meaningful name to the function, say FindCube( ).
- The function should accept a number whose cube is to be calculated. Hence, the function definition is FindCube(double num).
- Finally, the function should return the cube of num passed. Hence, the return type of the function should be double.
After observing the above points function looks like double FindCube(double num)
Note: Instead of taking double as a parameter and return type. You can also use int, float or any other integer/fractional types.
Solution
#include <iostream>
using namespace std;
/**
* Function to find cube of any number
*/
double FindCube(double num){
return (num * num * num);
}
int main(){
double num;
double c;
/* Input number to find cube from user */
cout << "Enter the number: ";
cin >> num;
c = FindCube(num);
cout << "Cube of number is " << c;
return 0;
}
Problem 3
Write a C++ program to input a number from the user and check whether a given number is even or odd using functions.
Input
Enter number : 10
Output
10 is even
Approach
Simple question, right !! Please try it on your own.
Intuition
Let us define a function to check whether the number is even or odd.
- First, give a meaningful name to function, say
IsEven()
. - Next, the function must accept one integer which is to be checked for even conditions, say
IsEven(int num)
. - Finally, the function must return
true
if the given integer is even otherwisefalse
.
So the function declaration to check an even number is bool IsEven(int num);
Are you thinking that why we have written only function i.e. IsEven as we have to check for odd numbers also?We can write two functions also separately for checking even and odd numbers.
Now, Are we ready to write our code? 🙌
Solution
#include <iostream>
using namespace std;
/**
* Function to check whether the number is even or odd
*/
bool IsEven(int num){
if(num % 2 == 0){
return true;
}
return false;
}
int main(){
int num;
bool check;
/* Input number from user */
cout << "Enter number : ";
cin >> num;
check = IsEven(num);
if(check == true){
cout << num << " is even";
}
else {
cout << num << " is odd";
}
return 0;
}
Problem 4
Write a C++ program to input the radius of the circle from the user and find the diameter, circumference and area of the given circle using functions. Use Pi=3.14 in calculations.
Input
Enter radius : 10
Output
Diameter = 20
Circumference = 62.8
Area = 314
Approach
Simple question, right !! Please try it on your own. In this program, we will define more than one user-defined function in a single program.
Intuition
- First, assign a meaningful name to all the functions. Say the function to calculate diameter, circumference and area are – GetDiameter(), GetCircumference() and GetArea() respectively.
- All the above functions use one input i.e. the radius of the circle to calculate the output. Hence, all the three functions must accept a parameter of
double
orint
type. - Finally, all three functions return either
double
orint
as output. Hence, the return type of the function must be eitherdouble
orint
.
Note: For our code, we will use double type, you can use other data types also.
After considering the above points function declaration looks like this –
double GetDiameter ( double radius );
double GetCircumference ( double radius );
double GetArea ( double radius );
Now, Are we ready to write our code? 🙌
Solution
#include <iostream>
using namespace std;
/**
* Calculate diameter of circle whose radius is given
*/
double GetDiameter(double radius)
{
return (2 * radius);
}
/**
* Calculate circumference of circle whose radius is given
*/
double GetCircumference(double radius)
{
return (2 * 3.14 * radius);
}
/**
* Find area of circle whose radius is given
*/
double GetArea(double radius)
{
return (3.14 * radius * radius);
}
int main() {
double radius, dia, circ, area;
/* Input radius of circle from user */
cout << "Enter radius of circle: ";
cin >> radius;
dia = GetDiameter(radius); // Call GetDiameter function
circ = GetCircumference(radius); // Call GetCircumference function
area = GetArea(radius); // Call GetArea function
cout << "Diameter of the circle is " << dia << endl;
cout << "Circumference of the circle is " << circ << endl;
cout << "Area of the circle is " << area << endl;
return 0;
}
Here, we come to the end of this article. I hope now we are comfortable with writing functions.
"Remember Practice leads to Perfection".
Happy Learning !! 😀See you in the next article with more interesting questions.