How to convert given number to a character array

Given an integer number N, the task is to convert it into a character array.

Example:  

Input: N = 2020 
Output: {2, 0, 2, 0} 
Explanation: Here char array arr[] = {2, 0, 2, 0}
Input: N = 12349 
Output: {1, 2, 3, 4, 9} 
Explanation: Here char array arr[] = {1, 2, 3, 4, 9}

 

Approach:  The basic approach to do this, is to recursively find all the digits of N, and insert it into the required character array. 

  1. Count total digits in the number.
  2. Declare a char array of size digits in the number.
  3. Separating integer into digits and accommodate it to a character array.
  4. Add ASCII value of character ‘0’ is 48 in each element of the array.

Below is the implementation of the above approach:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to convert integer
// number into character array
 
#include <stdio.h>
#include <stdlib.h>
 
// Function to convert integer to
// character array
char* convertIntegerToChar(int N)
{
 
    // Count digits in number N
    int m = N;
    int digit = 0;
    while (m) {
 
        // Increment number of digits
        digit++;
 
        // Truncate the last
        // digit from the number
        m /= 10;
    }
 
    // Declare char array for result
    char* arr;
 
    // Declare duplicate char array
    char arr1[digit];
 
    // Memory allocaton of array
    arr = (char*)malloc(digit);
 
    // Separating integer into digits and
    // accomodate it to character array
    int index = 0;
    while (N) {
 
        // Separate last digit from
        // the number and add ASCII
        // value of character '0' is 48
        arr1[++index] = N % 10 + '0';
 
        // Truncate the last
        // digit from the number
        N /= 10;
    }
 
    // Reverse the array for result
    int i;
    for (i = 0; i < index; i++) {
        arr[i] = arr1[index - i];
    }
 
    // Char array truncate by null
    arr[i] = '\0';
 
    // Return char array
    return (char*)arr;
}
 
// Driver Code
int main()
{
 
    // Given number
    int N = 12349;
    int len = 5;
 
    // Function call
    char* arr = convertIntegerToChar(N);
 
    // Print char array
    for (int i = 0; i < len; i++)
        printf("%c, ", arr[i]);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert integer
// number into character array
class GFG{
     
// Function to convert integer to
// character array
static char[] convertIntegerToChar(int N)
{
 
    // Count digits in number N
    int m = N;
    int digit = 0;
     
    while (m > 0)
    {
         
        // Increment number of digits
        digit++;
 
        // Truncate the last
        // digit from the number
        m /= 10;
    }
 
    // Declare char array for result
    char[] arr;
 
    // Declare duplicate char array
    char []arr1 = new char[digit + 1];
 
    // Memory allocaton of array
    arr = new char[digit];
 
    // Separating integer into digits and
    // accomodate it to character array
    int index = 0;
     
    while (N > 0)
    {
         
        // Separate last digit from
        // the number and add ASCII
        // value of character '0' is 48
        arr1[++index] = (char)(N % 10 + '0');
 
        // Truncate the last
        // digit from the number
        N /= 10;
    }
 
    // Reverse the array for result
    int i;
    for(i = 0; i < index; i++)
    {
        arr[i] = arr1[index - i];
    }
 
    // Return char array
    return (char[])arr;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 12349;
    int len = 5;
 
    // Function call
    char[] arr = convertIntegerToChar(N);
 
    // Print char array
    for(int i = 0; i < len; i++)
        System.out.printf("%c, ", arr[i]);
}
}
 
// This code is contributed by amal kumar choubey

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to convert integer
# number into character array
 
# Function to convert integer to
# character array
def convertIntegerToChar(N):
 
    # Count digits in number N
    m = N;
    digit = 0;
 
    while (m > 0):
 
        # Increment number of digits
        digit += 1;
 
        # Truncate the last
        # digit from the number
        m /= 10;   
 
    # Declare char array for result
    arr = ['0' for i in range(digit)]
 
    # Declare duplicate char array
    arr1 = ['0' for i in range(digit + 1)];
 
    # Separating integer
    # into digits and
    # accomodate it
    # to character array
    index = 0;
 
    while (N > 0):
        index += 1;
 
        # Separate last digit from
        # the number and add ASCII
        # value of character '0' is 48
        arr1[index] = chr(int(N % 10 + 48));       
 
        # Truncate the last
        # digit from the number
        N = N // 10;   
 
    # Reverse the array for result
    for i in range(0, index):
        arr[i] = arr1[index - i];   
 
    # Return char array
    return arr;
 
# Driver Code
if __name__ == '__main__':
 
    # Given number
    N = 12349;
    len = 5;
 
    # Function call
    arr = convertIntegerToChar(N);
 
    # Print array
    for i in range(0, len, 1):
        print(arr[i], end = " ");
 
# This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert integer
// number into character array
using System;
 
class GFG{
     
// Function to convert integer to
// character array
static char[] convertintToChar(int N)
{
 
    // Count digits in number N
    int m = N;
    int digit = 0;
     
    while (m > 0)
    {
         
        // Increment number of digits
        digit++;
 
        // Truncate the last
        // digit from the number
        m /= 10;
    }
 
    // Declare char array for result
    char[] arr;
 
    // Declare duplicate char array
    char []arr1 = new char[digit + 1];
 
    // Memory allocaton of array
    arr = new char[digit];
 
    // Separating integer into digits and
    // accomodate it to character array
    int index = 0;
     
    while (N > 0)
    {
         
        // Separate last digit from
        // the number and add ASCII
        // value of character '0' is 48
        arr1[++index] = (char)(N % 10 + '0');
 
        // Truncate the last
        // digit from the number
        N /= 10;
    }
 
    // Reverse the array for result
    int i;
    for(i = 0; i < index; i++)
    {
        arr[i] = arr1[index - i];
    }
 
    // Return char array
    return (char[])arr;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number
    int N = 12349;
    int len = 5;
 
    // Function call
    char[] arr = convertintToChar(N);
 
    // Print char array
    for(int i = 0; i < len; i++)
        Console.Write("{0}, ", arr[i]);
}
}
 
// This code is contributed by amal kumar choubey

chevron_right


Output: 

1, 2, 3, 4, 9,


 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:



If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.