1. ## Calculations problem...?

It's really basic. Sorry for posting the whole program, but I have no idea where I've gone wrong.
While executing the bold statement below: If I type 100000 as the policy amount, I get -124 as the output. It should be 400. What have I done?? :-(

Code:
```#include<stdio.h>
int main(void)
{
char hlth, resi, sex;
int age,pol_amt;
float prem;

printf("Enter health grade, residence, sex, age:");
scanf( "%c %c %c %d", &hlth, &resi, &sex, &age);

if( hlth=='E' && (age>=25 && age<=35) && resi=='c')

{
printf("\nThe person can be insured.");
if( sex=='M' )
{
printf("\nThe maximum policy amount is 200000");
printf("\nEnter the policy amount:");
scanf("%d", &pol_amt);

prem= (pol_amt/1000)*4;

}
else if( sex=='F' )
{
printf("\nThe maximum policy amount is 100000");
printf("\nEnter the policy amount:");
scanf("%d", &pol_amt);

prem=(pol_amt/1000)*3;
}
}
else if( hlth=='P' && (age>=25 && age<=35) && resi=='v' && sex=='M')
{
printf("\nThe person can be insured.");
printf("\nThe maximum policy amount is 10000.");
printf("Enter the policy amount:");
scanf("%d", &pol_amt);

prem=(pol_amt/1000)*6;
}
else
{
printf("\nThe person is not insured.");
}

getch();
clrscr();
}```

2. Don't know what you are doing wrong, as I get 400.0000000. [But you are not returning anything at the end of main, you should add return 0].

--
Mats

3. Oh dear... I typed in a return 0, but still I get -124.00000 as the answer... Why exactly do I need a return 0? I've made programs without it and I've had no problems...

4. Originally Posted by Beachblue
Oh dear... I typed in a return 0, but still I get -124.00000 as the answer... Why exactly do I need a return 0? I've made programs without it and I've had no problems...
Because your program is supposed to give a value back.

I believe (after trying it out) that your problem is that "int" is only 16 bits (perhaps you are using the old Turbo C compiler, rather than a modern compiler?), which makes 100000 an overflow, so the resulting value is -31072, and of course, 4*-31 -> -124.

Either use a compiler that isn't so old that it would be be good if it was the age of Whiskey, or change your int to either long int, or float/double.

--
Mats

5. Thanks, using float did the trick.