Program to print ASCII Value of all digits of a given number
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 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> |
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 |
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) { let num = N.toString(); for (let ch = 0; ch < num.length; ch++) { document.write(num[ch] + " (" + num[ch].charCodeAt(0) + ")<br>" ); } } // Driver Code let N = 36; convertToASCII(N); // This code is contributed by unknown2108 </script> |
3 (51) 6 (54)
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)