Using switch
/*numwords.c - print the English word representing a number
from 1 to 9.
Copyright (c) 1998 Matthew Belmonte.*/
/*print the words representing numbers*/
int main()
{
int n;
printf("Enter a positive integer from one to nine: ");
scanf("%d", &n);
switch(n)
{
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("You don't follow instructions well.");
}
putchar('\n');
return 0;
}
Next slide