201B011 - Lab 2
201B011 - Lab 2
201B011 - Lab 2
Technology, Guna
Name – Abhishek Sharma
Batch – B1 – BX1
Er. No – 201B011
LAB 2
REVISIT
Note: Write all the programs in C/C++ using function concept.
Code in C++
#include <iostream>
using namespace std;
void fibonacci(int n)
{
int t1 = 0, t2 = 1, nextTerm = 0;
int main()
{
int a;
cout << "Enter the number of terms:\n ";
cin >>a;
fibonacci(a);
return 0;
}
Output:-
_____________________________________________________________________
2. WAP to find out series sum of 1^2 + 2^2 + …. + n^2
Code in C++
#include <iostream>
using namespace std;
int seriessum(int n)
{
int sum=0;
int main()
{
int a;
cout << "Enter the number of terms:\n ";
cin >>a;
cout << "Sum of n elements is =\t "<<seriessum(a);
return 0;
}
Output:-
_____________________________________________________________________
Code in C++
// C++ program to find GCD of two numbers
#include <iostream>
using namespace std;
int gcd(int n1, int n2)
{
int hcf;
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}
return hcf;
_____________________________________________________________________
Code in C++
#include <iostream>
using namespace std;
int multiplyusingaddition(int n1, int n2)
{
int product;
for(int i=1;i<=n2;i++){
product+=n1 ;
}
return product ;
Code in C++
// C++ program to convert binary to decimal
#include <iostream>
using namespace std;
int base = 1;
base = base * 2;
}
return value;
}
Code in C++
#include <iostream>
using namespace std;
void decToBinary(int n)
{
int binaryNum[64];
int i = 0;
while (n > 0) {
_____________________________________________________________________
7. WAP to display lower triangular matrix of a given n by n size matrix entered by user.
Code in C++
#include <iostream>
using namespace std;
if (i < j)
{
cout << "0" << " ";
}
else
cout <<*((arr+i)+j) << " ";
}
cout<<endl;
}
}
int main()
{
int r,c;
cout<<"Enter row and column (note row==column):- \t";
cin>>r>>c;
int arr[r][c];
cout<<"Enter Elements :-";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>arr[i][j];
}
}
fun((int*)arr,r,c);
return 0;
}
Output:-
_____________________________________________________________________
Code in C++
#include <bits/stdc++.h>
using namespace std;
int factorial(int n)
{
int value = 1;
for (int i = 2; i <= n; i++)
value = value * i;
return value;
}
// Driver code
int main()
{
int n,r;
cout<<"Enter the value of n and r :- \n";
cin>>n>>r;
cout <<"Combination of "<<n<<" and "<<r<<" is:- "<< calc_nCr(n, r);
return 0;
}
Output:-
_____________________________________________________________________
Advanced Problems:
9. WAP for finding the element which appears maximum number of times in the array.
Code in C++
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Size:- \t";
cin>>n;
int num[n];
cout<<"Enter Elements :-\n";
for (int i=0; i < n; i++){
cin>>num[i];
}
cout << "Original array:-\n";
for (int i=0; i < n; i++)
cout << num[i] <<" ";
most_occurred_number(num, n);
}
Output:-
_____________________________________________________________________
10. Consider that you are given with a database of employee records (at least 5). Each
employee record having following information –
Emp_id(integer) Emp_name(string) Emp_city(string)
Assume that Emp_id is unique. Write a function for taking database and put it in your
header file. Use this function by including your own header file for following
questions.
{Use the structure for creating database}
a. Write a function to find out the employee record from this database on the basis of
Emp_id.
b. Write a function to sort the employee records on the basis of Emp_id.
c. Write a function to sort (alphabetically) the array of characters.
d. Write a function to count the number of employees in database.
e. Write a function to add 5 more records in database.
Code in C++
Header file Code
#include<bits/stdc++.h>
using namespace std;
struct employee_records
{
int Emp_id;
char Emp_name[100];
char Emp_city[100];
};
void input(struct employee_records x[],int n)
{
for(int i=0; i<n; i++)
{
cout<<"Enter "<<(i+1)<<" Employee id\n";
cin>>x[i].Emp_id;
cout<<"Enter "<<(i+1)<<" Employee city name\n";
cin>>x[i].Emp_city;
cout<<"Enter "<<(i+1)<<" Employee name\n";
cin>>x[i].Emp_name;
}
}
void display(struct employee_records x[],int n)
{
int i;
for(i=0; i<n; i++)
{
cout<<"Employee "<<(i+1)<<" id "<<x[i].Emp_id<<endl;
cout<<"Employee "<<(i+1)<<" city "<<x[i].Emp_city<<endl;
cout<<"Employee "<<(i+1)<<" name "<<x[i].Emp_name<<endl;
}
}
}
void sort_EmpID(struct employee_records x[],int n)
{
cout<<endl;
cout<<"sorted Employee Id \n";
int i,j,t;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(x[j].Emp_id>x[j+1].Emp_id)
{
t=x[j].Emp_id;
x[j].Emp_id=x[j+1].Emp_id;
x[j+1].Emp_id=t;
}
}
}
for(i=0; i<n; i++)
{
cout<<x[i].Emp_id<<endl;
}
}
void sort_names(struct employee_records x[],int n)
{
cout<<endl;
int i,j;
char t[100];
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if((strcmp(x[j].Emp_name,x[j+1].Emp_name))>0)
{
strcpy(t,x[j].Emp_name);
strcpy(x[j].Emp_name,x[j+1].Emp_name);
strcpy(x[j+1].Emp_name,t);
}
}
}
cout<<"sorted names\n";
for(i=0; i<n; i++)
{
cout<<x[i].Emp_name<<endl;
}
void sort_city(struct employee_records x[],int n)
{
cout<<endl;
int i,j;
char t[100];
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if((strcmp(x[j].Emp_city,x[j+1].Emp_city))>0)
{
strcpy(t,x[j].Emp_city);
strcpy(x[j].Emp_city,x[j+1].Emp_city);
strcpy(x[j+1].Emp_city,t);
}
}
}
cout<<"sorted cities\n";
for(i=0; i<n; i++)
{
cout<<x[i].Emp_city<<endl;
}
void employee_count(int &count)
{
cout<<endl;
cout<<"NO. of employee in database is "<<count<<endl;
}
void add_records(struct employee_records x[],int &a)
{
cout<<endl;
a=a+5;
x[a];
for(int i=a-5;i<a;i++)
{
cout<<"Enter "<<(i+1)<<" Employee id\n";
cin>>x[i].Emp_id;
cout<<"Enter "<<(i+1)<<" Employee city name\n";
cin>>x[i].Emp_city;
cout<<"Enter "<<(i+1)<<" Employee name\n";
cin>>x[i].Emp_name;
}
}
void message()
{
cout<<endl;
cout<<"Enter 1 to display database\nEnter 2 to see record in accordance with
id\n";
cout<<"Enter 3 to sort database of employee id\nEnter 4 to sort employee names\
n";
cout<<"Enter 5 to sort employee cities\nEnter 6 to print employee count\n";
cout<<"Enter 7 to add 5 more employee in database\nEnter 8 to exit\n";
}
int main()
{
int n,choice;
cout<<"Enter the employee count\n";
cin>>n;
Output :-