201B011 - Lab 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Jaypee university of engineering and

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. 

1. WAP to generate a Fibonacci series up to n terms. 


Input
Input number of terms: 10
Output
Fibonacci series: 
0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Code in C++
#include <iostream>
using namespace std;

void fibonacci(int n)
{
int t1 = 0, t2 = 1, nextTerm = 0;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {


// Prints the first two terms.
if(i == 1) {
cout << t1 << " ";
continue;
}
if(i == 2) {
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
}

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;

for (int i = 1; i <= n; ++i) {


sum+= i*i;
}
return sum;
}

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:-

_____________________________________________________________________

3. WAP to find out GCD of two numbers. 

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;
}

for (int i = 1; i <= n2; ++i) {


if (n1 % i == 0 && n2 % i ==0) {
hcf = i;
}
}

return hcf;

// Driver program to test above function


int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);


return 0;
}
Output:-

_____________________________________________________________________

4. WAP to multiply two numbers by using addition.

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 ;

// Driver program to test above function


int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

cout<<"Multiplication of "<<a<<" and "<<b<<" is


"<<multiplyusingaddition(a, b);
return 0;
}
Output:-
_____________________________________________________________________

5. WAP to convert a binary number into decimal.

Code in C++
// C++ program to convert binary to decimal
#include <iostream>
using namespace std;

// Function to convert binary to decimal


int binaryToDecimal(int n)
{
int num = n;
int value = 0;

int base = 1;

int temp = num;


while (temp) {
int lastdigit = temp % 10;
temp = temp / 10;

value += lastdigit * base;

base = base * 2;
}

return value;
}

// Driver program to test above function


int main()
{
int num;
cout <<"Enter Binary Number :- \t";
cin>>num;

cout << binaryToDecimal(num) << endl;


}
Output:-
_____________________________________________________________________

6. WAP to convert a decimal into binary number. 

Code in C++
#include <iostream>
using namespace std;

void decToBinary(int n)
{
int binaryNum[64];
int i = 0;
while (n > 0) {

// storing remainder in binary array


binaryNum[i] = n % 2;
n = n / 2;
i++;
}
cout <<"Binary converted number is :- \n";
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}

// Driver program to test above function


int main()
{
int n ;
cout<<"Enter number to get converted :-\t";
cin>>n;
decToBinary(n);
return 0;
}
Output:-

_____________________________________________________________________
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;

void fun(int *arr,int r,int c)


{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

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:-
_____________________________________________________________________

8. WAP to find out nCr factor of given numbers.


Note: n
Cr = n! /((n-r)!r!)

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;
}

int calc_nCr(int n, int r)


{
return factorial(n) / (factorial(r) * factorial(n - r));
}

// 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;

void most_occurred_number(int num[], int size)


{
int max_count = 0;
cout << "\nMost occurred number is: ";
for (int i=0; i<size; i++)
{
int count=1;
for (int j=i+1;j<size;j++)
if (num[i]==num[j])
count++;
if (count>max_count)
max_count = count;
}

for (int i=0;i<size;i++)


{
int count=1;
for (int j=i+1;j<size;j++)
if (num[i]==num[j])
count++;
if (count==max_count)
cout << num[i] << endl;
}
}

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;
}
}

Main Program Code


#include<bits/stdc++.h>
#include"db.h"
using namespace std;
void find_records(struct employee_records x[],int n)
{
cout<<endl;
int key;
cout<<"Enter employee id to display records\n";
cin>>key;
for(int i=0; i<n; i++)
{
if(key==(i+1))
{
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;
break;
}

}
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;

cout<<"Enter the database\n";


cout<<endl;
struct employee_records x[n];
input(x,n);
while(true)
{
message();
cin>>choice;
if(choice==1)
display(x,n);
else if(choice==2)
find_records(x,n);
else if(choice==3)
sort_EmpID(x,n);
else if(choice==4)
sort_names(x,n);
else if(choice==5)
sort_city(x,n);
else if(choice==6)
employee_count(n);
else if(choice==7)
add_records(x,n);
else if(choice==8)
break;
else
cout<<"wrong choice\nChoose again\n";
}
return 0;
}

Output :-

You might also like