-2

This is my code

#include <stdio.h>

int main() {
   float percentage;
   int sp;
   int bp;

   percentage = (sp-bp)/bp*100;

   scanf("%d %d", &sp, &bp );
   printf("%.2f%%", percentage);

   return 0;
}

Sample input :

150 85

Sample output :

76.47%

but my output is :

-100.00%

Can someone help me fix this?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 4
    Welcome to SO! Why is `percentage = (sp-bp)/bp*100;` above the `scanf` call that initializes the values? The program runs top to bottom, so this is doing arithmetic with a bunch of uninitialized variables. – ggorlen Sep 30 '19 at 00:20
  • 2
    @ggorlen That is among the issues. There's also the problem of performing arithmetic on integral types and expecting a result in a floating point type. – Christian Gibbons Sep 30 '19 at 00:36
  • Possible duplicate of [Why dividing two integers doesn't get a float?](https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float) – dandan78 Oct 01 '19 at 11:55

2 Answers2

0

First of all, you should really read the values before using them:

scanf("%d %d", &sp, &bp);
percentage = ...

Secondly, this:

percentage = (sp-bp)/bp*100;

Does an integer division, so the rest of the division is totally discarded and you will end up with:

(150 - 85) / 85 == 0
0 * 100 == 0. 

You should cast one end of the division to float before doing the operation:

percentage = (sp-bp)/(float)bp * 100;

Correct code:

#include <stdio.h>

int main() {
   float percentage;
   int sp;
   int bp;

   scanf("%d %d", &sp, &bp);

   percentage = (sp-bp)/(float)bp * 100;

   printf("%.2f%%", percentage);

   return 0;
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
-2

Explanation will add soon.

Code

#include <stdio.h>

int main()
{

   float percentage=0;
   int sp=0;
   int bp=0;

   scanf("%d%d", &sp, &bp );
   //printf("\n%d%d", bp,sp);

   percentage = (sp-bp)/(float)bp*100;


   printf("%.2f%%", percentage);


}

Edit

Explain what i changed

As you scanning two integer with one scanf() function. use scanf("%d%d", &sp, &bp );

Only remove space between two %d %d. This white space is useless, because scanf() with %d format specifier consume any number of white space. but if you want to scan with %c use that. scanf() consumes white space for all specifier except for %c, %n, %[…].

see more info on Jonathan Leffler answer

Initialize all variables like this (Declare and Define)

float percentage=0;
int sp=0;
int bp=0;

Because when we declare a variable inside function this is not initiate and have garbage value. This may cause undefined behavior.see C section

Suppose a situation that scanf() cant work properly and the integers sp and bp values remain with defaults. If we don't initiate them, garbage value remains.

Do declare and define at one time and initiate to zero. see more on declare define

Add (float) explicit conversion to the division.

(sp-bp)/(float)bp*100;

In C result number of An integer that divides to another integer converted to integer and you lost decimal part because that is truncated. see this

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31