25 March, 2012

C++ Program For Printing A 2D-Array [Two Dimensional] Onto The Screen/Console.

This Post Contains A C++ Program For Printing A 2D-Array [Two Dimensional] Onto The Screen/Console With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-Loop' & '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 Code Into The Compiler For Direct Result.



C++ Program For Printing A 2D-Array [Two Dimensional] Onto The Screen/Console.

/* 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 ];
                }
       }
       getch();
}
/* End Of Main Program */ 

Output  :

Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  3  3

Enter 9 Values For 2D Array [ Matrix ]  :

1  2  3  4  5  6  7  8  9

Values Of 2D Array [ Matrix ] Are :
1  2  3

4  5  6

7  8  9




C++ Program For Cascading Of Operators.

This Post Contains A C++ Program For Cascading Of Operators With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Cascading' 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 Into The Compiler For Direct Result. 


C++ Program For Cascading Of Operators.

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

/* Start Of Main Program */
void main()
{
       
        /* Declaration Of Variables */
        int  a  =  0,  b  =  0;
        clrscr();

        /* Asking For The Input From User */
        cout  <<  "  Enter Values Of A & B  :  ";
        cin  >>  a  >>  b;

        /* Source Code For Cascading Of Operators */
        cout  <<  "  \n The Sum Of  "  <<  a  <<  " & "  <<  b  <<  " : "  <<  a  +  b;
        cout  <<  "  \n The Net Of  "  <<  a  <<  " & "  <<  b  <<  " : "  <<  a  -  b;
        cout  <<  "  \n The Product Of  "  <<  a  <<  " & "  <<  b  <<  " : "  <<  a  *  b;
        if  (  a  >  b  )
        {
               cout  <<  "  \n The Quotient Of  "  <<  a  <<  " Divided By "  <<  b  <<  " : "  <<  a  /  b;
        }
        else
        {
               cout  <<  "  \n The Quotient Of  "  <<  b  <<  " Divided By " <<  a  <<  " : "  <<  b  /  a;
        }
        getch();
}
/* End Of Main Program */ 

Output  :

Enter Values Of A & B  :  2  2

The Sum Of 2 & 2  :  4

The Net Of  2 & 2  :  0

The Product Of 2 & 2  :  4

The Quotient Of 2 Divided By 2  :  1




C++ Program To Find Odd Or Even Number.

This Post Contains A C++ Program To Find Odd Or Even Number With Correct Source Code & Output.
This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'If..else' & 'Nested 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 Code Into Compiler For Direct Result. 



C++ Program To Find Odd Or Even Number.

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

/* Start Of Main Program */
void main()
{
               
                /* Declaration Of Variables */
                int  Num  =  0;
                clrscr();

                /* Asking For The Input From User */
                cout << "  Enter Any Number :-  ";
                cin >> Num;

                /* Source Code For Computing Odd Or Even Number */
                if  (  Num  ==  0  )
                {
                       cout<< "  Enter Number Other Than Zero. ";
                }
                else
                {
                       if  (  Num  %  2  ==  0  )
                       {
                              cout << Num << "  Is Even Number. ";
                       }
                       else
                       {
                              cout <<  Num << "  Is An Odd Number.  ";
                       }
               }
               getch();
}
/* End Of Main Program */ 

Output  :

Enter Any Number  :-  0

Enter Number Other Than Zero.

Enter Any Number  :-  1

1  Is An Odd Number.

Enter Any Number  :-  2

2  Is Even Number.




21 March, 2012

C++ Program For Printing Pascal's Triangle.

This Post Contains A C++ Program For Printing Pascal's Triangle With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else', 'For-Loop', 'While-Loop' & '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 For Printing Pascal's Triangle.

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

/* Start Of Main Program */
void main()
{
              
               /* Declaration Of Variables */
               int b, p, q, r, x;
               clrscr();

               /* Asking For The Input From User */
               cout << " Enter No. Of Rows : ";
               cin >> r;

               /* Source Code For Computing Pascal's Triangle */
               b = 1;
               q = 0;
               cout << "\n Pascal triangle : \n";
               while ( q < r )
               {
                        for ( p = 30 - 3 * q; p > 0; p-- )
                        cout <<" ";
                        for ( x = 0; x <= q; x++ )
                        {
                                   if ( (x == 0) || (q == 0) )
                                  {
                                             b = 1;
                                   }
                                   else
                                   {
                                              b = ( b * (q-x+1) / x );
                                   }
                                    cout <<"    "<<b;
                         }
                         cout <<"\n";
                         q++;
               }
               getch();
}
/* End Of Main Program */ 



Output  :







C++ Program For Printing Triangle Of Numbers.

This Post Contains A C++ Program For Printing Triangle Of Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-Loop' & '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 For Printing Triangle Of Numbers.

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

/* Start Of Main Program */
void main()
{
          /* Declaration Of Variables */
          int   i,   j,   num;
          clrscr();

          /* Asking For The Input From User */
          cout << " Enter No. : ";
          cin >>  num;

          /* Source Code For Printing Triangle Of Numbers */
          for ( i  =  1;   i  <=   num;   i++  )
         {
                    for (  j  =  1;  j   <=   i;  j++  )
                   {
                                if (  i  <  10  )
                                      cout << " " << i;
                                else
                                       cout << " " << i;
                   }
                   cout << " \n";
          }
          getch();
}
/* End Of Main Program */



Output  :





C++ Program To Print Pyramid Of Numbers.

This Post Contains A C++ Program To Print Pyramid Of Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' & '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 Pyramid Of Numbers.

/* Declaration Of Header Files */

# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
              
                /* Declaration Of Variables */
                int   i,   j,   k,   t,   r;
                clrscr();

                /* Asking For The Input From User */
                cout << " Enter No. Of Rows : ";
                cin >> r;

                /* Source Code For Computing Pyramid Of Numbers */
                t  =  1;
                for (  i  = 1;  i  <=  r;  i++  )
                {
                           for ( j=0; j < ( r - i ); j++ )
                          {
                                        cout <<" ";
                          }
                          k = ( i * 2 ) - 1;
                          for (  j  =  0;  j  <  k;  j++  )
                         {
                                        if (  t  >  99  )
                                                cout <<t;
                                        else
                                                if (  t  <  10  )
                                                          cout <<  "  "  <<  t;
                                                else
                                                          cout <<  "  " <<  t;
                                                           t++;
                         }
                         cout <<  "\n\n";
                }
                getch();
}
/* End Of Main Program */ 

Output  :





C++ Program To Print Asterisks Graph.

This Post Contains A C++ Program To Print Asterisks Graph With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-Loop' & '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 Asterisks Graph.

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

/* Start Of Main Program */
void main()
{
                /* Declaration Of Variables */
                int i, j, r;
                clrscr();

                /* Asking For The Input From User */
                cout << " Enter No. Of Rows : ";
                cin >> r;

                /* Source Code For Printing Asterisks Graph */
                for (  i  =  0;  i  <  r;  i++  )
               {
                               cout << " \n ";
                               for (  j  =  0;  j  <=  i;  j++  )
                              {
                                           cout << " * ";
                               }
               }
               getch();
}
/* End Of Main Program */

Ouput :

Enter No. Of Rows :

*
*     *
*     *      *
*     *      *      *
*     *      *      *      *







20 March, 2012

C++ Program For Generating Numbers In The Given Number's Combination.

This Post Contains A C++ Program For Generating Numbers In The Given Number's Combination With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-loop', 'While-loop' & '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 For Generating Numbers In The Given Number's Combination.

/* Declaration Of Header Files */

# include <iostream.h>
# include <conio.h>

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

   /* Declaration Of Variables */
   int   i,   j,   r,   s,   t,    d,   range;
   int p[ 5 ],   q[ 5 ];
   long  n;

   clrscr();

   /* Asking For The Input From User */
   cout << " Enter No. Of Numbers For Combination  :  ";
   cin >> d;
   cout << " Enter Nos. For Combination  :  ";
   for  (  i   =   0;   i   <   d;   i++  )
   {
           cin >> p[ i ];
   }
   cout << " Enter Range : ";
   cin >> range;
  
   /* Source Code For Generating Numbers In The Given Number's Combination */
   for  (  i   =   1;  i   <=   range;   i++   )
   {
             n   =    i;
             s   =   0;
             t   =   0;
     while(  n   >   0  )
     {
              j    =    0;
              r    =   n    %    10;
              t++ ;
             while  (  j    <    d  )
             {
                      if  (  r   ==   p[ j ]  )
                     {
                             s++ ;
                             j++ ;
                     }
             }
             n   =   n   /   10;
     }
     if  (  s   ==   t  )
            cout  <<   i   <<   "  \t  ";
  }
  getch();
}
/* End Of Main Program */ 

Output :

Enter No. Of Numbers For Combination : 2
Enter Nos. For Combination : 1  3
Enter Range : 1000
1  3  11  13  31  33  111   113   131   133  311 313  331  333      




C++ Program Printing Digits Of A Number In An Ascending Ordered Form.

This Post Contains A C++ Program Printing Digits Of A Number In An Ascending Ordered Form With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'for-Loops' & '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 For Printing Digits Of A Number In An Ascending Ordered Form.


# include <iostream.h>
# include <conio.h>

void main()
{
                long n, s;
                int    i,    j,    k,    t,    a[ 10 ];
                clrscr();
                cout << " Enter Any Number  :  ";
                cin >> n;
                 j   =   0 ;
                while  (    n    >    0    )
                {
                          i   =   n   %   10;
                         a[ j ]   =   i;
                         j++ ;
                         n   =   n   /   10;
                }
                for   (   i  =  0;   i  <  j;   i++   )
               {
                           for   (   k  =  i+1;    k  <  j;   k++   )
                          {
                                          if   (   a[ i ]   >   a[ k ]   )
                                               t   =   a[ i ] ;
                                               a[ i ]   =   a[ k ] ;
                                               a[ k ]   =   t ;
                           }
                }
                 t   =   j   -   2 ;
                 n   =   s   =   0 ;
                 for   (   i  =  0;   i  <  j;   i++  )
                {
                             s   =   a[ i ] ;
                             for   (   k  =  0;   k  <=  t;   k++   )
                            {
                                         s   =   s   *   10 ;
                                         t-- ;
                                         n   =   n   +   s ;
                            }
                }
                cout << " \n Ordered No.   :   " << n; 
                getch();
}

Output :

Enter Any No.   :   312

Ordered No.   :   123    




C++ Program For Swapping Of Two Integers.

This Post Contains A C++ Program For Swapping Of Two Integers Without Using Third Variable With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Swapping' 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 Swapping Of Two Integers.

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

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */
   int   i,   j;
   clrscr();

   /* Asking For The Input From User */
   cout << "  Enter Any 2 Numbers   :   ";
   cin >>   i >>   j;
   cout << " \n  Before Swapping   :   ";
   cout <<   i <<   j;

 /* Source Code For Computing Swapping Of Two Integers */
   cout << " \n  After Swapping   :   ";
   i   =   i   +   j ;
   j   =   i   -   j ;
   i   =   i   -   j ;
   cout <<   i <<   j;
  
   getch();
}
/* End Of Main Program */

Output :

Enter Any 2 Numbers :  12   21

Before Swapping :  12   21

After Swapping :  21    12    





C++ Program For Conversion Of Decimal No Into Octal No.

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 <iostream.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 */
   cout << " Enter Any No.   :   ";
   cin >>  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 */
   cout << " \n Octal No.   :   ";
   for  (  q  =  0;  q  <  r;  q++  )
   {
     cout << b[ q ] ;
   }
   getch();
}
/* End Of Main Program */ 




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

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 TurboC3.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 <iostream.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 */
   cout << " Enter Any Binary Number : ";
   cin >> 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--;
   }
   cout << r;
   getch();
}
/* End Of Main Program */     




C++ Program To Find Given Number As Binary Or Not.

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 TurboC3.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 <iostream.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 */
   cout << " Enter Any Number : ";
   cin >> 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)
     cout << t << " Is Binary ";
   else
     cout << t << " Is Not Binary ";     
   getch();
}
/* End Of Main Program */      




C++ Program For Swapping Of Two Arrays.

This Post Contains A C++ Program For Swapping Of Two Arrays With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays' & 'For-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.


