Most Commonly Asked Programs In C++ Language

Most Commonly Asked Programs In C++ Language


C Program To Print An Array Using Pointer.

Posted: 09 Jun 2013 01:25 AM PDT

This Post Contains A C Program To Print An Array Using Pointer With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays' & 'Pointers' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
  


  • A linear array is a list of finite number 'n' of homogeneous data elements.
  • The elements of array are referenced by index set consisting of 'n' consecutive numbers.
  • The elements of array are stored respectively in successive memory locations.
  • The number 'n' of elements is called as 'length' or 'size' of the array.
  • Length Of Array = UB - LB + 1.
          Where UB = largest index i.e. upper bound.
                     LB = smallest index i.e. lower bound.
  • The elements of array A may be denoted by subscript notation.
          A1A2A3,  .......... An
  • But in languages generally bracket notation is used.
          A[0], A[1], A[2], .......... A[n]

   
C Program To Print An Array Using Pointer.

/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10], *ptr;
clrscr();

/* Asking For The Input From User */
printf(" \n Please Enter 10 Numbers For An Array 'A' : ");
for(i=0; i<10; i++)
{
   scanf("%d", &a[i]);
}

/* Printing The Output Onto The Screen/Console */
ptr=&a;                                 // Or You Can Also Denote ptr=&a[0];
printf(" \n The Entered Numbers Are : \n ");
for(i=0; i<10; i++)
{
   printf("%d \t", *ptr);
   ptr++;
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

C Program To Print An Array Onto Screen/Console.

/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10];
clrscr();

/* Asking For The Input From User */
printf(" \n Please Enter 10 Numbers For An Array 'A' : ");
for(i=0; i<10; i++)
{
   scanf("%d", &a[i]);
}

/* Printing The Output Onto The Screen/Console */
printf(" \n The Entered Numbers Are : \n ");
for(i=0; i<10; i++)
{
   printf("%d \t", a[i]);
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10

C Program For Conversion Of Decimal No Into Octal No.

Posted: 09 Jun 2013 12:38 AM PDT

This Post Contains A C Program For Conversion Of Decimal No Into Octal No With Correct Source Code & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program For Conversion Of Decimal No Into Octal No.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{

   /* Declaration Of Variables */
   long Num;
   int   r,   q,   o,   a[ 20 ], b[ 20 ];
   clrscr();

   /* Asking For The Input From User */
   printf(" \n Enter Any No. : ");
   scanf("%d", &Num);

   /* Source Code For Computing Conversion Of Decimal Into Octal */
   o   =   0;
   while (   Num   >   0   )
   {
     if  (  Num  <  8  )
     {
       a[ o++ ]   =   Num;
       break;
     }
     else
     {
       a[ o++ ]   =   Num   %   8 ;
       Num   =   Num  /  8 ;
     }
   }
   r  =  0;
   for  (  q  =  0 - 1;  q  >=  0;  q--  )
   {
     b[ r++ ]  =  a[ q ] ;
   }

   /* Printing The Output On Console/Screen */
   printf(" \n Octal No. :  ");
   for  (  q  =  0;  q  <  r;  q++  )
   {
     printf("%d", b[ q ]);
   }
   getch();
}
/* End Of Main Program */ 

C Program For Conversion Of Decimal No Into Binary No.

Posted: 09 Jun 2013 12:31 AM PDT

This Post Contains A C Program For Conversion Of Decimal No Into Binary No. With Correct Source Code & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-Loop' & 'While-Loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

 # Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program For Conversion Of Decimal No Into Binary No.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
 
   /* Declaration Of Variables */
   int Num, i, j, k, a[20], b[20];
   clrscr();

   /* Asking For The Input From User */
   printf(" \n Enter Any No. : ");
   scanf("%d", &Num);
 
   /* Source Code For Conversion Of Decimal No Into Binary No */
   i = 0;
   while ( Num > 0 )
  {
     a[i] = Num % 2;
     i++;
     Num = Num / 2;
   }
   k = i - 1;
   printf("\n\n");
   for ( j = 0; j < i; j++ )
   {
     b[j] = a[k--];
     printf("%d", b[j]);
   }
   getch();
}
/* End Of Main Program */  

C Program For Conversion Of Binary No. Into Decimal No.

Posted: 09 Jun 2013 12:24 AM PDT

This Post Contains A C Program For Conversion Of Binary No. Into Decimal No With Correct Source Code & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays' & 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program For Conversion Of Binary No Into Decimal No.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>
# include <math.h>

