My problem is this.
First I need to read a single integer type digit from the keyboard.
Then I need to print that digit in character form.ie. if I entered 1 ,I need to get the character constant '1' as output.
In the second case I need to read a character from keyboard .If it is any one of characters 0 - 1, it will printed in the decimal form.
ie. if I enter character 1 , I need to get decimal value 1 as output.
The program is simple but I dont get correct output.
ie. I got output of first case .
In the second case I 'm not getting any output.ie program doesn't stops.
I checked whether there is an infinite loop by putting a printf() statement .
But there is no infinite loop.
Then what is my problem.
Code is this:
Code:
#include<stdio.h>
main()
{ int digit , n = 0;
char ch;
printf("ENTER DIGIT:");
scanf("%d",&digit);
for( ch = 48;n < digit;++n , ch++ );
printf("ENTERED DIGIT IS %c \n",ch);
printf("NOW ENTER A CHARACTER, IF IT IS A DIGIT THAT DIGIT IS PRINTED IN DECIMAL FORM:");
scanf(" %c ",&ch);
for( digit = 0 ; digit < ch - 48 ; digit++ );
printf("ENTERED CHARACTER IS %d \n",digit);
}
OS Linux
compiler g++