Fundamentals of Programming2 (C++) Chapter one (Lab manual)
Prepared by Ashenafi w. @ KIOT March 30, 2017
1
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
Function
#include <iostream>
#include <conio.h>
using namespace std;
void myFunction(); //Function declaration
int main() //The main function
{
cout << "The function main() is now working==>" << endl;
myFunction(); //calling myFunction() function
cout << "myFunction() is now finished <==" << endl;
cout <<"myFunction() has started again==>" << endl;
myFunction(); //calling myFunction() function again
cout << "myFunction() is finished <==" << endl;
cout <<"Finished all processes ";
return 0;
}//End of function main()
void myFunction() //Function definition
{
//Function body
cout << "myFunction has started==>" << endl;
cout << "myFunction will now pass control back over to function main()"
<< endl;
}//End of function myFunction
Output
Prepared by Ashenafi w. @ KIOT March 30, 2017
2
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
// This function returns the sum of squares of the first n integers
// Example program - runs sumsq function
#include <iostream>
#include <conio.h>
using namespace std;
// Function prototypes
int sumsq(int);
int main()
{
int n,sum;
// Testing function sumsq(n)
cout << "Testing function sumsq(n)"<< endl;
cout << "Enter a value for n: ";
cin >> n;
sum = sumsq(n);
cout << "Sum of squares of first " << n<< "integers is " << sum<< endl;
cout << "Tested"<< endl;
} // End of main
// Function Definitions
int sumsq(int n)
{
int sum = 0;
int i;
for (i = 1; i <= n; i++)
sum += i * i;
return sum;
} // End of sumsq
Prepared by Ashenafi w. @ KIOT March 30, 2017
3
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
a)function with return type and with arguments
#include<iostream> outputs
#include<conio.h>
using namespace std;
int add(int x,int y);
int main()
{
int a,b,c;
cout<<"enter the
value of x";
cin>>a;
cout<<"enter the
value of y";
cin>>b;
cout<<add(a,b);
//cout<<"given "<<7,10);
return 0;}
int add(int x,int y)
{
int z;
z=x+y;
return z;
}
b)function with return type and with arguments
#include<iostream>
#include<conio.h>
using namespace std;
int add();
int main()
{
cout<<add();
return 0;}
int add()
{
int x,y,z;
cout<<"enter the value of x";
cin>>x;
cout<<"enter the value of y";
cin>>y;
z=x+y;
return z;
}
Prepared by Ashenafi w. @ KIOT March 30, 2017
4
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
c)function without return type but with arguments
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
void power(int base ,int exp)
{
int z;
z=pow(base,exp);
cout<<"the value of z is="<<z;
}
int main()
{
int a,b,c;
cout<<"enter the value of base";
cin>>a;
cout<<"enter the value of exponent";
cin>>b;
power(a,b);
return 0;
}
d)function without return type but without arguments
#include<iostream>
#include<conio.h>
using namespace std;
void add();
int main()
{
add();
return 0;}
void add()
{
int x,y,z;
cout<<"enter the value of x";
cin>>x;
cout<<"enter the value of y";
cin>>y;
z=x+y;
cout<<"the value of z is="<<z;
}
Prepared by Ashenafi w. @ KIOT March 30, 2017
5
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
C++ allows programmers to define their own functions. For example the
following is a definition of a function which given the co-ordinates of a
point (x,y) will return its distance from the origin.
// Example program - runs distance function
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
// Function prototypes
float distance(float,float);
void main()
{
float x,y,dist;
// Test function distance(x,y)
cout << "Testing function distance(x,y)" << endl;
cout << "Enter values for x and y: ";
cin >> x >> y;
dist = distance(x,y);
cout << "Distance of (" << x << ',' << y<< ") from origin is " << dist<<
endl;
cout << "Tested" << endl;
} // End of main
// Function Definitions
float distance(float x, float y)
// Returns the distance of (x, y) from origin
{
float dist; //local variable
dist = sqrt(x * x + y * y);
return dist;
}
This function has two input parameters, real values x and y, and returns
the distance of the point (x,y) from the origin. In the function a local
variable dist is used to temporarily hold the calculated value inside the
function.
Exercises
1. Calculate the area and circumference of a circle with a given radius
from user (keyboard). Function area() for area and circum() for
circumference with radius rad (will be declare globally to visible on
functions) of a circle.
Prepared by Ashenafi w. @ KIOT March 30, 2017
6
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
2. Calculate the sum of square of all odd numbers between 1 and N (is an
input from user), using function sum() on recursive and normal function
method separately.
3. Read age of 10 persons from user and count number of children (age<=15),
young (age<=40), and old (age>40) using a function pers() and display
their count result on a function display(). If the user read invalid
age number (i.e. age<1 and age>120), display error message and reenter
correct age number.
// Example Two on Function declaration, definition, and calling
#include <iostream.h>
#include <conio.h>
void myFunctionOne();//Function prototype/Declarationthe method is
optional
void myFunctionTwo() //Function declaration with definition
{
//function body
cout<<"myFunctionTwo() is now running" << endl;
}
void myFunctionOne() //Function definition
{
//function body
cout << "myFunctionOne() is now running" << endl;
myFunctionTwo(); //calling function myFunctionTwo() in
function myFunctionOne()
}
int main()
{
clrscr();
cout << "main() is now running"<< endl;
myFunctionOne(); //calling function inside main functiion
return 0;
}
Output
main() is now running
myFunctionOne() is now running
myFunctionTwo() is now running
Parameter passing mechanism (Pass by value and Pass by reference)
Prepared by Ashenafi w. @ KIOT March 30, 2017
7
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
//Parameter Passing by value return 0;
#include <iostream.h> }
#include <conio.h> int add(int number)
int add(int n); {
int main()
number=number+100;
{
return number;
clrscr();
int number, result; }
number=5;
cout << " The initial value of
number : " Output
<< number << endl; The initial value of number :5
result=add(number); The final value of number :5
cout << " The final value of The result is :105
number : "
<< number << endl;
cout << " The result is : " <<
result
<< endl;
//Parameter passing by reference return(0);
#include<iostream.h> }
#include <conio.h> int add(int &p)
int add(int &n); {
int main () p=p+100;
{ return p;
clrscr(); }
int number, result; Output
number=5; The initial value of number :5
cout << " The initial value The final value of number :105
of number : " The result is :105
<< number << endl;
result=add(number);
cout << " The final value
of number : "
<< number << endl;
cout << " The result is : "
<< result
<< endl;
Prepared by Ashenafi w. @ KIOT March 30, 2017
8
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
Function with a Return Value
//Example on Function Return Value int factorial(int n)
#include <iostream.h> {
#include <conio.h> int i=0,fact=1;
int factorial(int n); if(n<=1)
int main () {
{ return(1);
clrscr(); }
int n,fact; else
cout <<"Enter the number {
\t"; for(i=1;i<=n;i++)
cin >> n; {
fact=factorial(n); fact=fact*i;
cout << "The factorial }
of " << n return(fact);
<< " is : " << fact }
<< endl; }
return(0);
}
Output:- Sample 1 Sample 2
Enter the number 5 Enter the number -3
The factorial of 5 is : 120 The factorial of -3 is : 1
(Because -3 is less than 1 then
returned 1)
Function Overloading
Prepared by Ashenafi w. @ KIOT March 30, 2017
9
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
//Function Overloading powerOf(i,j,k);
//using different number of return 0;
Arguments }
#include<iostream> long powerOf(int x)
#include<conio.h> {
using namespace std; cout<<x<<" Power of 2 is "
#include <math.h> <<pow(x,2)<<endl;
long powerOf(int); return 0;
long powerOf(int, int); }
long powerOf(int, int, int); long powerOf(int x, int y)
int main() {
{ cout<<x<<" Power of "<<y<<" is "
clrscr(); <<pow(x, y)<<endl;
int i, j, k; return 0;
cout<<"Enter first Number \t"; }
cin>>i; long powerOf(int x, int y, int z)
cout<<"Enter Second Number \t"; {
cin>>j; cout<<x<<"+"<<y<<" Power of "<<z<<" is
cout<<"Enter Third Number \t" ; "
cin>>k; <<pow((x+y), z);
powerOf(i); return 0;
powerOf(i,j); }
Output:- Sample 1 Sample 2
Enter first Number 3 Enter first Number 4
Enter Second Number 4 Enter Second Number 3
Enter Third Number 5 Enter Third Number 2
3 Power of 2 is 9 4 Power of 2 is 16
3 Power of 4 is 81 4 Power of 3 is 64
3+4 Power of 5 is 16807 4+3 Power of 2 is 49
//Function Overloading cin>>k;
//based on data types Arguments powerOf(i, k);
#include<iostream> powerOf(i, j);
#include<conio.h> return 0;
using namespace std; }
#include <math.h> long powerOf(int x, int y)
long powerOf(int, int); {
long powerOf(int, double); cout<<x<<" Power of "<<y<<" is "
int main() <<pow(x,y)<<endl;
{ return 0;
clrscr(); }
int i, k; long powerOf(int x, double y)
double j; {
cout<<"Enter first Number (int) \t"; cout<<x<<" Power of "<<y<<" is "
cin>>i; <<pow(x, y)<<endl;
cout<<"Enter Second Number (double) \t"; return 0;
cin>>j; }
cout<<"Enter Third Number (int) \t" ;
Output:- Sample 1 Sample 2
Prepared by Ashenafi w. @ KIOT March 30, 2017
10
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
Enter first Number (int) 2 Enter first Number (int) 3
Enter Second Number (double) 3.4 Enter Second Number (double) 2.44
Enter Third Number (int) 2 Enter Third Number (int) 2
2 Power of 2 is 4 3 Power of 2 is 9
2 Power of 3.4 is 10.55… 3 Power of 2.44 is 14.59…
//Function Overloading powerOf(j, i);
//based on data types Arguments powerOf(i, j);
#include<iostream> return 0;
#include<conio.h> }
using namespace std; long powerOf(double x, int y)
#include <math.h> {
long powerOf(double, int); cout<<x<<" Power of "<<y<<" is "
long powerOf(int, double); <<pow(x,y)<<endl;
int main() return 0;
{ }
clrscr(); long powerOf(int x, double y)
int i, k; {
double j; cout<<x<<" Power of "<<y<<" is "
cout<<"Enter first Number (int) \t"; <<pow(x, y)<<endl;
cin>>i; return 0;
cout<<"Enter Second Number (double) \t"; }
cin>>j;
Output:- Sample 2
Sample 1 Enter first Number (int)
Enter first Number (int) 3
2 Enter Second Number (double)
Enter Second Number (double) 3.4545 5.43
3.4545 Power of 2 is 11.93… 5.43 Power of 3 is 160.10…
2 Power of 3.4545 is 10.96…
Function overloading with different
number of arguments
#include<iostream>
#include<conio.h>
using namespace std;
int devide (int a, int b=2);
int devide(int z, int r, int y);
float devide (float a, float b);
int main()
{
int x=20, y=2;
float n=5.0, m=2.0;
cout<<devide(x,y);
cout<<endl;
cout<<devide(n,m);
Prepared by Ashenafi w. @ KIOT March 30, 2017
11
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
cout<<endl; 3 Power of 5.43 is 389.73…
cout<<devide(n,m,m);
cout<<endl;
return 0;
}
int devide (int a, int b)
{
return a/b;
}
int devide (int a, int b, int c)
{
int w=a/b ;
return w/c;
}
float devide (float x, float y)
{
return x/y;
}
Default Argument and Variable Visibility
#include <iostream> void sub(int i,double j)
Using namespace std; {
#include <conio.h> x=i-(j+30);
void sub(int i=10,double j=20.40); cout<<x<<endl;
//assigned all agreements }
void sub(double m, double n=20.60); //assign void sub(double m, double
values from n)
{
// right to left double y; //local
//e.g. void sub(double m=20.60, double n)-- variable y(visible
>error // only
// b/c assigned left to right on this function)
double x,a; //global/ external variables x and y=m-n;
a, we can access //these variables anywhere cout<<y<<endl;
in a program(visible anywhere) }
Prepared by Ashenafi w. @ KIOT March 30, 2017
12
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
void main() Output:-
{ -30.4
clrscr(); -40
sub(20); //calling function(the first 10.4
argument with int type) -10.38
sub(10, 20.00); -40.4
//calling function (arguments with
int and double type)
sub(20.40,10.00); //calling function(double
and double type)
sub(10.22);//calling function(first argument
with double type)
sub(); //calling function(all arguments
are assigned)
return 0;
}
Recursive Function
Prepared by Ashenafi w. @ KIOT March 30, 2017
13
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
//Add all even numbers between 2 and end(from //OR
keyboard) #include <iostream>
#include <iostream> Using namespace std;
Using namespace std; #include <conio.h>
#include <conio.h> int even(int end) //function even()
int even(int end) //function even() {
{ Programs are computing
if(end>2)
if(end>2) same application {
{ return end +even(end-2);
if(end%2==0) }
{ return 2;
return end +even(end-2); }
} int main()
else {
{ clrscr();
end=end -1; int end;
return end +even(end-2); cout<<"Enter the Maximum
} number:";
} lebel1:
else return 2; cin>>end;
} if(end<2)
int main() {
{ clrscr();
clrscr(); cout<<"Error!!! \n”;
int end; cout<<”\t Enter correct
cout<<"Enter the Maximum number:"; Number:";
lebel1: goto lebel1;
cin>>end; }
if(end<2) else
{ {
clrscr(); if(end%2!=0)
cout<<"Error!!! \n\t Enter correct {
Number:"; end=end-1;
goto lebel1; }
} cout<<even(end);//Function
else calling
cout<<even(end); }
return 0; Because of Return 0;
} }
validation,
Output:- Sample 1 Sample 2
Enter the Maximum number: 5 Enter the Maximum number: 1
6 Error!!!
Enter correct Number: 10
30
This means 5 is odd and the max even is 5-1=4 Because, 10 is even, then 10+8+6+4+2=30
Prepared by Ashenafi w. @ KIOT March 30, 2017
14
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
Then 4+2=6
Write c++ program that solve root of quadratic equation where the
equation given by ax2+bx+c==0
#include<iostream>
#include<conio.h>
using namespace std;
#include<math.h>
void quad(float a,float b,float c);
int main()
float a,b,c,disc,r1,r2;
cout<<"enter a,b,c\n";
cin>>a>>b>>c;
quad(a,b,c);
return 0;
void quad(float a,float b,float c)
{ float disc,r1,r2;
disc=pow(b,2)-(4*a*c);
if(a==0)
cout<<"undefind\n";
else if(disc<0)
cout<<"undefind\n";
Prepared by Ashenafi w. @ KIOT March 30, 2017
15
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
else if(disc==0)
r1=b/2*a;
r2=b/2*a;
cout<<"r1="<<r1;
cout<<"r2="<<r2;
else
r1=(-b-disc)/(2*a);
r2=(-b+disc)/(2*a);
cout<<"r1="<<r1;
cout<<"r2="<<r2;
cout<<"think u for solving qe\n";
Write cpp program using function to add, multiply, subtract and divide to
number but the program works as per user choice e.g. when the user want to
add he/she must type A or a likewise S or s to subtract ,M or m to multiply
and D or d to divide.
#include <iostream.h>
#include <conio.h>
double Add(double,double);
double Subtract(double,double);
double Multiply(double,double);
double Divide(double,double);
int main()
{ char ch;
do
Prepared by Ashenafi w. @ KIOT March 30, 2017
16
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
double num1,num2,result=0;
char choice;
cout<<"|========================================================|"<<endl;
cout<<"||press A or a to Add |"<<endl;
cout<<"||press S or s to substract |"<<endl;
cout<<"||press M or m to multiply |"<<endl;
cout<<"||press D or d to Devide |"<<endl;
cout<<"|========================================================|"<<endl;
cin>>choice;
cout<<"Enter the value of first and second number"<<endl;
cin>>num1>>num2;
switch(choice)
case 'A':
case 'a':
result=Add(num1,num2);
cout<<result;
break;
case 'S':
case 's':
result=Subtract(num1,num2);
cout<<result;
break;
case 'M':
case 'm':
result=Multiply(num1,num2);
cout<<result;
break;
Prepared by Ashenafi w. @ KIOT March 30, 2017
17
Fundamentals of Programming2 (C++) Chapter one (Lab manual)
case 'D':
case 'd':
if(num2==0)
cout<<"devision by zero is not Allowed";
else {
result=Divide(num1,num2);
cout<<result;
break;
default:
cout<<choice
<<" is unreconginized operator";
break; }
cout<<"Do you want to continue (Y/N) ";
cin >>ch;
} while (ch != 'N');
return 0; }
double Add(double num1,double num2)
{ return num1+num2;}
double Subtract(double num1,double num2)
{ return num1-num2;
double Multiply(double num1,double num2)
{ return num1*num2; }
double Divide(double num1,double num2)
return num1/num2; }
Prepared by Ashenafi w. @ KIOT March 30, 2017
18