Monday, 18 May 2015

Write a program in c++ which read two matrix and then print a matrix which is addition of these two matrix.

Coding:- 

                    #include<iostream.h>

void main()

{
 int a[2][3],b[2][3],c[2][3];

 int i,j;

 cout<<"enter values in first matrix  \n";
 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
cin>>a[i][j];


 }
cout<<"the matrix is : \n";

 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
 cout<<"  "<<a[i][j];

 cout<<"\n";


 }
   
 cout<<"enter values in 2nd matrix  \n";
 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
cin>>b[i][j];


 }
cout<<"the matrix is : \n";

 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
 cout<<"  "<<b[i][j];

 cout<<"\n";


 }
           for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)

 c[i][j]=a[i][j]+b[i][j];

cout<<"\n";

}

         cout<<"the sum of two matrix is: = \n" ;

         for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
 cout<<"  "<<c[i][j];

 cout<<"\n";
} }


Output:-   

                                 

Write a program in c++ to input the value in two dimensional array and print all the elements in matrix form.

Coding:- 

                         

#include<iostream.h>

void main()

{
 int a[2][4];

 int i,j;

 cout<<"enter values in an array  \n";
 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
cin>>a[i][j];


 }
         cout<<"the array is : \n";

     for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
 cout<<"  "<<a[i][j];

 cout<<"\n";


 }
}

Output:- 

                   

Write a program in c++ which initialize a two dimensional array and print all the elements in matrix form.

Coding:- 

  

#include<iostream.h>

void main()

{
 int a[2][3]={1,2,3,4,5,6,7,8};

 int i,j;

 cout<<"the array is : \n";
 for(i=0;i<=1;i++)
 {

for(j=0;j<=2;j++)
cout<<"  "<<a[i][j];
cout<<"\n";

 }

}

Output:-

              

Write a Program in c++ which read 6 no. from keyboard and store these no into array and then calculate sum of these no. using function.

Coding:- 

 #include<iostream.h>
 int sum (int a[])
 {
int n=0;
for(int i=0;i<=5;i++)
n=n+a[i];
return (n);
 }

 int output(int a[])

 {
cout<<"the elements of array are : "<< endl ;

for(int i=0;i<=5;i++)

cout<<a[i]<<endl;

 }

 int main()

 {
 int a[6],s,i;
 cout<<"enter 6 elements : ";
 for(i=0;i<=5;i++)
 cin>>a[i];

 output(a);

 cout<<"sum of array element is : ="<<sum(a)<<endl;


 }

Output:- 
   

Write a Program in c++to input the marks of five students in an array and then print all the marks.

Coding:- 

  #include<iostream.h>
void main()
{
 int marks[5] ;
int i;


 cout<<"enter marks of five students ";

 for(i=0;i<=4;i++)
 {
cin>>marks[i];
 }


cout<<"the marks of students are: \n";

 for(i=0;i<=4;i++)

 cout<<"marks of " << i+1<<"student is :" << marks[i]<<endl;


}

Output:- 

                       

Write a Program in c++ which store the marks of five students in an array and then print all the marks.

Coding:- 


    #include<iostream.h>
void main()
{
 int marks[5]= {50,60,70,80,90};
int i;

 cout<<"the marks of students are: \n";

 for(i=0;i<=4;i++)

 cout<<"marks of " << i+1<<"student is :" << marks[i]<<endl;


}

Output:- 

    

Write a Program in c++ for function Overloading.

Program:- Find the volume of cylinder, cube and rectangular box.

Coding:- 

                     #include<iostream.h>


float volume(float r, float h)
{
float vol;
vol= 3.14*(r*r)*h;
return vol;

}


float volume(float s)
{
float vol;
vol=s*s*s;
return vol;

}

float volume (float l, float b, float h)

{
float vol;
vol=l*b*h;
return vol;
}


void main()

{
float radius, side , length, breadth, height , vol;
int option;
 do{
cout<<" \n enter 1 for cylinder, 2 for cube, 3 for rectangular box and 4 for exit ";

cin>>option;

switch(option)
{
 case 1 :    cout<<"enter the value of radius and height of cylinder\n";
 cin>>radius>>height;
 vol=volume(radius, height);
 cout<< "volume = "<<vol<<" cubic units \n";
break;

case 2: cout<<"enter the value of side \n";
cin>>side;
vol=volume (side);
cout<<"\n volume  = "<<vol<<" cubic unit";
break;


 case 3: cout<<" enter the value of length, breadth and height ";
cin>>length>>breadth>>height;
vol=volume(length,breadth,height);
cout<<"\n volume  = "<<vol<<" cubic unit";
break;

default:  cout<<"invalid entry ";
break;

}


}  while (option>0 && option< 4);

}

Output:-