You are on page 1of 14

Experiment 02-1

Number
Experiment Title Write a C++ program to calculate the GCD
of two numbers using recursive functions.
Usecall-by value as a method of passing
parameters to the function
Date of 24/01/2023
Experiment
Date of 10/03/2023
Submission

Objective:- Write a C++ program to calculate the GCD of


two numbers using recursive functions. Usecall-by value
as a method of passing parameters to the function

Theory:-
Flow Diagram / Algorithm:-

 The flow diagram is about calculate the GCD of two


numbers using recursive functions.

Compiler Details:- DEV C++

Code:-
include<iostream>
using namespace std;
int gcd(int a,int b);

int main( )
{
cout<<"Name :SRINJOY SAHOO Rollno. : 2130083 \n";
cout<<"Enter two number : ";
int a,b; cin>>a>>b;
int c=gcd(a,b);
cout<<"The GCD is :"<<c<<endl;
}
int gcd(int a,int b){ if(a==0 || b==0) return 0;
else if(a == b){
return a;
}
else if(a>b){ return gcd(a-b,b);
}
else{
return gcd(a,b-a);
}
}

Results/Output:-

 Output for calculate the GCD of two numbers using


recursive functions.

Remarks:-
Experiment 02-2
Number
Experiment Title Write a C++ program to interchange the
largest and the smallest numbers in the
arrayusingfunctions. Use call-by reference
as a method of passing parameters to the
function.
Date of 24/01/2023
Experiment
Date of 10/03/2023
Submission

Objective:- Write a C++ program to interchange the


largest and the smallest numbers in the
arrayusingfunctions. Use call-by reference as a method of
passing parameters to the function.

Theory:-
Flow Diagram / Algorithm:-

 The flow-diagram is about to interchange the largest and


the smallest numbers in the array using functions.
Compiler Details:- DEV C++
Code:- #include<iostream>
using namespace std;
void findMinMax(int arr[], int n, int &min, int &max)
{
min = arr[0];
max = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
else if (arr[i] < min) { min = arr[i];
}
}
}
void sawpMinMax(int arr[], int n, int &min, int &max)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == min)
{ arr[i] = max;
}
else if (arr[i] == max) { arr[i] = min;
}
}

int temp = min; min = max; max = temp;


}
int main()
{ int n;
cout<<"Name :SRINJOY SAHOO Rollno. : 2130083 \n";
cout<<"Enter the length of the array :"<<endl; cin>>n;
int *arr = new int[n];

cout<<"Enter the element of the array :"<<endl; for(int


i=0;i<n;i++){
cin>>arr[i];
}

cout<<"Before Swaping,The Array is :"<<endl; for(int


i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl; int min,max;
findMinMax(arr, n, min, max); sawpMinMax(arr, n, min, max);
cout<<"After Swapping, The Array is :"<<endl; for(int
i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl; return 0;
}

Results/Output:-

 Output is to interchange the largest and the smallest


numbers in the array using functions.
Remarks:-
Experiment 03-3
Number
Experiment Title Write a C++ program to calculate the area
of a circle, rectangle, triangle and a square
usingfunction overloading
Date of 24/01/2023
Experiment
Date of 10/03/2023
Submission

Objective:- Write a C++ program to calculate the area of a circle,


rectangle, triangle and a square usingfunction overloading
Theory:-
Flow Diagram / Algorithm:-

 The flow diagram is about calculate the area of a circle,


rectangle, triangle and a square usingfunction overloading

Compiler Details:- DEV C++

Code:-#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
string name;
cout << "SRINJOY SAHOO 2130083 \n: ";
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is "<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return (3.14 * r * r);
}
float area(float bs, float ht)
{
return ((bs * ht) / 2);
}
Results/Output:-

 The output is about calculate the area of a circle, rectangle,


triangle and a square using function overloading
Remarks:-

Signature of the
Student

Srinjoy sahoo Signature of the Lab Coordinator


(Name of the _________________________________
Student) (Name of the Coordinator)

You might also like