Sunday, August 2, 2009

How do i add decimal places to my C program?

/* calculating simple interest*/





# include %26lt;stdio.h%26gt;


main()


{


printf("\n\n\n");


printf("Calculating your simple interest.\n\n\n\n");


int principal, time,simpleinterest;


float rate;


printf("Enter the Principal amount.\n$");


scanf("%d", %26amp; principal);


printf("Enter the Rate. \n");


scanf("%f", %26amp; rate);


printf("Enter the time given in years.\n");


scanf("%d",%26amp; time);


simpleinterest= principal*rate*time /100;


printf("The simple Interest is $ %2d \n", simpleinterest);


}

How do i add decimal places to my C program?
Make simple interest, and principal amount float variables.
Reply:On your printf(("The simple Interest is $ %2d \n", simpleinterest); change the 2 to a point 2 (.2) that will tell the program how many decimal places you want the program to display.
Reply:try the following modified program of yours to show answers upto 3 decimal places.


#include %26lt;stdio.h%26gt;


main()


{


printf("\n\n\n");


printf("Calculating your simple interest.\n\n\n\n");


int principal, time;


float rate,simpleinterest;


printf("Enter the Principal amount.\n$");


scanf("%d", %26amp; principal);


printf("Enter the Rate. \n");


scanf("%f", %26amp; rate);


printf("Enter the time given in years.\n");


scanf("%d",%26amp; time);


simpleinterest= principal*rate*time /100;


printf("The simple Interest is $ %5.3f \n", simpleinterest);


}


No comments:

Post a Comment