C program to write all digits into words using for loop

CServer Side ProgrammingProgramming

Suppose we have two digits a and b. We shall have to convert each digit into words and print them one by one. Printing digits into words means for a digit 5, it should print "Five".

So, if the input is like a = 3, b = 8, then the output will be

  • Three
  • Four
  • Five
  • Six
  • Seven
  • Eight

To solve this, we will follow these steps −

Define a function solve(), this will take d,

  • if d < 0 and d > 9, then:
    • return ("Beyond range of 0 - 9")
  • otherwise when d is same as 0, then:
    • return ("Zero")
  • otherwise when d is same as 1, then:
    • return ("One")
  • otherwise when d is same as 2, then:
    • return ("Two")
  • otherwise when d is same as 3, then:
    • return ("Three")
  • otherwise when d is same as 4, then:
    • return ("Four")
  • otherwise when d is same as 5, then:
    • return ("Five")
  • otherwise when d is same as 6, then:
    • return ("Six")
  • otherwise when d is same as 7, then:
    • return ("Seven")
  • otherwise when d is same as 8, then:
    • return ("Eight")
  • otherwise when d is same as 9, then:
    • return ("Nine")
  • From the main method, do the following:
  • for i in range a to be, do
    • solve(i)
    • move cursor to the next line

Example

Let us see the following implementation to get better understanding −

#include <stdio.h>
void solve(int d){
    if(d < 0 && d > 9){
        printf("Beyond range of 0 - 9");
    }else if(d == 0){
        printf("Zero");
    }else if(d == 1){
        printf("One");
    }else if(d == 2){
        printf("Two");
    }else if(d == 3){
        printf("Three");
    }else if(d == 4){
        printf("Four");
    }else if(d == 5){
        printf("Five");
    }else if(d == 6){
        printf("Six");
    }else if(d == 7){
        printf("Seven");
    }else if(d == 8){
        printf("Eight");
    }else if(d == 9){
        printf("Nine");
    }
}
int main(){
    int a = 3, b = 8;
    for(int i = a; i <= b; i++){
        solve(i);
        printf("\n");
    }
}

Input

3, 8

Output

Three
Four
Five
Six
Seven
Eight
raja
Updated on 08-Oct-2021 10:58:16

Advertisements