0% found this document useful (0 votes)
3 views10 pages

Loops in C Programming

The document outlines multiple programming scenarios for calculating fares, bills, and other financial computations using C. It includes examples for metro fare calculation, restaurant billing, school result generation, ATM withdrawals, cinema ticket booking, fitness tracking, e-commerce discounts, water tank monitoring, car rental billing, and hospital patient billing. Each scenario provides rules, problem statements, and worked examples with expected outputs.

Uploaded by

dhruvvsingh7819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Loops in C Programming

The document outlines multiple programming scenarios for calculating fares, bills, and other financial computations using C. It includes examples for metro fare calculation, restaurant billing, school result generation, ATM withdrawals, cinema ticket booking, fitness tracking, e-commerce discounts, water tank monitoring, car rental billing, and hospital patient billing. Each scenario provides rules, problem statements, and worked examples with expected outputs.

Uploaded by

dhruvvsingh7819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1️⃣ Metro Fare Calculator

Scenario:

City metro charges:

Up to 5 km → ₹1️0/km

Next 1️0 km → ₹8/km

Beyond 1️5 km → ₹5/km

1️0 % discount if total fare > ₹300.

Problem Statement:

Write a C program to calculate fare based on distance.

Example 1️: Distance=4 → ₹40

Example 2: Distance=20 → ₹1️55

Test Cases & Output


Distance (km) Output Fare

3 3 × 1️0 ₹30

8 (5 × 1️0) + (3 × 8) = 50 + 24 ₹74

1️5 (5 × 1️0) + (1️0 × 8) = 50 + 80 ₹1️30

1️8 (5 × 1️0) + (1️0 × 8) + (3 × 5) = 50 + 80 + 1️5 ₹1️45

25 (5 × 1️0) + (1️0 × 8) + (1️0 × 5) = 50 + 80 + 50 ₹1️80

40 (5 × 1️0) + (1️0 × 8) + (25 × 5) = 50 + 80 + 1️25 = 255 → No discount ₹255

70 50 + 80 + (55 × 5) = 50 + 80 + 275 = 405 → 1️0 % off ₹364.5

2⃣ Restaurant Billing with Service Charge

Scenario:

5 % GST if total < ₹1️000, else 1️0 %.

If items > 5, add 5 % service charge.


Problem Statement:
Read food total and number of items, output final bill.

Example 1️: Food = ₹900, Items = 3

GST = 5 % of 900 = 45 → ₹945

Example 2: Food = ₹1️500, Items = 6

GST = 1️0 % of 1️500 = 1️50

Service = 5 % of 1️500 = 75

Total = 1️500 + 1️50 + 75 = ₹1️725

Test Cases & Output

Food Amt Items Output

500 2 500 + 5% = ₹525

800 6 800 + 5% GST + 5% service = ₹880

1️200 3 1️200 + 1️0% GST = ₹1️320

2000 6 2000 + 1️0% GST + 5% service = ₹2300

950 5 950 + 5% GST = ₹997.50

3⃣ School Result Generator

Scenario:

Grade: A≥90, B 80–89, C 60–79, D 40–59, F<40.

Problem Statement:
Input marks of 5 subjects; output total, percentage, grade, and failed subjects.

Example 1️: 78 85 69 90 88 → Total=41️0, 82 %, Grade B, Fail=0

Example 2: 45 30 72 55 60 → Total=262, 52 %, Grade D, Fail=1️

Test Cases & Output

Marks (5) Output

95 96 92 94 98 Total=475, 95 %, Grade=A, Fail=0


80 85 78 82 88 Total=41️3, 82.6 %, Grade=B, Fail=0
65 60 70 68 64 Total=327, 65.4 %, Grade=C, Fail=0

35 20 40 1️5 50 Total=1️60, 32 %, Grade=F, Fail=3

45 55 65 75 85 Total=325, 65 %, Grade=C, Fail=0

4⃣ ATM Withdrawal with Service Charge

Rules

Withdrawal multiples of ₹1️00 only

If the amount is not a multiple of 1️00, print “Invalid withdrawal amount!” and stop.

Service charge: ₹1️0 deducted each successful withdrawal.

After withdrawal and service charge:


