Beruflich Dokumente
Kultur Dokumente
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>
argc = atoi(argv);
float s=0;
if(argc==0 || argc==1)
cout<<"\n Enter the no. upto which the operation perform : ";
cin>>argc;
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>
int main()
int i,j,k,n,a[30];
cin>>n;
cin>>a[i];
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++;
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>
string str="";
int x;
for(int i=0;i<argc;i++)
cout<<"\n "<<argv[i];
str+=argv[i];
for(int i=0;i<str.length();i++)
x=((int)str[i])-65;
alphabet[x]++;
x=((int)str[i])-97;
alphabet[x]++;
}
//Displaying No. of occurrences of each alphabets in the command line argument
for(int i=0;i<26;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>
int mid;
if (beg>end)
else
mid=(beg+end)/2;
if(arr[mid]==n)
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;
for(i=0;i<n;i++)
cin>>arr[i];
cin>>num;
beg=0;
end=n-1;
binary_search(arr,num,beg,end);
return 0;
OUTPUT:
(ii) without recursion.
#include<iostream>
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;
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";
cin>>num;
if(index != -1)
else
return 0;
OUTPUT
7. Write a program to calculate GCD of two numbers (i) with recursion (ii)
without recursion.
#include<iostream>
#include<cstdlib>
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:
#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;
}
}
return 0;
}
OUTPUT:
10. Create a class Triangle. Include overloaded functions for calculating
area. Overload assignment operator and equality operator.
#include<iostream>
#include <cmath>
class triangle
public:
};
float s=(i+j+k)/2;
float ar=sqrt(s*(s-i)*(s-j)*(s-k));
int main()
int a, b;
int i,j,k;
triangle obj;
cout<<"\n\t\tFunction Overloading";
cin>>a>>b;
obj.area( a, b);
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);
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>
class student
int rno;
char name[20];
float mark;
public:
void get()
cin>>rno;
cin>>name;
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<<"=============================\n";
for(int j=0;j<10;j++)
while(f.read((char*)&s,sizeof(s)));
s[j].disp();
return 0;
OUTPUT: