# Calculating hypotenuse, missing remainder.

This is a discussion on Calculating hypotenuse, missing remainder. within the C Programming forums, part of the General Programming Boards category; Hello I'm a noob and I wrote this piece of code: Code: #include <stdio.h> #include <math.h> int CalcHyp(int cat1, int ...

1. ## Calculating hypotenuse, missing remainder.

Hello I'm a noob and I wrote this piece of code:

Code:
```#include <stdio.h>
#include <math.h>

int CalcHyp(int cat1, int cat2);
int CalPer(int l1, int l2, int l3);

int main()
{
int l1,l2,l3,cat1,cat2;
printf("Choose options in calculator: \n1 - perimeter \n2 - hypotenuse\n"
"Exit (any other key or letter)\n");
{
case 1:
printf("Insert the three lenghts:\n");
scanf("%d %d %d", &l1,&l2,&l3);
printf("This is the perimeter: %d\n\n",CalPer(l1,l2,l3));
main();
break;
case 2:
printf("Insert the two catheti:\n");
scanf("%d %d", &cat1,&cat2);
printf("This is the hypotenuse: %f\n", CalcHyp(cat1,cat2));
main();
break;
default:
printf("Exit\n");

}

}

int CalcHyp(int cat1, int cat2)
{
float Hyp;
Hyp = sqrt(pow(cat1,2)+pow(cat2,2));
return Hyp;
}

int CalPer(int l1, int l2, int l3)
{
int perimeter;
perimeter = l1+l2+l3;
return perimeter;
}```
When I want to calculate hypotenuse, I have the integer value of it and no remainder. I tried to make Hyp variable float type but I can't get it.

Hope you understand what's written in printfs cause I'm not a native speaker. Thanks for help.

2. Code:
`int CalcHyp(int cat1, int cat2)`
The compiler casts Hyp back to an int because that's what the function returns. Try making your function return a float (and the prototype at the top too, or course).

3. In addition to anduril's comment, here are some other things to consider:

- The "pow()" and "sqrt()" functions expect the double data type, not integer

4. Code:
```            printf("This is the perimeter: %d\n\n",CalPer(l1,l2,l3));
main();
break;```
Calling main() from inside a function is not a good idea. You end up with a recursive program that keeps tunnelling deeper and deeper until it runs out of stack space and crashes.

If you want your program to start over... use loops.

Also... the correct form of main is int main (void) and it returns a value (usually 0)...
Code:
```int main (void)
{

return 0;
}```

5. Originally Posted by Matticus
They start with a lower case ell, not a 1, though I agree it's difficult to tell. It wouldn't compile otherwise.

The "pow()" and "sqrt()" functions expect the double data type, not integer
The operands will be automatically promoted to doubles, so there's no problem there, though there is lost precision when it tries to stuff the double result into a single float.

@Blacklukes: You should change all your floats to doubles. Also, you said main would return an int, so return one. Zero is common for success.

6. Thank you very much for your answers, I modified the CalcHyp function and its prototype. I tried float and double and it works with both. But I still can't figure out how to start over the program. Can I do it keeping the switch/case or do I have to change it?

7. Originally Posted by CommonTater
Also... the correct form of main is int main (void) and it returns a value (usually 0)...
Blacklukes's definition of main is also valid though, especially with respect to C99. (But you knew that, didn't you )

8. I know float and double both work, but I recommend double for precision. As for starting the program over, look into loops. We have a tutorial here, and I'm sure any class notes or textbooks you have will cover them too. I would use a do while loop (since it always executes at least once, unlike while or for loops). Try something like:
Code:
```do
prompt user for calculation type
while answer is 1 or 2```

9. Thanks anduril462 now it loops. Thank all for support this was a great first time asking

10. Originally Posted by Blacklukes
Thank you very much for your answers, I modified the CalcHyp function and its prototype. I tried float and double and it works with both. But I still can't figure out how to start over the program. Can I do it keeping the switch/case or do I have to change it?

Code:
```do
{
}
while (not_exit_condition);```