Remaining Balance = Current Balance – Withdrawal – 1️0

If the remaining balance is below ₹1️000, append “(Low Balance)”.

Worked Examples
Example 1️

Input: Balance = 5000, Withdraw = 1️200

Check multiple of 1️00 → OK

New balance = 5000 – 1️200 – 1️0 = ₹3790

Example 2

Input: Balance = 1️500, Withdraw = 600

Check multiple of 1️00 → OK

New balance = 1️500 – 600 – 1️0 = ₹890 (Low Balance)

Correct Test Cases & Output

Balance Withdraw Calculation (minus ₹1️0 service charge) Output

5000 500 5000 – 500 – 1️0 = ₹4490 Balance=₹4490


3000 2900 3000 – 2900 – 1️0 = ₹90 Balance=₹90 (Low Balance)
1️500 600 1️500 – 600 – 1️0 = ₹890 Balance=₹890 (Low Balance)

1️000 1️00 1️000 – 1️00 – 1️0 = ₹890 Balance=₹890 (Low Balance)

2000 200 2000 – 200 – 1️0 = ₹1️790 Balance=₹1️790

5⃣ Cinema Ticket Booking

Scenario:

Adult ₹200, Child ₹1️50, 1️0 % discount if total seats ≥5.

Problem Statement

Write a C program to read the number of adult and child tickets sold and print the
final amount payable after applying the discount rule.
Worked Examples

Example 1️

Adults = 2, Children = 1️

Total = (2 × 200) + (1️ × 1️50) = ₹550 (No discount)

Example 2

Adults = 5, Children = 0
Total before discount = 5 × 200 = 1️ 000

Seats = 5 → eligible for 1️0 % off

Final = 1️ 000 − 1️0 % = ₹900

Correct Test Cases & Output

Adults Children Seats Calculation Output

1️ 1️ 2 (1️×200) + (1️×1️50) = 200 + 1️50 ₹350

2 3 5 (2×200) + (3×1️50) = 400 + 450 = 850 → 1️0 % off ₹765

5 0 5 (5×200) = 1️ 000 → 1️0 % off ₹900

3 4 7 (3×200) + (4×1️50) = 600 + 600 = 1️ 200 → 1️0 % off ₹1️ 080


0 5 5 (5×1️50) = 750 → 1️0 % off ₹675

6⃣ Fitness Tracker – Weekly Calories

Scenario:

Calories/day = steps × 0.04.

Problem Statement:

Write a C program that:

Reads step counts for 7 days.

Calculates:
Total calories = sum of (steps × 0.04) for all 7 days.

Average calories per day = total / 7.

Prints the total and average.

Correct Test Cases & Output

Steps (7 days) Total Calories (≈) Avg Calories (≈)

3000 each 3 000×0.04×7 = 8401️20

5 000 each 5 000×0.04×7 = 1️ 400 200


7 000 each 7 000×0.04×7 = 1️ 960 280

1️0 000 each 1️0 000×0.04×7 = 2 800 400

2000,3000,…,8000 (each +1️000) Total steps = 35 000 → 35 000×0.04 = 1️ 400


200

(If you meant 2000 through 8000 increasing by 1️000 each day: 2+3+4+5+6+7+8 =
35 thousand total steps.)

7⃣ E-commerce Discount Engine

Coupon Discount Calculator


Rules:
SAVE1️0 → Always gives 1️0 % off.

SAVE20 → Gives

20 % off if price ≥ ₹2000

1️0 % off otherwise.

Any other code → No discount.

Problem Statement

Write a C program to read the price and a coupon code, then output the final price
after discount.

Worked Examples
Example 1️

Price = ₹1️500, Code = SAVE1️0

→ 1️0 % off → ₹1️500 – 1️50 = ₹1️350

Example 2
Price = ₹2500, Code = SAVE20

→ ≥2000 so 20 % off → ₹2500 – 500 = ₹2000

Correct Test Cases & Output

Price Code Calculation Final Price

1️000 SAVE1️0 1️000 – 1️0 % = 1️000 – 1️00 ₹900

2500 SAVE20 2500 – 20 % = 2500 – 500 ₹2000

1️800 SAVE20 <2000 → 1️0 % = 1️800 – 1️80 ₹1️620

3000 NONE No discount ₹3000

500 SAVE1️0 500 – 1️0 % = 500 – 50 ₹450


8⃣ Water Tank Monitor

Scenario

Tank capacity = 5000 L.

Read daily usage repeatedly until the input is 0 (sentinel).

After reading all values:

Display total water used.

If total > 5000 L, show overflow amount (total - 5000), otherwise show Overflow = 0.

Worked Examples

1️⃣ Example 1️

Inputs: 1️000, 1️200, 1️500, 2000

Total = 1️000 + 1️200 + 1️500 + 2000 = 5700

Overflow = 5700 – 5000 = 700

2⃣ Example 2

Inputs: 1️000, 500, 0


Total = 1️000 + 500 = 1️500

≤ 5000 → No overflow

Test Cases & Expected Output

Daily Usage Sequence Total Used Overflow

500, 600, 700, 800, 900 3500 0

2000, 2000, 2000 6000 1️000

1️000, 1️000, 1️000, 1️000, 1️000 5000 0

4500, 600 51️00 1️00


1️000 1️000 0
9⃣ Car Rental Billing

Rules

Rate per km:

First 200 km → ₹1️5/km

Beyond 200 km → ₹1️2/km

Add 1️8 % GST to the total.

Problem Statement
Write a C program that:

Reads the distance travelled.

Calculates the total rental cost according to the above slabs.

Adds 1️8 % GST.

Outputs the final amount.

Worked Examples
Example 1️: 1️50 km

All ≤ 200 → 1️50 × 1️5 = 2250

GST = 2250 × 0.1️8 = 405

Total = 2250 + 405 = ₹2655

Example 2: 250 km

First 200 km → 200 × 1️5 = 3000

Next 50 km → 50 × 1️2 = 600

Subtotal = 3000 + 600 = 3600

GST = 3600 × 0.1️8 = 648

Total = 3600 + 648 = ₹4248


Correct Test Cases & Output

Distance (km) Calculation Output

50 50×1️5 = 750 → +1️8% GST → 750×1️.1️8 = 885 ₹885

1️80 1️80×1️5 = 2700 → +1️8% GST → 2700×1️.1️8 = 31️86 ₹31️86

200 200×1️5 = 3000 → +1️8% GST → 3000×1️.1️8 = 3540 ₹3540

220 200×1️5 + 20×1️2 = 3000 + 240 = 3240 → ×1️.1️8 = 3823 ₹3823

300 200×1️5 + 1️00×1️2 = 3000 + 1️200 = 4200 → ×1️.1️8 = 4956 ₹4956

🔟 Hospital Patient Billing

Hospital Bill Calculator

Rules
Room rent = ₹2000/day

Medicine cost = variable input

Discount: 5 % if stay > 7 days

Problem Statement

Write a C program that:

Reads the number of days stayed and medicine cost.

Calculates total bill = (days × 2000) + medicine.

Applies 5 % discount if days > 7.


Outputs the final bill.

Worked Examples

Example 1️: 5 days, ₹3000 medicine

Room = 5 × 2000 = 1️0,000

Total = 1️0,000 + 3000 = 1️3,000 (No discount)

Example 2: 1️0 days, ₹5000 medicine


Room = 1️0 × 2000 = 20,000
Total = 20,000 + 5000 = 25,000

Discount = 5 % of 25,000 = 1️,250

Final bill = 25,000 – 1️,250 = 23,750

Correct Test Cases & Output

Days Medicine Calculation Output

3 2000 3×2000 + 2000 = 6000 + 2000 = 8000 ₹8000

7 4000 7×2000 + 4000 = 1️4,000 + 4000 = 1️8,000 ₹1️8,000

8 5000 8×2000 + 5000 = 1️6,000 + 5000 = 21️,000 → 5 % off = 21️,000 – 1️050 =


1️9,950 ₹1️9,950

1️0 0 1️0×2000 + 0 = 20,000 → 5 % off = 20,000 – 1️000 = 1️9,000 ₹1️9,000

1️2 3000 1️2×2000 + 3000 = 24,000 + 3000 = 27,000 → 5 % off = 27,000 – 1️350
= 25,650 ₹25,650

You might also like