Problem Statement
Write a C program which reads six key presses in six variables. If the first key press is ‘x’, then the remaining five key presses are meant to correspond to hexadecimal digits of a number starting from the most significant digit; else all the six key presses should correspond to digits of a decimal number. The program prints proper error message if the input does not conform to this specification. For the first kind of valid key presses, it prints the decimal equivalent of the hexadecimal number with proper message. For the second kind of valid key presses, it prints the decimal number with proper message.
Write a C program which reads six key presses in six variables. If the first key press is ‘x’, then the remaining five key presses are meant to correspond to hexadecimal digits of a number starting from the most significant digit; else all the six key presses should correspond to digits of a decimal number. The program prints proper error message if the input does not conform to this specification. For the first kind of valid key presses, it prints the decimal equivalent of the hexadecimal number with proper message. For the second kind of valid key presses, it prints the decimal number with proper message.
#include<stdio.h>
int power(int x);
int main()
{
int b,a[50],i;
long int c,num;
printf("enter the first digit \n");
b=getchar();
getchar();
if(b>=48&&b<=57)
{
printf("i got that this is a decimal number now enter the rest part\n");
scanf("%ld",&c);
printf("u entered a decimal integer which is ");
putchar(b);
printf("%ld\n",c);
}
else if(b==120)
{
printf("enter the rest part of hexadecimal number press enter in the end");
for(i=0;i<5;i++)
{
a[i]=getchar();
}
num=0;
for(i=0;i<5;i++)
{
if(a[i]>=48&&a[i]<=57)
{
num= num+ (power(4-i)*(a[i]-48));
}
else if( a[i]==97)
{
num= num+ (power(4-i)*10);
}
else if(a[i]==98)
{
num = num+ (power(4-i)*11);
}
else if(a[i]==99)
{
num =num+ (power(4-i)*12);
}
else if(a[i]==100)
{
num=num+ (power(4-i)*13);
}
else if(a[i]==101)
{
num=num+ (power(4-i)*14);
}
else if(a[i]==102)
{
num=num+ (power(4-i)*15);
}
}
printf("the decimal eqt. of number entered is %ld \n", num);
}
else
{
printf("enter a valid number");
}
return 0;
}
int power(int x)
{
int power,r;
power=1;
for(r=1;r<=x;r++)
{
power = 16*power;
}
return power;
}
No comments:
Post a Comment