Home > Backend Development > C++ > Write a C program to print numbers as words using elseif statement

Write a C program to print numbers as words using elseif statement

王林
Release: 2023-09-04 14:17:05
forward
1214 people have browsed it

Write a C program to print numbers as words using elseif statement

Question

How to print a given number in literal form using C programming language without using switch case?

Solution

In this program we check three conditions to print two digits in words-

  • if(no 99)

    • if(no99) p>

      The number entered is not a two-digit number

    • else if( no==0)

      Print the first number as zero

    • else if(no>=10 && no

      Print single digits in text

    • else if(no>=20 && no

      if(no == 0)

      Print two digits with text

    Program

    Live demonstration

    #include<stdio.h>
    #include<string.h>
    int main(){
       int no;
       char *firstno[]={"zero","ten","eleven","twelve","thirteen", "fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"};
       char *secondno[]={"twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninty"};
       char *thirdno[]={"one","two","three","four","five","six","seven","eight","nine"};
       printf("enter a number:");
       scanf("%d",&no);
       if(no<0 || no>99)
          printf("enter number is not a two digit number</p><p>");
       else if(no==0)
          printf("the enter no is:%s</p><p>",firstno[no]);
       else if(no>=10 && no<=19)
          printf("the enter no is:%s</p><p>",firstno[no-10+1]);
       else if(no>=20 && no<=90)
          if(no%10 == 0)
             printf("the enter no is:%s</p><p>",secondno[no/10 - 2]);
       else
          printf("the enter no is:%s %s</p><p>",secondno[no/10-2],thirdno[no%10-1]);
    return 0;
    }
    Copy after login

    Output

    enter a number:79
    the enter no is: seventy nine
    enter a number:234
    enter number is not a two digit number
    Copy after login

The above is the detailed content of Write a C program to print numbers as words using elseif statement. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template