You are on page 1of 53

Name – Sahil Dattatray Kshirsagar

Roll_no - 2110877
COURSE - BSC(H) COMPUTER SCIENCE
SECTION - B
SUBJECT - C++
MOBILE NO - 8454989172
E-MAIL – sahil2110877@keshav.du.ac.in
DATE - 09/03/2022

Programs
1. Write a program to compute the sum of the first n terms of the following series:

S = 1 - 1 / (2 ^ 2) + 1 / (3 ^ 3) - ... 1 / (n ^ n)

where ^ is exponentiation.

The number of terms n is to be taken from user through command line. If command line

argument is not found then prompt the user to enter the value of n.

code:-

#include<iostream>

#include<string>

#include<cmath>

using namespace std;

int main(int argc, char *argv[]){


int n;

if(argc==1){

cout<<"Not through cmd , enter the no of terms for the sum : ";

cin>>n;

else{

n=atoi(argv[1]);

float ans=0;

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

if(i%2==0){

ans-=1.0/pow(i,i);

else{

ans+=1.0/pow(i,i);

cout<<ans;

Output:-
2. Write a program to remove the duplicates from an array.

code:-

#include <iostream>

using namespace std;

int main(){

int i,j,n,count;

cout<<"enter array size :";

cin>>n;

count=n;

int arr[n];

for(i=0;i<n;i++){

cout<<"enter element at "<<i<<endl;

cin>>arr[i];

for(i=0; i<n;i++){

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

if(arr[j]==arr[i]){

for(int k=j;k<n-1;k++){

arr[k]=arr[k+1];
}

n--;

}else{

//do nothing

for(i=0;i<n;i++){

cout<<arr[i]<<endl;

return 0;

3. Write a program that prints a table indicating the number of occurrences of


each alphabet in the text entered as command line arguments.

code:-

#include<iostream>
using namespace std;

int main(int argc, char *argv[]){

string s="";

if(argc==1){

cout<<"Not through command line ";

cout<<"enter string : ";

cin>>s;

else{

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

s+=argv[i];

int aru[26]={0};

int arl[26]={0};

for(int i=0;i<s.length();i++){

if(s[i]>='A' && s[i]<='Z'){

aru[int(s[i]-65)]++;

else if(s[i]>='a' && s[i]<='z'){

arl[int(s[i]-97)]++;
}

for(int i=0;i<s.length();i++){

if(s[i]>='A' && s[i]<='Z'){

if(aru[int(s[i]-65)]!=-1){

cout<<"Number of "<<s[i]<<" character is "<<aru[int(s[i]-65)]<<endl;

aru[int(s[i]-65)]=-1;

for(int i=0;i<s.length();i++){

if(s[i]>='a' && s[i]<='z'){

if(arl[int(s[i]-97)]!=-1){

cout<<"Number of "<<s[i]<<" character is "<<arl[int(s[i]-97)]<<endl;

arl[int(s[i]-97)]=-1;

return 0;

Output:-
4. Write a menu driven program to perform following operations on strings
(without using

inbuilt string functions):

a) Show address of each character in string

b) Concatenate two strings.

c) Compare two strings

d) Calculate length of the string (use pointers)

e) Convert all lowercase characters to uppercase

f) Reverse the string

code:-

#include<iostream>

#include<string>

#include<cstring>

using namespace std;

void address(){

string s;

cout<<"Enter any string : ";

cin>>s;

void *ptr;
for(int i=0;i<s.length();i++){

ptr=&s[i];

cout<<ptr<<endl;

void concat(){

string s1;

cout<<"Enter 1st string : ";

cin>>s1;

cout<<"Enter 2nd string : ";

string s2;

cin>>s2;

string ans;

for(int i=0;i<s1.length();i++){

ans.push_back(s1[i]);

for(int i=0;i<s2.length();i++){

ans.push_back(s2[i]);

cout<<ans<<endl;

void compare(){

string s1;

string s2;
cout<<"Enter 1st string ";

cin>>s1;

cout<<"Enter 2nd string ";

cin>>s2;

if(s1==s2){

cout<<"Both are equal"<<endl;

else if(s1>s2){

cout<<s1<<" is greater than "<<s2<<endl;

else{

cout<<s2<<" is greater than "<<s1<<endl;

void length(){

char a[100];

cin>>a;

char *ptr=a;

int cnt=0;

while(*ptr!='\0'){

cnt++;

ptr++;

cout<<"The length of string is "<<cnt<<endl;


}

void ltu(){

string s;

cout<<"Enter the lower case string "<<endl;

cin>>s;

for(int i=0;i<s.length();i++){

s[i]=s[i]-32;

cout<<s<<endl;

void reverse(){

string s;

cout<<"Enter string "<<endl;

cin>>s;

int i=0,j=s.length()-1;

while(i<=j){

swap(s[i++],s[j--]);

cout<<s<<endl;

int main(){

bool flag=true;
while(flag){

cout<<"1. to show address of each character in string "<<endl;

cout<<"2. Concatenate two strings."<<endl;

cout<<"3. Compare two strings"<<endl;

cout<<"4. Calculate length of the string (use pointers)"<<endl;

cout<<"5. Convert all lowercase characters to uppercase"<<endl;

cout<<"6. Reverse the string"<<endl;

cout<<"Enter your choice : ";

int m;

cin>>m;

switch(m)

case 1:

address();

break;

case 2:

concat();

break;

case 3:

compare();

break;
case 4:

length();

break;

case 5:

ltu();

break;

case 6:

reverse();

break;

cout<<"Do you want to continue 1- continue, 0- break : ";

cin>>flag;

return 0;

}
5. Write a program to merge two ordered arrays to get a single ordered array.

code:-

// to merge two ordered array to get an ordered array

#include<iostream>

using namespace std;

void merge(int a[],int n,int b[],int m){

int i=0,j=0;

int ans[n+m];

int k=0;

while(i<n && j<m){

if(a[i]>b[j]){
ans[k]=b[j];

k++;

j++;

else{

ans[k]=a[i];

i++;

k++;

while(i<n){

ans[k]=a[i];

i++;

k++;

while(j<m){

ans[k]=b[j];

k++;

j++;

for(int i=0;i<k;i++){

cout<<ans[i]<<" ";

}
}

int main(){

int n,m;

cout<<"Enter the size of 1st array : ";

cin>>n;

int a[n];

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

cin>>a[i];

cout<<"Enter the size of 2nd array : ";

cin>>m;

int b[m];

for(int i=0;i<m;i++){

cin>>b[i];

merge(a,n,b,m);

return 0;

}
6. Create Matrix class. Write a menu-driven program to perform following Matrix

operations:

a) Sum

b) Product

c) Transpose

code:-

#include <iostream>

using namespace std;

class matrix{

public:

void sum(){

cout<<"Enter no of rows and column : ";

int n,m;

cin>>n>>m;

int ar1[n][m];

cout<<"Enter first matrix : "<<endl;

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

for(int j=0;j<m;j++){

cin>>ar1[i][j];

int ar2[n][m];

cout<<"Enter second matrix : "<<endl;

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){

cin>>ar2[i][j];

int ans[n][m];

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

for(int j=0;j<m;j++){

ans[i][j]=ar1[i][j]+ar2[i][j];

cout<<"sum matrix is "<<endl;

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

for(int j=0;j<m;j++){

cout<<ans[i][j]<<" ";

cout<<endl;

void transpose(){

cout<<"Enter the no of rows and col : ";

int n,m;

cin>>n>>m;

int ar[n][m];
cout<<"Enter elements of matrix "<<endl;

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

for(int j=0;j<m;j++){

cin>>ar[i][j];

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

for(int j=i;j<m;j++){

int temp=ar[i][j];

ar[i][j]=ar[j][i];

ar[j][i]=temp;

cout<<"transpose is : "<<endl;

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

for(int j=0;j<m;j++){

cout<<ar[i][j]<<" ";

cout<<endl;

void product(){

cout<<"Enter the row and col of 1st matrix : ";


int m,n;

cin>>m>>n;

int ar1[m][n];

for(int i=0;i<m;i++){

for(int j=0;j<n;j++){

cin>>ar1[i][j];

cout<<"Enter the row and col of 2nd matrix : ";

int p,q;

cin>>p>>q;

if(n!=p){

cout<<"Please ensure the row and col "<<endl;

return ;

int ar2[p][q];

for(int i=0;i<p;i++){

for(int j=0;j<q;j++){

cin>>ar2[i][j];

}
int ans[m][q];

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

ans[i][j]=0;

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

for(int k=0;k<n;k++){

ans[i][j]+=ar1[i][k]*ar2[k][j];

cout<<"The product matrix is "<<endl;

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

cout<<ans[i][j]<<" ";

cout<<endl;

};
int main()

matrix a;

bool flag=true;

while(flag){

int c;

cout<<"Enter your choice : ";

cout<<"1. For sum ";

cout<<"2. For transpose ";

cout<<"3. For Product ";

cin>>c;

switch(c)

case 1:

a.sum();

break;

case 2:

a.transpose();

break;

case 3:

a.product();

break;
}

cout<<"Enter 1. to continue , 0. for break ;";

cin>>flag;

return 0;

7. Define a class Person having name as a data member. Inherit two classes Student and
Employee from Person. Student has additional attributes as course, marks and year and
Employee has department and salary. Write display() method in all the three classes to
display the corresponding attributes. Provide the necessary methods to show runtime
polymorphism.

code:-

#include<iostream>

#include<string>

using namespace std;

class Person{

protected:

string name;

public:

virtual void display(){ // here function is made virtual so as for late binding
cout<<"The person name is "<<name<<endl;

};

class Student:public Person{

private:

string course;

float marks;

int year;

public:

Student(string name,string course,float marks,int year){

this->name=name;

this->course=course;

this->marks=marks;

this->year=year;

void display(){

cout<<"Student name is "<<name<<endl;

cout<<"course is "<<course<<endl;

cout<<"marks is "<<marks<<endl;

cout<<"year is "<<year<<endl;

};

class Employee:public Person{


private:

string department;

int salary;

public:

Employee(string name,string department,int salary){

this->name=name;

this->department=department;

this->salary=salary;

void display(){

cout<<"Employee name is "<<name<<endl;

cout<<"department is "<<department<<endl;

cout<<"salary is "<<salary<<endl;

};

int main(){

Student a("vinit","bsc_cs",89.3,1);

a.display();

cout<<endl;

Employee b("rajesh","HR",90000);
b.display();

cout<<endl;

Person *p;

p=&a;

p->display();

return 0;

8. Create a class Triangle. Include overloaded functions for calculating area.


Overload assignment operator and equality operator.

code:-

#include<iostream>

#include<cmath>

using namespace std;

class Triangle{
float a,b,c,h;

public:

Triangle(float a,float b,float c,float h){

this->a=a;

this->b=b;

this->c=c;

this->h=h;

void display(){

cout<<"side a "<<this->a<<endl;

cout<<"side b "<<this->b<<endl;

cout<<"Side c "<<this->c<<endl;

cout<<"height h "<<this->h<<endl;

float area(float b,float h){

float ans=(b+h)/2;

return ans;

} // funtion overloading same funtion with diff


arguments

float area(float a,float b,float c){

float n=(a+b+c)/2;

float ans=sqrt(n*(n-a)*(n-b)*(n-c));

return ans;
}

// operator overloading of assignment operator

Triangle &operator= ( const Triangle &triangle)

this->a = triangle.a;

this->b = triangle.b;

this->c = triangle.c;

this->h=triangle.h;

return *this;

// operator overloading for equality operator

bool operator == ( Triangle & t2){

if((this->a == t2.a) && (this->b == t2.b) && (this->c == t2.c) && (this->h == t2.h)){

return true;

else{

return false;

};

int main(){

Triangle a(2,3,4,5);

float ans= a.area(2, 3, 4);

a.display();
Triangle b = a;

b.display();

if(a==b){

cout<<"triangle are equal"<<endl;

else{

cout<<"triangle are not equal"<<endl;

return 0;

9. Write a program to read two numbers p and q. If q is 0 then throw an exception


else display the result of p/q.

code:-

// Write a program to read two numbers p and q. If q is 0 then throw an exception else display the result
of p/q.

#include<iostream>

using namespace std;


int main(){

float p,q;

cout<<"Enter first number P : ";

cin>>p;

cout<<"Enter second number Q : ";

cin>>q;

try {

if(q==0){

throw q;

else{

cout<<"the division of the number is : ";

cout<<p/q<<endl;

return 0;

catch(float x){

cout<<"denominator cannot be zero "<<endl;

return 0;

}
10. Rewrite Matrix class of Q8 with exception handling. Exceptions should be
thrown by the functions if matrices passed to them are incompatible and handled
by main() function.

code:-

#include <iostream>

using namespace std;

class matrix{

public:

void sum(){

cout<<"Enter no of rows and column for first matrix : ";

int n,m;

cin>>n>>m;

int ar1[n][m];

cout<<"Enter first matrix : "<<endl;

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

for(int j=0;j<m;j++){

cin>>ar1[i][j];

cout<<"Enter no of rows and column for second matrix : ";

int x,y;

cin>>x>>y;

try{

if(x!=n || y!=m ){

throw false;
}

else{

int ar2[n][m];

cout<<"Enter second matrix : "<<endl;

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

for(int j=0;j<m;j++){

cin>>ar2[i][j];

int ans[n][m];

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

for(int j=0;j<m;j++){

ans[i][j]=ar1[i][j]+ar2[i][j];

cout<<"sum matrix is "<<endl;

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

for(int j=0;j<m;j++){

cout<<ans[i][j]<<" ";

cout<<endl;

}
}

catch (bool a){

cout<<"since row and column of the two matrices mismatches "<<endl;

cout<<"cannot perform the sum operation on them"<<endl;

void transpose(){

cout<<"Enter the no of rows and col : ";

int n,m;

cin>>n>>m;

int ar[n][m];

cout<<"Enter elements of matrix "<<endl;

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

for(int j=0;j<m;j++){

cin>>ar[i][j];

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

for(int j=i;j<m;j++){

int temp=ar[i][j];

ar[i][j]=ar[j][i];

ar[j][i]=temp;
}

cout<<"transpose is : "<<endl;

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

for(int j=0;j<m;j++){

cout<<ar[i][j]<<" ";

cout<<endl;

void product(){

cout<<"Enter the row and col of 1st matrix : ";

int m,n;

cin>>m>>n;

int ar1[m][n];

for(int i=0;i<m;i++){

for(int j=0;j<n;j++){

cin>>ar1[i][j];

cout<<"Enter the row and col of 2nd matrix : ";

int p,q;
cin>>p>>q;

try{

if(n!=p){

throw false;

else{

int ar2[p][q];

for(int i=0;i<p;i++){

for(int j=0;j<q;j++){

cin>>ar2[i][j];

int ans[m][q];

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

ans[i][j]=0;

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

for(int k=0;k<n;k++){

ans[i][j]+=ar1[i][k]*ar2[k][j];
}

cout<<"The product matrix is "<<endl;

for(int i=0;i<m;i++){

for(int j=0;j<q;j++){

cout<<ans[i][j]<<" ";

cout<<endl;

catch (bool a){

cout<<"Since there is mismatch in the row and column of the two matrices "<<endl;

cout<<"cannot perform multiplication"<<endl;

};

int main()

{
matrix a;

bool flag=true;

while(flag){

int c;

cout<<"Enter your choice : ";

cout<<"1. For sum ";

cout<<"2. For transpose ";

cout<<"3. For Product ";

cin>>c;

switch(c)

case 1:

a.sum();

break;

case 2:

a.transpose();

break;

case 3:

a.product();

break;

cout<<"Enter 1. to continue , 0. for break ;";

cin>>flag;
}

return 0;

11. Create a class Student containing fields for Roll No., Name, Class, Year and
Total Marks. Write a program to store 5 objects of Student class in a file. Retrieve
these records from file and display them.

code:-

//create a class student containing records and store the data into the file

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class Student{

// Roll No., Name, Class, Year and Total marks

public:

int rollno;

string name;

int std;

int year;
float tmarks;

public:

Student(int rollno,string name,int std,int year,float tmarks){

this->rollno=rollno;

this->name=name;

this->std=std;

this->year=year;

this->tmarks=tmarks;

void display(){

cout<<"Roll no is "<<rollno<<endl;

cout<<"Student Name is "<<name<<endl;

cout<<"class is "<<std<<endl;

cout<<"year is "<<year<<endl;

cout<<"total marks is "<<tmarks<<endl;

};

int main(){

// Student ar[5];

// for(int i=0;i<5;i++){
// }

ofstream out;

out.open("data.txt");

if(!out){

cout<<"the file may not exist"<<endl;

return 0;

out<<"Roll_NO"<<" "<<" NAME "<<" "<<" class "<<" year "<<" "<<" Total Marks "<<endl;

for(int i=0;i<5;i++){

string name;

int rollno,std,year;

float tmarks;

cout<<"Enter the name : ";

cin>>name;

cout<<"Enter rollno : ";

cin>>rollno;

cout<<"Enter class : ";

cin>>std;

cout<<"Enter year : ";


cin>>year;

cout<<"Enter total marks : ";

cin>>tmarks;

Student s(rollno,name,std,year,tmarks);

out<<s.rollno<<" "<<s.name<<" "<<s.std<<" "<<s.year<<" "<<s.tmarks<<endl;

12. Copy the contents of one text file to another file, after removing all
whitespaces.

code:-

// to copy text one file to another after removing all whitespaces

#include<iostream>

#include<fstream>

using namespace std;

int main(){

ifstream in;

in.open("data.txt");
if(!in){

cout<<"File may not exist ..";

return 0;

ofstream out;

out.open("copy.txt");

while(in.eof()==0){

string s;

in>>s;

out<<s;

return 0;

ADDITIONAL PROGRAMS
1. Program to find whether the string entered by user is Palindrome or not.

code:-

#include<iostream>

using namespace std;

bool checkpalin(string s){

int st=0,e=s.size()-1;

while(st<e){

if(s[st++]!=s[e--]){

return 0;

return 1;

int main(){

string s;

cout<<"Enter the string"<<endl;

cin>>s;

if(checkpalin(s)){

cout<<"String is palindrome"<<endl;

else{

cout<<"String is not a palindrome"<<endl;

}
return 0;

Output:-

2. Write a function to print Fibonacci series and call it from main.

code :-

// to print the fibonacci series upto n

#include<iostream>

using namespace std;

void fib(int n){

int a=0;

int b=1;

int next=1;

int cnt=1;

while(cnt<=n){

cout<<a<<" ";

a=b;

b=next;

next=a+b;
cnt++;

int main(){

int n;

cout<<"Enter upto n : ";

cin>>n;

fib(n);

return 0;

Output:-

3. WAP to print the pattern as follows (take number of lines from user)

121

12321

1234321

123454321

12345654321
code:-

#include<iostream>

using namespace std;

int main(){

int n;

cout<<"Enter the no of rows : ";

cin>>n;

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

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

cout<<" ";

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

cout<<j;

for(int j=i-1;j>=1;j--){

cout<<j;

cout<<endl;

return 0;

}
4. To copy the content one file to another such that copied file do have contents
such as word#vowels_in_the word#length of the word

code:-

#include<iostream>

#include<fstream>

using namespace std;

int main(){

ifstream in;

in.open("new.txt");

ofstream out;

out.open("main.txt");

// checking wether the file is there or it is txt file or not

if(!in){

cout<<"Yur file cannot be opened ! "<<endl;

return 0;

while(in.eof()==0){

string word;
in>>word;

int len=word.length();

int vowel=0;

for(int i=0;i<len;i++){

if(word[i]=='A' | word[i] == 'E' | word[i] == 'I' | word[i] == 'O' | word[i] == 'U' | word[i] =='a' |
word[i] =='e' | word[i] =='i' | word[i] =='o' | word[i] =='u'){

vowel++;

out<<word<<"#"<<vowel<<"#"<<len<<endl;

return 0;

The output of this code would be observed through the file. So once the file is created user
can see the output

5. To join two strings using class by their operator overloading application

code:-

#include<iostream>

#include<string>

using namespace std;

class join{
string s;

public:

void set(string a){

s=a;

void display(){

cout<<s<<endl;

join operator +(join i){

join temp;

temp.s=s+i.s;

return temp;

};

int main(){

join a,b,c;

a.set("hello ");

b.set("world");

c=a+b;

c.display();
return 0;

6. Print all possible subarrays of any given array

code:-
#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<<"Possible subarrays are : "<<endl;

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

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

for(int k=i;k<=j;k++){

cout<<a[k]<<" ";

}cout<<endl;
}

return 0;

7. To find the maximum sum of any subaaray of given array

code:-

#include<iostream>

#include<climits>

using namespace std;

int main(){

int n;

cin>>n;
int a[n];

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

cin>>a[i];

int maxsum=INT_MIN;

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

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

int sum=0;

for(int k=i;k<=j;k++){

sum+=a[k];

maxsum=max(maxsum,sum);

cout<<maxsum<<endl;

return 0;

8. To check whether the triplet passed is a pytha gorean triplet or not


code:-

#include<iostream>

using namespace std;

bool check(int x,int y,int z){

int a=max(x,max(y,z));

int b,c;

if(a==x){

b=y;

c=z;

else if(a==y){

b=x;

c=z;

else{

b=x;

c=y;

if (a*a==b*b+c*c){

return true;

else{

return false;
}

int main(){

int a,b,c;

cin>>a>>b>>c;

if(check(a,b,c)){

cout<<"Pythagorian triplet";

else{

cout<<"Not a pythagorian triplet";

return 0;

You might also like