How do this

Print the decimal, octal and hexadecimal values of all characters between the start
and stop characters entered by a user. For example, if the user enters an a and z, the
program should print all the characters between a and z and their respective
numerical values. Make sure that the second character entered by the user occur
later in the alphabet than the first character. If it does not, write a loop that
repeatedly asks the user for a valid second character until one is entered.

Recommended Answers

All 6 Replies

Read the list of format specifiers for printf() function. You will see that %d is decimal, %o is octal and %x is hex. With that information you should be able to write your program in just a few lines of code -- one line of code for the actual print statement.

#include <stdio.h>
void main ()
{
char char1,char2;

printf("Enter first character :");
scanf("%c", char1);
printf("Enter second character :");
scanf("%c", char2);

how can i declare lowercase n uppercase and how to Make sure that the second character entered by the user occur
later in the alphabet than the first character. If it does not, write a loop that
repeatedly asks the user for a valid second character until one is entered.

you can cast the entered value to int (this is the ascii code of that character) like below.

int i1 = (int)char1;
int i2 = (int)char2;

then you can compare i1 and i2. if i2 is upper than i1 you can continue. and you can control the ascii table, the A-Z and a-z are in a range. you can control i1 and i2 if they are in this range.

can i do like this

char char1,char2;
printf("\tEnter first character :");
scanf("\t%c", &char1);
printf("\tEnter second character :");
scanf("\n\t%c", &char2);

while (char2 < char1)
{
printf("\t!!!User must enter second character before the first\n");
printf("\tPlease enter valid second character :");
scanf("\n\t%c", &char2);
}

printf("\n\tNumerical Values Between %c and %c\n",char1,char2);
printf("\t................................\n");
printf("\n\tChar\tDecimal\tOctal\tHex\n");
printf("\t=============================\n");

while (char1 <= char2)
{
printf("\t%c\t%d\t%o\t%x\n",char1,char1,char1,char1);
char1++;
}
getch();
}

>>how can i declare lowercase n uppercase
You have to use tupper() or tolower() to convert the character. scanf() won't do it for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.