int main(void)
{
int hexa = 16, deci , num , ans , quotient ,count=0;
int Bin1 , Bin2 , Bin3 , Bin4 , Bin5 , Bin6 , Bin7 , Bin8;
printf("\n\tDecimal to Hexadecimal Conversation");
printf("\n\t====================================");
printf("\n\tPlease enter decimal Value< 1 - 999999 > : ");
scanf("%d",&deci);
if ( deci < 1 )
{
printf("\n\tPlease enter value in range");
printf("\n\tPlease enter decimal Value< 1 - 999999 >: ");
scanf("%d",&deci);
}
else if ( deci >999999)
{
printf("\n\tPlease enter value in range");
printf("\n\tPlease enter decimal Value< 1 - 999999 >: ");
scanf("%d",&deci);
}
else
{
ans = deci / hexa;
quotient = deci % hexa;
1. I've seen a couple of problems today with scanf. Try using <iostream> stuff like cout or cin (unless your course is in C).
2. If you put something higher than 999999 you enter that elseif statement in line (you didn't use code tags and I am not counting). Maybe you weren't expecting that?
I noticed that if the user inputs a number higher than 999999 twice, the program will print out a hex for it. I'm sure that was not your intention. I tweaked the code a bit so that it uses a do/while. It shortens up your program, and ensures an expected range of numbers, though things do mess up if an alpha is entered.
// Hexi-Deci.cpp : Defines the entry point for the console application.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int main(void)
{
int hexa = 16, deci , num , ans , quotient ,count=0;
int Bin1 , Bin2 , Bin3 , Bin4 , Bin5 , Bin6 , Bin7 , Bin8;
do
{
printf("\n\tDecimal to Hexadecimal Conversation");
printf("\n\t====================================");
printf("\n\tPlease enter decimal Value < 1 - 999999 > : ");
scanf("%d",&deci);
if (deci>=1 && deci <=999999)
{
ans = deci / hexa;
quotient = deci % hexa;
printf("\n\t16 > %d - %d" , deci , quotient );
printf("\n\t _______ ");
printf("\n\t %d" , ans);
printf("\n\n\tThe Hexadecimal equivalent of decimal %d = %X ", deci , deci);
}
else
printf("\n\tPlease enter value in range of 1 to 999999 only.\n");
} while (deci<1 || deci > 999999);
getche();
printf("\n\n\t");
return 0;
}