Help fix a code- c language

Write a program to compute the current in an AC circuit that has three resistors R1, R2, and R3, in parallel. The voltage source is V. Test your solution with various voltage levels and resistor values. Execute and submit the program and the results in screen captures. Repeat the problem if the resistors are in series.

I started with the Series code first since its the easiest but I'm not getting the correct current output. It keeps saying the current is 0, regardless of what numbers I put in rather than doing the actual calculations.

Parallel formula:

Rt = (R1 || R2) || R3
I= V/Rt

For the code : current= V/ (1/(1/R1+1/R2+1/R3));

Series formula:

Rt= R1+R2+R3
I= V/Rt

For the code : current = V/ (R1 + R2 + R3) ;


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
#include <stdio.h>

int main(void)
{
    int R1; //First Resistor
    int R2; //Second Resistor
    int R3; //Third Resistor
    int V ; // Voltage Source

    printf(" Enter first resistor: \n"); //prompt for R1
    scanf( "%d", &R1);

     printf(" Enter second resistor: \n");//prompt for R2
    scanf( "%d", &R2);

     printf(" Enter third resistor: \n");//prompt for R3
    scanf( "%d", &R3);

     printf(" Enter the voltage source: \n");//prompt for V
    scanf( "%d", &V);

int current;
current = current = V/ (R1 + R2 + R3) ; ;

printf("The current is: %d\n", current);


    return 0;
}



Last edited on
Integer division will truncate the fractional part, so if your expect current was 0.5A, the calculated current would be 0A, as integers can only store whole numbers.
To fix this, simply use doubles.

You have an extra semi-colon on line 23.
Topic archived. No new replies allowed.