0% found this document useful (0 votes)
16 views14 pages

Function Questions

The document contains 11 questions about writing C++ programs that use functions. The questions cover topics like passing arguments to functions, defining functions, and having functions return values. Sample code is provided for each question to demonstrate how to structure programs that call user-defined functions.

Uploaded by

Abrash Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Function Questions

The document contains 11 questions about writing C++ programs that use functions. The questions cover topics like passing arguments to functions, defining functions, and having functions return values. Sample code is provided for each question to demonstrate how to structure programs that call user-defined functions.

Uploaded by

Abrash Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

(FUNCTIONS QUESTIONS SOLUTIONS)

(MADE BY ALI AKBER)

(BSCS IST SS1)

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>

using namespace std;

void num(int,int);

int main ()

int n1,n2;

cout<<"enter two number"<<endl;

cin>>n1>>n2;

num (n1,n2);

return 0;

void num(int n1,int n2)

int sum=0;

sum=n1+n2;

cout<<"sum of numbers is "<<sum<<endl;

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>

using namespace std;

void num(int);

int main ()

int n;

cout<<"enter number"<<endl;

cin>>n;

num (n);

return 0;

void num(int n)

int fact=1;

for (int i=1; i<=n ; i++)

fact= fact*i;

cout<< "factorial of the number "<<n<<" is "<< fact<<endl;

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>

using namespace std;

void max(int a,int b);

int main ()
{

int n1,n2;

cout<<"enter two numbers"<<endl;

cin>>n1>>n2;

max (n1,n2);

return 0;

void max(int a,int b)

if (a<b)

cout<<"Maximum number is "<<b<<endl;

else

cout<<"Maximum number is "<<a<<endl;

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>

using namespace std;

void num(int,int);

int main ()

int n1,n2;

cout<<"enter two number"<<endl;

cin>>n1>>n2;

num (n1,n2);
return 0;

void num(int n1,int n2)

int temp=0;

temp=n1;

n1=n2;

n2=temp;

cout<<"Value of n1 after swapping is "<<n1<<endl;

cout<<"Value of n2 after swapping is "<<n2<<endl;

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>

using namespace std;

void temp (float);

int main ()

float f;

cout<<"enter temperature in Fahrenheit"<<endl;

cin>>f;

temp (f);

return 0;

void temp (float f)

{
float c;

c =(f-32)*5/9;

cout<<"Temperature in Celsius is "<< c<<endl;

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>

using namespace std;

void num (int);

int main ()

int n;

cout<<"enter a number "<<endl;

cin>>n;

num(n);

return 0;

void num (int n)

bool prime=false;

if (n%2==0)

prime =false;

cout<<"Given number is not a prime number"<<endl;


}

else if (n%2!=0)

prime=true;

cout<<"Given number is a prime number"<<endl;

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>

using namespace std;

void power(int,int);

int main ( )

int no1,no2;

cout<<"Enter number 1 : "<<endl;

cin>>no1;

cout<<"Enter number 2 : "<<endl;

cin>>no2;

power(no1,no2);

return 0;

void power(int no1,int no2)

int num;

num=no1;
for(int i=2;i<=no2;i++)

num=num*no1;

cout<<"power of "<<no2<<" from "<<no1<<" is "<<num<<endl;

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 A 80 or above marks

Grade B 60 to 79 marks

Grade C 40 to 59 mark

Grade D Below 40 marks

#include <iostream>

using namespace std;

void marks(int);

int main ()

int n;

cout<<"enter number obtained by student"<<endl;

cin>>n;

marks (n);

return 0;

void marks(int n)

if (n>=80)
cout<<"You have got Grade A"<<endl;

if (n>=60 && n<80)

cout<<"You have got Grade B"<<endl;

if (n>=40 && n<60)

cout<<"You have got Grade C"<<endl;

if (n<40)

cout<<"You have got Grade D"<<endl;

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>

using namespace std;

void operation(int,int,char);

int main ()

int n1,n2;

char ch;

cout<<"enter ist number"<<endl;

cin>>n1;

cout<<"enter 2nd number"<<endl;

cin>>n2;

cout<<"enter an operator"<<endl;

cin>>ch;

operation(n1,n2,ch);
return 0;

void operation(int n1,int n2,char ch){

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:

cout<<"You have entered invalid operation";

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

difference on the screen.

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

quotient on the screen.

V. Function Modulus(): This function accepts two number as arguments and display their reminder
on the screen.

vi.Function Message(): This function display a message "Error...invalid option" on the

screen.

#include <iostream>

using namespace std;

int add (int,int);

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;

cout<<"enter two integers"<<endl;

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 add (int n1,int n2){

int sum;

sum=n1+n2;
return sum;

int subtract(int n1,int n2){

int subtract;

subtract=n1-n2;

return subtract;

int multiply (int n1,int n2){

int multiply;

multiply=n1*n2;

return multiply;

int divison(int n1,int n2){

int divison;

divison=n1/n2;

return divison;

int modelus(int n1,int n2){

int modelus;

modelus=n1%n2;

return modelus;

void message(){

cout<<"you have entered invalid operator";

}
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>

using namespace std;

char vowel(char g);

int main ()

char ch,v,r;

cout<<"enter a character"<<endl;

cin>>ch;

v=vowel(ch);

if (v==true)

cout<<"your entered character "<<ch<<" is a vowel";

else

cout<<"your entered character "<<ch<<" is not a vowel";

return 0;

char vowel(char g)

if (g=='a' || g=='e' || g=='i' || g=='o' ||g=='u')

return true;

else

return false;

THE END.

You might also like