when the number user key in is more than 999999 it dosent seems to work

#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;

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;


printf("\n\t16 > %d - %d" , deci , quotient );
printf("\n\t _______ ");
printf("\n\t %d" , ans);


}







printf("\n\tThe Hexadecimal equivalent of decimal %d = %X ", deci , deci);

getche();
return 0;
}



Try declaring your variables as long instead of int.
@ Wisely Done: The UB for (signed) integers is over 2bil. 999999 isn't going to be a problem.

@ Valz: I'm not sure what your code is supposed to do, and you're not telling us what's going wrong. Please be more specific.
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 else if statement in line (you didn't use code tags and I am not counting). Maybe you weren't expecting that?
I think the problem is in else if ( deci >999999). Just make the number higher and it should work.
Last edited on
@Valz

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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;
}
Topic archived. No new replies allowed.