I wrote a program that will ask for an integer input (1 - 9999) and will convert the inputted integer into its corresponding word format in English.
And I'm trying to modify it so that switch statements will be used instead of if-statements (where it is applicable).
Example 1:
Input number: 2481
Output: two thousand four hundred eighty one
Here is my code:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
//&& - check if within the range
//|| - check if outside of the range
if (num < 1 || num > 9999)
printf("Invalid number.");
else
//if (num > 0 && num < 10000)
{
thousands = num / 1000;
hundreds = num % 1000 / 100;
tens = num % 1000 % 100 / 10;
ones = num % 1000 % 100 % 10;
//if (num / 1000 == 1)
if(thousands == 1)
printf("one thousand ");
if(thousands == 2)
printf("two thousand ");
if(thousands == 3)
printf("three thousand ");
if(thousands == 4)
printf("four thousand ");
if(thousands == 5)
printf("five thousand ");
if(thousands == 6)
printf("six thousand ");
if(thousands == 7)
printf("seven thousand ");
if(thousands == 8)
printf("eight thousand ");
if(thousands == 9)
printf("nine thousand ");
//if (num % 1000 / 100 == 1)
if(hundreds == 1)
printf("one hundred ");
if(hundreds == 2)
printf("two hundred ");
if(hundreds == 3)
printf("three hundred ");
if(hundreds == 4)
printf("four hundred ");
if(hundreds == 5)
printf("five hundred ");
if(hundreds == 6)
printf("six hundred ");
if(hundreds == 7)
printf("seven hundred ");
if(hundreds == 8)
printf("eight hundred ");
if(hundreds == 9)
printf("nine hundred ");
//if (num % 1000 % 100 / 10 == 1)
if(tens == 1)
{
//if (num % 1000 % 100 % 10 == 0)
if(ones == 0)
printf("ten ");
if(ones == 1)
printf("eleven ");
if(ones == 2)
printf("twelve ");
if(ones == 3)
printf("thirteen ");
if(ones == 4)
printf("fourteen ");
if(ones == 5)
printf("fifteen ");
if(ones == 6)
printf("sixteen ");
if(ones == 7)
printf("seventeen ");
if(ones == 8)
printf("eighteen ");
if(ones == 9)
printf("nineteen ");
}
if(tens == 2)
printf("twenty ");
if(tens == 3)
printf("thirty ");
if(tens == 4)
printf("forty ");
if(tens == 5)
printf("fifty ");
if(tens == 6)
printf("sixty ");
if(tens == 7)
printf("seventy ");
if(tens == 8)
printf("eighty ");
if(tens == 9)
printf("ninety ");
if (tens != 1)
{
if(ones == 1)
printf("one ");
if(ones == 2)
printf("two ");
if(ones == 3)
printf("three ");
if(ones == 4)
printf("four ");
if(ones == 5)
printf("five ");
if(ones == 6)
printf("six ");
if(ones == 7)
printf("seven ");
if(ones == 8)
printf("eight ");
if(ones == 9)
printf("nine ");
}
}
//else
//printf("Invalid number.");
getch();
}
However, when I tried substituting the if statements into switch statements it displays a blank.
I'm stuck at the thousands place. I'm trying to fix it first before getting to the hundreds, tenths, and ones.
Here is what I tried:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
if (num < 1 || num > 9999)
printf("Invalid number.");
else
{
thousands = num / 1000;
switch (thousands)
{
case '1': printf("one thousand ");
break;
case '2': printf("two thousand ");
break;
case '3': printf("three thousand ");
break;
case '4': printf("four thousand ");
break;
case '5': printf("five thousand ");
break;
case '6': printf("six thousand ");
break;
case '7': printf("seven thousand ");
break;
case '8': printf("eight thousand ");
break;
case '9': printf("nine thousand ");
break;
}
}
}
When you write case '1':, you compare to character '1''s value (converted to an integer), not integer; you should write case 1:.
You're not comparing the same values you were before.
In your original code, you were comparing thousands against 0, 1, 2, etc. In your updated code, you're comparing thousands against '0', '1', '2', etc.
Use the same values you had before and you'll get the expected results.
Single quotes in C are used to define single characters. Since characters size is 1 byte in C, a char can be assigned up to 128 different values (1 byte = 8 bits = 2⁸ = from 0 to 127). But how are char represented, according to that integer value?
That is implementation-defined, but usually C uses the seven-bit US ASCII character set to represent char by default (for example, you might change the locale to have it use something different, like utf-8).
Back to your question, in the second snippet you're comparing the value of thousands with the ASCII values of the characters '1', '2', '3', etc.
That means you are not comparing thousands with the value between single quotes, but with the character's ASCII decimal value corresponding to the character, so for example, case '1': equals to case 49:
decimal - character
[...]
49 1
50 2
51 3
52 4
53 5
54 6
[...]
Practical Example
The following program shows just what I've said:
#include <stdio.h>
int main(int argc, char** argv)
{
char ch = '1';
printf("size of a char: %ld bytes\n", sizeof(char));
printf("character representation: %c\n", ch);
printf("character corresponding value in ASCII table: %d\n", ch);
return 0;
}
Output:
size of a char: 1 bytes
character representation: 1
character corresponding value in ASCII table: 49
Related
I need to print the digit from the first to the last without use arrays, binary operations or recursion. In addition every digit should be print as a text.
#include <stdio.h>
int main () {
int a,x;
printf("Give a number : ");
scanf("%i", &a);
do {
switch ( x % 10 ) {
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;
default:
printf("Error \n");
break;
}
x /= 10;
} while ( a );
printf("\n");
}
The output for 123 is three two one. The desirable: one two three.
Any ideas?
Reverse the number before passing it to the Switch statement. See if the following program works. i haven't tested it yet.
#include <stdio.h>
int main () {
int a, temp, reverse = 0;
printf("Give a number : ");
scanf("%i", &a);
temp = a;
while (temp != 0)
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
do {
switch ( reverse % 10 ) {
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;
default:
printf("Error \n");
break;
}
reverse /= 10;
} while ( reverse );
printf("\n");
}
You can count the digits in number and then use the / instead %.
For example, for 123 there are 3 digits, so you calculate pow (10,3-1) that is equal to 100. Then, 123/100 is equal to one. After that new number get 123%100 and next time you divide by 100/10. So 23/10 is equal to 2. Until you reach the less than 10 number and print that at the end. So 3 printed finally.
I have made a program that can output whatever numeral is inputted in to it but it will convert it to words instead of just numerals for example if you input "1234.56" it will convert it to "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". The cents should always be in numerals. So far everything works great, however if I put in an amount that is less that one thousand I will get the excess words such as "Thousand" or "Hundred" in there. For example if I input "15.77" my output will be "Thousand Hundred Fifteen Dollars and ... 77 Cents".
I don't want the Thousand or Hundred to be there, without those it would be perfect!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.01 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 )
{
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Ninteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
void printNum3(int x)
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
I have been coding for exactly one month now so if it seems like I made simple mistakes that's why.
You need to add conditionals around your printfs:
if (a > 0)
{
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
}
The problem is that you are unconditionally printing the "Thousand", "Hundred", etc...
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
if the number you send to printNum is zero, you don't want to print out your text string, you'd have to check for that condition:
/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
printf(/* whatever string is appropriate */);
}
as an example, say the input was 512.26
512.26/1000 <<< that should be 512.26/ 1000.f so both operands of the divide are floats
will result in a float value of : 0.51226f NOT 0.0f
using the floor(0.51226f) Will result in 0.0f
Suggest always applying the floor() function to yield a 0.0f
Even with a float, a comparison such as if( value != 0.0f ) then print it
would work after applying the floor() function.
I've made a program that is a cheque generator. Whatever amount you input in the scanf will output in words. For example if I were to to input "1234.56" My output will be "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents", or if I wanted to input the amount "0.01" the output will be "Zero Dollars and ... 1 Cents". The program works perfectly, however there is a minor issue, if I wanted to input the amount "9909" it will output "Nine Thousand Nine Hundred Ninety Nine Dollars and ... 0 Cents". The output should be "Nine Thousand Nine Hundred Nine Dollars and ... 0 Cents". Please help!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.00 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 ){
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
/*Printing if the variables are in the thousands, hundreds, tens or ones categories.*/
if (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... %d Cents\n", printcents);
}
else{
if (a > 0){
printNum(a);
printf("Thousand ");
}
if (b > 0){
printNum(b);
printf("Hundred ");
}
printNum2(c);
if (d >= 0){
printNum(d);
printf("Dollars and ... ");
}
printf("%d", printcents);
printf(" Cents\n");
}
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Nineteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
I have been coding for a month now so I apologize for the simple errors.
After you have calculated b and adjusted inclusive so it is now 9, you get to your else case, which sets c to 9 (hence the Ninety output) and d to 0, but immediately afterwards sets d to 9 (hence the Nine in the output). I think you mixed up c and d in the else case.
I have an assignment were I have to write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. I'm not able to use arrays or recursion, we're just starting with programming.
For example:
"123" returns "one two three"
My program is working well (for the most part), but the problem is that when you enter something like "0123" in the terminal the program returns "eight three"... WTH??
This is my code:
// Program that takes an integer and displays each digit in English
#include <stdio.h>
int main (void)
{
int num, digit;
int reversed = 0, backupZero = 0;
printf("Please enter an integer:\n");
scanf("%i", &num);
if (num == 0) // In case the input is just "0"
{
printf("zero");
}
while (num > 0) // Loop to reverse the integer
{
digit = num % 10;
reversed = (reversed * 10) + digit;
if ((reversed == 0) && (digit == 0)) // If the integer finishes in zero
{
++backupZero; // Use this to add extra zeroes later
}
num /= 10;
}
while (reversed > 0)
{
digit = reversed % 10;
reversed /= 10;
switch (digit)
{
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("zero ");
break;
}
}
for (int counter = 0; counter < backupZero; ++counter) // Prints the extra zeroes at the end
{
printf("zero ");
--backupZero;
}
printf("\n");
return 0;
}
Probably is something on the mathematics, I admit I'm not good at it.
When you read in the number with
scanf("%i", &num);
You are letting scanf infer the base of the number. Numbers starting with 0 followed by other digits are interpreted as octal. So 0123 is not the same as 123. It is in fact, 83.
0100 = 64
020 = 16
03 = 3
---------
0123 = 83
To read the number as base 10, use
scanf("%d", &num);
If you want to handle numbers that start with '0', then I suggest that you read the user input as a string (array of characters) rather than as an integer.
In addition to that, instead of "doing a switch" on each character, you can use a simple array in order to map the correct word to each digit.
Here is one way for implementing it:
#include <stdio.h>
#define MAX_INPUT_LEN 100
const char* digits[] = {"zero","one","two" ,"three","four",
"five","six","seven","eight","nine"};
int main()
{
int i;
char format[10];
char str[MAX_INPUT_LEN+1];
sprintf(format,"%c%us",'%',MAX_INPUT_LEN); // now format = "%100s"
scanf(format,str); // will write into str at most 100 characters
for (i=0; str[i]!=0; i++)
{
if ('0' <= str[i] && str[i] <= '9')
printf("%s ",digits[str[i]-'0']);
else
printf("invalid character ");
}
return 0;
}
Oh, wow. It took me 3 or 4 hours to write following code. I'm into c only first week, so please be considerate.
Update: added working minus + some comments.
#include <stdio.h>
#include <math.h>
int main(void)
{
int num, count, user, out;
count = 0;
printf("Type in any int: ");
scanf("%d", &num);
// adding minus to the beginning if int is negative
if (num < 0)
{
num = -num;
printf("minus ");
}
user = num;
// creating a power to the future number
while (num != 0)
{
num = num / 10;
count++;
}
int i2;
i2 = count;
// main calculations: dividing by (10 to the power of counter) and subtracting from the initial number
for (int i = 0; i < i2; i++)
{
out = user / pow(10, count - 1);
user = user - out * pow(10, count - 1);
count--;
switch (out)
{
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;
case 0:
printf("zero ");
break;
default:
break;
}
}
printf("\n");
return 0;
}
There are some mistakes:
if ((reversed == 0) && (digit == 0)) (incorrect)
if ((reversed == 0) || (digit == 0)) (correct)
And in the last loop you should remove
--backupZero;
And code will read numbers better
Hi im trying to understand why if 9 or above is entered for judge it passes but it shouldnt cause the if says >= 4 and <= 8
Thanks
while(!(judge >= 4) && (judge <= 8))
{
printf("How many judges are there ? Enter a number between 4 - 8 \n");
scanf("%d", &judge);
while(!(judge >= 4) && (judge <= 8))
{
printf("You entered %d Enter a number between 4 - 8 \n", judge);
scanf("%d", &judge);
if((judge >= 4) && (judge <= 8))
{
break;
}
}
}
It looks like you are missing a pair of parentheses in
while(!((judge >= 4) && (judge <= 8)))
^ ^
(This mistake appears in two places.)
By the way, you can avoid a lot of the repetition by restructuring your code like so:
printf("How many judges are there ? Enter a number between 4 - 8 \n");
for (;;) {
scanf("%d", &judge);
if (judge >= 4 && judge <= 8) {
break;
}
printf("You entered %d Enter a number between 4 - 8 \n", judge);
}