Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

stuck with first c program

toku1991

New Coder
Hey people,
i was playing around with my first c program and i have a strange thing happening
when output is 0.001 the value is 1000 nOhm but it should be 100mOhm

C:
// calculate resistance and print out how much omh it is
   case 1:
            printf("what is your Voltage?\n");
            scanf("%Lf", &volt);
            printf("what is your Amperage?\n");
            scanf("%Lf", &ampere);
            output = volt / ampere;
            if (output >= 0.0000000001 && output < 0.000001) {
                printf("%0.0Lf %s\n", output * 1e6, "nOhm"); // Convert to nano
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is
            }
            if (output >= 0.000001 && output < 0.001) {
                printf("%0.0Lf %s\n", output * 1e6, "uOhm"); // Convert to micro
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is

            }
            if (output >= 0.001 && output < 1 ) {
                printf("%0.0Lf %s\n", output * 1e3, "mOhm"); // Convert to mili
                printf("%0.9Lf %s\n", output, "Ohm"); // Display as is
            }
            if (output > 1) {
                printf("%0.0Lf Ohm\n", output);
            }

            break;
 
Dividing with floats does not always produce accurate results. This is because at some point the division is rounded due the amount of decimals. I cant explain it better in english.

Next code is not tested, but i think it gets you atleast closer to what you need. Hope it help.


C:
#include <stdio.h>
#include <float.h>

int main() {
    double volt, ampere, output;
    int rounding_mode;

    // calculate resistance and print out Ohm
    printf("What is your voltages? ");
    scanf("%lf", &volt);
    printf("What is your ampers? ");
    scanf("%lf", &ampere);
    output = volt / ampere;

    // set the rounding mode to as "nearest"
    rounding_mode = fesetround(FE_TO_NEAREST);

    // convert to Ohm
    if (output >= 0.0000000001 && output < 0.000001) {
        printf("%0.0lf nOhm\n", output * 1e6);
    } else if (output >= 0.000001 && output < 0.001) {
        printf("%0.0lf uOhm\n", output * 1e6);
    } else if (output >= 0.001 &&
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom