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 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:
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++
#include <iostream>
using namespace std;
int convertToASCII( int N)
{
while (N > 0) {
int d = N % 10;
cout << d << " ("
<< d + 48 << ")\n" ;
N = N / 10;
}
}
int main()
{
int N = 36;
convertToASCII(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void convertToASCII( int N)
{
while (N > 0 ) {
int d = N % 10 ;
System.out.println(d + " ("
+ (d + 48 ) + ")" );
N = N / 10 ;
}
}
public static void main(String[] args)
{
int N = 36 ;
convertToASCII(N);
}
}
|
Python3
def convertToASCII(N):
while (N > 0 ):
d = N % 10
print (d, "(" + str (d + 48 ) + ")" )
N = N / / 10
if __name__ = = '__main__' :
N = 36
convertToASCII(N)
|
C#
using System;
public class GFG {
static void convertToASCII( int N)
{
while (N > 0) {
int d = N % 10;
Console.WriteLine(d + " ("
+ (d + 48) + ")" );
N = N / 10;
}
}
public static void Main(String[] args)
{
int N = 36;
convertToASCII(N);
}
}
|
Javascript
<script>
function convertToASCII(N)
{
while (N > 0) {
var d = N % 10;
document.write( d + " ("
+ (d + 48) + ")<br>" );
N = parseInt(N / 10);
}
}
var N = 36;
convertToASCII(N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int convertToASCII( int N)
{
string num = to_string(N);
for ( char ch : num) {
cout << ch << " ("
<< ( int )ch << ")\n" ;
}
}
int main()
{
int N = 36;
convertToASCII(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void convertToASCII( int N)
{
String num = Integer.toString(N);
for ( char ch : num.toCharArray()) {
System.out.print(ch + " ("
+ ( int )ch + ")\n" );
}
}
public static void main(String[] args)
{
int N = 36 ;
convertToASCII(N);
}
}
|
Python3
def convertToASCII(N):
num = str (N)
i = 0
for ch in num:
print (ch, "(" , ord (ch), ")" )
N = 36
convertToASCII(N)
|
C#
using System;
public class GFG{
static void convertToASCII( int N)
{
String num = N.ToString();
foreach ( char ch in num.ToCharArray()) {
Console.Write(ch + " ("
+ ( int )ch + ")\n" );
}
}
public static void Main(String[] args)
{
int N = 36;
convertToASCII(N);
}
}
|
Javascript
<script>
function convertToASCII(N)
{
let num = N.toString();
for (let ch = 0; ch < num.length; ch++)
{
document.write(num[ch] + " ("
+ num[ch].charCodeAt(0) + ")<br>" );
}
}
let N = 36;
convertToASCII(N);
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)
Last Updated :
08 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...