Practice questions of Cpp - Function (2024)

Table of Contents
Level 1 Level 2 Level 3 References

Welcome.please sign up.

Welcome.please login.

  • Level 1
  • Level 2

Level 1

1.

Write a program to print the sum of two numbers entered by user by defining your own function.

#include<iostream>using namespace std;int sum(int x, int y){ return x+y;}int main(){ cout << sum(2,4) << "\n"; return 0;}

2.

Define a function that returns the product of two numbers entered by user.

3.

Write a program to print the circumference and area of a circle of radius entered by user by defining your own function.

#include<iostream>using namespace std;void circle(int radius){ cout << "Perimeter is "<<2*3.14*radius<<"\n"; cout << "Area is "<<3.14*radius*radius<<"\n";}int main(){ circle(4); return 0;}

4.

Define two functions to print the maximum and the minimum number respectively among three numbers entered by user.

5.

Define a program to find out whether a given number is even or odd.

#include<iostream>using namespace std;void eo(int x){ if (x%2 == 0) cout << "Even\n"; else cout << "Odd\n";}int main(){ eo(4); eo(5); return 0;}

6.

A person is elligible to vote if his/her age is greater than or equal to 18. Define a function to find out if he/she is elligible to vote.

7.

Define a function to find out if number is prime or not.

#include<iostream>using namespace std;bool prime(int x){ for(int i = 2; i<x; i++) { if(x%i == 0) return false; } return true;}int main(){ cout << prime(5) << "\n"; cout << prime(10) << "\n"; return 0;}

8.

Write a program which will ask the user to enter his/her marks (out of 100). Define a function that will display grades according to the marks entered as below:
MarksGrade
91-100AA
81-90AB
71-80BB
61-70BC
51-60CD
41-50DD
<=40Fail

9.

Write a program to print the factorial of a number by defining a function named 'Factorial'.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0

#include<iostream>using namespace std;int factorial(int x){ int fac = 1; if (x == 0 || x == 1) return fac; for(int i=1;i<=x;i++) fac = fac*i; return fac;}int main(){ cout << factorial(5) << "\n"; return 0;}

Level 2

1.

Print the multiplication table of 15 using recursion.

#include<iostream>using namespace std;void table(int x, int y){ if(y != 1) { table(x,y-1); } cout << x*y << "\n";}int main(){ table(15,10); return 0;}

2.

Define a function to print the prime factors of a number.

3.

Using recursion, define a function to know nth term of a Fibonacci series.
Nth term of Fibonacci series is
F(n) = F(n-1)+F(n-2)
F(0) = 0
F(1) = 1
F(2) = F(1)+F(0) = 1+0 = 1
F(3) = F(2)+F(1) = 1+1 = 2
F(4) = F(3)+F(2) = 2+1 = 3

#include<iostream>using namespace std;int fib(int x){ if(x == 0) { return 0; } else if(x==1) { return 1; } else { return fib(x-1)+fib(x-2); }}int main(){ cout << fib(0) << "\n"; cout << fib(1) << "\n"; cout << fib(2) << "\n"; cout << fib(3) << "\n"; cout << fib(4) << "\n"; cout << fib(5) << "\n"; return 0;}

4.

Define a function named 'perfect' that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be "perfect number" if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].

5.

Define a function to calculate power of a number raised to other i.e. ab using recursion where the numbers 'a' and 'b' are to be entered by the user

#include<iostream>using namespace std;int power(int a, int b){ if(b==1) { return a; } else { return(a*power(a,b-1)); }}int main(){ cout << power(2,4) << "\n"; return 0;}

6.

Write a program that takes as input your gross salary and your total saving and uses another function named taxCalculator() to calculate your tax. The taxCalculator()function takes as parameters the gross salary as well as the total savings amount. Thetax is calculated as follows:
(a) The savings is deducted from the gross income to calculate the taxable income. Maximum deduction of savings can be Rs. 100,000, even though the amount can be more than this.
(b) For up to 100,000 as taxable income the tax is 0 (Slab 0); beyond 100,000 to200,000 tax is 10% of the difference above 100,000 (Slab 1); beyond 200,000 up to500,000 the net tax is the tax calculated from Slab 0 and Slab 1 and then 20% ofthe taxable income exceeding 200,000 (Slab 2); if its more than 500,000, then thetax is tax from Slab 0, Slab 1, Slab 2 and 30% of the amount exceeding 500,000.

7.

Write a function that takes your date of birth in YYYY, MM and DD format (separatedby spaces) as input as well as the current date, in same format, and calculates your agein years, months and days. You must check for leap years also. Write a separate functionto check for leap year.

Level 3

C++ in English Launched Get C++ (English) course for 999 Days Hours Min Sec
Pro Course Features Simple Videos Questions to Practice Solved Examples Internship Chance* Certificate of Completion Discussion with Experts Learn for FREE
New Questions
Cybersecurity-Architecture-and-Engineering Dumps PDF: Unlock Success with Expertly Crafted Content - Other
D-MN-OE-23 Dell Metro Node Operate 2023 Exam Dumps - Other
Back-End Developer - ABAP Cloud C_ABAPD_2309 Dumps - Other
Elevate Your 1Z0-340-24 PDF Dumps Preparation - Java
Improve Your 1Z0-1003-24 PDF Dumps Readiness - Java
Increase Your 1D0-1047-24-D Dumps PDF Readiness - Java
1Z0-1087-24 PDF Exam Dumps Preparation - Python
Ask Yours

Recent Posts

pow() in PythonDutch National Flag problem - Sort 0, 1, 2 in an arraymemoryview() in Pythonnext() in Python
Post Yours
Practice questions of Cpp - Function (2024)

References

Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6481

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.