) Chapter One (: Fundamentals of Programming2 (Lab Manual)
) Chapter One (: Fundamentals of Programming2 (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
#include <iostream>
#include <conio.h>
using namespace std;
// Function prototypes
int sumsq(int);
int main()
{
int n,sum;
} // 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
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;
}
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;
}
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.
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
// Function prototypes
float distance(float,float);
void main()
{
float x,y,dist;
} // End of main
// Function Definitions
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.
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.
Function Overloading
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);
Recursive Function
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>
#include<math.h>
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;
{ float disc,r1,r2;
disc=pow(b,2)-(4*a*c);
if(a==0)
cout<<"undefind\n";
else if(disc<0)
cout<<"undefind\n";
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;
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
double num1,num2,result=0;
char choice;
cout<<"|========================================================|"<<endl;
cout<<"|========================================================|"<<endl;
cin>>choice;
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;
case 'D':
case 'd':
if(num2==0)
else {
result=Divide(num1,num2);
cout<<result;
break;
default:
cout<<choice
break; }
cin >>ch;
return 0; }
{ return num1+num2;}
{ return num1-num2;
{ return num1*num2; }
return num1/num2; }