Tutorialspoint.dev

A creative C++ program to Zoom digits of an integer

Write a C (or C++) program to ZOOM (magnify) the digits of an integer. It should take an integer from the user and display each digit of the integer in magnified form using some pattern.

Examples:

Input : 123
Output : 

  @
 @@
  @
  @
@@@@@
-------------------------------

@@@@
@  @
  @
 @
@@@@
-------------------------------


@@@@@
    @
@@@@@
    @
@@@@@
-------------------------------

This creative program takes an integer from the user and print each and every digit of that integer after zooming it.



The given number is first converted to string using stringstream. After that, each character (digit) is accessed and put to switch-case structure which ZOOMED each digit and printed in the form of pattern.

Below is C++ implementation

// C++ program to zoon digits of an integer
#include <bits/stdc++.h>
using namespace std;
  
void zoomDigits(int number)
{
    // Converting number to string
    stringstream ss;
    ss << number;
    string str = ss.str();
  
    for (int k=0; k<str.length(); k++)
    {
        switch(str[k]-'0')
        {
        case 0:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (i==0 || i==4)
                        cout << '@';
                    else if (j==0 || j==4)
                        cout << '@';
                    else
                        cout << " ";
  
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 1:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (j==2)
                        cout << '@';
                    else if ((i==1 && j==1))
                        cout << '@';
                    else if (i==4)
                        cout << '@';
                    else
                        cout << " ";
  
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 2:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<4; j++)
                {
                    if (i==0 && j==4)
                        cout << " ";
                    else if (i==0 || i==4)
                        cout << '@';
                    else if (i==1 && j==0)
                        cout << '@';
                    else if (i==(4-j))
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 3:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (i==0 || i==2 || i==4)
                        cout << '@';
                    else if (j==4)
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 4:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (j==4)
                        cout << '@';
                    else if (i==2)
                        cout << '@';
                    else if (j==0 && (i==0 || i==1))
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 5:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (i==0 || i==2 || i==4)
                        cout << '@';
                    else if ((j==0 && i==1) ||
                             (j==4 && i==3))
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 6:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (i==0 || i==2 || i==4)
                        cout << '@';
                    else if ((j==0 && (i==1 || i==3)) ||
                                       (j==4 && i==3))
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 7:
            for (int i=0 ; i<5; i++)
            {
                for (int j=0 ; j<5; j++)
                {
                    if (i==0 && (j!=4))
                        cout << '@';
                    else if (i==2 && (j==2 || j==4))
                        cout << '@';
                    else if (j==3)
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 8:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if (i==0 || i==2 || i==4)
                        cout << '@';
                    else if ((j==0 && (i==1 || i==3) ||
                            (j==4 && (i==1 || i==3))))
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
  
        case 9:
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    if ( i==0 || i==2 || j==4)
                        cout << '@';
                    else if (i==1 && j==0)
                        cout << '@';
                    else
                        cout << " ";
                }
                cout << endl;
            }
            cout << "------------------------------- ";
            continue;
        }
    }
}
  
// Driver code
int main()
{
    long long number = 12305;
    zoomDigits(number);
    return 0;
}

Output:

  @
 @@
  @
  @
@@@@@
-------------------------------

@@@@
@  @
  @
 @
@@@@
-------------------------------

@@@@@
    @
@@@@@
    @
@@@@@
-------------------------------

@@@@@
@   @
@   @
@   @
@@@@@
-------------------------------

@@@@@
@
@@@@@
    @
@@@@@
-------------------------------

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter