Beruflich Dokumente
Kultur Dokumente
18B17CI371
LAB RECORD
Submitted By
Rahul Singh
ER NO:-191B317
2020-2021
#include <bits/stdc++.h>
using namespace std;
void Fibonacci(int n)
{
int f1 = 0, f2 = 1, i;
if (n < 1)
return;
for (i = 1; i <= n; i++)
{
cout<<f2<<" ";
int m = f1 + f2;
f1 = f2;
f2 = m;
}
}
int main()
{
int no;
cin>>no;
Fibonacci(no);
}
2. WAP to find out series sum of 1^2 + 2^2 + …. + n^2
/////This program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
void sumseries(int n)
{
cout<<(n*(n+1)*((2*n)+1))/6;
}
int main()
{
int no;
cin>>no;
sumseries(no);
}
int main()
{
int a ,b;
cin>>a>>b;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
}
//taken reference from internet in search of better algorithm
QUE4: WAP to multiply two numbers by using addition.
//////This program is developed by Rahul Singh (191B317)
#include<bits/stdc++.h>
using namespace std;
int multiply(int x, int y)
{
if(y == 0)
return 0;
if(y > 0 )
return (x + multiply(x, y-1));
if(y < 0 )
return -multiply(x, -y);
}
int main()
{
int m,n;
cin>>m>>n;
cout <<multiply(m,n);
}
Que7: WAP to sort the array and ask the choice from user for
ascending/descending.
///program developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
#define rep(i,n) for (i = 0; i < n; i++)
#define REPR(i,k,n) for (i = k; i >= n; --i)
using namespace std;
void asc_desc(int array[],int l)
{
int j;
rep(j,l)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
j = -1;
}
}
int op;
cout<<"enter 1 for ascending and 0 for descending order resptively:";
cin>>op;
if(op)
{
rep(j,l) cout<<array[j]<<" ";
}
else
{
REPR(j,l-1,0)cout<<array[j]<<" ";
}
}
int main()
{
int m,i;
cin>>m;
int arr[m];
cout<<"enter the array elements:";
rep(i,m)cin>>arr[i];
asc_desc(arr,m);
}
void getZarr(string str, int Z[]);
void search(string text, string pattern)
{
string concat = pattern + "$" + text;
int l = concat.length();
int Z[l];
getZarr(concat, Z);
for (int i = 0; i < l; ++i)
{
if (Z[i] == pattern.length())
cout << "Pattern found at index "
<< i - pattern.length() -1 << endl;
}
}
void getZarr(string str, int Z[])
{
int n = str.length();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
else
{
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else
{
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
int main()
{
string text;
string pattern ;
getline(cin,text);
getline(cin,pattern);
search(text, pattern);
return 0;
}
struct TIME
{
int seconds;
int minutes;
int hours;
};
void computeTimeDifference(struct TIME, struct TIME, struct TIME *);
int main()
{
struct TIME t1, t2, difference;
cout << "Enter start time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;
cout << "Enter stop time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;
computeTimeDifference(t1, t2, &difference);
cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":
" << t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
cout << " = " << difference.hours << ":" << difference.minutes << ":" << d
ifference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *differ
ence){
if(t2.seconds > t1.seconds)
{
--t1.minutes;
t1.seconds += 60;
}
difference->seconds = t1.seconds - t2.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
difference->minutes = t1.minutes-t2.minutes;
difference->hours = t1.hours-t2.hours;
}
//// this program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
typedef struct complexNumber {
float real;
float imag;
};
complexNumber addCN(complexNumber num1,complexNumber num2) {
complexNumber temp;
temp.real = num1.real + num2.real;
temp.imag = num1.imag + num2.imag;
return(temp);
}
int main() {
complexNumber num1, num2, sum;
cout << "Enter real part of Complex Number 1: " << endl;
cin >> num1.real;
cout << "Enter imaginary part of Complex Number 1: " << endl;
cin >> num1.imag;
cout << "Enter real part of Complex Number 2: " << endl;
cin >> num2.real;
cout << "Enter imaginary part of Complex Number 2: " << endl;
cin >> num2.imag;
sum = addCN(num1, num2);
if(sum.imag >= 0)
cout << "Sum of the two complex numbers is "<< sum.real <<" + "<< sum.imag
<<"i";
else
cout << "Sum of the two complex numbers is "<< sum.real <<" + ("<< sum.imag
<<")i";
return 0;
}