📌  相关文章
📜  程序打印给定数字的所有数字的ASCII值

📅  最后修改于: 2021-04-17 18:18:56             🧑  作者: Mango

给定整数N ,任务是打印N的所有数字的ASCII值。

例子:

方法:使用下面显示的ASCII表,可以打印N的所有数字的ASCII值:

Digit ASCII Value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57

可以看出,数字[0 – 9]的ASCII值范围是[48 – 57] 。因此,为了打印任何数字的ASCII值,需要在数字上添加48。

下面是上述方法的实现:

C++
// C++ program to convert the digits
// of a number to its ASCII values
#include 
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


C++
// C++ program to convert the digits
// of a number to its ASCII values
#include 
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


输出:
6 (54)
3 (51)

时间复杂度: O(log 10 N)
辅助空间: O(1)

替代方法:想法是使用类型转换。转换的整数等效的字符串并打印字符串的每个字符的ASCII值。

下面是上述方法的实现:

C++

// C++ program to convert the digits
// of a number to its ASCII values
#include 
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
输出:
3 (51)
6 (54)

时间复杂度: O(log 10 N)
辅助空间: O(log 10 N)