Railway Ticket Booking Logic in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    char n[20]; int a, t; float p = 100;
    scanf("%s %d %d", n, &a, &t);
    if (a < 12) p *= 0.5;
    else if (a > 60) p *= 0.7;
    printf("Total: %.2f", p * t);
}

C Output

Input:
Amit 65 2

Output:
Total: 140.00



C++ Program

#include <iostream>
using namespace std;
int main() {
    string n; int a, t; float p = 120;
    cin >> n >> a >> t;
    if (a < 12) p *= 0.5;
    else if (a > 60) p *= 0.7;
    cout << "Total: " << p * t;
}

C++ Output

Input:
Neha 10 1

Output:
Total: 60



JAVA Program

import java.util.*;
public class Main {
    public static void main(String[] a) {
        Scanner s = new Scanner(System.in);
        String n = s.next(); int age = s.nextInt(), t = s.nextInt();
        float p = 150;
        if (age < 12) p *= 0.5f;
        else if (age > 60) p *= 0.7f;
        System.out.printf("Total: %.2f", p * t);
    }
}

JAVA Output

Input:
Ravi 30 3

Output:
Total: 450.00



Python Program

n, a, t = input().split(); a, t = int(a), int(t); p = 90
p *= 0.5 if a < 12 else 0.7 if a > 60 else 1
print("Total:", round(p * t, 2))

Python Output

Input:
Sara 8 2

Output:
Total: 90.0



In-Depth Explanation
Example
Amit, aged 65, purchases 2 tickets in the C version. Ticket price is ₹100. As he's a senior (age > 60), a 30% discount: 
₹100 × 0.7 = ₹70 per ticket
Total = ₹70 × 2 = ₹140.

In another scenario, if a child under 12 (such as Neha, aged 10) purchases 1 ticket, she receives 50% off: ₹120 × 0.5 = ₹60.

Real-Life Analogy
Consider purchasing railway tickets at a station. The counter clerk requests your age and ticket quantity. As a child or senior, you qualify for a discount. That's what this logic does automatically: final fare calculation with applicable discounts.

Why It Matters
This program illustrates conditional pricing logic, an actual business concept applied in:

Railway and airline reservations

Movie and event ticketing

Subscription services (student/senior plans)

Discount engines in online shopping

It teaches you about nested conditions, parsing inputs, dynamic prices, and how to set rules according to user types.

Learning Insights
You will learn:

How to set tiered conditions with if-else

Type casting (such as float operations)

Reading and applying multiple inputs simultaneously

Constructing real-world logic with basic arithmetic

Also, you know how to:

Implement pricing functions

Apply inputs such as name, age, and quantity

Implement percentage discounts according to age

This reasoning is transferable — applies to desktop, web, or mobile ticketing applications.

Interview & Project Relevance
This question is typically posed in a fresher interview or class test to ensure:

Fluency in conditional logic

Handling calculations

Appreciation of discount-based rules

Input/output formatting

In practical software, this is the foundation of:

Booking engines (IRCTC, RedBus, MakeMyTrip)

Retail discount logic

Dynamic pricing dashboards

Railway ticket reservation is the most useful exercise for novice programmers. It has user input, if-else logic, and dynamic fare calculation, similar to real-life projects. Being the shortest working code in C, C++, Java, and Python, this logic makes you ready for coding interviews, project work at the college level, and designing real-world systems such as reservation pages and fare calculators with discounting logic.