/* Start Of Main Program */
void main()
{
 
   /* Declaration Of Variables */
   int p, q, r, b[20];
   long Num;
   clrscr();
 
   /* Asking For The Input From User */
   printf(" \n Enter Any Binary Number : ");
   scanf("%d", &Num);
 
   /* Source Code For Computing Conversion Of Binary Into Decimal */
   p = 0;
   while ( Num > 0 )
   {
     b[p++] = Num % 10;
     Num = Num / 10;
   }
   q = p - 1;
   r = 0;
   while ( q >= 0 )
   {
     r = r + ( b[q] * pow ( 2 , q ) );
     q--;
   }
   printf(" \n Decimal No. Of Given Binary No. Is : %d ", r);
   getch();
}
/* End Of Main Program */     

C Program To Find Given Number As Binary Or Not.

Posted: 09 Jun 2013 12:18 AM PDT

This Post Contains A C Program To Find Given Number As Binary Or Not With Correct Source Code & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find Given Number As Binary Or Not.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
 
   /* Declaration Of Variables */
   int r, c;
   long n, t;
   clrscr();
 
   /* Asking For The Input From User */
   printf(" \n Enter Any Number : ");
   scanf( "%d", &n );

   /* Source Code For Computing Binary Number */
   t = n;   c = r = 0;
   while (  n  >  0  )
   {
     if ( n % 10 == 0 || n % 10 == 1 )
     {
       c++;
       r++;
       n = n / 10;
     }
   }
   if ( c == r)
     printf(" \n %d Is Binary ", t);
   else
     printf(" \n %d Is Not Binary ", t);
   getch();
}
/* End Of Main Program */      

C Program To Find LCM & GCD Of Integers/Numbers.

Posted: 09 Jun 2013 12:10 AM PDT

LCM: Least Common Multiple.

In Arithmetic, The LCM: Least Common Multiple[Also Called As 'Lowest Common Multiple'] Of Two Integers a & b, Usually Denoted By LCM[a,b], Is The Smallest Positive Integer That Is Divisible By Both a & b. If Either a Or b Is '0' [Zero], LCM[a,b] Is '0' [Zero].

Overview

A Multiple Of A Number Is The Product Of That Number & An Integer. For Ex, 10 Is A Multiple Of 5 Because 5 x 2 = 10, So 10 Is Divisible By 5 & 2. Because 10 Is The Smallest Positive Integer That Is Divisible By Both 5 & 2, It Is The Least Common Multiple Of 5 & 2. By The Same Principle, 10 Is The Least Common Multiple Of -5 & 2 As Well.

Example

What Is The LCM Of 4 & 6?

Multiples Of 4 Are:
  
   4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, ...

and the multiples of 6 are:
  
   6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...

Common multiples of 4 and 6 are simply the numbers that are in both lists:

   12, 24, 36, 48, 60, 72, ....

So, from this list of the first few common multiples of the numbers 4 and 6, their least common multiple is 12.

Application

When adding, subtracting, or comparing vulgar fractions, it is useful to find the least common multiple of the
denominators, often called the lowest common denominator, because each of the fractions can be expressed as a
fraction with this denominator. For instance,

[2\21] + [1\6] = [4\42] + [7\42] = [11\42]

where the denominator 42 was used because it is the least common multiple of 21 and 6.

This Post Contains A C Program To Find LCM & GCD Of Integers/Numbers With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Cascading Of Operators' & 'Functions' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find LCM & GCD Of Integers/Numbers.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>
int gcd ( int , int );      // Function Declaration.
void lcm ( int , int );   // Function Declaration.

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
   int a, b;
   clrscr();

/* Asking For The Input From User */
   printf(" \n Enter Two Nos : ");
   scanf("%d %d", &a, &b);
   if (  a  <  b  )
   {
      printf(" \n GCD Of %d & %d Is : %d",a, b, gcd (a , b));    // Function Call.
   }
   else
   {
      printf(" \n GCD Of %d & %d Is : %d",a, b, gcd (a , b));   // Function Call.
   }
   lcm (a , b);    // Function Call.
   getch();
   }
/* End Of Main Program */

   int gcd ( int c , int d )     // Function Definition.
   {
      int r;
      r = d % c;
      while ( r  !=  0  )
      {
         d = c;
         c = r;
         r = d % c;
      }
      return (c);
   }
void lcm ( int x , int y )     // Function Definition.
{
   int l;
   if ( x < y )
   {
     l = ( x * y ) / gcd ( x , y );
   }
   else
   {
     l = ( x * y ) / gcd ( y , x );
   }
   printf(" \n LCM Of %d & %d Is : %d", a, b, l);
}

GCD: Greatest Common Divisor

In mathematics, the GCD: Greatest Common Divisor[also known as the 'Greatest Common Factor (GCF)', or 'Highest Common Factor (HCF)'], of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

Notation

In this article we will denote the greatest common divisor of two integers a and b as gcd(a,b). Some older textbooks use (a,b).

Example

