Skip to content
Related Articles

Related Articles

Program to print ASCII Value of all digits of a given number
  • Last Updated : 27 Apr, 2021

Given an integer N, the task is to print the ASCII value of all digits of N.

Examples:

Input: N = 8
Output: 8 (56)
Explanation:
ASCII value of 8 is 56

Input: N = 240
Output:
2 (50)
4 (52)
0 (48)

Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed:



DigitASCII Value
048
149
250
351
452
553
654
755
856
957

It can be observed that ASCII value of digits [0 – 9] ranges from [48 – 57]. Therefore, in order to print the ASCII value of any digit, 48 is required to be added to the digit.

Below is the implementation of the above approach:

C++




// C++ program to convert the digits
// of a number to its ASCII values
#include <iostream>
using namespace std;
 
// Function to convert digits of
// N to respective ASCII values
int convertToASCII(int N)
{
    while (N > 0) {
        int d = N % 10;
        cout << d << " ("
             << d + 48 << ")\n";
 
        N = N / 10;
    }
}
 
// Driver Code
int main()
{
    int N = 36;
    convertToASCII(N);
 
    return 0;
}

Java




// Java program convert the digits
// of a number to its ASCII values
import java.io.*;
class GFG {
 
    // Function to convert digits of
    // N to respective ASCII values
    static void convertToASCII(int N)
    {
        while (N > 0) {
            int d = N % 10;
            System.out.println(d + " ("
                               + (d + 48) + ")");
 
            N = N / 10;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 36;
        convertToASCII(N);
    }
}

Python3




# Python3 program to convert the digits
# of a number to its ASCII values
 
# Function to convert digits of
# N to respective ASCII values
def convertToASCII(N):
    while (N > 0):
        d = N % 10
        print(d, "(" + str(d + 48) + ")")
        N = N // 10
 
# Driver Code
if __name__ == '__main__':
    N = 36
    convertToASCII(N)
 
    # This code is contributed by mohit kumar 29.

C#




// C# program convert the digits
// of a number to its ASCII values
using System;
public class GFG {
 
  // Function to convert digits of
  // N to respective ASCII values
  static void convertToASCII(int N)
  {
    while (N > 0) {
      int d = N % 10;
      Console.WriteLine(d + " ("
                        + (d + 48) + ")");
 
      N = N / 10;
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 36;
    convertToASCII(N);
  }
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
 
// Javascript program to convert the digits
// of a number to its ASCII values
 
// Function to convert digits of
// N to respective ASCII values
function convertToASCII(N)
{
    while (N > 0) {
        var d = N % 10;
        document.write( d + " ("
             + (d + 48) + ")<br>");
 
        N = parseInt(N / 10);
    }
}
 
// Driver Code
var N = 36;
convertToASCII(N);
 
</script>
Output: 
6 (54)
3 (51)

 

Time Complexity: O(log10N) 
Auxiliary Space: O(1)

Alternate Approach: The idea is to use type conversion. Convert the integer to equivalent string and print ASCII value of each character of the string.

Below is the implementation of the above approach: 

C++




// C++ program to convert the digits
// of a number to its ASCII values
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert digits of
// N to respective ASCII values
int convertToASCII(int N)
{
    string num = to_string(N);
    for (char ch : num) {
        cout << ch << " ("
             << (int)ch << ")\n";
    }
}
 
// Driver Code
int main()
{
    int N = 36;
    convertToASCII(N);
 
    return 0;
}

Java




// Java program to convert the digits
// of a number to its ASCII values
import java.util.*;
class GFG{
  
// Function to convert digits of
// N to respective ASCII values
static void convertToASCII(int N)
{
    String num = Integer.toString(N);
    for (char ch : num.toCharArray()) {
        System.out.print(ch + " ("
            + (int)ch + ")\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 36;
    convertToASCII(N);
}
}
  
// This code is contributed by sanjoy_62.

Python3




# Python3 program to convert the digits
# of a number to its ASCII values
 
# Function to convert digits of
# N to respective ASCII values
def convertToASCII(N):
 
    num = str(N)
    i = 0
     
    for ch in num:
        print(ch, "(", ord(ch), ")")
 
# Driver Code
N = 36
 
convertToASCII(N)
 
# This code is contributed by Dharanendra L V.

C#




// C# program to convert the digits
// of a number to its ASCII values
using System;
 
public class GFG{
  
// Function to convert digits of
// N to respective ASCII values
static void convertToASCII(int N)
{
    String num = N.ToString();
    foreach (char ch in num.ToCharArray()) {
        Console.Write(ch + " ("
            + (int)ch + ")\n");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 36;
    convertToASCII(N);
}
}
 
// This code contributed by shikhasingrajput
Output: 
3 (51)
6 (54)

 

Time Complexity: O(log10N) 
Auxiliary Space: O(log10N)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :