M3-R4: January 2015 (O-Level)
Q. 5(b):
Write a program to print the value of numbers in words when number entered is in the range of 0 to 999.
Ans:
Number to Words printing program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(){
int num,unit_digit,ten_digit,hundred_digit;
char numbers[10][10] = {"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
char tens[10][10] = {"","Ten","Twenty","Thirty","Forty",
"Fifty","Sixty","Seventy","Eighty","Ninety"};
char teens[10][10] = {"Ten","Eleven","Twelve","Thirteen",
"Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen"};
printf("\nEnter a number between 0 and 999: ");
scanf("%d",&num);
if(num<0 || num>999){
printf("\nThe number is not between 0 and 999.");
getch();
exit(0);
}
unit_digit = num%10;
num=num/10;
ten_digit = num%10;
num=num/10;
hundred_digit = num;
printf("\n");
if(hundred_digit > 0){
printf("%s Hundred ",numbers[hundred_digit]);
}
if(ten_digit == 1){
printf("%s ",teens[ten_digit]);
}
else if(ten_digit > 1){
printf("%s ",tens[ten_digit]);
}
if(unit_digit == 0){
if(hundred_digit == 0 && ten_digit == 0){
printf("%s",numbers[unit_digit]);
}
}
else{
printf("%s",numbers[unit_digit]);
}
getch();
}
Q. 5(b):
Write a program to print the value of numbers in words when number entered is in the range of 0 to 999.
Ans:
Number to Words printing program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(){
int num,unit_digit,ten_digit,hundred_digit;
char numbers[10][10] = {"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
char tens[10][10] = {"","Ten","Twenty","Thirty","Forty",
"Fifty","Sixty","Seventy","Eighty","Ninety"};
char teens[10][10] = {"Ten","Eleven","Twelve","Thirteen",
"Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen"};
printf("\nEnter a number between 0 and 999: ");
scanf("%d",&num);
if(num<0 || num>999){
printf("\nThe number is not between 0 and 999.");
getch();
exit(0);
}
unit_digit = num%10;
num=num/10;
ten_digit = num%10;
num=num/10;
hundred_digit = num;
printf("\n");
if(hundred_digit > 0){
printf("%s Hundred ",numbers[hundred_digit]);
}
if(ten_digit == 1){
printf("%s ",teens[ten_digit]);
}
else if(ten_digit > 1){
printf("%s ",tens[ten_digit]);
}
if(unit_digit == 0){
if(hundred_digit == 0 && ten_digit == 0){
printf("%s",numbers[unit_digit]);
}
}
else{
printf("%s",numbers[unit_digit]);
}
getch();
}