Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my C program printing 0.000000 here?

I just started to learn C programming. In my book there is this piece of code:

/*Code Start*/
/*This code is use to find the simple interest*/

main ()
{
int p, n;
float r, si;

p = 1000;
n = 3;
r = 8.5;

si= p*n*r/100;
printf("%f", si);

}

/*Code end*/

The output i got was " 255.000000 "

I though i'll modify it with scanf function so i wrote this:

/*Code Start*/

main ()
{
int p, n;
float r, si;

printf("Enter value for p: \n");
scanf("%d", &p);
printf("Enter value for n: \n\n");
scanf("%d", &n);
printf("Enter valuse for r: \n\n");
scanf("%d", &r);

si= p*n*r/100;

printf("\nYour Simple Interest is %f\n\n", si);
}

/*Code End*/

No matter what values i give to p,n,r the answer i get is always 0.000000..
I also tried giving the values, p=1000, n=3, r=8.5 but still i get 0.000000..

like image 604
912M0FR34K Avatar asked Dec 01 '22 23:12

912M0FR34K


1 Answers

Change the specifier in scanf. You're using %d instead of %f:

scanf("%f", &r);
        ^
  • First side note: the code looks kind of bad (no return type for main ?!). Are you sure it's a good book ?
  • Second side note: using floats today is kind of pointless. Maybe you should use doubles ?
like image 104
cnicutar Avatar answered Dec 17 '22 16:12

cnicutar