marinator
marinator

Reputation: 31

calculate with double / float variables

It is really basic stuff, im just stuck and cant procede.

Im trying following calculation, but it just wont work:

#include<stdio.h>

int main(){

    double amount;
    double interest_rate;

    double tax_d = (amount * interest_rate) / 100;
    float tax_f = (amount * interest_rate) / 100;


    printf("\n write amount: ");
    scanf("%lf" , &amount);
    printf("\n write interest rate: ");
    scanf("%lf", &interest_rate);
    printf("Tax (double) : %lf\n" , tax_d);
    printf("Tax (float) : %lf\n" , tax_f);


    return 0;

}

This doesnt work properly:

double tax_d = (amount * interest_rate) / 100;
float tax_f = (amount * interest_rate) / 100;

Any help will be appreciated.

Upvotes: 1

Views: 707

Answers (1)

static_cast
static_cast

Reputation: 1178

double tax_d = (amount * interest_rate) / 100;
float tax_f = (amount * interest_rate) / 100;

These lines of code need to execute (be placed) after amount and interest_rate have been read and populated with user data

Specifically after these 2 lines:

scanf("%lf" , &amount);
scanf("%lf", &interest_rate);

It would also be good practice to initialize those values (i.e. double amount = 0 and double interest_rate = 0) upon their declaration..

*For future reference: Edit the question to be more specific as to what your inputs are and what you were expecting as output. This helps the community in driving to what the problem is and the solution.

Upvotes: 1

Related Questions