Thursday, December 26, 2013

Assignment 4: User Defined Function Set1

user defined function
[SET – 1]

1
Write a program using function which accept two integers as an argument and return its sum. Call this function from main( ) and print the results in main( ). solution
2
Write a function to calculate the factorial value of any integer as an argument. Call this function from main( ) and print the results in main( ). solution
3
Write a function that receives two numbers as an argument and display all prime numbers between these two numbers. Call this function from main( ). solution
4
Raising a number to a power p is the same as multiplying n by itself p times. Write a function called power that takes two arguments, a double value for n and an int value for p, and return the result as double value. Use default argument of 2 for p, so that if this argument is omitted the number will be squared. Write the main function that gets value from the user to test power function. solution
5
Write a function called zero_small() that has two integer arguments being passed by reference and sets the smaller of the two numbers to 0. Write the main program to access the function. solution
6
Write the output of the following program :
#include <iostream.h>
void X(int &A, int &B)
{
A = A + B;
B = A - B;
A=A-B;
}
void main()
{
int a = 4, b =18;
X(a,b);
cout<<a<<”,”<<b;
}
7
Write the output of the following program:
#include <iostream.h>
void X(int A, int &B)
{
A = A+B;
B = A-B;
A = A-B;
}
void main()
{
int a=4, b=l8;
X(a,b);
cout<< a <<”,”<<b;
}
8
Write the output of the following program:
# include <iostream.h>
void Execute(int &B, int C=100)
{
int TEMP = B + C;
B += TEMP;
if (C == 100)
cout<<TEMP<<" "<<B<<" "<<C<<endl;
}
void main()
{
int M= 90, N = 10;
Execute(M);
cout << M << “ “<< N << endl;
Execute(M,N);
cout << M<<“ "<<N<< endl;
}
9
Give the output of the following program
# include <iostream.h>
int global = 10;
void func(int &x, int y)
{
x = x - y;
y = x * 10;
cout << x << << y << '\n';
}
void main()
{
int global = 7:
func (::global, global);
cout << global <<”,” << ::global <<’\n’;
func(global,:: global);
cout<< global << “,”<<::global<<‘\n’;
}
10
Write the output of the following program :
# include <iostream.h>
static int i=100;
void abc()
{
static int i=8;
cout<< "first=" <<i++;
}
main()
{
static int i = 2;
abc();
cout << "second =” << i << endl;
abc();
}
11
Write the output of the following program:
# include <iostream.h>
int func(int &x, int y = 10)
{
if (x%y == 0)
return ++x;
else
return y--;
}
void main()
{
int p=20, q=23;
q=func (p,q);
cout << p << " " << " " << q << endl;
p=func (q);
cout<< p << " " << " " << q << endl;
q=func (p);
cout << p << " " << " " << q << endl;
}

No comments:

Post a Comment