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 56Input: 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:
| Digit | ASCII Value |
| 0 | 48 |
| 1 | 49 |
| 2 | 50 |
| 3 | 51 |
| 4 | 52 |
| 5 | 53 |
| 6 | 54 |
| 7 | 55 |
| 8 | 56 |
| 9 | 57 |
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 valuesint convertToASCII(int N){Â Â Â Â while (N > 0) {Â Â Â Â Â Â Â Â int d = N % 10;Â Â Â Â Â Â Â Â cout << d << " ("Â Â Â Â Â Â Â Â Â Â Â Â Â << d + 48 << ")\n";Â
        N = N / 10;    }}Â
// Driver Codeint main(){Â Â Â Â int N = 36;Â Â Â Â convertToASCII(N);Â
    return 0;} |
Java
// Java program convert the digits// of a number to its ASCII valuesimport 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 valuesdef convertToASCII(N):Â Â Â Â while (N > 0):Â Â Â Â Â Â Â Â d = N % 10Â Â Â Â Â Â Â Â print(d, "(" + str(d + 48) + ")")Â Â Â Â Â Â Â Â N = N // 10Â
# Driver Codeif __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 valuesusing 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 valuesfunction convertToASCII(N){Â Â Â Â while (N > 0) {Â Â Â Â Â Â Â Â var d = N % 10;Â Â Â Â Â Â Â Â document.write( d + " ("Â Â Â Â Â Â Â Â Â Â Â Â Â + (d + 48) + ")<br>");Â
        N = parseInt(N / 10);    }}Â
// Driver Codevar N = 36;convertToASCII(N);Â
</script> |
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 valuesint convertToASCII(int N){Â Â Â Â string num = to_string(N);Â Â Â Â for (char ch : num) {Â Â Â Â Â Â Â Â cout << ch << " ("Â Â Â Â Â Â Â Â Â Â Â Â Â << (int)ch << ")\n";Â Â Â Â }}Â
// Driver Codeint main(){Â Â Â Â int N = 36;Â Â Â Â convertToASCII(N);Â
    return 0;} |
Java
// Java program to convert the digits// of a number to its ASCII valuesimport java.util.*;class GFG{Â Â // Function to convert digits of// N to respective ASCII valuesstatic void convertToASCII(int N){Â Â Â Â String num = Integer.toString(N);Â Â Â Â for (char ch : num.toCharArray()) {Â Â Â Â Â Â Â Â System.out.print(ch + " ("Â Â Â Â Â Â Â Â Â Â Â Â + (int)ch + ")\n");Â Â Â Â }}Â Â // Driver Codepublic 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 valuesdef convertToASCII(N):Â
    num = str(N)    i = 0         for ch in num:        print(ch, "(", ord(ch), ")")Â
# Driver CodeN = 36Â
convertToASCII(N)Â
# This code is contributed by Dharanendra L V. |
C#
// C# program to convert the digits// of a number to its ASCII valuesusing System;Â
public class GFG{Â Â // Function to convert digits of// N to respective ASCII valuesstatic void convertToASCII(int N){Â Â Â Â String num = N.ToString();Â Â Â Â foreach (char ch in num.ToCharArray()) {Â Â Â Â Â Â Â Â Console.Write(ch + " ("Â Â Â Â Â Â Â Â Â Â Â Â + (int)ch + ")\n");Â Â Â Â }}Â Â // Driver Codepublic static void Main(String[] args){Â Â Â Â int N = 36;Â Â Â Â convertToASCII(N);}}Â
// This code 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 valuesfunction convertToASCII(N){Â Â Â Â Â let num = N.toString();Â Â Â Â for (let ch = 0; ch < num.length; ch++) Â Â Â Â {Â Â Â Â Â Â Â Â document.write(num[ch] + " ("Â Â Â Â Â Â Â Â Â Â Â Â + num[ch].charCodeAt(0) + ")<br>");Â Â Â Â }}Â
// Driver Codelet N = 36;convertToASCII(N);Â
// This code is contributed by unknown2108</script> |
3 (51) 6 (54)
Â
Time Complexity: O(log10N)Â
Auxiliary Space: O(log10N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
