coLabfile
coLabfile
1
fi
fi
fi
fi
fi
fi
2. Take an input year, and check leap year or not.
Code :
#include<iostream>
using namespace std;
Output :
Conclusion:
#include<iostream>
using namespace std;
// 1 1 2 3 5 8 13 21 34 55 89 ...
int fibo(int n){
if(n==2||n==1) return 1;
return fibo(n-1)+fibo(n-2); // double call
follows Euler our Tree
}
int main(){
int n, sum = 0;
cin>>n;
for(int i= 1;i<=n;i++){
sum += fibo(i);
cout<<fibo(i)<<" ";
}
cout<<"\nSum : "<<sum;
}
Output :
Conclusion:
3
4. Find the minimum and maximum of a number in an
array.
Code :
#include<iostream>
#include<climits>
using namespace std;
int minOfArray(int arr[] ,int len){
int min = INT_MAX;
for (int i = 0; i < len; i++){
if(arr[i]< min) min = arr[i];
}
return min;
}
int maxOfArray(int arr[] ,int len){
int max = INT_MIN;
for (int i = 0; i < len; i++){
if(arr[i] > max) max = arr[i];
}
return max;
}
int main(){
int arr[10] = {12,34,5,2,56,87,26,88,54,21};
cout << "Minimum : "<<minOfArray(arr,10)<<
endl;
cout <<"Maximum : " <<maxOfArray(arr,10)<<
endl;}
Output :
Conclusion:
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
char s;
cin >> str >> s;
freq( s , str);
}
Output :
Conclusion:
5
6. Sort the frequency of character of a word and print it
in ascending and descending order.
Code :
#include<bits/stdc++.h>
using namespace std;
string frequencySort(string s) {
unordered_map<char,int>mp;
for(int i=0;i<s.length();i++)
mp[s[i]]++;
priority_queue<pair<int,char>>pq;
for(auto x:mp)
pq.push({x.second,x.first});
string ans="";
while(pq.size())
{
auto x=pq.top();
pq.pop();
for(int i=0;i<x.first;i++)
ans+=x.second;
}
return ans;
}
int main(){
string str;
cout << "Enter the string : “; cin >> str;
cout << endl; cout << frequencySort(str);
}
Output :
Conclusion:
Successfully sorted and displayed character
frequencies.
6
7. Factorial of a number.
Code :
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cout << "Enter the number : ";
cin >> n;
cout << "Factorial of "<< n <<" is :
"<<fac(n);
}
Output :
Conclusion:
7
8. Create a Linked List, print it and do the deletion and
insertion operation on it.
Code :
#include<iostream>
using namespace std;
class node {
public:
int val;
node* next;
node(int val) {
this->val = val;
this->next = NULL;
}
};
class LinkedList {
public:
node* head;
node* tail;
int size;
LinkedList() {
head = NULL;
tail = NULL;
size = 0;
}
void insertAtTail(int val) {
node* temp = new node(val);
if (size == 0) {
head = tail = temp;
} else {
tail->next = temp;
tail = temp;
}
size++;
}
void insertAtHead(int val) {
node* temp = new node(val);
8
if (size == 0) {
head = tail = temp;
} else {
temp->next = head;
head = temp;
}
size++;
}
void insert(int idx, int val) {
if (idx == 1) {
insertAtHead(val);
} else if (idx == size + 1) {
insertAtTail(val);
} else if (idx < 1 || idx > size + 1) {
cout << "INVALID" << endl;
} else {
node* new_node = new node(val);
node* temp = head;
for (int i = 1; i < idx - 1; i++) {
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
size++;
}
}
void deleteAtHead() {
if (size == 0) {
cout << "List is empty" << endl;
} else {
node* temp = head;
head = head->next;
delete temp;
size--;
if (size == 0) tail = NULL;
}
}
void deleteAtTail() {
9
if (size == 0) {
cout << "List is empty" << endl;
} else if (size == 1) {
delete head;
head = tail = NULL;
size--;
} else {
node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
tail = temp;
size--;
}
}
void deleteAt(int idx) {
if (idx == 1) {
deleteAtHead();
} else if (idx == size) {
deleteAtTail();
} else if (idx < 1 || idx > size) {
cout << "INVALID" << endl;
} else {
node* temp = head;
for (int i = 1; i < idx - 1; i++) {
temp = temp->next;
}
node* to_delete = temp->next;
temp->next = temp->next->next;
delete to_delete;
size--;
}
}
void display() {
node* temp = head;
while (temp != NULL) {
10
cout << temp->val << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
LinkedList ll;
ll.insertAtTail(10);
ll.insertAtTail(20);
ll.insertAtTail(30);
ll.insertAtTail(40);
ll.insertAtTail(50);
ll.display();
ll.insert(3,60);
ll.display();
ll.deleteAt(6);
ll.display();
}
Output :
Conclusion:
11
9. Program a calculator.
Code :
#include<bits/stdc++.h>
using namespace std;
int main(){
// Calculator
int a, b, calc;
cout << "Enter no. : ";
cin >> a; cout<<endl;
cout << "Enter no. : ";
cin >> b; cout << endl;
cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout<<"Calculate : ";
cin >> calc; cout << endl;
switch (calc){
case 1:
cout << "Result : " << a+b; cout << endl;
break;
case 2:
cout << "Result : " << a-b; cout << endl;
break;
case 3:
cout << "Result : " << a*b; cout << endl;
break;
case 4:
cout << "Result : " << a/b; cout << endl;
break;
default:
break;
} }
12
Output :-
Conclusion:
13
10. Check the string/number is palindrome or not.
Code :
#include<iostream>
#include<string>
using namespace std;
bool check_palindrome(string a){
int len = size(a);
bool flag = true;
for (int i = 0, j = len -1 ; i < j; i++ ,j--){
if(a[i] == a[j]) flag = true;
else flag = false; break;
}
return flag;
}
int main(){
string str ;
cin >> str;
cout << "Palindrome :" <<
check_palindrome(str);
// 1 = true,0 = false
}
Output :
Conclusion: