C++ Program to print the pattern of alphabet [DEVCPP/GCC]

/*Program to print the pattern of alphabets.

A
BC
DEF
GHIJ
KLMNO

*/

PROGRAM

# include <iostream>

using namespace std;

int main()
{
char c='A';
int i,j,rows;

cout<<"ENTER THE NUMBER OF ROWS:\n";
cin>>rows;
cout<<"\nPATTERN FORMED:\n";

for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
                       if(c=='Z')
                       {
                               cout<<c;
                               c='A';
                               continue;
                       }
cout<<c;
c++;
}
cout<<endl;
}

       return 0;
}

OUTPUT

C++ Program to print the pattern of alphabet

C++ Program to illustrate function overloading [DEVCPP/GCC]


FUNCTION OVERLOADING

C++ allows specification of more than one function under the same name in the same scope. These functions must have different signatures and are called overloaded functions. Overloading of functions allow programmer to define different semantics for a function, depending upon number and type of arguments.

For instance, in order to find maximum number, the function max() can be overloaded as:

int max (int num1, int num2);
int max (int num1, int num2, int num3);

Although functions can be distinguished on the basis of return type, they cannot be overloaded on this basis. 

PROGRAM

/* Program to calculate area of square, rectangle and circle using overloaded function area */

#include <iostream>

using namespace std;

float area( int length, int breadth )
{
        return (length * breadth);
}

float area( int side )
{
        return (side * side);
}

float area( float radius )
{
        return (3.14 * radius * radius);
}

int main()
{
        char ch='y';
        int s,c,l,b;
        float r;

        do
        {
              cout<<"\n\n\t***AREA CALCULATOR***";
              cout<<"\n 1. SQUARE";
              cout<<"\n 2. RECTANGLE";
              cout<<"\n 3. CIRCLE";
              cout<<"\n 4. EXIT";
              cout<<"\nENTER YOUR CHOICE(1-4): ";
              cin>>c;

              switch(c)
              {
                      case 1:
                      {
                              cout<<"\nENTER SIDE: ";
                              cin>>s;

                              cout<<"AREA OF SQUARE: "<<area(s);
                              break;
                    }

                    case 2:
                      {
                              cout<<"\nENTER LENGTH: ";
                              cin>>l;
                              cout<<"ENTER BREADTH: ";
                              cin>>b;

                              cout<<"AREA OF RECTANGLE: "<<area(l,b);
                              break;
                      }

                      case 3:
                      {
                               cout<<"\nENTER RADIUS: ";
                               cin>>r;

                               cout<<"AREA OF CIRCLE: "<<area(r);
                               break;
                       }

                       case 4:
                       {
                                exit(0);
                        }

                        default:
                        {
                                 cout<<"YOU ENTERED WRONG CHOICE!";
                        }
             }

             cout<<"\n\nDO YOU WANT TO CONTINUE(Y/N): ";
             cin>>ch;

         }while(ch=='Y' || ch=='y');
}

OUTPUT


C++ Program to illustrate function overloading


C++ Program to illustrate the difference between call by value and call by reference [DEVCPP/GCC]

CALL BY VALUE

In call by value, a copy of actual parameter is passed to the calling function. The passed value to the function is locally stored by the function parameter in stack memory. Here both formal parameters and actual parameters share different address apace. Thus any changes in formal parameters is not reflected back in actual parameters.

CALL BY REFERENCE

In call by reference, reference(address) of the actual parameters is passed to the calling function (See Reference Variables). It results in creation of alias for the actual parameters and both actual parameters and formal parameters refers to the same memory location. Thus any changes in formal parameters is reflected back in actual parameters.

PROGRAM

// Program to illustrate the difference between call by value and call by reference

#include <iostream>

using namespace std;

void callByValue(int n)
{
        n=n*n;
        cout<<"\nNUM IN FUNCTION: "<<n;
}

void callByReference(int &n)
{
        n=n*n;
        cout<<"\nNUM IN FUNCTION: "<<n;
}

int main()
{
        int num;

        cout<<"\nENTER A NUMBER: ";
        cin>>num;

        cout<<"\n\nCALLING BY VALUE!\n";
        callByValue(num);

        cout<<"\nNUM IN MAIN: "<<num;

        cout<<"\n\nCALLING BY REFERENCE!\n";
        callByReference(num);

         cout<<"\nNUM IN MAIN: "<<num;
 return 0;
}

OUTPUT

C++ Program to illustrate the difference between call by value and call by reference with output

FIND US ON FACEBOOK!