When It Comes To Arrays, Most Of Us Get Confused & Hence Some Of Us Find Or Thinks That This Particular Concept Is Complicated As Well As Tricky. But The Following Code Will Make It Really Simple For You To Understand.

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

  
C++ Program For Swapping Of Two Arrays.

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

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int a[10], b[10], f, p;
   clrscr();
 
   /* Asking For The Input From User */
   cout << " Enter Dimension : ";
   cin >> f;
   cout << " Enter " << f << " Values For Array A: ";
   for ( p = 0; p < f; p++ )
   {
     cin >> a[p];
   }

   /* Asking For The Input From User */
   cout << " Enter " << f << " Values For Array B: ";
   for ( p = 0; p < f; p++ )
   {
     cin >> b[p];
   }
 
   /* Printing The Values Of Array A */
   cout << " Values For Array A: ";
   for ( p = 0; p < f; p++ )
   {
     cout << a[p] << " ";
   }

   /* Printing The Values Of Array B */
   cout << " Values For Array B: ";
   for ( p = 0; p < f; p++ )
   {
     cout << b[p] << " ";
   }
 
   /* Source Code For Swapping Of Two Arrays */
   for ( p = 0; p < f; p++ )
   {
     a[p] = a[p] + b[p];
     b[p] = a[p] - b[p];
     a[p] = a[p] - b[p];
   }

   /* Printing The Output On The Screen/Console */
   cout << " \n After Swapping : \n ";
   cout << " Values For Array A: ";
   for ( p = 0; p < f; p++ )
   {
     cout << a[p] << " ";
   }
   cout << " Values For Array B: ";
   for ( p = 0; p < f; p++ )
   {
     cout << b[p] << " ";
   }
  
   getch();
}
/* End Of Main Program */

Output  :

Enter Dimension  :  3

Enter 3 Values For Array A  :  1  2  3

Enter Dimension  :  3

Enter 3 Values For Array B  :  4  5  6

Values Of Array A  :  1  2  3

Values Of Array B  :  4  5  6

After Swapping  :

Values Of Array A  :   4  5  6

Values Of Array B  :   1  2  3



  




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

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 <iostream.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 */
   cout << " Enter Two Nos : ";
   cin >> a >> b;
   if (  a  <  b  )
   {
      cout << " \n GCD Of " << a << "&" << b << " : " << gcd (a , b);    // Function Call.
   }
   else
   {
      cout << " \n GCD Of " << b << "&" << a << " : " << gcd (b , a);   // 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 );
   }
   cout << " \n LCM Of " << x << "&" << 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.




19 March, 2012

C++ Program To Find Areas Of 'X' Rectangles Stored In 2D-Array & Determining Largest & Smallest Area.

This Post Contains A C++ Program To Find Areas Of 'X' Rectangles Stored In 2D-Array & Determining Largest & Smallest Area With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of '2D-Arrays', 'For-loop', 'If...else' & '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 Areas Of 'X' Rectangles Stored In 2D-Array & Determining Largest & Smallest Area.


/* Header Files */
# include <iostream.h>
# include <conio.h>
# define Max 20

/* Main Program */
void main()
{
   // Initialization Of Variables
   int Num=0, i=0, j=0, k=0, a[Max][2], b[Max];
   int Large=0, Small=0;
  
   clrscr();
  
   // Accepting No. Of Rect. From User
   cout<<"\nEnter The No. Of Rectangles :  ";
   cin>>Num;
  
   // Accepting The Dimensions Of 'N' Rect. From User
   // & Storing Them In A 2D Array
   cout<<"\nEnter The Dimensions Of "<<Num<<" Rectangles i.e Length & Breadth :";
   cout<<"\n";
   for(i=0;i<Num;i++)
   {
       for(j=0;j<2;j++)
       {
   cin>>a[i][j];
       }
       cout<<"\n";
   }
  
   // Calculating The Areas Of 'N' Rect. & Storing
   // Them Into Another Array
   j=0;
   k=0;
   for(i=0; i<Num; i++)
   {
       b[k] = a[i][j] * a[i][j+1];
       k++;   // Here 'k' Act As A Counter Variable
   }
  
   // Displaying The Array Which Contains The Areas
   // Of 'N' Rect.
   cout<<"\n\nThe Areas Of "<<Num<<" Rect. Are As Follows : ";
   for(i=0; i<k; i++)
   {
       cout<<"\n\n"<<b[i];
   }
   cout<<"\n\n";
  
   // Finding The Large & Small Area From 'N' Rect.
   Large=b[0];
   Small=b[0];
   for(i=0; i<k; i++)
   {
       if(b[i] > Large)
    Large = b[i];
       if(b[i] < Small)
    Small = b[i];
   }
  
   // Displaying The Large & Small Area Of Rect.
   cout<<"\n\nLarge Area :  "<<Large;
   cout<<"\n\nSmall Area :  "<<Small;
  
getch();
}




