Function Questions
Function Questions
Question 1: Write a program using function which accept two integers as an argument from main().
The function calculate its sum print the results.
#include <iostream>
void num(int,int);
int main ()
int n1,n2;
cin>>n1>>n2;
num (n1,n2);
return 0;
int sum=0;
sum=n1+n2;
Question 2: Write a program that accept an integer number in main(). Define a function which
accept integer value as an argument. It calculate and print the factorial of this passing number. Call
this function from main ().
#include <iostream>
void num(int);
int main ()
int n;
cout<<"enter number"<<endl;
cin>>n;
num (n);
return 0;
void num(int n)
int fact=1;
fact= fact*i;
Question 3: Write a program that inputs two numbers in main() function, passes these numbers to a
function. The function displays the maximum number.
#include <iostream>
int main ()
{
int n1,n2;
cin>>n1>>n2;
max (n1,n2);
return 0;
if (a<b)
else
Question 4: Write a program that accepts two integer numbers in main(). Define a function SWAP ()
which accept integer values as an arguments. Function swap the value of these numbers and print
them after swapping in main(). Call this function from main().
#include <iostream>
void num(int,int);
int main ()
int n1,n2;
cin>>n1>>n2;
num (n1,n2);
return 0;
int temp=0;
temp=n1;
n1=n2;
n2=temp;
Question 5: Write a program that accept temperature in Fahrenheit in main(). Define a user define
function which accept temperature as an argument. It calculate and print the temperature in
centigrade. Call this function from main().
#include <iostream>
int main ()
float f;
cin>>f;
temp (f);
return 0;
{
float c;
c =(f-32)*5/9;
Question 6: Write a function that receive one integer number as an argument from main(). The
function display a message on screen, the passing number is prime or not.
#include <iostream>
int main ()
int n;
cin>>n;
num(n);
return 0;
bool prime=false;
if (n%2==0)
prime =false;
else if (n%2!=0)
prime=true;
Question 7: Write a function that receives two integer numbers as an arguments from main(). The
function display the power of 2nd number from 1st number and then print the result on screen.
#include<iostream>
void power(int,int);
int main ( )
int no1,no2;
cin>>no1;
cin>>no2;
power(no1,no2);
return 0;
int num;
num=no1;
for(int i=2;i<=no2;i++)
num=num*no1;
Question 8: Write a program that inputs mark in main function and pass the mark to a function. The
function finds grade of student on the basis of the following criteria
Grade B 60 to 79 marks
Grade C 40 to 59 mark
#include <iostream>
void marks(int);
int main ()
int n;
cin>>n;
marks (n);
return 0;
void marks(int n)
if (n>=80)
cout<<"You have got Grade A"<<endl;
if (n<40)
Question No 9:
Write a program that inputs two numbers and one arithmetic operator in main function and passes
them to a function. The function applies arithmetic operation on two numbers on the basis of the
operator entered by user using switch statement.
#include <iostream>
void operation(int,int,char);
int main ()
int n1,n2;
char ch;
cin>>n1;
cin>>n2;
cout<<"enter an operator"<<endl;
cin>>ch;
operation(n1,n2,ch);
return 0;
switch(ch)
case '+':
cout<<n1<<"+"<<n2<<"="<<n1+n2;
break;
case '-':
cout<<n1<<"-"<<n2<<"="<<n1-n2;
break;
case '/':
cout<<n1<<"/"<<n2<<"="<<n1/n2;
break;
case '*':
cout<<n1<<"*"<<n2<<"="<<n1*n2;
break;
case '%':
cout<<n1<<"%"<<n2<<"="<<n1%n2;
break;
default:
break;
Question 10: Write a program that ask the user to perform arithmetic operations on two numbers.
Your program allow the user to select the operation (+, -, *, / or %) and input the two integers
numbers in main(). Furthermore, your program must consist of following functions.
i. Function Add(): This function accepts two number as arguments and display their sum on screen.
ii. Function Subtract(): This function accepts two number as arguments and display their
Function Multiply(): This function accepts two number as arguments display their product on the
screen.
Function Divide(): This function accepts two number as arguments and display their
V. Function Modulus(): This function accepts two number as arguments and display their reminder
on the screen.
screen.
#include <iostream>
int subtract(int,int);
int multiply(int,int);
int divison(int,int);
int modelus(int,int);
void message();
int main(){
int n1,n2;
char choice;
cin>>n1>>n2;
cout<<"enter operator"<<endl;
cin>>choice;
switch(choice){
case '+':
cout<<add(n1,n2);
break;
case '-':
cout<<subtract(n1,n2);
break;
case '*':
cout<<multiply(n1,n2);
break;
case '/':
cout<<divison(n1,n2);
break;
case '%':
cout<<modelus(n1,n2);
break;
default:
message();
return 0;
int sum;
sum=n1+n2;
return sum;
int subtract;
subtract=n1-n2;
return subtract;
int multiply;
multiply=n1*n2;
return multiply;
int divison;
divison=n1/n2;
return divison;
int modelus;
modelus=n1%n2;
return modelus;
void message(){
}
Question 11: Write a value returning function that receives a character and returns true if the
character is a vowel and false otherwise. For this example, vowels include the characters 'a', 'e', 'ï',
'o', and 'u'.
#include <iostream>
int main ()
char ch,v,r;
cout<<"enter a character"<<endl;
cin>>ch;
v=vowel(ch);
if (v==true)
else
return 0;
char vowel(char g)
return true;
else
return false;
THE END.