functions

can be compiled but get wrong output(answer for power consumed)
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
#include <stdio.h>
#include <stdlib.h>

double HeaterPower(double voltage, double current);

int main()
{
    double v, i, P;
    
    printf("Enter voltage value: ");
    scanf("%f", &v);
    printf("Enter current value: ");
    scanf("%f", &i);
    P = HeaterPower (v, i);
    printf("The power that a heater consumed is: %.2f\n",P);
    
    system ("pause");
    return 0;
}
double HeaterPower(double voltage, double current)
{
       double k;
       k = voltage*current;
       return k;
}

Last edited on
try %lf instead of %f in your scanfs.
You are asking it to read a float into a double.
Last edited on
still, it doesn't work..
In that case, What results are you getting form what input?
input:
Enter voltage value= 3.5
Enter current value=2.5

output:
0.00
This is what I have
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
#include <stdio.h>
#include <stdlib.h>

double HeaterPower(double voltage, double current);

int main()
{
    double v, i, P;
    
    printf("Enter voltage value: ");
    scanf("%lf", &v);
    printf("Enter current value: ");
    scanf("%lf", &i);
    P = HeaterPower (v, i);
    printf("The power that a heater consumed is: %.23f\n",P);
    
    system ("pause");
    return 0;
}
double HeaterPower(double voltage, double current)
{
    double k;
    k = voltage*current;
    return k;
}

Enter voltage value: 3.5
Enter current value: 2.5
The power that a heater consumed is: 8.75
sh: pause: command not found


When the scanf has %f it does give a zero result.

you should be using %lf, %lg, %le or %la to read in doubles. Check you compilers documentation.
Last edited on
k..at last,i got it..tq,,
Topic archived. No new replies allowed.