Welcome To Uday Satya Blog

31-40 PROGRAMS



/**************************************************************
    31. To find factorial of a number using recursive function
***************************************************************/

#include
#include
int fact(int);
void main()
{
 int n,f;
 clrscr();
 cout<<"\n Enter The Number Whose Factorial Has To Be Found :"<
 cin>>n;
 f = fact(n);
 cout<<"\n The Factorial Of "<
 getch();
 }
  int fact(int n)
  {
   if(n==1)
   return(1);
   else
   return(n*fact(n-1));
  }
                                                                               
OUTPUT:-

 Enter The Number Whose Factorial Has To Be Found :
 5

The Factorial Of 5 is : 120





/****************************************************************
    32. Program to find gcd of 2 numbers using recursive function
******************************************************************/

#include
#include

int gcd(int n,int m)
            {
            if(m == 0)
                        return(n);
            else
                        return(gcd(m,n%m));
            }

void main()
            {
            int a,b,f;
            clrscr();
            cout<<"Enter a,b"<
            cin>>a>>b;
            f = gcd(a,b);
            cout<<"Gcd is : "<
            getch();
            }

OUTPUT :

Enter a  b
10  34

Gcd  is : 2







/****************************************************************
    33. To find sum of elements of a upper triangle in a matrix
*****************************************************************/

#include
#include
void main()
            {
            int a[3][3],s=0;
            clrscr();
            cout<<"Enter the elements of Matrix A"<
            for(int i=0;i<3;i++)
                        for(int j=0;j<3;j++)
                                    cin>>a[i][j];
            cout<<"Sum of Upper Triangle elements of A is : ";
            for(i = 0;i<3;i++)
                        for(j = 0;j<3;j++)
                                    if(i <= j)
                                                s = s + a[i][j];
            cout<
            getch();
            }


OUTPUT :

Enter the elements of Matrix A
1  2  3
4  5  6
7  8  9

Sum of Upper Triangle Elements of A is : 26







/**************************************************************
34. Program to find the greatest element in a matrix
***************************************************************/

#include
#include
void main()
            {
            int a[3][3],x;
            clrscr();
            cout<<"Enter the elements of Matrix A"<
            for(int i=0;i<3;i++)
                        for(int j=0;j<3;j++)
                                    cin>>a[i][j];
            x = a[0][0];
            cout<<"Greatest element of A is : ";
            for(i = 0;i<3;i++)
                        for(j = 0;j<3;j++)
                                    if(a[i][j] > x)
                                                x = a[i][j];
            cout<
            getch();
            }


OUTPUT :

Enter the Elements of Matrix A
1  2  3
4  5  6
7  8  9

Greatest Element of A is : 9






/*************************************************
        35. Program to find sum of 2 matrices
**************************************************/

#include
#include
void main()
{
 clrscr();
 int i,j;
 int ar[3][3],ar1[3][3],sum[3][3];
 cout<<"\n Enter The Elements To The First Matrix :"<

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cin>>ar[i][j];
 }
 cout<<"\n Enter The Elements To The Second Matrix :"<

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cin>>ar1[i][j];
  }
  for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   sum[i][j]=0;
   sum[i][j] = ar[i][j]+ar1[i][j];
   }
  }
  cout<<"\n\n The Sum Of The Two Matrices Is :"<
  for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cout<<"\t "<
  cout<<"\n";
  }
  getch();
 }

 OUTPUT :-

 Enter The Elements To The First Matrix :
 2 2 2 2 2 2 2 2 2

 Enter The Elements To The Second Matrix :
 3 3 3 3 3 3 3 3 3

 The Sum Of The Two Matrices Is :
             5       5       5
             5       5       5
             5       5       5























/*********************************************************
        36. Program to find the product of 2 matrices 
**********************************************************/

#include
#include
void main()
{
 clrscr();
 int i,j,k;
 int ar[3][3],ar1[3][3],prod[3][3];
 cout<<"\n Enter The Elements To The First Matrix :"<

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cin>>ar[i][j];
 }
 cout<<"\n Enter The Elements To The Second Matrix :"<

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cin>>ar1[i][j];
  }
  for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
  prod[i][j]=0;
  for(k=0;k<3;k++)
  {
  prod[i][j] = prod[i][j]+ar[i][k]*ar1[k][j];
   }
  }
  }
  cout<<"\n\n The Product Of The Two Matrices Is :"<
  for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cout<<"\t "<
  cout<<"\n";
  }
  getch();
 }

