C Programming Problem Set - 25 Questions
Topics: Conditionals, Loops, Functions, Scope & Lifetime, Recursion
Question 1: Grade Calculator
Write a program that takes a student's marks and prints the grade using nested if-else statements.
90-100: A+
80-89: A
70-79: B
60-69: C
Below 60: F
Input: 85
Expected Output: Grade: A
Question 2: Number Pattern
Write a program using nested loops to print the following pattern:
1
12
123
1234
12345
Expected Output:
1
12
123
1234
12345
Question 3: Calculator with Switch
Create a calculator using switch statement that performs basic operations (+, -, *, /, %).
Input: 15 + 25
Expected Output: Result: 40
Question 4: Prime Number Check Function
Write a function isPrime(int n) that returns 1 if the number is prime, 0 otherwise. Use it in main to
check numbers from 10 to 20.
Expected Output:
11 is prime
13 is prime
17 is prime
19 is prime
Question 5: Factorial using While Loop
Write a program using while loop to calculate factorial of a number.
Input: 6
Expected Output: Factorial of 6 is 720
Question 6: Global vs Local Variables
int x = 10; // Global variable
void function1() {
int x = 20; // Local variable
printf("Inside function1: x = %d\n", x);
}
void function2() {
printf("Inside function2: x = %d\n", x);
}
int main() {
printf("In main: x = %d\n", x);
function1();
function2();
return 0;
}
Expected Output:
In main: x = 10
Inside function1: x = 20
Inside function2: x = 10
Question 7: Sum of Digits using Recursion
Write a recursive function to find the sum of digits of a number.
Input: 12345
Expected Output: Sum of digits: 15
Question 8: Do-While Menu System
Create a menu-driven program using do-while loop:
1. Add two numbers
2. Multiply two numbers
3. Exit
Keep showing menu until user chooses exit.
Sample Run:
Menu:
1. Add
2. Multiply
3. Exit
Choose: 1
Enter two numbers: 5 3
Sum: 8
Menu:
1. Add
2. Multiply
3. Exit
Choose: 3
Goodbye!
Question 9: Fibonacci Series Function
Write a function to generate first n terms of Fibonacci series using loops.
Input: 8
Expected Output: 0 1 1 2 3 5 8 13
Question 10: Triangle Type Checker
Write a program using if-else to check if three sides can form a triangle and determine its type
(equilateral, isosceles, scalene).
Input: 5 5 5
Expected Output: Equilateral Triangle
Input: 3 4 5
Expected Output: Scalene Triangle
Question 11: Power Function using Recursion
Write a recursive function to calculate x^n (x raised to power n).
Input: Base: 2, Exponent: 5
Expected Output: 2^5 = 32
Question 12: Static Variable Demo
void counter() {
static int count = 0;
count++;
printf("Function called %d times\n", count);
}
int main() {
counter();
counter();
counter();
return 0;
}
Expected Output:
Function called 1 times
Function called 2 times
Function called 3 times
Question 13: Number Guessing Game
Create a number guessing game where computer generates a random number (1-100) and user has to
guess it. Use loops and conditionals.
Sample Run:
Guess the number (1-100): 50
Too high!
Guess the number (1-100): 25
Too low!
Guess the number (1-100): 37
Correct! You guessed it in 3 attempts.
Question 14: Array Sum using Function
Write a function that takes an array and its size, returns the sum of all elements.
Input: Array: [1, 2, 3, 4, 5], Size: 5
Expected Output: Sum: 15
Question 15: GCD using Recursion
Write a recursive function to find GCD of two numbers using Euclidean algorithm.
Input: 48, 18
Expected Output: GCD: 6
Question 16: Star Pattern with Functions
Write functions to print different star patterns:
void printStars(int n);
void printTriangle(int rows);
Input: 5 rows
Expected Output:
*
**
***
****
*****
Question 17: Palindrome Check using Recursion
Write a recursive function to check if a number is palindrome.
Input: 12321
Expected Output: 12321 is a palindrome
Question 18: Scope Chain Example
int a = 1;
void func1() {
int a = 2;
{
int a = 3;
printf("Block scope: a = %d\n", a);
}
printf("Function scope: a = %d\n", a);
}
int main() {
printf("Global scope: a = %d\n", a);
func1();
return 0;
}
Expected Output:
Global scope: a = 1
Block scope: a = 3
Function scope: a = 2
Question 19: Perfect Number Checker
Write a function to check if a number is perfect (sum of its proper divisors equals the number).
Input: 28
Expected Output: 28 is a perfect number (1+2+4+7+14=28)
Question 20: Binary to Decimal using Recursion
Write a recursive function to convert binary number to decimal.
Input: 1101
Expected Output: Decimal: 13
Question 21: Switch with Character Operations
Write a program using switch to perform operations on characters:
'U': Convert to uppercase
'L': Convert to lowercase
'C': Check if vowel or consonant
Input: 'a', 'U'
Expected Output: A
Question 22: Nested Function Calls
int multiply(int a, int b) {
return a * b;
}
int square(int n) {
return multiply(n, n);
}
int sumOfSquares(int a, int b) {
return square(a) + square(b);
}
int main() {
printf("Sum of squares of 3 and 4: %d\n", sumOfSquares(3, 4));
return 0;
}
Expected Output: Sum of squares of 3 and 4: 25
Question 23: Armstrong Number using Functions
Write functions to:
1. Count digits in a number
2. Calculate power
3. Check if number is Armstrong
Input: 153
Expected Output: 153 is an Armstrong number
Question 24: Tower of Hanoi (Basic)
Write a recursive function to solve Tower of Hanoi for n disks and print the moves.
Input: 3 disks
Expected Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Question 25: Complex Conditional Logic
Write a program that determines the day of week based on day number (1-7) using switch, and then uses
nested if-else to categorize it as:
Weekend (Saturday, Sunday)
Weekday (Monday-Friday)
Handle invalid input
Input: 7
Expected Output:
Day: Sunday
Category: Weekend
Input: 8
Expected Output: Invalid day number
Difficulty Level: Moderate
Skills Tested:
Conditional statements and nested conditions
Loop variations and nested loops
Function declaration, definition, and calling
Understanding of scope and variable lifetime
Basic recursion concepts
Problem-solving and logical thinking
Instructions:
1. Write complete C programs for each question
2. Test your programs with the given inputs
3. Ensure your output matches the expected output format
4. Pay attention to variable scope and lifetime concepts
5. Use appropriate loop types based on the problem requirements