LearnFrenzy
Sign in Take a free test

Floating Point Issues · Floating Point Programs · Question 3

Store and print money in paise (integer) in C Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

Keep amounts in whole paise, add a list of prices, apply 18% GST with correct rounding, and print rupees.

Solution
main.c
#include <stdio.h>

void print_rupees(long long paise) {
    const char *sign = paise < 0 ? "-" : "";
    if (paise < 0)
        paise = -paise;
    printf("%sRs. %lld.%02lld\n", sign, paise / 100, paise % 100);
}

int main(void) {
    long long prices[] = {19999, 4950, 125};       /* Rs. 199.99, 49.50, 1.25 */
    long long total = 0;
    for (int i = 0; i < 3; i++)
        total += prices[i];
    long long gst = (total * 18 + 50) / 100;       /* 18%, rounded to the nearest paisa */
    print_rupees(total);
    print_rupees(gst);
    print_rupees(total + gst);
    print_rupees(-5);
    return 0;
}

Output Verified by compiling and running

Rs. 250.74
Rs. 45.13
Rs. 295.87
-Rs. 0.05

All arithmetic is exact integer arithmetic. Adding 50 before dividing by 100 rounds the GST to the nearest paisa (25074 × 18 = 451332 → 4513.32 → 4513). Only printing splits rupees and paise.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links