Experiment Numbe 3
Experiment Numbe 3
Experiment Numbe 3
Number
Experiment Title To learn writing, executing and debugging C++
programs related to point.
Objective:
To learn writing, executing and debugging C++ programs related to
point.
Q1. Write a C++ program to calculate the GCD of two numbers using
recursive functions. Use call-by address as a method of passing parameters
to the function.
Theory:
Flow Diagram / Algorithm:
Flow chart to program to calculate the GCD of two numbers using recursive
functions.
Code:
#include<iostream>
using namespace std;
int gcd(int a, int b);
int main()
{
cout<<"srinjoy sahoo 2130083"<<endl;;
int a,b;
cout<<"Enter the number whose gcd is to be calculated: ";
cin>>a>>b;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
return 0;
}
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:
Remarks:
Experiment Experiment - 03-2
Number
Experiment Title Write a C++ program using pointers to search a
value from a dynamically created 1-d array.
Date of Experiment 03/02/2023
Date of Submission 10/03/2023
Theory:
Flow Diagram / Algorithm:
Results/Output:
Q3. Write a C++ program using pointers to sort a dynamically created 1-d
array.
Theory:
Flow Diagram / Algorithm:
Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"srinjoy sahoo 2130083"<<endl;;
int *ptr,m,j,i,t;
cout<<"Enter the number of elements: ";
cin>>m;
ptr=new int[m];
cout<<"Enter the elements of the array\n";
for(i=0;i<m;i++)
{
cin>>ptr[i];
}
for (i= 0 ; i<m ; i++)
{
for (j= 0 ; j< m - i - 1; j++)
{
if (ptr[j] > ptr[j+1])
{
t= ptr[j];
ptr[j]= ptr[j+1];
ptr[j+1]= t;
}
}
}
cout<<"Sorted element in ascending order:";
for (i= 0 ; i< m ; i++)
{
cout<<ptr[i]<<" ";
}
}
Results/Output:
Remarks: