0% found this document useful (0 votes)
38 views17 pages

C++ Program

The document contains C++ code to perform matrix addition, multiplication and pattern generation using classes and functions. It defines a Matrix class with private data members to store matrix elements and public member functions for input, calculation and output of matrices. The main() function creates a Matrix object and calls its member functions to input two matrices, perform addition/multiplication and display the results. It also includes functions to generate patterns like the Fibonacci series and factorial of a number using recursion. One function copies a string into another array. The code provides sample output for each program.

Uploaded by

helpdeskbillldh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

C++ Program

The document contains C++ code to perform matrix addition, multiplication and pattern generation using classes and functions. It defines a Matrix class with private data members to store matrix elements and public member functions for input, calculation and output of matrices. The main() function creates a Matrix object and calls its member functions to input two matrices, perform addition/multiplication and display the results. It also includes functions to generate patterns like the Fibonacci series and factorial of a number using recursion. One function copies a string into another array. The code provides sample output for each program.

Uploaded by

helpdeskbillldh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

10(a) A PROGRAM FOR ADDITION OF TWO MATRICS.

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int a[3][3],b[3][3],c[3][3],i,j;
public:
void input();
void calculate();
void disp();
};
void matrix::input()
{
cout<<"Enter First Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter Second Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}
}
void matrix::calculate()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrix::disp()
{
cout<<"\nUr First Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
cout<<"\nUr Second Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
cout<<"\nUr Addition of Both Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<endl;
}
}
void main()
{
matrix m;
clrscr();
m.input();
m.calculate();
m.disp();
getch();
}
/*

OUTPUT:

UR FIRST MATRIX.

1 2 3
4 5 6
7 8 9

UR SECEND MATRIX.

9 8 7
6 5 4
3 2 1

UR SECEND OF BOTH MATRIX.

10 10 10
10 10 10
10 10 10 */
11. W.A.P TO DO BINARY OPERATOR OVERLOAD USING AIRTHMETIC OPERATER.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *n;
public:
string();
string(char *);
void get();
void show();
string operator +(string);
};
string::string()
{
n=new char[1];
}
string::string(char *a)
{
int i=strlen(a);
n=new char[i+1];
strcpy(n,a);
}
string string::operator +(string s)
{
int i=strlen(n)+strlen(s.n);
string tmp;
tmp.n=new char[i+1];
strcpy(tmp.n,n);
strcat(tmp.n,s.n);
return tmp.n;
}
void string::get()
{
static int i;
if(i>0)
{
cout<<"Enter String: ";
cin.get();
cin.get(n,20);
}
else
{
cout<<"Enter String: ";
cin.get(n,20);
i++;
}
}
void string::show()
{
cout<<"\n\nString: "<<n;
}
void main()
{
clrscr();
string s;
string s1,s2;
s1.get();
s2.get();
s=s1+s2;
s.show();
getch();
}

/*
OUTPUT:
ENTER STRING NAN
ENTER STRING NA
STRING NANNA.*/
1. W.A.P WHICH READS A NO AND THEN PRINT THE MESSAGE FOR THAT NO IS EVEN OR ODD.

#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();

cout<<"enter the no.";


cin>>a;
if(a%2==0)
{
cout<<"the number is even";
}
else
{
cout<<"the numer is odd";
}

getch();

/*
output:

ENTER THE NO: 6


THE NUMBER IS EVEN.
ENTER THE NO: 9
THE NO IS ODD.
*/
6. W.A.P FOR CALCULTING THE FACTORIAL OF NUMBER USING RECURSION

#include<iostream.h>
#include<conio.h>
long int fact(int);
void main()
{
int n;
long int f;
clrscr();
cout<<"Enter Number: ";
cin>>n;
f=fact(n);
cout<<"\nThe Factorial Of Given Number Is: "<<f;
getch();
}
long int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}
/*
OUTPUT
ENTER ANY NUMBER: 6
THE FACTORIAL OF GIVEN NUMBER IS: 720
*/
4. W.A.P WHICH GENRATING THE FIBONACCI SERIES.

#include<iostream.h>
#include<conio.h>
void main()
{
int a=0,b=1,c=0,n,i;
clrscr();
cout<<"Enter Number Upto U Want To print The Series: ";
cin>>n;
cout<<"\n\nThe Fibonnaci Series Is: \n\n";
for(i=1;i<=n;i++)
{
cout<<"\t"<<c;
a=b;
b=c;
c=a+b;
}
getch();
}
/*

OUTPUT:
ENTER NUMBER UPTO U WANT TO PRINT THE SERIES. 8
THE FIBONACI SERIES IS: 0 1 1 2 3 5 8 13 */
5. W.A.P WHICH CALCULATE THE SUM OF SERIES USING FUNCTION.

#include<iostream.h>
#include<conio.h>
float series(float x,int n)
{
float s=0;
int i,j,f;
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
s=s+x/f;
}
}
return s;
}
void main()
{
float x,res;
int n;
clrscr();
cout<<"Enter N: ";
cin>>n;
cout<<"Enter The Value Of X: ";
cin>>x;
res=series(x,n);
cout<<"\n The Sum Of Series: "<<res;
getch();
}

/*
OUTPUT:

ENTER NO 5

ENTER THE VALUE X : 4*/


2. W.A.P WHICH CALCULATE THE FACTORIAL OF A GIVEN NO.

#include<iostream.h>
#include<conio.h>
void main()
{
long int n,i,fact=1;
clrscr();
cout<<"Enter The Number: ";
cin>>n;
for(i=n;i>=1;i--)
{
fact=fact*i;
}
cout<<"\n\n\t The Factorial Of A Given No Is: "<<fact;
getch();
}
/*
OUTPUT:
ENTER THE NUMBER. 6
THE FACTORIAL IS 720.*/
7. W.A.P TO READ THE MARK OF STUDENT AND THEN PRINT MARK SHEET USING ARRAY.

#include<iostream.h>
#include<conio.h>
void main()
{
char n[20],course[20];
int age,rn,m[4],t=0,i;
float avg;
clrscr();
cout<<"Enter The Name of Student: ";
cin.get(n,20);
cout<<"Enter The Course Of Student: ";
cin.get();
cin.get(course,20);
cout<<"Enter The Age Of Student: ";
cin>>age;
cout<<"Enter The Roll No: ";
cin>>rn;
cout<<"Enter Marks In Four Subjects: ";
for(i=0;i<4;i++)
{
cin>>m[i];
t=t+m[i];
}

avg=float(t)/4;
cout<<"\nThe Name Is: "<<n;
cout<<"\nThe Course Is: "<<course;
cout<<"\nThe Total Marks Are: "<<t;
cout<<"\nThe Average Is: "<<avg;
getch();
}

/*
OUTPUT:
ENTER THE NAME OF STUDENT NITIN SHARMA.
COURSE OF STUDENT ELECTRICAL ENGG.
AGE OF STODENT 18.
ROLL NO LEET-08.
ENTER MARKS IN FOUR SUBJECT 65,75,85,70.

THE NAME IS NITIN SHARMA.


COURSE IS ELECTRICAL ENGG.
TOTAL MARKS 295.
AVERAGE 73

/*
10(c) W.A.P FOR MULTIPLICATION OF TWO MATRICS USING CLASSES.

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int a[3][3],b[3][3],c[3][3],i,j;
public:
void input();
void calculate();
void disp();
};
void matrix::input()
{
cout<<"Enter First Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter Second Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}
}
void matrix::calculate()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}
}
void matrix::disp()
{
cout<<"\nUr First Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
cout<<"\nUr Second Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
cout<<"\nUr Multiplication of Both Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<endl;
}
}
void main()
{
matrix m;
clrscr();
m.input();
m.calculate();
m.disp();
getch();
}

/*
OUTPUT:

UR FIRST MATRIX:

7 6 5
4 3 2
1 1 1

UR SECEND MATRIX:

1 2 3
4 5 6
4 5 5

UR MULTIPLICATIONOF BOTH MATRIX:

7 12 15
16 15 12
4 5 5
/*
3. W.A.P For GENRATING A PATTERN

#include<iostream.h>
#include<conio.h>
void main()
{
int i,b;
clrscr();
cout<<"\n\n\t Ur Series Is: \n\n";
for(i=1;i<=5;i++)
{
for(b=1;b<=i;b++)
{
cout<<"\t"<<b;
}
cout<<endl;
}
getch();
}

/*
OUTPUT:

UR SERIES IS:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
/*
8. W.A.P WHICH READS A STRING AND THEN COPY THAT STRING INTO ANOTHER STRING.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char n[20],s[20];
clrscr();
cout<<"Enter String: ";
cin.get(n,20);
strcpy(s,n);
cout<<"\nThe Copied String: "<<s;
getch();
}

/*
OUTPUT:

ENTER STRING NITIN SHARMA RIKHI.

THE COPY STRING NITIN SHARMA RIKHI.


/*
10(b) W.A.P FOR SUBTRACTION OF TWO MATRICS USING CLASSES

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int a[3][3],b[3][3],c[3][3],i,j;
public:
void input();
void calculate();
void disp();
};
void matrix::input()
{
cout<<"Enter First Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter Second Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}
}
void matrix::calculate()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
}
void matrix::disp()
{
cout<<"\nUr First Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
cout<<"\nUr Second Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
cout<<"\nUr Subtraction of Both Matrix: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<endl;
}
}
void main()
{
matrix m;
clrscr();
m.input();
m.calculate();
m.disp();
getch();
}

/*
OUTPUT:

UR FIRST MATRIX:

7 6 5
4 3 2
3 1 2

UR SECEND MATRIX:

1 2 3
4 2 1
0 1 1

UR SUBTRACTION OF BOTH MATRIX:

6 4 2
0 1 1
3 0 1
/*
9. W.A.P WHICH READ THE SUM OF TWO NUMBER AND THEN PRINT SUM USING CLASS

#include<iostream.h>
#include<conio.h>
class simple
{
private:
int a,b,c;
public:
simple()
{
a=0;
b=0;
c=0;
}
void input()
{
cout<<"Enter Two Numbers: ";
cin>>a>>b;
}
void sum()
{
c=a+b;
}
void disp()
{
cout<<"\nThe A Is: "<<a;
cout<<"\nThe B Is: "<<b;
cout<<"\nThe Sum Of Both: "<<c;

}
};
void main()
{
simple s;
clrscr();
s.input();
s.sum();
s.disp();
getch();
}

/*OUTPUT:

ENTER TWO NUMBER 33,22.

A IS 33.

B IS 22.

SUM OF BOTH 55.

*/

You might also like