CS501 · Practice

Practice problems.

16 hands-on coding problems, one or two per module. Try to solve each one yourself before checking the hint or solution.

Your progress

0 of 16 problems solved

0%

Foundations

Programming basics, algorithms, language processors, data types.

Print your name

easy

Write a program that declares a string variable with your name and prints "Hello, <name>!" to the console.

Largest of two numbers (algorithm)

easy

Write pseudocode (step-by-step algorithm) to find the largest of two numbers entered by the user, then implement it in C++.

Operators & Control Flow

Expressions, operators, if/else and switch decisions.

Even or odd

easy

Take an integer input and print whether it is even or odd using the modulus operator.

Grade calculator (switch-case)

medium

Given marks (0-100), print a letter grade: A (>=90), B (>=80), C (>=70), D (>=60), F (below 60). Use if/else if.

Loops

for, while, do…while, random numbers and nested loops.

Sum of 1 to N

easy

Take an integer N and print the sum of all numbers from 1 to N using a for loop.

Multiplication table

easy

Print the multiplication table (1 to 10) for a number entered by the user using a while loop.

Prime number checker

medium

Write a program that checks whether a given number is prime, using a for loop and break.

Arrays

1D and 2D arrays plus common array tasks.

Find the largest element

easy

Given an array of 5 integers, find and print the largest element.

Bubble sort implementation

medium

Implement bubble sort to sort an array of integers in ascending order, then print the sorted array.

Functions

Defining, calling and passing values to functions.

Factorial function

easy

Write a function `int factorial(int n)` that returns n! and call it from main for a user-entered number.

Swap two numbers (pass by reference)

medium

Write a function `void swapNums(int &a, int &b)` that swaps two numbers using pass by reference, and demonstrate it in main.

Recursion & Strings

Recursive thinking and working with character arrays.

Fibonacci with recursion

medium

Write a recursive function `int fib(int n)` that returns the nth Fibonacci number.

Check if a string is a palindrome

medium

Write a program that checks whether a given string reads the same forwards and backwards.

Structures & Files

User-defined types and persistent file I/O.

Student record with struct

medium

Define a `struct Student` with name, roll number, and marks. Create one student, fill in the values, and print them.

Write and read a text file

hard

Write a program that writes "Hello File" into a file called data.txt, then reads and prints it back.

Code Quality

Refactoring techniques to keep code clean.

Refactor a messy sum function

easy

Refactor this poorly-named, hard-to-read function to use clear variable names and consistent formatting, while keeping the same behavior (sums even numbers from 1 to n): int f(int x){int s=0;for(int i=1;i<=x;i++){if(i%2==0){s=s+i;}}return s;}