Program to print numbers in digital form

Given a number n then print number in Digital form.

Examples :

Input : 5
Output : 
  - - 
|    
 - - 
    |
 - - 

Input :  8 
Output :
 - - 
|   |
 - - 
|   |
 - - 

Explanation:
Take a matrix of size 5*5 and store 0 and 1 in the matrix. If matrix cell is 0 then it is used for space and if matrix cell is 1 then it is used either for horizontal or vertical line.
If row number is even then print the horizontal (-) line and if row number is odd then print the vertical ( | ) line.


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

//  C++ profgram to print 
// number in digital form
#include <iostream>
#include <vector>
using namespace std;
  
// Function to print numbers
void print(int mat[][5])
{
  
    // If in matrix row number is even then print "-"
    // otherwise print "|"
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i % 2 == 0) {
                if (mat[i][j] == 1)
                    cout << "-";
                else
                    cout << " ";
            }
            else {
                if (mat[i][j] == 1)
                    cout << "|";
                else
                    cout << " ";
            }
        }
        cout << endl;
    }
}
void digit0()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit1()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit2()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit3()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit4()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit5()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit6()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit7()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit8()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit9()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
  
// Function to check number
void checkDigit(int num)
{
    // for digit 0
    if (num == 0)
        digit0();
  
    // for digit 1
    else if (num == 1)
        digit1();
  
    // for digit 2
    else if (num == 2)
        digit2();
  
    // for digit 3
    else if (num == 3)
        digit3();
  
    // for digit 4
    else if (num == 4)
        digit4();
  
    // for digit 5
    else if (num == 5)
        digit5();
  
    // for digit 6
    else if (num == 6)
        digit6();
  
    // for digit 7
    else if (num == 7)
        digit7();
  
    // for digit 8
    else if (num == 8)
        digit8();
  
    // for digit 9
    else if (num == 9)
        digit9();
}
  
