-2

I know my program is correct, but netbeans is not operating it the way it suppose to be. I ran the same program on android mobile c compiler app and it worked fine. I am new here so I cannot insert a photo to show you the output here, but please click the link provided to see the output my netbeans is giving me.
Please tell me what is wrong with my IDE and what can i do to fix it.

here is the program below:

#include <stdio.h>
#include <math.h>

int main() {
    int x = 1, q, n, p;
    float r, b, a;
    while (x <= 10) {
        printf("enter the value of principle\n");
        scanf("%d", &p);

        printf("enter the number of years\n");
        scanf("%d", &n);

        printf("enter number of times interest is added\n");
        scanf("%d", &q);

        printf("enter rate of interest\n");
        scanf("%d", &r);

        b = pow((1 + r / q), n * q);
        a = p * b;
        printf("amount=%f\n", a);
        x++;
    }
    return 0;
}

enter image description here enter image description here

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 3
    `scanf("%d",&r);` <<< wrong. `r` is declared as a `float` variable, so you have to use `scanf("%f",&r);`. Try turning on your compiler warnings. – r3mainer Sep 17 '18 at 09:43
  • @Observer That makes absolutely no difference. – melpomene Sep 17 '18 at 09:47
  • 1
    "*i know my program is correct*" [X] Doubt. – melpomene Sep 17 '18 at 09:47
  • @AustinSpark It would be worth spending a little time analyzing what went wrong in your program. When the `scanf()` function (and all its relatives) encounters an unexpected character, it will bail out without consuming this character. Since it was not expecting to find a period (`.`) when reading an integer, this was left in the input stream and caused all the subsequent `scanf("%d",...)` calls to fail. It's often a good idea to check the return value from this function. If it is zero (or any number less than the number of expected variable assignments), then you know something went wrong. – r3mainer Sep 17 '18 at 09:55
  • @AustinSpark "Why the same program works on Android Mobile C compiler app?" This is one of the tough lessons to learn about C programming -- or really about programming in general. There are sort of two definitions of "work": 1. Seems to "work", but by accident, even though it's got bugs, which just happen not to cause problems, or manifest differently on different platforms. 2. Guaranteed to work, works for the right reasons, works the same on *all* platforms. – Steve Summit Sep 17 '18 at 11:12

1 Answers1

0

this is just a very small error. The logic is totally fine. I think you overlooked just one tiny bit of an access specifier for the rate of interest. You have correctly and aptly declared it as a float. But while accepting the value from the user, you are accepting it as an Integer using %d. The compiler doesn't expect this and hence behaves unusually. Just replace The accepting rate of interest part with this:

   `printf("enter rate of interest\n");
    scanf("%f", &r);`

It should work fine now. Just attaching the output for you to be sure of it:

gcc version 4.6.3

enter the value of principle 5000

enter the number of years 2

enter number of times interest is added 4

enter rate of interest 2.5

amount=243106.703125

enter the value of principle

and yes, it does this 10 times as per your while loop.

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35