The number 54 can be expressed as a product of two other integers in several different ways:

   54 x 1 = 27 x 2 = 18 x 3 = 9 x 6.

Thus the divisors of 54 are:

    1, 2, 3, 6, 9, 18, 27, 54.

Similarly the divisors of 24 are:

    1, 2, 3, 4, 6, 8, 12, 24.

The numbers that these two lists share in common are the common divisors of 54 and 24:

    1, 2, 3, 6.

The greatest of these is 6. That is the greatest common divisor of 54 and 24. One writes:

    gcd(54,24) = 6.

Reducing fractions

The greatest common divisor is useful for reducing fractions to be in lowest terms. For example, gcd(42, 56) = 14, therefore,

   {42\56} = {(3)(14) \ (4)(14)} = {3\4}.

Co-prime numbers

Two numbers are called relatively prime, or co-prime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.

C++ Program To Find & Print Largest & Smallest Element Of Matrix.

Posted: 08 Jun 2013 11:49 PM PDT

This Post Contains A C++ Program To Find & Print Largest & Smallest Element Of Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Find & Print Largest & Smallest Element Of Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
     
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

/* Source Code For Computing Largest & Smallest Element Of Matrix */
l=0;
s=a[0][0];
for(i=0; i<r; i++)
{
   for(j=0; j<c; j++)
   {
      if(l<a[i][j])
         l=a[i][j];
      if(a[i][j]<s)
         s=a[i][j];
   }
}

/* Printing The Output Onto The Screen/Console */   
cout<<" \n Largest Element Of Matrix Is : "<<l;     
cout<<" \n Smallest Element Of Matrix Is : "<<s;
getch();
}
/* End Of Main Program */ 


Output :


Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

Largest Element Of Matrix Is : 9

Smallest Element Of Matrix Is : 1

C++ Program To Find & Print Upper Right & Lower Left Triangles Of Matrix.

Posted: 08 Jun 2013 11:52 PM PDT

This Post Contains A C++ Program To Find & Print Upper Right & Lower Left Triangles Of Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Find & Print Upper Right & Lower Left Triangles Of Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
    
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

/* Source Code For Computing Upper Right & Lower Left Triangles Of Matrix If & Only If Rows & Columns Are Equal */
if(r==c)
{
   cout<<" \n Upper Right Triangle Of Matrix Is : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         if(j>=i)
         {
            cout<<a[i][j]<<"\t";
         }
         else
         {
            cout<<"\t";
         }
      }
   }

   cout<<" \n Lower Left Triangle Of Matrix Is : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         if(j<=i)
         {
            cout<<a[i][j]<<"\t";
         }
         else
         {
            cout<<"\t";
         }
      }
   }
}
else
{
   cout<<" \n Upper Right & Lower Left Triangles Of Matrix Are Not Possible";
}
getch();
}
/* End Of Main Program */


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

Upper Right Triangle Of Matrix Is :
1  2  3
    5  6
        9

Lower Left Triangle Of Matrix Is :

4  5 
7  8  9

C++ Program To Find & Print Sum Of Diagonal Elements Of Matrix.

Posted: 08 Jun 2013 11:52 PM PDT

This Post Contains A C++ Program To Find & Print Sum Of Diagonal Elements Of Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Find & Print Sum Of Diagonal Elements Of Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
    
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }
/* Source Code For Computing Sum Of Diagonal Elements If & Only If Rows & Columns Are Equal */
sum=0;
if(r==c)
{
   for(i=0; i<r; i++)
   {
      for(j=0; j<c; j++)
      {
         if(i+j==0 || i+j==2 || i+j==4)
         {
            sum=sum+a[i][j];
         }
      }
   }
   /* Printing The Output Onto The Screen/Console */
   cout<<"\n Sum Of Diagonal Elements Of Array Is : "<<sum;
}
else
{
   cout<<" \n Addition Is Not Possible";
}
getch();
}
/* End Of Main Program */ 


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

Sum Of Diagonal Elements Of Array Is : 15


C++ Program To Print Transpose Of A Matrix.

Posted: 08 Jun 2013 11:53 PM PDT

This Post Contains A C++ Program To Print Transpose Of A Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Print Transpose Of A Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
    
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0, t = 0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

/* Source Code For Computing Transpose Of Matrix  If & Only If Rows & Columns Are Equal */
if(r==c)
{
   for(i=0; i<r; i++)
   {
      for(j=0; j<i; j++)
      {
         t=a[i][j];
         a[i][j]=a[j][i];
         a[j][i]=t;
      }
   }
/* Printing The Output Onto The Screen/Console */
   cout<<" \n After Transposing : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
   }
}
else
{
   cout<<" \n Transpose Is Not Possible";
}
getch();
}
/* End Of Main Program */


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

After Transpose :
1  4  7
2  5  8
3  6  9


Post a Comment