Exercise 1: Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 10 Date: 14 July, 2020
Exercise 1: Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 10 Date: 14 July, 2020
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
Exercise 1:
Source Code:
#include <iostream>
using namespace std;
void main()
{
int num;
line:
cout << "\n Enter a positive integer : ";
cin >> num;
if (num < 0)
{
cout << "\n Negitive numbers hae no factorial." << endl;
goto line;
}
else
{
cout << " " <<num <<" Factorial = " << factorial(num) << endl;
}
system("PAUSE");
}
int factorial(int n)
{
if (n <= 1)
return 1;
else
return (n * factorial(n - 1));
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
Output:
Exercise 2:
Source Code:
#include <iostream>
using namespace std;
int fab(int);
int main()
{
int N;
cout << "\n Enter a positive number : ";
cin >> N;
if (N <= 0)
cout << " Enter a correct number " << endl;
else
{
cout << "\n";
cout << " Fibonacci series is : ";
for (int i = 1; i <= N; i++)
{
cout << fab(i - 1) << ",";
}
cout << "\n";
}
system("pause");
return 0;
}
int fab(int x)
{
if (x == 0)
return 0;
else if (x == 1)
return 1;
else
return (fab(x - 1) + fab(x - 2));
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
Exercise 3:
Source Code:
#include <iostream>
using namespace std;
int main()
{
int myarray[30], num;
cout << "\n Enter number of elements to be sorted : ";
cin >> num;
cout << "\n Enter " << num << " elements to be sorted : ";
for (int i = 0; i < num; i++)
{
cin >> myarray[i];
}
merge_sort(myarray, 0, num - 1);
cout << "\n Sorted array : {";
for (int i = 0; i < num; i++)
{
cout << myarray[i] << ",";
}
cout << "\b}" <<endl;
system("pause");
}
void merge_sort(int *arr, int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
merge_sort(arr, low, mid);
merge_sort(arr, mid + 1, high);
merge(arr, low, high, mid);
}
}
Output:
Exercise 4:
Source Code:
#include <iostream>
using namespace std;
int main()
{
int arr[50], n;
cout << "\n Enter number of elements : ";
cin >> n;
cout << "\n Enter the array elements : ";
for (int i = 0; i<n; i++)
{
cin >> arr[i];
}
QuickSort(arr, 0, n - 1);
cout << "\n sorted array : {";
for (int a = 0; a < n; a++)
{
cout << arr[a] << ",";
}
cout << "\b}" << endl;
system("PAUSE");
return 0;
}
Output: