codinasion-archive / codinasion-programme

An open source codebase for sharing programming solutions.
https://codinasion.vercel.app/programme
MIT License
62 stars 147 forks source link

Number to Strings #47

Closed harshraj8843 closed 2 years ago

harshraj8843 commented 2 years ago

Title of article

Write a c program to convert number to strings.

Additional information

Input:  51
Output: five one
Input:  201
Output: two zero one

Code of Conduct

iUttamRao commented 2 years ago

Write a c program to convert number to strings. closes #47


#include <stdio.h>
#include <math.h>

int main()
{
    int n, num = 0, digits;

    printf("Enter any number to print in words: ");
    scanf("%d", &n);

    digits = (int) log10(n); 

    while(n != 0)
    {
        num = (num * 10) + (n % 10);
        n /= 10;
    }
    digits =  digits - ((int) log10(num));  

    while(num != 0)
    {
        switch(num % 10)
        {
            case 0: 
                printf("Zero ");
                break;
            case 1: 
                printf("One ");
                break;
            case 2: 
                printf("Two ");
                break;
            case 3: 
                printf("Three ");
                break;
            case 4: 
                printf("Four ");
                break;
            case 5: 
                printf("Five ");
                break;
            case 6: 
                printf("Six ");
                break;
            case 7: 
                printf("Seven ");
                break;
            case 8: 
                printf("Eight ");
                break;
            case 9: 
                printf("Nine ");
                break;
        }
        num /= 10;
    }
    while(digits)
    {
        printf("Zero ");
        digits--;
    }

    return 0;
} 
harshraj8843 commented 2 years ago

Hii @iUttamRao

Thanks for contributing to this project 💜

Can you please open a pull request to add this.