0

this is the code i wrote for my school program my question is why wont the printf show the float value it comes up as inf ( which i believe is infinity?) i know my values previously are all int's which if why i put float in () anyways help is appreciated

cheerzx

#include <stdio.h>
#include <conio.h>

int main()
{
    int c1, c2, c3, parallelCap;
    float seriesCap;

    printf("please enter the value of the first resistor:");
    scanf(" %d", &c1);
    printf("please enter the value of the second rstr:");
    scanf(" %d", &c2);
    printf("please enter the value of the third resistor:");
    scanf(" %d", &c3);
    parallelCap = c1 + c2 + c3;
    seriesCap = (float)1/(1/c1 + 1/c2 + 1/c3);
    printf("the parallel capacitance is: %d the series 
    capacitance is: %f/n ", parallelCap, seriesCap);
getch();
return 0;
}
babySteps
  • 39
  • 8

1 Answers1

3

You are getting inf because each of 1/c1 + 1/c2 + 1/c3 evaluates to 0 when c1, c2 and c2 are greater than 1 and so you end up dividing 1 by 0. This is because it works as integer division. You can either cast each one of them separately to float or use 1.0 as numerator like this

seriesCap = 1.0/(1.0/c1 + 1.0/c2 + 1.0/c3);
xashru
  • 3,400
  • 2
  • 17
  • 30