CPP Enclyclopedia
Sunday, September 22, 2013
Monday, November 26, 2012
C++ PROGRAM TO CONVERT THE ROMAN NUMERAL INTO AN ARABIC INTEGER
/* A C++ PROGRAM TO CONVERT THE ROMAN NUMERAL INTO AN ARABIC INTEGER */
#include<iostream.h>
#include<conio.h>
void main()
{
char s[10];
int sum=0,i=0;
clrscr();
cout<<"Enter the roman numeral :";
cin>>s;
while(s[i]!='\0')
{
switch(s[i])
{
case 'm':sum+=1000;
break;
case 'd':if(s[i-1]=='c')
sum+=400;
else if(s[i-1]=='l')
sum+=450;
else
sum+=500;
break;
case 'c':if(s[i+1]!='d')
{
if(s[i-1]=='l')
sum+=50;
else
sum+=100;
}
break;
case 'l':if(s[i+1]!='c')
sum+=50;
break;
case 'x':if(s[i-1]=='i')
sum+=9;
else
sum+=10;
break;
case 'v':if(s[i-1]=='i')
sum+=4;
else
sum+=5;
break;
case 'i':if(s[i+1]!='x' || s[i+1]!='v')
sum+=1;
break;
default :cout<<"Invalid Roman numeral arises :";
}
i++;
}
cout<<"The equivalent Arabic integer is :"<<sum;
getch();
}
/* OUTPUT
------
Enter the roman numeral :mdlvi
The equivalent Arabic integer is :1556
*/
'C++' PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS
/* A 'C++' PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS */
#include<iostream.h>
#include<conio.h>
int gcd(int m,int n);
void main()
{
int m,n;
clrscr();
cout<<"Enter m,n values :";
cin>>m>>n;
cout<<"The G.C.D of given two numbers :"<<gcd(m,n);
getch();
}
int gcd(int m,int n)
{
if(m==n)
return m;
else
{
int gcd;
while(n!=0)
{
int d=m%n;
if(d==0)
gcd=n;
m=n;
n=d;
}
return gcd;
}
}
/* OUTPUT
------
Enter m,n values :6 8
The G.C.D of given two numbers :2
*/
#include<iostream.h>
#include<conio.h>
int gcd(int m,int n);
void main()
{
int m,n;
clrscr();
cout<<"Enter m,n values :";
cin>>m>>n;
cout<<"The G.C.D of given two numbers :"<<gcd(m,n);
getch();
}
int gcd(int m,int n)
{
if(m==n)
return m;
else
{
int gcd;
while(n!=0)
{
int d=m%n;
if(d==0)
gcd=n;
m=n;
n=d;
}
return gcd;
}
}
/* OUTPUT
------
Enter m,n values :6 8
The G.C.D of given two numbers :2
*/
'C++' PROGRAM TO IMPLEMENT MATRIX OPERATIONS USING OVERLOADED OPERATORS. (<<,>>,+,-,*)
/*A 'C++' PROGRAM TO IMPLEMENT MATRIX OPERATIONS USING OVERLOADED OPERATORS.
(<<,>>,+,-,*)
THE OPERATIONS SUPPORTED BY THIS ADT ARE:
a)READING OF A MATRIX b)PRINTING OF A MATRIX
c)ADDITION OF MATRICES d)SUBTRACTION OF MATRICES
e)MULTIPLICATION OF MATRICES
NAME :G.SanjeevaReddy
ROLLNO :09121F0026
BRANCH :M.C.A,II SEM A-SEC */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int m1,n1,m2,n2;
class matrix
{
int a[10][10],m,n;
public:
matrix(int a,int b)
{
m=a;
n=b;
}
friend istream & operator >>(istream &get,matrix &m);
friend ostream & operator <<(ostream &put,matrix &m);
matrix operator +(matrix);
matrix operator -(matrix);
matrix operator *(matrix);
};
istream & operator >>(istream &get,matrix &x)
{
for(int i=0;i<x.m;i++)
for(int j=0;j<x.n;j++)
get>>x.a[i][j];
return get;
}
ostream & operator <<(ostream &put,matrix &x)
{
for(int i=0;i<x.m;i++)
{
for(int j=0;j<x.n;j++)
put<<x.a[i][j]<<"\t";
put<<endl;
}
return put;
}
matrix matrix:: operator +(matrix x)
{
matrix c(m1,n1);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]+x.a[i][j];
return c;
}
matrix matrix:: operator -(matrix x)
{
matrix c(m1,n1);
c.m=m;
c.n=n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]-x.a[i][j];
return c;
}
matrix matrix:: operator *(matrix x)
{
matrix c(m1,n2);
for(int i=0;i<m1;i++)
for(int j=0;j<n2;j++)
{
c.a[i][j]=0;
for(int k=0;k<n1;k++)
c.a[i][j]+=(a[i][k]*x.a[k][j]);
}
return c;
}
void main()
{
clrscr();
cout<<"\nEnter the order of first matrix :";
cin>>m1>>n1;
cout<<"\nEnter the order of second matrix :";
cin>>m2>>n2;
matrix a(m1,n1),b(m2,n2),c(m1,n1),m(m1,n1),n(m1,n2);
if(m1==n2)
{
cout<<"\nEnter the elements of first matrix :";
cin>>a;
cout<<"\nEnter the elements of second matix :";
cin>>b;
c=a+b;
cout<<"\nThe first matrix A :\n";
cout<<a;
cout<<"\nThe second matrix B :\n";
cout<<b;
cout<<"\nThe resultant matrix after addition is :\n";
cout<<c;
m=a-b;
cout<<"\nThe resultant matrix after subtraction is :\n";
cout<<m;
n=a*b;
cout<<"\nThe resultant matrix after multiplication is :\n";
cout<<n;
}
else
cout<<"\nMatrix operation is not possible";
getch();
}
/* OUTPUT
------
Enter the order of first matrix :2
2
Enter the order of second matrix :2
2
Enter the elements of first matrix :1
2
3
4
Enter the elements of second matix :1
0
0
1
The first matrix A :
1 2
3 4
The second matrix B :
1 0
0 1
The resultant matrix after addition is :
2 2
3 5
The resultant matrix after subtraction is :
0 2
3 3
The resultant matrix after multiplication is :
1 2
3 4
*/
(<<,>>,+,-,*)
THE OPERATIONS SUPPORTED BY THIS ADT ARE:
a)READING OF A MATRIX b)PRINTING OF A MATRIX
c)ADDITION OF MATRICES d)SUBTRACTION OF MATRICES
e)MULTIPLICATION OF MATRICES
NAME :G.SanjeevaReddy
ROLLNO :09121F0026
BRANCH :M.C.A,II SEM A-SEC */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int m1,n1,m2,n2;
class matrix
{
int a[10][10],m,n;
public:
matrix(int a,int b)
{
m=a;
n=b;
}
friend istream & operator >>(istream &get,matrix &m);
friend ostream & operator <<(ostream &put,matrix &m);
matrix operator +(matrix);
matrix operator -(matrix);
matrix operator *(matrix);
};
istream & operator >>(istream &get,matrix &x)
{
for(int i=0;i<x.m;i++)
for(int j=0;j<x.n;j++)
get>>x.a[i][j];
return get;
}
ostream & operator <<(ostream &put,matrix &x)
{
for(int i=0;i<x.m;i++)
{
for(int j=0;j<x.n;j++)
put<<x.a[i][j]<<"\t";
put<<endl;
}
return put;
}
matrix matrix:: operator +(matrix x)
{
matrix c(m1,n1);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]+x.a[i][j];
return c;
}
matrix matrix:: operator -(matrix x)
{
matrix c(m1,n1);
c.m=m;
c.n=n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.a[i][j]=a[i][j]-x.a[i][j];
return c;
}
matrix matrix:: operator *(matrix x)
{
matrix c(m1,n2);
for(int i=0;i<m1;i++)
for(int j=0;j<n2;j++)
{
c.a[i][j]=0;
for(int k=0;k<n1;k++)
c.a[i][j]+=(a[i][k]*x.a[k][j]);
}
return c;
}
void main()
{
clrscr();
cout<<"\nEnter the order of first matrix :";
cin>>m1>>n1;
cout<<"\nEnter the order of second matrix :";
cin>>m2>>n2;
matrix a(m1,n1),b(m2,n2),c(m1,n1),m(m1,n1),n(m1,n2);
if(m1==n2)
{
cout<<"\nEnter the elements of first matrix :";
cin>>a;
cout<<"\nEnter the elements of second matix :";
cin>>b;
c=a+b;
cout<<"\nThe first matrix A :\n";
cout<<a;
cout<<"\nThe second matrix B :\n";
cout<<b;
cout<<"\nThe resultant matrix after addition is :\n";
cout<<c;
m=a-b;
cout<<"\nThe resultant matrix after subtraction is :\n";
cout<<m;
n=a*b;
cout<<"\nThe resultant matrix after multiplication is :\n";
cout<<n;
}
else
cout<<"\nMatrix operation is not possible";
getch();
}
/* OUTPUT
------
Enter the order of first matrix :2
2
Enter the order of second matrix :2
2
Enter the elements of first matrix :1
2
3
4
Enter the elements of second matix :1
0
0
1
The first matrix A :
1 2
3 4
The second matrix B :
1 0
0 1
The resultant matrix after addition is :
2 2
3 5
The resultant matrix after subtraction is :
0 2
3 3
The resultant matrix after multiplication is :
1 2
3 4
*/
C++ PROGRAM TO MAKE THE FREQUENCY COUNT OF WORDS IN A GIVEN TEXT
/*A C++ PROGRAM TO MAKE THE FREQUENCY COUNT OF WORDS IN A GIVEN TEXT*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
char temp[30][30];
int check(char x[],int n)
{
for(int i=0;i<n;i++)
{
if(strcmp(temp[i],x)==0)
return 0;
}
return 1;
}
void main()
{
char a[100],b[20],ch,c[10][10];
int i=0,j,n=0,k=0,count,m;
clrscr();
cout<<"\n Enter any string and end with $::";
cin.get(ch);
do
{
a[n]=ch;
n++;
cin.get(ch);
}while(ch!='$');
a[n]='\0';
for(;;)
{
for(j=0;(a[i]!=' ')&&(a[i]!='\0');j++)
{
b[j]=a[i];
i++;
}
b[j]='\0';
strcpy(c[k],b);
k++;
if(a[i]==0)
break;
i++;
}
cout<<"\n\n------------------------------------\n";
cout<<"\n\tword\tFrequency\n";
cout<<"\n\n------------------------------------\n";
for(i=0;i<k;i++)
{
count=1;
m=check(c[i],i);
for(j=i+1;j<k;j++)
{
if(strcmp(c[i],c[j])==0 && m==1)
count++;
}
if(m==1)
{
strcpy(temp[i],c[i]);
cout<<"\t"<<c[i]<<"\t"<<count<<endl;
}
}
cout<<"\n\n------------------------------------\n";
getch();
}
/* OUTPUT
------
Enter any string and end with $::he is studying in svec and he is a good boy$
------------------------------------
word Frequency
------------------------------------
he 2
is 2
studying 2
in 1
svec 1
and 1
a 1
good 1
boy 1
------------------------------------
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
char temp[30][30];
int check(char x[],int n)
{
for(int i=0;i<n;i++)
{
if(strcmp(temp[i],x)==0)
return 0;
}
return 1;
}
void main()
{
char a[100],b[20],ch,c[10][10];
int i=0,j,n=0,k=0,count,m;
clrscr();
cout<<"\n Enter any string and end with $::";
cin.get(ch);
do
{
a[n]=ch;
n++;
cin.get(ch);
}while(ch!='$');
a[n]='\0';
for(;;)
{
for(j=0;(a[i]!=' ')&&(a[i]!='\0');j++)
{
b[j]=a[i];
i++;
}
b[j]='\0';
strcpy(c[k],b);
k++;
if(a[i]==0)
break;
i++;
}
cout<<"\n\n------------------------------------\n";
cout<<"\n\tword\tFrequency\n";
cout<<"\n\n------------------------------------\n";
for(i=0;i<k;i++)
{
count=1;
m=check(c[i],i);
for(j=i+1;j<k;j++)
{
if(strcmp(c[i],c[j])==0 && m==1)
count++;
}
if(m==1)
{
strcpy(temp[i],c[i]);
cout<<"\t"<<c[i]<<"\t"<<count<<endl;
}
}
cout<<"\n\n------------------------------------\n";
getch();
}
/* OUTPUT
------
Enter any string and end with $::he is studying in svec and he is a good boy$
------------------------------------
word Frequency
------------------------------------
he 2
is 2
studying 2
in 1
svec 1
and 1
a 1
good 1
boy 1
------------------------------------
*/
'C++' PROGRAM TO FIND Nth FIBONACCI NUMBER USING RECURSIVE FUNCTIONS
/* A 'C++' PROGRAM TO FIND Nth FIBONACCI NUMBER USING RECURSIVE FUNCTIONS*/
#include<iostream.h>
#include<conio.h>
int c;
int fibo(int n,int a,int b)
{
if(n-2!=0)
{
c=a+b;
fibo(--n,b,c);
return c;
}
}
void main()
{
clrscr();
cout<<"Enter n value:";
int n;
cin>>n;
if(n==1)
cout<<"\nThe First fibonacci number is :0";
else if(n==2)
cout<<"\nThe second fibonacci number is :1";
else
cout<<"\nThe fibonacci number "<<n<<" is :"<<fibo(n,0,1);
getch();
}
/* OUTPUT
------
Enter n value:10
The fibonacci number 10 is :34
*/
#include<iostream.h>
#include<conio.h>
int c;
int fibo(int n,int a,int b)
{
if(n-2!=0)
{
c=a+b;
fibo(--n,b,c);
return c;
}
}
void main()
{
clrscr();
cout<<"Enter n value:";
int n;
cin>>n;
if(n==1)
cout<<"\nThe First fibonacci number is :0";
else if(n==2)
cout<<"\nThe second fibonacci number is :1";
else
cout<<"\nThe fibonacci number "<<n<<" is :"<<fibo(n,0,1);
getch();
}
/* OUTPUT
------
Enter n value:10
The fibonacci number 10 is :34
*/
'C++' PROGRAM TO SOLVE THE TOWERS OF HANOI PROBLEM USING RECURSIVE FUNCTIONS
/* A 'C++' PROGRAM TO SOLVE THE TOWERS OF HANOI PROBLEM USING RECURSIVE FUNCTIONS
*/
#include<iostream.h>
#include<conio.h>
void hanoi(int n,char s,char t,char d)
{
if(n!=0)
{
hanoi(n-1,s,d,t);
cout<<"\nMOVE DISK "<<n<<" FROM "<<s<<" TO "<<d;
hanoi(n-1,t,s,d);
}
}
void main()
{
int n;
char s='A',t='B',d='C';
clrscr();
cout<<"Enter how many disks :";
cin>>n;
cout<<"\t\t\tTOWERS OF HANOI"<<"\n\t\t\t---------------";
hanoi(n,s,t,d);
getch();
}
/* OUTPUT
------
Enter how many disks :3
TOWERS OF HANOI
---------------
MOVE DISK 1 FROM A TO C
MOVE DISK 2 FROM A TO B
MOVE DISK 1 FROM C TO B
MOVE DISK 3 FROM A TO C
MOVE DISK 1 FROM B TO A
MOVE DISK 2 FROM B TO C
MOVE DISK 1 FROM A TO C
*/
*/
#include<iostream.h>
#include<conio.h>
void hanoi(int n,char s,char t,char d)
{
if(n!=0)
{
hanoi(n-1,s,d,t);
cout<<"\nMOVE DISK "<<n<<" FROM "<<s<<" TO "<<d;
hanoi(n-1,t,s,d);
}
}
void main()
{
int n;
char s='A',t='B',d='C';
clrscr();
cout<<"Enter how many disks :";
cin>>n;
cout<<"\t\t\tTOWERS OF HANOI"<<"\n\t\t\t---------------";
hanoi(n,s,t,d);
getch();
}
/* OUTPUT
------
Enter how many disks :3
TOWERS OF HANOI
---------------
MOVE DISK 1 FROM A TO C
MOVE DISK 2 FROM A TO B
MOVE DISK 1 FROM C TO B
MOVE DISK 3 FROM A TO C
MOVE DISK 1 FROM B TO A
MOVE DISK 2 FROM B TO C
MOVE DISK 1 FROM A TO C
*/
Subscribe to:
Posts (Atom)