CSS Computer Science (Optional) — Paper-I, Section-A, Topic II. These 9 coding/theory patterns recur across FPSC past papers (2016, 2017, 2018, 2019, 2020, 2022, 2024). Every coding question includes a complete, compilable C++ solution plus the reasoning an examiner is checking for.

Q1. Temperature Conversion (Celsius ↔ Fahrenheit, menu-driven)

image.png

Appeared: 2016, Q2(a); 2024, Q4(b)

Write a program that prompts the user to choose a conversion direction (Fahrenheit→Celsius or Celsius→Fahrenheit), inputs the temperature, and displays the converted value.

Solution:

#include <iostream>
using namespace std;

int main()
{
    int choice;
    double temp, result;

    cout << "1. Fahrenheit to Celsius\n2. Celsius to Fahrenheit\nEnter choice: ";
    cin >> choice;

    if (choice == 1)
    {
        cout << "Enter temperature in Fahrenheit: ";
        cin >> temp;
        result = (temp - 32) * 5.0 / 9.0;
        cout << temp << " F = " << result << " C" << endl;
    }
    else if (choice == 2)
    {
        cout << "Enter temperature in Celsius: ";
        cin >> temp;
        result = (temp * 9.0 / 5.0) + 32;
        cout << temp << " C = " << result << " F" << endl;
    }
    else
    {
        cout << "Invalid choice." << endl;
    }
    return 0;
}

Key reasoning points: use 5.0/9.0 (not 5/9, which truncates to 0 in integer division) — this is the single most common bug examiners look for; wrap both directions in one menu-driven program rather than writing two separate programs, since "prompts the user for a choice" is explicitly part of the question.


Q2. Grade Calculator (marks → average → letter grade, and the reverse)

Appeared: 2018, Q3(a) — marks to grade; 2024, Q2(a) — letter to numeric

(a) Input marks for 5 subjects, compute the average, and display a letter grade with a contextual message.

(b) Input a letter grade (A/B/C/D/F) and display its numeric value (4/3/2/1/0).

Solution (a):

#include <iostream>
using namespace std;

int main()
{
    double marks[5], sum = 0, average;
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter marks for subject " << i + 1 << ": ";
        cin >> marks[i];
        sum += marks[i];
    }
    average = sum / 5;

    char grade;
    if (average >= 90) grade = 'A';
    else if (average >= 80) grade = 'B';
    else if (average >= 70) grade = 'C';
    else if (average >= 60) grade = 'D';
    else grade = 'F';

    cout << "Average: " << average << ", Grade: " << grade << endl;
    if (grade == 'F')
        cout << "You need to improve significantly." << endl;
    else
        cout << "Good job, keep it up!" << endl;
    return 0;
}

Solution (b):

#include <iostream>
using namespace std;

int main()
{
    char grade;
    int value;
    cout << "Enter letter grade (A/B/C/D/F): ";
    cin >> grade;

    switch (grade)
    {
        case 'A': value = 4; break;
        case 'B': value = 3; break;
        case 'C': value = 2; break;
        case 'D': value = 1; break;
        case 'F': value = 0; break;
        default:
            cout << "Invalid grade." << endl;
            return 0;
    }
    cout << "Numeric value: " << value << endl;
    return 0;
}

Key reasoning points: this family of question always tests the same skill — mapping a continuous or categorical input to a fixed set of output bands using if-else if or switch. Recognize which direction is being asked (value→grade needs if-else ranges; grade→value needs a switch/lookup) and don't mix them up under exam pressure.