I've written a program which asks the user for a two-digit number and it prints out the English word for the number.
Example:
Enter a two digit number: 45
You entered fourty-five
Basically, what I have done is I've put a switch case for numbers 10 to 19, then used another switch case for the Ten's and another for the One's.
the problem is that after entering the number, for some reason it doesn't show anything after printing You entered the number:
here is the code:
#include <stdio.h>
int main(void) {
int Num, Tens, Ones;
printf("Enter a Two Digit Number: ");
scanf("%d", &Num);
printf("You entered the number: ");
if (10 <= Num && Num >= 19) {
switch (Num) {
case 10: printf("Ten\n"); break;
case 11: printf("Eleven\n"); break;
case 12: printf("Twelve\n"); break;
case 13: printf("Thirteen\n"); break;
case 14: printf("Fourteen\n"); break;
case 15: printf("Fifteen\n"); break;
case 16: printf("Sixteen\n"); break;
case 17: printf("Seventeen\n"); break;
case 18: printf("Eighteen\n"); break;
case 19: printf("Nineteen\n"); break;
}
}
if (20 <= Num && Num >= 99) {
Tens = Num / 10;
switch (Tens) {
case 2: printf("Twenty"); break;
case 3: printf("Thirty"); break;
case 4: printf("Fourty"); break;
case 5: printf("Fifty"); break;
case 6: printf("Sixty"); break;
case 7: printf("Seventy"); break;
case 8: printf("Eighty"); break;
case 9: printf("Ninety"); break;
}
}
Ones = Num % 10;
if (Ones == 0)
printf("\n");
else
if (1 <= Ones && Ones >= 9) {
printf("-");
switch (Ones) {
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;
}
printf("\n");
}
return 0;
}
Your conditions are wrong and will always be false in normal execution.
Try these modification:
10 <= Num && Num >= 19 -> 10 <= Num && Num <= 19
20 <= Num && Num >= 99 -> 20 <= Num && Num <= 99
1 <= Ones && Ones >= 9 -> 1 <= Ones && Ones <= 9
Besides the issues with the operators, consider replacing the switch with look-up tables, for the sake of performance:
#include <stdio.h>
void englishize (int n)
{
static const char* const TEXTUAL_0_9 [] =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
};
static const char* const TEXTUAL_10_19 [] =
{
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
};
static const char* const TENS [] =
{
"zero",
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
if((n % 10) == 0) // divisible by ten
{
printf("%s\n", TENS[n/10]);
return ;
}
else if(n >= 20) // all numbers from 20 and above behave logically
{
printf("%s-%s\n", TENS[n/10], TEXTUAL_0_9[n%10]);
}
else if(n >= 10) // special case for numbers between 10-19
{
printf("%s\n", TEXTUAL_10_19[n-10]);
}
else // n < 10
{
printf("%s\n", TEXTUAL_0_9[n]);
}
}
int main ()
{
for(int i=0; i<100; i++)
{
englishize(i);
}
return 0;
}
All your conditions are incorrect. As a matter of fact, you can simplify the code and remove most of the tests:
#include <stdio.h>
int main(void) {
int Num, Tens, Ones;
printf("Enter a Two Digit Number: ");
scanf("%d", &Num);
printf("You entered the number: ");
switch (Num) {
case 0: printf("Zero\n"); break;
case 10: printf("Ten\n"); break;
case 11: printf("Eleven\n"); break;
case 12: printf("Twelve\n"); break;
case 13: printf("Thirteen\n"); break;
case 14: printf("Fourteen\n"); break;
case 15: printf("Fifteen\n"); break;
case 16: printf("Sixteen\n"); break;
case 17: printf("Seventeen\n"); break;
case 18: printf("Eighteen\n"); break;
case 19: printf("Nineteen\n"); break;
}
Tens = Num / 10;
switch (Tens) {
case 2: printf("Twenty"); break;
case 3: printf("Thirty"); break;
case 4: printf("Fourty"); break;
case 5: printf("Fifty"); break;
case 6: printf("Sixty"); break;
case 7: printf("Seventy"); break;
case 8: printf("Eighty"); break;
case 9: printf("Ninety"); break;
}
Ones = Num % 10;
if (Ones > 0) {
if (Tenths > 2)
printf("-");
switch (Ones) {
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;
}
}
printf("\n");
return 0;
}
Related
Why didn't I get the output of the array? I want to save the value to array,m[10], by switch case,but i can't print out the value of array.
#include <stdio.h>
/******************************************
* 公元年分非4的倍數,為平年。
* 公元年分為4的倍數但非100的倍數,為閏年。
* 公元年分為100的倍數但非400的倍數,為平年。
* 公元年分為400的倍數為閏年。
*****************************************/
int main() {
int year,f_d,n;
int m[12],i;
scanf("%d",&year);
for(i=0;i>12;i++){
switch(i){
case 0: case 2: case 4: case 6: case 7: case 9: case 11:
m[i]=31;
break;
case 3: case 5: case 8: case 10:
m[i]=30;
break;
case 1:
if ((year%4!=0)||
((year%100==0)&&(year%400!=0)))
m[i]=28;
else
m[i]=29;
break;
default:
m[i]=0;
}
}
for(i=0;i>12;i++)
printf("%d/n",m[i]);
return 0;
}
The very first loop
for(i=0;i>12;i++)
does not do what you want: when i is set to 0, it is not greater than 12 so the whole loop is skipped.
replace for (i = 0; i > 12; i++) to for (i = 0; i < 12; i++) will work.
So Here I am trying to write a code, that converts a numerical number to its spelling format. For example, the user inputs any number like 320, then the output should be "Three Two Zero". Following is what I Have tried-
#include <stdio.h>
void main(){
long int num,rev=0 ;
printf("Enter any number to print in words: ");
scanf("%ld",&num);
while(num!=0){
rev=(rev*10)+(num%10);
num/=10 ;
}
while(rev!=0){
long int x=rev%10;
switch(x){
case 0:
printf("Zero ");
break;
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;
}
}
}
Now the problem is that, this code is producing an infinite loop, like I input a number say 21, then it starts printing "Two Two Two Two........." till infinity.
Please Help me in resolving this question.
You need something that will make "rev!=0" true, which is rev = rev / 10 after the end of the switch. But creating an array and assigning zero, one, two, etc. and calling them using index could be better, I think you can think about this.
#include <stdio.h>
void main(){
long int num,rev=0 ;
printf("Enter any number to print in words: ");
scanf("%ld",&num);
while(num!=0){
rev=(rev*10)+(num%10);
num/=10 ;
}
while(rev!=0){
long int x=rev%10;
switch(x){
case 0:
printf("Zero ");
break;
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;
}
rev = rev / 10;
}
}
And this is array approach:
#include <stdio.h>
void main(){
long int num,rev=0 ;
printf("Enter any number to print in words: ");
num = 123456789;
const char arr[10][6]= {
"Zero", "One", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
while(num!=0){
rev=(rev*10)+(num%10);
num/=10 ;
}
while(rev!=0){
long int x=rev%10;
printf("%s ", arr[x]);
rev = rev / 10;
}
}
Here is a program to print Roman Numeral values up to a
hundred everything works fine except for when i==88, on this line tab indents one extra set to stop.
I sort of understand why that is 8 character.
How can I fix that ?
Thank you.
int main() {
unsigned int i, tensDigit, singleUnit;
printf( "Roman Numeral\t\t\t\tDecimal\n");
for ( i = 1; i <= 100; i++ ) {
tensDigit = i / 10;
singleUnit = i % 10;
switch ( tensDigit ) {
case 0:
break;
case 1:
printf("X");
break;
case 2:
printf("XX");
break;
case 3:
printf("XXX");
break;
case 4:
printf("XL");
break;
case 5:
printf("L");
break;
case 6:
printf("LX");
break;
case 7:
printf("LXX");
break;
case 8:
printf("LXXX");
break;
case 9:
printf("XC");
break;
case 10:
printf("C");
break;
default:
break;
}
switch ( singleUnit ) {
case 0:
printf("\t\t\t\t%d\n", i);
break;
case 1:
printf("I\t\t\t\t%d\n", i);
break;
case 2:
printf("II\t\t\t\t%d\n", i);
break;
case 3:
printf("III\t\t\t\t%d\n", i);
break;
case 4:
printf("IV\t\t\t\t%d\n", i);
break;
case 5:
printf("V\t\t\t\t%d\n", i);
break;
case 6:
printf("VI\t\t\t\t%d\n", i);
break;
case 7:
printf("VII\t\t\t\t%d\n", i);
break;
case 8:
printf("VIII\t\t\t\t%d\n", i);
break;
case 9:
printf("IX\t\t\t\t%d\n", i);
break;
}
}
return 0; }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying write a C program to print the string form for an integer. E.g 1234 -> one two three four. Is there a function that would convert it or would I make an array to do this?
A simple recursive solution:
void print_number(unsigned x) {
static const char *digit[10] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
if (x >= 10) {
print_number(x / 10);
fputc(' ', stdout);
}
fputs(digit[x % 10], stdout);
}
I dont think you have a direct function to convert to text. But you can do something like this
C code to covert each digits of a number in English word
#include<stdio.h>
int main(){
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number){
digit = number %10;
number = number /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}
Sample output:
Enter any integer: 23451208
two three four five one two zero eight
Also this is previously asked question. So refer to the link Convert digit to text in C
There is no function to do this . You need to do it Manually .I am giving you the Code that will Help you.
#include<stdio.h>
void check_print(int k);
int main()
{int n,i,m;
printf("Enter any number");
Scanf("%d",&i);
n=i;
while(n!=0)
{
m=n%10;
check_print(m);
n=n/10;
}
}
void check_print(int k)
{
switch(k)
{
case 0:
printf("zero");
break;
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;
}
}
First post in this awesome site!
So, I'm pretty much a dabbler in C language, and I am making a program that could return the day at the user-entered date. I didn't use any functions or structures, for I want to build my basic concepts in C first, which here are, if-else, switch statements, etc...
The program compiled quickly & ran w/o errors, but the answer is delayed by 1 day ;) I mean, when I entered '21 7 1993', which was a 'Wednesday', I'm getting 'Thursday'. Similarly for others.
I'm sure there's a fault in the concept. Please help me sort it out.Any comments welcome on the code below:
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
char flag='0';
int main()
{
clrscr();
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{ if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
/* Leap Year */
if(yr%400==0 && yr%100!=0)
flag='1';
/* month */
switch(month)
{
case 1:
{odd+=(date%7); break; }
case 2:
{odd+=(date%7);break;}
case 3:
{ odd+=((date%7)+3);
if(flag=='1')
odd+=1;
break ;
}
case 4:
{ odd+=((date%7)+6);
if(flag=='1')
odd+=1;
break; }
case 5:
{ odd+=((date%7)+8);
if (flag=='1')
odd+=1;
break;
}
case 6:
{ odd+=((date%7)+11);
if (flag=='1')
odd+=1;
break; }
case 7:
{ odd+=((date%7)+13);
if(flag=='1')
odd+=1;
break; }
case 8:
{ odd+=((date%7)+16);
if(flag=='1')
odd+=1;
break ;}
case 9:
{ odd+=((date%7)+19);
if(flag=='1')
odd+=1;
break;}
case 10:
{ odd+=((date%7)+21);
if(flag=='1')
odd+=1;
break;}
case 11: {
odd+=((date%7)+24);
if(flag=='1')
odd+=1;
break; }
case 12:
{ odd+=((date%7)+26);
if(flag=='1')
odd+=1;
break;
}
}
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
getch();
return 0;
}
I think you are having some problem in your switch case!. Try the following changes, It is some what different logic from yours-
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
int main()
{
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{
if(yr == 2000) // Note this change also
yr_new=yr-1900;
else if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
yr_latest=yr_latest-1; // Here i am leaving the current year in odd days calculation.
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
for(i=1;i<month;i++) // this logic is to calculate the odd days for the current year.
{
switch(i)
{
case 1:
odd+=3;
break;
case 2:
if((yr_latest+1)%4 == 0)
odd+=1;
else odd+=0;
break;
case 3:
odd+=3;
break;
case 4:
odd+=2;
break;
case 5:
odd+=3;
break;
case 6:
odd+=2;
break;
case 7:
odd+=3;
break;
case 8:
odd+=3;
break;
case 9:
odd+=2;
break;
case 10:
odd+=3;
break;
case 11:
odd+=2;
break;
case 12:
odd+=3;
break;
}
}
odd+=date;
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
return 0;
}
Current year means,if your input is '21 7 1993', first i am calculating odd days till '12.12.1992', then i am calculating the odd days for the remaining days!