0% found this document useful (0 votes)
59 views

Matrix Void Main (CLRSCR

The document contains code examples demonstrating various C++ programming concepts including: 1) Calculating factorials and Fibonacci sequences with for loops. 2) Multiplying matrices by getting user input for matrix elements and dimensions then calculating product values. 3) Calculating average of user input numbers by storing in an array and summing. 4) Defining a class with methods to get and display student name and roll number data. 5) Defining a class with separate methods for getting and displaying person name and age data. 6) Overloading functions to calculate volume with different parameters. 7) Defining nested functions to get user input and find largest value in a class.

Uploaded by

Piyush Sood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Matrix Void Main (CLRSCR

The document contains code examples demonstrating various C++ programming concepts including: 1) Calculating factorials and Fibonacci sequences with for loops. 2) Multiplying matrices by getting user input for matrix elements and dimensions then calculating product values. 3) Calculating average of user input numbers by storing in an array and summing. 4) Defining a class with methods to get and display student name and roll number data. 5) Defining a class with separate methods for getting and displaying person name and age data. 6) Overloading functions to calculate volume with different parameters. 7) Defining nested functions to get user input and find largest value in a class.

Uploaded by

Piyush Sood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FACTORIAL #include "iostream.h" #include "conio.

h" void main() { clrscr(); int n,fact=1; cout<<"Enter the number:"<<endl; cin>>n; for(int i=1;i<=n;i++) { fact=fact*i; } cout<<"Factorial of the number is:"<<fact; getch(); }

FIBONACCI #include "iostream.h" #include "conio.h" void main() { clrscr(); int n,c; cout<<"Enter the number of elements:"; cin>>n; int a,b; cout<<"Enter the 2 numbers where you want to start:"; cin>>a>>b; cout<<a<<"\t"<<b<<"\t"; for(int i=0;i<n-2;i++) { c=a+b; a=b; b=c; cout<<c<<"\t"; } getch(); }

MATRIX void main() { clrscr();

int a[10][10],b[10][10],r1,c1,r2,c2,c[10][10]; cout<<"Enter the number of rows and columns of Matrix 1:"; cin>>r1>>c1; cout<<"Enter the number of rows and columns of Marix 2:"; cin>>r2>>c2; if(c1!=r2) { cout<<"Matrices cannot be multiplied"; exit(0); } cout<<"Enter the elements of Matrix 1:"<<endl; for(int i=1;i<=r1;i++) { for(int j=1;j<=c1;j++) { cout<<"Enter the element a["<<i<<j<<"]:"; cin>>a[i][j]; } } cout<<"Enter the elements of Matrix 2:"<<endl; for(i=1;i<=r2;i++) { for(int j=1;j<=c2;j++) { cout<<"Enter the element b["<<i<<j<<"]:";

cin>>b[i][j]; } } for(i=1;i<=r2;i++) { for(int j=1;j<=c2;j++) { for(int k=1;k<=r2;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } cout<<"Matrix after multiplication is:"<<endl; for(i=1;i<=r1;i++) { for(int j=1;j<=c2;j++) { cout<<c[i][j]<<"\t"; } cout<<endl; } getch(); }

Average
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0; cout<<"Enter the number of elements for average:"; cin>>n; int num[100]; for(int i=1;i<=n;i++) { cout<<"Enter element "<<i<<":"<<endl; cin>>num[i]; sum=sum+num[i]; } cout<<"Average of the numbers entered is:"<<(sum/n); getch(); }

Class intRo #include<iostream.h> #include<conio.h> class p { char name[20]; int rollno; public: void getdata(); void putdata(); }; void p::getdata(void) { cout<<"Enter the name and rollno:"; cin>>name>>rollno;

} void p::putdata(void) { cout<<"Name:"<<name<<endl<<"RollNo."<<rollno; } void main() { clrscr(); p obj; obj.getdata(); obj.putdata(); getch(); }

5.one function in and out #include<iostream.h> #include<conio.h> class person { char name[30]; int age; public: void getdata(void) { cout<<"Enter name:"; cin>>name; cout<<"Enter age:"; cin>>age; } void display(void); }; void person::display(void) { cout<<endl<<"Name:"<<name; cout<<endl<<"Age:"<<age; } int main() {

person p; p.getdata(); p.display(); return(0); }

6.Function overloading
#include<iostream.h> #include<conio.h> int volume(int); double volume(double,int); long volume(long,double,int); int main() { cout<<volume(10)<<endl; cout<<volume(25,8)<<endl; cout<<volume(100,7.5,15)<<endl; return(0); } int volume(int s) { return(s*s*s); } double volume(double r,int h) { return(3.1459*r*r*h); } long volume(long l,int b,int h) { return(l*b*h); }

7.Nested functions
#include<iostream.h> #include<conio.h> class set { int m,n; public: void input(void); void display(void); int largest(void); }; int set::largest(void) { if(m>=n) return(m); else

return(n); } void set::input(void) { cout<<"Input value of m & n"<<endl; cin>>m>>n; } void set::display(void) { cout<<"Largest value:"<<largest(); } void main() { set s; s.input(); s.display(); s.largest(); }

9.inline
#include<iostream.h> #include<conio.h> inline int mult(int x,int y) { return(x*y); } inline float div(float x,float y) { return(x/y); } int main() { int a,b; clrscr(); cout<<"Enter a:"; cin>>a; cout<<"Enter b:"; cin>>b; cout<<"Product is:"<<mult(a,b)<<endl; cout<<"Quoteint is:"<<div(a,b)<<endl; getch(); }

You might also like