Lab Manual 03: CSC 2207 Programming Language 2 (EEE)
Lab Manual 03: CSC 2207 Programming Language 2 (EEE)
LAB MANUAL 03
CSC 2207 Programming Language 2 [EEE]
1
TITLE
PREREQUISITE
OBJECTIVE
THEORY
User-defined functions are the functions which are created by the C++ programmer, so that
he/she can use it many times. It reduces complexity of a big program and optimizes the code.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;
2
return 0;
}
//Function definition
int add(int a, int b)
{
int add;
add = a + b;
// Return statement
return add;
}
Library Functions are the functions which are declared in the C++ header files such as ceil(x),
cos(x), exp(x) sqrt (square root) etc. from C++ math Header().
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double number, squareRoot;
number = 25.0;
// sqrt() is a library function to calculate the square root
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
3
int main()
{
int whole_number;
cout << "Enter a positive integer:\n";
cin>> whole_number;
cout << "The factorial of " << whole_number << " is ";
cout<< factorial(whole_number);
cout << ", and the square root of " << whole_number << " is ";
cout << sqrt(whole_number) << ".\n";
return 0;
}
Call By Reference
The call by reference method of passing arguments to a function copies the address of an
argument into the formal parameter. Inside the function, the address is used to access the actual
argument used in the call. It means the changes made to the parameter affect the passed
argument.
#include<iostream>
using namespace std;
int main(){
int a=10;
int b = 20;
swap(a,b);
cout<<a<<" "<<b;
return 0;
}
4
#include<iostream>
using namespace std;
return 0;
}
Function Template
#include<iostream>
using namespace std;
template<class T>
T maximum(T a, T b){
return a>b?a:b;
}
int main(){
cout<<maximum(10,15)<<endl;
cout<<maximum(10.20f,20.2f)<<endl;
cout<<maximum(2.5,5.6)<<endl;
return 0;
}
5
#include<iostream>
using namespace std;
int main(){
cout<<add(5,5,5)<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int x=10000000;
print(x);
x++;
cout<<x<<endl;
return 0;
}
int main() {
6
int randNum = rand()%100+1;
cout << "A random number: " << randNum << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a = 5, b = 3, c = 10;
cout << "The integer average of " << a << " and ";
cout<< b << " is ";
cout << average(a, b) << ".\n\n";
cout << "The integer average of " << a << ", ";
cout << b << " and " << c << " is ";
cout << average(a, b, c) <<".\n";
cout<<"Double function: "<<average(2.0,3.4);
return 0;
}
#include <iostream>
#include "averages.h"
using namespace std;
int main()
{
int a = 5, b = 3, c = 10;
cout << "The integer average of " << a << " and ";
cout<< b << " is ";
cout << average(a, b) << ".\n\n";
cout << "The integer average of " << a << ", ";
cout << b << " and " << c << " is ";
cout << average(a, b, c) <<".\n";
return 0;
}
8
LAB WORK
1. Write a program to calculate the area of Rectangle. Use function and take input length
and width from user.
2. Write a program in C++ to convert temperature in Celsius to Fahrenheit and Fahrenheit
to Celsius. Use two Functions.
3. Write a program that find a numbers is even or odd. Hint: use function
4. Write a program in C++ to check whether a number is prime or not. Use function
Sample Output:
Input a number to check prime or not: 13
The entered number is a prime number.
5. Write a program in C++ to find the factorial of a number. use function
Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120
6. Write a program that takes two inputs from the user and adds, subtracts, multiplies and
divide it and also print the output. Use function.
7. Write a program to find area of circle. Hints: Area = PI x radius x radius. Use function.
ASSIGNMENT
Sample Output:
3. Write a program in C++ to display n terms of natural number and their sum. Use function.
Sample Output: