Wednesday, February 10, 2016

C Program to convert given number (between 1 to 5) into words using switch.

#include<stdio.h>
int main ()
{
int number;
printf("Enter your number:");
scanf("%d",&number);
switch(number)
{
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;
default :printf("Invalid number\n" );
}
 return 0;
}

Output of the program:

Enter your number:5
Five

* * * * *

2 comments:

  1. will you give description about this program

    ReplyDelete
    Replies
    1. Dear visitor,
      - scanf() function allows us to enter a number, which is stored under a "number" variable.
      - Value of "number" will be used by switch statement as a input
      - if value of number=2, in that case, statement under "case 2" will be executed (printf("Two")).
      - if value of number=3, in that case, statement under "case 3" will be executed (printf("Three")).
      -Likewise all cases will be executed.
      -In case of no matching number, default statement will be executed.
      -Hence, you can interpret each case of switch statement as a kind "if statement for some value".

      Delete