Skip to content

Latest commit

 

History

History
81 lines (71 loc) · 2.71 KB

practice-problems.md

File metadata and controls

81 lines (71 loc) · 2.71 KB

Practice Problems by Topic

Solve each problem first without looking at a solution. Use small test inputs and compile with warnings enabled.

1. Fundamentals and I/O

  1. Print your name and three details on separate lines.
  2. Read two integers and print sum, difference, product and quotient (handle division by zero).
  3. Read radius and calculate circle area.
  4. Convert Celsius to Fahrenheit.
  5. Swap two values using a temporary variable.

2. Operators and conditions

  1. Check even or odd.
  2. Find the largest of three numbers.
  3. Check positive, negative or zero.
  4. Check leap year.
  5. Check whether a character is an uppercase letter, lowercase letter, digit or other character.
  6. Calculate grade using a clearly stated marks range.
  7. Build a calculator using switch.

3. Loops

  1. Print 1 to N and N to 1.
  2. Print the multiplication table of N.
  3. Find sum of first N natural numbers.
  4. Find factorial.
  5. Count digits and find digit sum.
  6. Reverse a number.
  7. Check numeric palindrome.
  8. Check prime number.
  9. Print all primes in a range.
  10. Generate Fibonacci sequence.

4. Nested loops and patterns

  1. Solid square.
  2. Right triangle.
  3. Inverted right triangle.
  4. Number triangle.
  5. Multiplication grid.
  6. Hollow rectangle.
  7. Pyramid with centered stars.

5. Functions and recursion

  1. Function to return maximum of two numbers.
  2. Function to check prime.
  3. Recursive factorial.
  4. Recursive sum from 1 to N.
  5. Recursive sum of digits.
  6. Recursive Fibonacci; compare its performance with iteration.
  7. GCD using Euclid's algorithm.

6. Arrays

  1. Find sum and average.
  2. Find minimum and maximum.
  3. Search for an element.
  4. Reverse an array in place.
  5. Find second-largest distinct value.
  6. Sort values in ascending order.
  7. Merge two arrays.
  8. Add and transpose matrices.

7. Strings

  1. Find length without strlen.
  2. Count vowels and consonants.
  3. Reverse a string.
  4. Check palindrome.
  5. Count words in a line.
  6. Copy a string without strcpy.
  7. Compare two strings without strcmp.
  8. Extract a substring using start and end indexes.

8. Pointers, structures and files

  1. Swap values using pointers.
  2. Traverse an array using pointer arithmetic.
  3. Create a Student structure and display a record.
  4. Store multiple records in an array of structures.
  5. Write text to a file and read it back.
  6. Append a record to a file.
  7. Count lines and characters in a file.

Suggested test cases

For a number-checking problem, test a negative number, zero, one, an ordinary positive number and a boundary value. For strings, test an empty string, one character, spaces, uppercase letters and repeated characters. For arrays, test one element, duplicate values, sorted values and reverse-sorted values.