Bhavika Ramesh
Bhavika Ramesh

Reputation: 54

How to remove the extra zeros from the output?

I am trying to solve a problem from CodeChef. The problem statement is as follows.

Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal, the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction.

Input: 42 120.00

Output: 120.00

For this input, I am getting an output of 120.000.00.

#include<stdio.h>
int main()
{
    int withdraw;
    float initial_balance, final_balance;
    scanf("%d %f", &withdraw, &initial_balance);
    if(withdraw % 5 == 0)
        {
            final_balance = initial_balance - withdraw - 0.50;
        }
    else
    {
        printf("%.2f", initial_balance);
    }
    printf("%.2f", final_balance);
    return 0;
}

Upvotes: 1

Views: 49

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You are printing 120.00 via printf("%.2f", initial_balance); and after that printing uninitialized indeterminate value via printf("%.2f", final_balance);.

Instead of the printing printf("%.2f", initial_balance); in the if statement, you should set the answer to final_balance like final_balance = initial_balance;.

Upvotes: 2

Related Questions