/*------------Lab1-------------
1)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(int i=0;i<n;i++)
{
cin >>a[i];
cout<<a[i]<<" ";
}
return 0;
}
2)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(int i=0;i<n;i++)
{
cin >>a[i];
}
for(int i=0;i<n;i++ )
{
if(a[0]<a[i])
a[0]=a[i];
}
cout<<a[0]<<endl;
return 0;
}
3)
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, x1, x2, x;
cin >> a >> b >> c;
double d = b * b - 4 * a * c;
if (d > 0) {
x1 = (-b + sqrt(d)) / (2 * a);
cout << "x1 = " << x1 << endl;
x2 = (-b - sqrt(d)) / (2 * a);
cout << "x2 = " << x2 << endl;
} else if (d == 0) {
x = -b / (2 * a);
cout << "Unique solution: " << x << endl;
} else {
cout << "No real solution" << endl;
}
return 0;
}
------------------lab 2----------------
4)
#include <iostream>
using namespace std;
int main()
{
int arr[10],even[10],odd[10],evncnt=0,oddcnt=0,i;
for(i=0;i<10;i++)
cin>>arr[i];
for(i=0;i<10;i++)
{
if(arr[i]%2==0)
even[evncnt++]=arr[i];
else
odd[oddcnt++]=arr[i];
}
for(i=0;i<evncnt;i++)
cout<<"Even:"<<even[i]<<" ";
for(i=0;i<oddcnt;i++)
cout<<"Odd:"<<odd[i]<<" ";
}
5)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n],pos,item;
for(int i=0;i<n;i++){
cin>>a[i];
}
cin >> pos>>item;
for(int i=n;i>=pos;i--){
a[i]=a[i-1];
}
a[pos]=item;
n++;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
6)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n], item, found = 0;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> item;
for(int i = 0; i < n; i++) {
if(a[i] == item) {
found = 1;
for(int j = i; j < n - 1; j++) {
a[j] = a[j + 1];
}
n--;
i--;
}
}
if(found) {
for(int i = 0; i < n; i++) {
cout << a[i] << " ";
}
} else {
cout << "not found";
}
return 0;
}
7)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < n - i - 1; j++) {
if(a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
}
}
}
for(int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
--------------lab3--------------
8)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n], x, pos = -1;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> x;
for(int i = 0; i < n; i++) {
if(a[i] == x) {
pos = i;
break;
}
}
if(pos != -1)
cout << "Element found at index: " << pos << endl;
else
cout << "Element not found" << endl;
return 0;
}
9)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
int item;
cin >> item;
int beg = 0, end = n - 1;
while(beg <= end)
{
int mid = beg + (end - beg) / 2;
if(a[mid] == item)
{
cout << mid << endl;
return 0;
}
if(a[mid] < item)
beg = mid + 1;
else
end = mid - 1;
}
cout << "Item not found" << endl;
return 0;
}
10)
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(int n)
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
int n;
cin >> n;
cout << n << endl;
SieveOfEratosthenes(n);
return 0;
}
----------------la4--------------------
12)
#include <iostream>
#include <string>
using namespace std;
int main() {
string t;
getline(cin, t);
string s ;
cin >>s;
int pos;
cin>>pos;
if (pos < 0 || pos > t.length()) {
cout << "Invalid position!\n";
} else {
t.insert(pos, s);
cout << "Text after insertion: " << t << endl;
}
return 0;
}
13)
#include<bits/stdc++.h>
using namespace std;
int main() {
string t;
getline(cin, t);
size_t position;
cin >> position;
size_t l;
cin >> l;
t.erase(position, l);
cout << t << endl;
return 0;
}
14)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s, p;
getline(cin, s);
getline(cin, p);
size_t pos = s.find(p);
if (pos != string::npos)
cout << "Pattern found at position: " << pos << endl;
else
cout << "Pattern not found." << endl;
return 0;
}
15)
#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
getline(cin, str);
int freq[256] = {0};
for(int i = 0; i < str.length(); i++) {
freq[str[i]]++;
}
for(int i = 0; i < 256; i++) {
if(freq[i] != 0) {
cout << char(i) << " : " << freq[i] << endl;
}
}
return 0;
}
16
#include <iostream>
// Function to find the length of a string
int strLength(const char* str) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
// Function to copy string S2 to S1
void strCopy(char* s1, const char* s2) {
int len = strLength(s2);
for (int i = 0; i <= len; i++) {
s1[i] = s2[i];
}
}
// Function to concatenate string S2 to S1
void strConcatenate(char* s1, const char* s2) {
int len1 = strLength(s1);
int len2 = strLength(s2);
for (int i = 0; i < len2; i++) {
s1[len1 + i] = s2[i];
}
s1[len1 + len2] = '\0';
}
// Function to compare two strings S1 and S2
int strCompare(const char* s1, const char* s2) {
int len1 = strLength(s1);
int len2 = strLength(s2);
if (len1 != len2) {
return 1;
}
for (int i = 0; i < len1; i++) {
if (s1[i] != s2[i]) {
return 1;
}
}
return 0;
}
// Function to reverse a string S
void strReverse(char* s) {
int len = strLength(s);
for (int i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}
}
int main() {
// Create two strings S1 and S2
char S1[100] = "Hello";
char S2[100] = "World";
// Find the length of S1
std::cout << "Length of S1: " << strLength(S1) << std::endl;
// Copy S2 to S1
strCopy(S1, S2);
std::cout << "Copied S2 to S1: " << S1 << std::endl;
// Concatenate S2 to S1
strConcatenate(S1, S2);
std::cout << "Concatenated S2 to S1: " << S1 << std::endl;
// Compare S1 and S2
if (strCompare(S1, S2)) {
std::cout << "S1 and S2 are not equal" << std::endl;
} else {
std::cout << "S1 and S2 are equal" << std::endl;
}
// Reverse S1
strReverse(S1);
std::cout << "Reversed S1: " << S1 << std::endl;
return 0;
}