OUTPUT:-

 Enter The Elements To The First Matrix :
 1 2 3 4 5 6 7 8 9

 Enter The Elements To The Second Matrix :
 1 0 0 0 1 0 0 0 1

 The Product Of The Two Matrices Is :
             1       2       3
             4       5       6
             7       8       9




















/*********************************************************
      37. Program to find the transpose of a given matrix
**********************************************************/
#include
#include
void main()
{
 clrscr();
 int i,j;
 int ar[3][3],tra[3][3];
 cout<<"\n Enter The Elements To the Matrix :"<
 for(i=0;i<3;i++)
  for(j=0;j<3;j++)
                        cin>>ar[i][j];
   for(i=0;i<3;i++)
              for(j=0;j<3;j++)
                           tra[j][i] = ar[i][j];
  cout<<"\n\n The Transpose Of The Given Matrix Is :"<
  for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  cout<<"\t "<
  cout<<"\n ";
  }
  getch();
 }
                                                                                
OUTPUT:-

 Enter The Elements To The Matrix :
 1 2 3 4 5 6 7 8 9

 The Transpose Of The Given Matrix Is :
             1       4       7
             2       5       8
             3       6       9



/******************************************************
         38. Program to find trace of a given matrix
********************************************************/
#include
#include
void main()
{
 clrscr();
 int i,j;
 int ar[3][3],sum=0;
 cout<<"\n Enter The Elements To The Matrix :"<
 for(i=0;i<3;i++)
  for(j=0;j<3;j++)
                        cin>>ar[i][j];
  cout<<"\n The Trace Elements Are : ";
  for(i=0;i<3;i++)
  for(j=0;j<3;j++)
            if(i==j)
            {
cout<<"\n\t "<
            sum = sum + ar[i][j];
            }
  cout<<"\n\n The Trace Of The Given Matrix Is = "<
  getch();
 }

OUTPUT:-

 Enter The Elements To The Matrix :
 1 2 3 4 5 6 7 8 9

 The Trace Elements Are :
             1
             5
             9

 The Trace Of The Given Matrix Is = 45


/************************************************************
          39. To print student's mark sheet for n students
*************************************************************/

#include
#include
struct student
                        {
                        char name[10];
                        int roll,m1,m2,m3,m4,m5,total;
                        float avg;
                         }s[20];
void main()
                         {
                         int i,n;
                         clrscr();
                         cout<<"how many students are there?"<
                         cin>>n;
                         cout<<"enter the student's details:"<
                         for(i=0;i
                         {
                         cout<<"enter the name ,roll and marks"< 
                                     cin>>s[i].name>>s[i].roll>>s[i].m1>>s[i].m2>>s[i].m3>>s[i].m4>>s[i].m5;
                         }
                         for(i=0;i
                        {
                        s[i].total=(s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5);
                        s[i].avg=s[i].total/5;
                        }
                        cout<<"      STUDENT'S MARKS SHEET"<
                         cout<<"NAME    "<<"   ROLL"<<"  M1"<<"  M2"<<"                                                                                 M3"<<"  M4"<<"  M5"<<"  TOTAL"<<"   AVG"<
                        for(i=0;i
                         {
cout<    "<  "<  "<<
s[i].m2<<"  "<  "<< s[i].m4<<"  "<
<<" "<    "<
                                     cout<<"\n";
                         }
                         getch();
                         }




Output:

how many students are there?
2
enter the student’s details:
enter the name,roll and marks of the first student
Satya
34
57
76
78
89
78
enter the name , roll and marks of next students
Satish
43
67
76
78
87
86

         STUDENT”S MARK  SHEET

NAME        ROLL    M1     M2     M3      M4      M5      TOTAL      AVG
Satya             34         57      67      78      89       78         369          73
Satish            43         67      76      78      87       86          385         77




/*************************************************************
    40. Using inline function to find greatest of two numbers
**************************************************************/

#include
#include
inline int great(int a,int b)
 {
  if(a>b)
  return(a);
  else
  return(b);
 }
 void main()
 {
  int a,b;
  clrscr();
  cout<<"\n Enter Any Two Numbers : "<
  cin>>a>>b;
  cout<<"\n The Greatest Of Two Numbers Is = "<
  getch();
 }

OUTPUT:-

 Enter Any Two Numbers :
 89 56

 The Greatest Of Two Numbers Is = 89


0 comments:

Post a Comment