Write a c program to convert number in characters.

Ram Pothuraju

#include <stdio.h>


int main()

{

    int num;


    // Read number from user

    printf("Enter a number: ");

    scanf("%d", &num);


    // Convert number to characters

    printf("The number in characters is: ");

    switch (num)

    {

        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;

        default:

            printf("Invalid input");

    }


    return 0;

}



In this program, we first read the number to be converted from the user using the scanf() function.

We then use a switch statement to convert the number to its corresponding characters. We check each digit of the number and print the corresponding word using the printf() function. If the input number is not a single digit or is not within the range of 0 to 9, we print an error message instead.

Note that this program assumes that the input number is a single digit. You may want to add input validation to ensure that the input is valid. Additionally, this program only handles positive numbers. If you need to handle negative numbers, you may want to add additional logic to handle the sign of the input.
Tags

Post a Comment

0Comments

Post a Comment (0)