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;
}
}
Related
It was challenging to word the title for this question, but I hope you can forgive me as I will elaborate here.
The problem i'm having is my C program floods the console with "Enter the student's grade: F" if I enter anything other than an integer. I'm new to C, so I don't understand how to check if the input is of the valid type.
int main() {
int grade; //number 0-10 associated with the letter grade
while (1) {
printf("Enter the student's grade: ");
scanf("%i", &grade);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}
Thanks for the help!
Check if scanf succeeded.
int nread = scanf("%i", &grade);
if (nread != 1) // scanf failed, start cleanup
{
scanf("%*[^\n]%*c");
}
Look for any book and you'll know that scanf returns number of elements that were successfully read, so if you type a letter, it will fail to read an integer and return 0 (nothing read), then you can know there's something wrong and discard wrong stuffs.
Since scanf don't read anything if it encounters an error, the wrong stuff will remain in the input buffer, and badly it'll break the next scanf, leading to an infinite output flushing.
P.S. You don't need to repeat the statements after case 5 4 3..., just merge them into one:
case 5: // Remove these and leave the last one there
case 4:
case 3:
case 2:
case 1:
case 0: printf("F \n"); break;
Check the return of scanf. 1 means an integer was successfully scanned. 0 means the input was not an integer. Clean the input stream and try again.
#include <stdio.h>
int main ( void) {
int grade; //number 0-10 associated with the letter grade
int valid = 0;
while (1) {
do {
printf("Enter the student's grade: ");
if ( 1 != ( valid = scanf("%i", &grade))) {// 1 is success
if ( EOF == valid) {
printf ( "found EOF\n");
return 0;
}
while ( '\n' != getchar ( )) {//clear input stream
}
}
} while ( !valid);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}
From the book "Programming in C"
Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display
nine three two
Remember to display “zero” if the user types in just a 0.
Its been hours and its still cant be solved.. Do anyone know how to? This is the code so far
#include <stdio.h>
int right_digit,number;
int main ()
{
scanf("%i",&number);
right_digit = number % 10;
switch (right_digit)
{
case '0':
printf("0");
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:
break;
}
number = number / 10;
return 0;
}
The first problem here is, you're (wrongly) trying to use the character representation of the integer numbers. In your code, right_digit is supposed to represent an integer digit, not a character literal.
You must not to use the ''s, just write
case 0:
...
case 1:
and so on.
Just to add a bit on your mistake, it was considering the corresponding integer values of the character literal '0', '1' and so on. For ASCII, they are equivalent to
case 48:
case 49:
.
.
which is not what you intended.
That said,
You need to put the modulo calculation and switch-case inside a loop and carry out the conversion for all the digits of the input integer.
You need to start printing from the beginning (MSB), currently , you're printing from LSB. (Hint: Start printing the result of the modulo operation)
printf("0"); should be printf("Zero ");, as per the requirement.
/*USING SWITCH CASE ...ALSO YOU CAN USE '0' and negative numbers */
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int rem,num,sum=0,rem1,num1,add;
printf("enter the number:\n");
scanf("%i",&num);
if(num<0)
{
printf("minus ");
num=-num;
}
if(num==0)
{
printf("zero");
}
while(num!=0)
{
rem=num%10;
num=num/10;
sum=sum*10 +rem;
}
/*printf("%i\n",sum);*/
while(sum!=0)
{
rem1=sum%10;
sum=sum/10;
switch(rem1)
{
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("invalid no");
}
}
return 0;
}
Hope this program will help you to understand the logic and I am also posting the solution for same problem using switch case....
/* Write a program that takes an integer keyed in from
* the terminal and extracts and displays each digit of the
* integer in English. So, if the user types in 932, the
* program should display >>> nine three two <<<.
* (Remember to display “zero” if the user types in
* just a 0.)
*/
/*USING IF-ELSE IF*/
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int rem,num,sum=0,rem1;
printf("enter the number:\n");
scanf("%i",&num);
if(num<0)
{
printf("minus ");
num=-num;
}
if(num==0)
{
printf("zero");
}
while(num!=0)
{
rem=num%10;
num=num/10;
sum=sum*10 +rem;
}
/*printf("%i\n",sum);*/
while(sum!=0)
{
rem1=sum%10;
sum=sum/10;
if(rem1==0)
{
printf("zero ");
}
else if(rem1==1)
{
printf("one ");
}
else if(rem1==2)
{
printf("two ");
}
else if(rem1==3)
{
printf("three ");
}
else if(rem1==4)
{
printf("four ");
}
else if(rem1==5)
{
printf("five ");
}
else if(rem1==6)
{
printf("six ");
}
else if(rem1==7)
{
printf("seven ");
}
else if(rem1==8)
{
printf("eight ");
}
else if(rem1==9)
{
printf("nine ");
}
else
{
printf("invalid no");
}
}
return 0;
}
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'm making this program using switch statements that will assign letter grades based on if the user enters numbers 0 - 10. If the user enters a number that is not 0-10, the program outputs an error message and has the user re-enter. However, if the user enters a character the program will loop at the default case. I want it to output the error message from the default case once, and have them re-enter if they enter a character. I'm not sure as to why it loops the default case when a character is entered though.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
}
}
return 0;
}
Try something like:
#include <stdio.h>
int main()
{
int grade;
int r=0;
while(r != 1)
{
scanf("%i", &grade);
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
r=1
break;
case 8:
printf("Your grade is a B\n");
r=1
break;
case 7:
printf("Your grade is a C\n");
r=1
break;
case 6:
printf("Your grade is a D\n");
r=1
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
r=1
break;
default:
printf("Invalid score, please re-enter\n");
break;
}
}
return 0;
}
This will clear the input buffer on an invalid input and allow a retry.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
if ( r != 1) {//r == 1 is successful input of integer
grade = -1;//reset grade on invalid input
}
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
while ( getchar() != '\n');//clear input buffer
}
}
return 0;
}
The reason your code always loops is because there is no way to exit out of your while other than to kill the program. Remember that break only breaks out of the inner-most switch or loop.
The cleanest way to break out of multiple levels is to use a flag. One way to do what you want is like this:
bool valid_grade = false;
while(!valid_grade && (r = scanf("%i", &grade)) != EOF)
{
valid_grade = true;
switch(grade)
{
case 10:
// unchanged from your code
default:
valid_grade = false;
printf("Invalid score, please re-enter\n");
}
}
newcomer to C here.
I seem to have run afoul on a problem that I have to do. The goal here is to make a program that writes out a two-digit number in words. Alternatively, the user can enter "Q" to quit. This is the bit that I am having trouble with. Can anyone point out what I am doing wrong?
#include <stdio.h>
int main(void)
{
int digit_one;
int digit_two;
enum state {fail, quit};
int status = fail;
printf("Enter a two-digit number or press Q to quit: ");
scanf("%1d%1d",&digit_one,&digit_two);
if(digit_one == 'Q'){
status = quit;
}
else {
if (digit_one == 1) {
switch(digit_two % 10) {
case 0: printf("You entered: Ten"); break;
case 1: printf("You entered: Eleven"); break;
case 2: printf("You entered: Twelve"); break;
case 3: printf("You entered: Thirteen"); break;
case 4: printf("You entered: Fourteen"); break;
case 5: printf("You entered: Fifteen"); break;
case 6: printf("You entered: Sixteen"); break;
case 7: printf("You entered: Seventeen"); break;
case 8: printf("You entered: Eighteen"); break;
case 9: printf("You entered: Ninteen"); break;
}
return 0;
}
switch(digit_one % 10) {
case 2: printf("You entered: Twenty-"); break;
case 3: printf("You entered: Thirty-"); break;
case 4: printf("You entered: Forty-"); break;
case 5: printf("You entered: Fifty-"); break;
case 6: printf("You entered: Sixty-"); break;
case 7: printf("You entered: Seventy-"); break;
case 8: printf("You entered: Eighty-"); break;
case 9: printf("You entered: Ninety-"); break;
}
switch(digit_two % 10) {
case 0: 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;
}
return 0;
}
}
You can't read Q into an integer variable. Read the input into a character one.
Problem is that your scanf reads a string and converts it to integer immediately (thats what %d does).
You need to first read only a string (with %s or similar) to a buffer and then check that it is not 'Q'. After that you can do the conversion to integer with sscanf.
See http://en.cppreference.com/w/c/io/fscanf
Edit: Also, your code completely ignores the quit status.