C++ Program For Conversion Of Celsius To Fahrenheit.

This Post Contains A C++ Program For Conversion Of Celsius To Fahrenheit With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arithematic Manipulations' 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 Celsius To Fahrenheit.

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

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */
   float c, f;

   clrscr();

   /* Asking For The Input From User */
   cout << " Enter Temp. In Celsius : ";
   cin >> c;

   f  =  (  c  +  32  )  *  9.0  /  5.0;

   /* Printing The Output Onto The Screen/Console */
   cout << " \nTemp. In Fahrenheit : " << f;

   getch();
}
/* End Of Main Program */


Output :

Enter Temp. In Celsius : 37 C
Temp. In Fahrenheit : 98.6 F

# Click : Here To View 'C++ Program For Conversion Of Fahrenheit To Celsius' Program.




C++ Program To Print Multiplication Tables.

This Post Contains A C++ Program To Print Multiplication Tables With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' & '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 Multiplication Tables.

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

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int i,  j;
   clrscr();
 
    /* Source Code For Computing Multiplication Tables */
    for ( i = 1; i <= 10; i++ )
   {
      for ( j = 1; j <= 10; j++ )
     {
             cout << "\n " << i << " * " << j << " = " << i * j;
      }
      cout << " \n ";
      getch();
   }
}
/* End Of Main Program */ 

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


Main Program Written In IDE
 ------------------------------------------------------------------------------------------------------------


Alt + F9 : Compiling The Program
 ------------------------------------------------------------------------------------------------------------


Ctrl + F9 : Output Of The Above Program





C++ Program For Printing A Floyd's Triangle.

This Post Contains A C++ Program For Printing A Floyd's Triangle With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'If...else' & '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.


Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

1
2   3
4   5   6
7   8   9   10

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


C++ Program For Printing A Floyd's Triangle.

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

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int a, b, c, rows;
   clrscr();
 
   /* Asking For The Input From User */ 
   cout << " Enter No. Of Rows To Print : ";
   cin >> rows;
 
   /* Source Code For Computing Floyd's Triangle */
   b  =  1;
   for ( a = 0; a < rows; a++ )
   {
       for ( c = 0; c <= a; c++ )
       {
           if (  b  <  10  )
              cout << b << "  ";
           else
              cout << b<< "  ";
              b++;
       }
       cout << " \n  \n ";
   }
   getch();
}
/* End Of Main Program */ 

Output :

Enter No. Of Rows : 4
1
2   3
4   5   6
7   8   9   10




C++ Program For Printing Fibonacci Series.

This Post Contains A C++ Program For Printing Fibonacci Series With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of '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.


What Is A Fibonacci Series ?.

The Fibonacci Sequence Is The Series Of Numbers :

0,   1,   1,   2,   3,   5,   8,   13,   21,   34,   .....

The next number is found by adding up the two numbers before it.
  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3), & so on.
Following Is The Simple & Easy To Understand Algorithm Of Fibonacci Series, Which Will Help You To Analyse The Source Code Of This Program.

Algorithm :

1.  Assign sum=0,  A=0,  B=1,  i=1.
2.  Get the no. of terms upto which u want to generate the Fibonacci no,  i.e.,  n.
3.  Add A and B to get the next Fibonacci number. 4. Assign the value of B to A i.e. A=B
5.  Assign the value of sum to B  i.e.  B = sum.
6.  Write the value of su to get next Fibonacci number in the series.
7.  Increment  i  with  1  i.e.  i = i + 1 and repeat step 3, 4, 5, 6 with the last value of i = n ( n is the no. of terms which u  want to generate Fibonacci no. series. )
8. Stop.

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


C++ Program For Printing A Fibonacci Series.

# include <iostream.h>
# include <conio.h>

void main()
{
   int a, b, c, value;
   clrscr();
   cout << " Enter Any Value : ";
   cin >> value;
   a  =  0;  b  =  1;  c  =  0;
   cout << a << "\t" << b;
   c  =  a  +  b;
   while ( c   <=   value )
   {
      cout << "\t" << c;
      a  =  b;
      b  =  c;
      c  =  a  +  b;
   }
   getch();
}

Output :

Enter Any Value  :  55

0   1   1   2    3    5     8   13    21     34     55




Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.