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