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.
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc , char*argv)
argc = atoi(argv);
float s=0;
if(argc==0 || argc==1)
// taking input from the user
cout<<"\n Enter the no. upto which the operation perform : ";
cin>>argc;
for (int i=1;i<argc;i++)
if(i%2!=0)
s = s+1/pow(i,2);
else
s = s-1/pow(i,2);
cout<<"\nSum = "<<s;
return 0;
OUTPUT :
2. Write a program to remove the duplicates from an array.
#include<iostream>
using namespace std;
int main()
int i,j,k,n,a[30];
cout<<"Enter the no. of elements of array : ";
cin>>n;
cout<<"\nEnter elements of array : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n Array created is : ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
for(j=i+1;j<n;)
if(a[i]==a[j])
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
else
j++;
cout<<"\n Array after removing duplicates : ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
OUTPUT :
3. Write a program that prints a table indicating the number of occurrences
of each alphabet in the text entered as command line arguments.
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
string str="";
static int alphabet[26];
int x;
cout<<"\n\n Command-Line Argument\n";
for(int i=0;i<argc;i++)
cout<<"\n "<<argv[i];
str+=argv[i];
for(int i=0;i<str.length();i++)
if(str[i]>='A' && str[i]<='Z')
x=((int)str[i])-65;
alphabet[x]++;
else if(str[i]>='a' && str[i]<='z')
x=((int)str[i])-97;
alphabet[x]++;
}
//Displaying No. of occurrences of each alphabets in the command line argument
cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n Alphabet No. of
Occurrences\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
for(int i=0;i<26;i++)
cout<<"\n "<<(char)(65+i)<<" "<<alphabet[i];
return 0;
OUTPUT :
5. Write a program to merge two ordered arrays to get a single ordered array.
#include <iostream>
#include <cstdlib>
using namespace std;
int merge_arrays(int arr1[], int arr2[], int arr3[], int m, int n)
{
int i,j;
for(i = 0; i < m; i++)
{
arr3[i] = arr1[i];
}
for(i = m, j = 0 ; i < m + n; i++, j++)
{
arr3[i] = arr2[j];
}
}
int main()
{
int n,m;
cout << "\nEnter the size of Array 1 : ";
cin >> m;
cout << "\nEnter the size of Array 2 : ";
cin >> n;
int arr1[m],arr2[n];
int arr3[m+n];
int i;
cout << "\nInput the Array 1 elements : ";
for(i = 0; i<m;i++)
{
cin >> arr1[i];
}
cout << "\nInput the Array 2 elements : ";
for(i = 0;i<n;i++)
{
cin >> arr2[i];
}
merge_arrays(arr1,arr2,arr3,m,n);
cout << "\nThe Merged Array : ";
for(i = 0; i < n + m; i++)
{
cout << arr3[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT :
6. Write a program to search a given element in a set of N numbers using
Binary search
(i) with recursion
#include<iostream>
using namespace std;
int binary_search(int arr[],int n,int beg,int end)
int mid;
if (beg>end)
cout<<"number not found";
else
mid=(beg+end)/2;
if(arr[mid]==n)
cout<<endl<<"number found at "<<mid<<" index ";
else if(n>arr[mid])
binary_search(arr,n,mid+1,end);
else if(n<arr[mid])
binary_search(arr,n,beg,mid-1);
return 0;
int main()
int arr[200],n,num,i,beg,end;
cout<<endl<<" enter the size of the array:";
cin>>n;
cout<<endl<<" enter any sorted array:";
for(i=0;i<n;i++)
cin>>arr[i];
cout<<endl<<" enter a value to be searched :";
cin>>num;
beg=0;
end=n-1;
binary_search(arr,num,beg,end);
return 0;
OUTPUT:
(ii) without recursion.
#include<iostream>
using namespace std;
int binary_search(int arr[],int n, int num)
int low=0,high=n-1;
while(low<=high)
int mid=(low+high)/2;
if (num==arr[mid])
return mid;
else if (num<arr[mid])
high=mid-1;
else
low=mid+1;
return -1;
int main()
int arr[10],n,num;
cout<<"\n enter array size:";
cin>>n;
cout<<"\n enter array elements :";
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"\n the array you entered"<<endl;
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";
cout<<"\n enter element to be searched :";
cin>>num;
int index = binary_search(arr,n,num);
if(index != -1)
cout<<"\n element found at "<<index;
else
cout<<"\n element not found in the array";
return 0;
OUTPUT
7. Write a program to calculate GCD of two numbers (i) with recursion (ii)
without recursion.
#include<iostream>
#include<cstdlib>
using namespace std;
int gcd(int n,int m)
{
if((n>=m)&&((n%m)==0))
return(m);
else
gcd(m,(n%m));
}
int main()
{
int n,m,result;
cout<<"Input the first integer number:";
cin>>n;
cout<<"Input the second integer number:";
cin>>m;
result=gcd(n,m);
cout<<"GCD of "<<n<<" and "<<m<<" is "<<result;
return 0;
}
OUTPUT:
8. Create Matrix class. Write a menu-driven program to perform following
Matrix operations:
a) Sum
b) Product
c) Transpose
#include<iostream>
using namespace std;
int main()
{
int mat1[10][10],mat2[10][10],mat3[10][10],mult[10][10],transpose[10]
[10],rows,columns,i,j,k,ch;
cout<<"\n -----------------------------------------";
cout<<"\n * MENU DRIVEN PROGRAM * ";
cout<<"\n ----------------------------------------";
cout<<"\n ";
cout<<"\n 1). sum \n 2). product \n 3).transpose ";
cout<<"\n Enter number of rows:";
cin>>rows;
cout<<"\n Enter number of coloumns:";
cin>>columns;
cout<<"\n Enter elements for matrix 1:";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cin>>mat1[i][j];
}
}
cout<<"\n Matrix 1 created is:";
cout<<"\n ";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cout<<mat1[i][j]<<" ";
}
cout<<"\n ";
}
cout<<"\n Enter elements for matrix 2:";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
cin>>mat2[i][j];
}
cout<<"\n Matrix 2 created is:";
cout<<"\n ";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cout<<mat2[i][j]<<" ";
}cout<<"\n ";
}
cout<<"\n Enter your choice to perform operations : ";
cin>>ch;
switch(ch)
{
case 1:
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
cout<<"\n Sum of the matrices is:";
cout<<"\n ";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
cout<<mat3[i][j]<<" ";
cout<<endl;
}
case 2:
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
mat3[i][j]=0;
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
for(k=0;k<columns;k++)
{
mult[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
cout<<"\n Product of the matrices is:";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cout<<" "<<mult[i][j];
cout<<endl;
}
}
cout<<mat3[i][j]<<" ";
cout<<endl;
case 3:cout<<"\n Transpose of matrix is : ";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
transpose[i][j]=mat1[j][i];
cout<<transpose[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
OUTPUT:
10. Create a class Triangle. Include overloaded functions for calculating
area. Overload assignment operator and equality operator.
#include<iostream>
#include <cmath>
using namespace std;
class triangle
public:
void area(int , int , int);
void area( int, int);
};
void triangle::area( int a, int b) {
cout<<"Area of triangle : "<<.5 *a*b;
void triangle::area(int i, int j, int k)
float s=(i+j+k)/2;
float ar=sqrt(s*(s-i)*(s-j)*(s-k));
cout<<"\n The area of triangle when 3 sides are given is : "<<ar;
int main()
int a, b;
int i,j,k;
triangle obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n Area of Triangle";
cout<<"\n Enter Sides of the Triangle : ";
cin>>a>>b;
obj.area( a, b);
cout<<"\n when three sides of a triangle are given : ";
cin>>i>>j>>k;
obj.area(i,j,k);
return 0;
OUTPUT :
11. 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;
float Division(float num,float den)
{
if (den == 0)
{
throw -1;
}
return (num / den);
}
int main()
{
float p , q ,result;
cout<<"\n Enter dividend : ";
cin>>p;
cout<<"\n Enter divisor : ";
cin>>q;
try
{
result = Division(p,q);
cout << "The quotient is "
<< result << endl;
}
catch (int x)
{
cout << "Exception occurred try to divide by 0 !!!" ;
}
return 0;
}
OUTPUT:
13. 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.
#include<iostream>
#include<fstream>
using namespace std;
class student
int rno;
char name[20];
float mark;
public:
void get()
cout<<"\nEnter data: ";
cout<<"\n Enter roll no. : ";
cin>>rno;
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter Marks : ";
cin>>mark;
void disp()
cout<<rno<<"\t\t\t"<<name<<"\t\t"<<mark<<endl;
};
int main()
{
fstream f;
f.open("school.txt",ios::in|ios::out);
student s[10];
for(int i=0;i<10;i++)
s[i].get();
f.write((char*)&s,sizeof(s));
cout<<"\nRoll no \tName \tMarks\n";
cout<<"=============================\n";
for(int j=0;j<10;j++)
while(f.read((char*)&s,sizeof(s)));
s[j].disp();
return 0;
OUTPUT: