Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

c - my simple intrest calculator is not working for some reason

my simple intrest calculator that I create used c programming is not working for some reason. i want to bring me the answer but always returns zero and i literally don't know what should I do. I am kinda new to c and I really cannot figur it out

i would really appreciate if any of you could help me out. thanks

// Assignment - 1, Section - 2.
/** Please read the instructions in the comment and 
    write the code accordingly. 
    This assignment will test your understanding with console input and output.
    Just in case if you do not know the meaning of the term 'console', we typically
    indicate it to mean keyboard and terminal. Input console indicates to keyboard and 
    output console indicates terminal.
    This will also test your ability to use variables.
*/  
int main()
{
    // Please follow the instructions and write the code chronologically.
    //  1. Declare a double type variable named rate and assign 0.06 to it
double rate=0.06;


    //  2. Declare an integer variable, name it duration.

int duration;

    //  3. Declare a double variable and name it principal_amount

double principal_amount;

    //  4. Declare a double variable and name it total_interest
double total_interest;


    //  5. Prompt user to input duration in year. Read the year value in duration using 
    //     scanf. Since duration is integer you must use %d.

 printf("input duration in year");

scanf("%d", &duration);



    //  6. Prompt user to input principal amount of loan. Read the value in the 
    //     variable principal_amount. Think about the format specifier this time. 
    //     principal_amount is not an integer, it is a double, so use the format specifier
    //     of double.

printf("input the amount of loan:
");

scanf("%f", &principal_amount);

    //  7. Now you will calculate the total interest, if R is rate, D is duration and P 
    //     is principal then the total interest should be calculated as follows:
    //      I = (P * D * R)

int I= (rate*duration*principal_amount);
    printf("%d", I);
    //     Use the above formula to calculate the total interest and finally print the 
    //     amount into the console. Your output should be like the following: 
    //     Output: 
    //          Your total payable interest is $3000.000000  when principal_amount given as 1000.0, duration is 5                    
    //          



    return 0;

}```
question from:https://stackoverflow.com/questions/65923557/my-simple-intrest-calculator-is-not-working-for-some-reason

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use %lf format specifier for double and %d format specifier for int. This solves the issue and prints the required answer.

scanf("%lf", &principal_amount);

double I= (rate*duration*principal_amount);
printf("%lf", I);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...