// Driver program
int main()
{
    // Input a number
    int num = 9;
  
    // function call to check digit
    checkDigit(num);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java profgram to print 
// number in digital form
import java.io.*;
  
class GFG {
    // Function to print numbers
    static void print(int mat[][])
    {
      
        // If in matrix row number is even then print "-"
        // otherwise print "|"
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i % 2 == 0) {
                    if (mat[i][j] == 1)
                        System.out.print("-");
                    else
                        System.out.print(" ");
                }
                else {
                    if (mat[i][j] == 1)
                        System.out.print("|");
                    else
                        System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void digit0()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit1()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit2()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit3()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit4()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit5()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit6()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit7()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit8()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit9()
    {
        // In matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    // Function to check number
    static void checkDigit(int num)
    {
        // for digit 0
        if (num == 0)
            digit0();
      
        // for digit 1
        else if (num == 1)
            digit1();
      
        // for digit 2
        else if (num == 2)
            digit2();
      
        // for digit 3
        else if (num == 3)
            digit3();
      
        // for digit 4
        else if (num == 4)
            digit4();
      
        // for digit 5
        else if (num == 5)
            digit5();
      
        // for digit 6
        else if (num == 6)
            digit6();
      
        // for digit 7
        else if (num == 7)
            digit7();
      
        // for digit 8
        else if (num == 8)
            digit8();
      
        // for digit 9
        else if (num == 9)
            digit9();
    }
      
    // Driver program
    public static void main (String[] args) 
    {
        // Input a number
        int num = 9;
      
        // function call to check digit
        checkDigit(num);
          
    }
}
  
// This code is contributed by vt_m.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# profgram to print 
// number in digital form
using System;
  
class GFG {
      
    // Function to print numbers
    static void print(int [,]mat)
    {
      
        // If in matrix row number is even 
        // then print "-" otherwise print "|"
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i % 2 == 0) {
                    if (mat[i,j] == 1)
                        Console.Write("-");
                    else
                        Console.Write(" ");
                }
                else {
                    if (mat[i,j] == 1)
                        Console.Write("|");
                    else
                        Console.Write(" ");
                }
            }
              
            Console.WriteLine();
        }
    }
      
    static void digit0()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit1()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
      
    static void digit2()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit3()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit4()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
      
    static void digit5()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit6()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit7()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
      
    static void digit8()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    static void digit9()
    {
          
        // In matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
      
    // Function to check number
    static void checkDigit(int num)
    {
          
        // for digit 0
        if (num == 0)
            digit0();
      
        // for digit 1
        else if (num == 1)
            digit1();
      
        // for digit 2
        else if (num == 2)
            digit2();
      
        // for digit 3
        else if (num == 3)
            digit3();
      
        // for digit 4
        else if (num == 4)
            digit4();
      
        // for digit 5
        else if (num == 5)
            digit5();
      
        // for digit 6
        else if (num == 6)
            digit6();
      
        // for digit 7
        else if (num == 7)
            digit7();
      
        // for digit 8
        else if (num == 8)
            digit8();
      
        // for digit 9
        else if (num == 9)
            digit9();
    }
      
    // Driver program
    public static void Main () 
    {
          
        // Input a number
        int num = 9;
      
        // function call to check digit
        checkDigit(num);
          
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print
// number in digital form
  
// Function to print numbers
function printnum(array $mat)
{
    // If in matrix row number is even 
    // then print "-" otherwise print "|"
    for ($i = 0; $i < 5; $i++) 
    {
        for ($j = 0; $j < 5; $j++) 
        {
            if ($i % 2 == 0) 
            {
                if ($mat[$i][$j] == 1)
                    echo "-";
                else
                    echo " ";
            }
            else
            {
                if ($mat[$i][$j] == 1)
                    echo "|";
                else
                    echo " ";
            }
        }
        echo "\n";
    }
}
  
function digit0()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat= array(array( 0, 1, 0, 1, 0 ),
                array( 1, 0, 0, 0, 1 ),
                array( 0, 0, 0, 0, 0 ),
                array( 1, 0, 0, 0, 1 ),
                array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit1()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 0, 0, 0, 0 ),
                 array( 0, 0, 1, 0, 0 ),
                 array( 0, 0, 0, 0, 0 ),
                 array( 0, 0, 1, 0, 0 ),
                 array( 0, 0, 0, 0, 0 ));
    printnum($mat);
}
  
function digit2()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat= array(array( 0, 1, 0, 1, 0 ),
                array( 0, 0, 0, 0, 1 ),
                array( 0, 1, 0, 1, 0 ),
                array( 1, 0, 0, 0, 0 ),
                array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit3()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat= array(array( 0, 1, 0, 1, 0 ),
                array( 0, 0, 0, 0, 1 ),
                array( 0, 1, 0, 1, 0 ),
                array( 0, 0, 0, 0, 1 ),
                array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit4()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat= array(array( 0, 0, 0, 0, 0 ),
                array( 1, 0, 0, 0, 1 ),
                array( 0, 1, 0, 1, 0 ),
                array( 0, 0, 0, 0, 1 ),
                array( 0, 0, 0, 0, 0 ));
    printnum($mat);
}
  
function digit5()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 0 ),
                 array( 0, 1, 0, 1, 0 ),
                 array( 0, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit6()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 0 ),
                 array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit7()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 1, 0, 1, 0 ),
                 array( 0, 0, 0, 0, 1 ),
                 array( 0, 0, 0, 0, 0 ),
                 array( 0, 0, 0, 0, 1 ),
                 array( 0, 0, 0, 0, 0 ) );
    printnum($mat);
}
  
function digit8()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
function digit9()
{
    // In matrix 0 used for space
    // and 1 for either - or |
    $mat = array(array( 0, 1, 0, 1, 0 ),
                 array( 1, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ),
                 array( 0, 0, 0, 0, 1 ),
                 array( 0, 1, 0, 1, 0 ));
    printnum($mat);
}
  
// Function to check number
function checkDigit($num)
{
    // for digit 0
    if ($num == 0)
        digit0();
  
    // for digit 1
    else if ($num == 1)
        digit1();
  
    // for digit 2
    else if ($num == 2)
        digit2();
  
    // for digit 3
    else if ($num == 3)
        digit3();
  
    // for digit 4
    else if ($num == 4)
        digit4();
  
    // for digit 5
    else if ($num == 5)
        digit5();
  
    // for digit 6
    else if ($num == 6)
        digit6();
  
    // for digit 7
    else if ($num == 7)
        digit7();
  
    // for digit 8
    else if ($num == 8)
        digit8();
  
    // for digit 9
    else if ($num == 9)
        digit9();
}
  
// Driver code
$num = 9;
checkDigit($num);
  
// This code is contributed by Mithun Kumar
?>

chevron_right


Output:

 - - 
|   |
 - - 
    |
 - -


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar