Professional Documents
Culture Documents
201B011 - Lab 1
201B011 - Lab 1
technology , Guna
Name – Abhishek Sharma
Batch – B1 – BX1
Er. No – 201B011
LAB 1
REVISIT
C++ Code
#include<iostream>
using namespace std;
int Largest (int arr[] , int n)
{
int max = arr[0];
for(int i=0;i<n;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
return max;
}
int main()
{
int n,a[1005];
cout<<"Enter no of elements"<<endl;
cin>>n;
cout<<"Enter elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Largest element is "<<Largest(a,n);
return 0;
}
O/P :-
C++ Code
#include<iostream>
using namespace std;
int search (int arr[] , int n , int key)
{
for(int i=0;i<n;i++)
{
if(arr[i]==key)
{
return i;
}
}
}
int main()
{
int arr[10],n,key;
cout<<"Enter no of elements "<<endl;
cin>>n;
cout<<"enter elements : "<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"enter key : ";
cin>>key;
cout<<"position of element is : "<<search(arr,n,key);
return 0;
}
O/P :-
C++ Code
#include<iostream>
using namespace std;
bool isprime (int n)
{
if (n<=1)
return false;
for(int i=2;i<n;i++)
{
if(n % i==0)
return false;
else
return true;
}
}
int main()
{
int n;
cout<<"enter number to be checked "<<endl;
cin>>n;
if (isprime(n)==true)
cout<<"it is prime number "<<endl;
else
cout<<"not a prime number "<<endl;
return 0;
}
O/P :-
C++ Code
#include<iostream>
using namespace std;
int power (int p , int b)
{
int res=1;
for(int i=0;i<p;i++)
{
res=res*b;
}
return res;
}
int main()
{
int p,b;
cout<<"Enter power and base respectively : "<<endl;
cin>>p>>b;
cout<<"result is : "<<power(p,b);
return 0;
}
O/P :-
C++ Code
#include<iostream>
#include<string>
using namespace std;
string replace (string a , char b , char c)
{
for(int i=0 ; i<a.length();i++)
{
if(a[i]==b)
{
a[i]=c;
}
}
return a;
}
int main()
{
char b,c;
string a;
cout<<"enter a string : "<<endl;
getline(cin,a);
cout<<"Enter character to be replaced : "<<endl;
cin>>b;
cout<<"Enter character to be replaced with : "<<endl;
cin>>c;
cout<<"New string is : "<<replace(a,b,c);
return 0;
}
O/P : -
C++ Code
#include<iostream>
using namespace std;
void reverse_string (string s)
{
for(int i=s.length();i>=0;i--)
{
cout<<s[i];
}
}
int main()
{
string s;
cout<<"Enter your string "<<endl;
getline(cin,s);
reverse_string(s);
return 0;
}
O/P :-
C++ Code
#include<iostream>
using namespace std;
void ascend(int arr[100] , int n )
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Array in ascending order is : "<<endl ;
for(int i=0;i<n;i++)
cout<<arr[i]<<"\t";
}
void descend (int arr[100] , int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Array in descending order is : "<<endl;
for(int i=0;i<n;i++)
cout<<arr[i]<<"\t";
}
int main()
{
int n,arr[100],choice;
cout<<"Enter size of array : "<<endl;
cin>>n;
cout<<"Enter array elements : "<<endl;
for(int i=0 ; i<n ; i++)
{
cin>>arr[i];
}
cout<<"Press 1 for ascending sort and 2 for descending sort "<<endl;
cin>>choice;
if(choice==1)
{
ascend(arr , n );
}
if(choice==2)
{
descend(arr , n);
}
return 0;
}
O/P :-
Ascending sort -
Descending Sort –
C++ Code
#include <bits/stdc++.h>
#include <string>
using namespace std;
void found_in_string(string s, string fnd)
{
int flag = 0;
string word;
stringstream iss(s);
while (iss >> word)
{
int key = fnd.compare(word);
if (key == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
cout << "word Found in your statement";
else
cout << "Sorry , word not found";
}
int main()
{
cout << "Enter a sentence\n";
string s, fn;
getline(cin, s);
cout << "Enter a word to be searched in the sentence\n";
getline(cin, fn);
found_in_string(s, fn);
return 0;
}
O/P :-
C++ Code
#include <iostream>
using namespace std;
int main()
{
char str1[100], str2[100];
char *s1 = str1;
char *s2 = str2;
cout << "Enter 1st string: ";
cin >> str1;
cout << "Enter 2nd string: ";
cin >> str2;
while (*(++s1));
while (*(s1++) = *(s2++));
cout << "Concatenated string is : " << str1;
return 0;
}
O/P :-
C++ Code
#include <iostream>
using namespace std;
void search_in_arr(int arr[], int n, int fnd)
{
int i, flag = 0;
for (i = 0; i < n; i++)
{
if (fnd == arr[i])
{
flag = 1;
cout<<"Element found at position : "<<i<<endl;
break;
}
}
if (flag != 1)
cout << "Element not found\n";
}
int main()
{
int n, key;
cout << "Enter the size of array" << endl;
cin >> n;
int *arr = new int[n];
cout << "Enter elements in array\n";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter element to be found\n";
cin >> key;
search_in_arr(arr, n, key);
delete[] arr;
return 0;
}
O/P :-
Advance Problems
C++ Code
#include<iostream>
using namespace std;
struct timezone
{
int hour;
int min;
int sec;
};
int main()
{
timezone t1,t2,difference;
cout<<"Enter start time "<<endl;
cout<<"Enter hours , minutes and seconds resp. "<<endl;
cin>>t1.hour>>t1.min>>t1.sec;
cout<<"Enter Stop time "<<endl;
cout<<"Enter hours , minutes and seconds resp. "<<endl;
cin>>t2.hour>>t2.min>>t2.sec;
difference.hour=t2.hour-t1.hour;
difference.min=t2.min-t1.min;
difference.sec=t2.sec-t1.sec;
cout<<"TIME DIFFERENCE : "<<t2.hour<<":"<<t2.min<<":"<<t2.sec<<" - "<<t1.h
our<<":"<<t1.min<<":"<<t1.sec<<" = "<<difference.hour<<":"<<difference.min<<":
"<<difference.sec<<endl;
return 0;
}
O/P :-
#include<iostream>
using namespace std;
struct Complex
{
float img;
float real;
};
int main()
{
Complex c1 , c2 , sum;
cout<<"For 1st complex number : "<<endl;
cout<<"Enter real and imaginary part respectively "<<endl;
cin>>c1.real>>c1.img;
cout<<"For 2nd complex number : "<<endl;
cout<<"Enter real and imaginary part respectively "<<endl;
cin>>c2.real>>c2.img;
sum.img=c1.img+c2.img;
sum.real=c1.real+c2.real;
cout<<"Sum is "<<sum.real<<" + "<<sum.img<<"i "<<endl;
return 0;
}
O/P :-