c
exercises
exercises.cđ§c
/**
* @file exercises.c
* @brief Function Basics Exercises
*
* Practice writing functions in C.
*
* Compile: gcc -Wall exercises.c -o exercises -lm
* Run: ./exercises
*/
#include <stdio.h>
#include <math.h>
/* ============================================================
* EXERCISE 1: GREET BY NAME
* ============================================================
* Difficulty: Easy
*
* Task: Write a function that prints "Hello, <name>!"
*/
void greetByName(char name[]) {
// TODO: Print "Hello, <name>!"
// Your code here
}
void exercise_1() {
printf("\n=== Exercise 1: Greet By Name ===\n");
greetByName("Alice");
greetByName("Bob");
}
/* ============================================================
* EXERCISE 2: CALCULATE POWER
* ============================================================
* Difficulty: Easy
*
* Task: Write a function that calculates base^exponent.
* Do not use pow() from math.h.
*/
int power(int base, int exponent) {
// TODO: Return base raised to exponent
// Assume exponent >= 0
// Your code here
return 0; // Replace this
}
void exercise_2() {
printf("\n=== Exercise 2: Power Function ===\n");
printf("2^10 = %d (should be 1024)\n", power(2, 10));
printf("5^3 = %d (should be 125)\n", power(5, 3));
printf("7^0 = %d (should be 1)\n", power(7, 0));
}
/* ============================================================
* EXERCISE 3: IS POSITIVE
* ============================================================
* Difficulty: Easy
*
* Task: Write a function that returns 1 if positive, 0 otherwise.
*/
int isPositive(int n) {
// TODO: Return 1 if n > 0, else 0
// Your code here
return 0; // Replace this
}
void exercise_3() {
printf("\n=== Exercise 3: Is Positive ===\n");
printf("isPositive(5) = %d (should be 1)\n", isPositive(5));
printf("isPositive(0) = %d (should be 0)\n", isPositive(0));
printf("isPositive(-3) = %d (should be 0)\n", isPositive(-3));
}
/* ============================================================
* EXERCISE 4: ABSOLUTE VALUE
* ============================================================
* Difficulty: Easy
*
* Task: Write a function that returns absolute value of a number.
*/
int absoluteValue(int n) {
// TODO: Return |n|
// Your code here
return 0; // Replace this
}
void exercise_4() {
printf("\n=== Exercise 4: Absolute Value ===\n");
printf("abs(5) = %d (should be 5)\n", absoluteValue(5));
printf("abs(-10) = %d (should be 10)\n", absoluteValue(-10));
printf("abs(0) = %d (should be 0)\n", absoluteValue(0));
}
/* ============================================================
* EXERCISE 5: AVERAGE OF THREE
* ============================================================
* Difficulty: Easy
*
* Task: Calculate average of three numbers.
*/
float averageOfThree(int a, int b, int c) {
// TODO: Return average of a, b, c
// Your code here
return 0.0f; // Replace this
}
void exercise_5() {
printf("\n=== Exercise 5: Average of Three ===\n");
printf("avg(10, 20, 30) = %.2f (should be 20.00)\n",
averageOfThree(10, 20, 30));
printf("avg(1, 2, 3) = %.2f (should be 2.00)\n",
averageOfThree(1, 2, 3));
}
/* ============================================================
* EXERCISE 6: IS LEAP YEAR
* ============================================================
* Difficulty: Medium
*
* Task: Return 1 if year is leap year, 0 otherwise.
* A leap year is divisible by 4, but not by 100 unless also by 400.
*/
int isLeapYear(int year) {
// TODO: Return 1 if leap year, 0 otherwise
// Your code here
return 0; // Replace this
}
void exercise_6() {
printf("\n=== Exercise 6: Leap Year ===\n");
printf("2000: %s\n", isLeapYear(2000) ? "Leap" : "Not leap");
printf("2024: %s\n", isLeapYear(2024) ? "Leap" : "Not leap");
printf("1900: %s\n", isLeapYear(1900) ? "Leap" : "Not leap");
printf("2023: %s\n", isLeapYear(2023) ? "Leap" : "Not leap");
}
/* ============================================================
* EXERCISE 7: CELSIUS TO FAHRENHEIT
* ============================================================
* Difficulty: Easy
*
* Task: Convert Celsius to Fahrenheit.
* Formula: F = C * 9/5 + 32
*/
float celsiusToFahrenheit(float celsius) {
// TODO: Return temperature in Fahrenheit
// Your code here
return 0.0f; // Replace this
}
void exercise_7() {
printf("\n=== Exercise 7: Celsius to Fahrenheit ===\n");
printf("0°C = %.1f°F (should be 32.0)\n", celsiusToFahrenheit(0));
printf("100°C = %.1f°F (should be 212.0)\n", celsiusToFahrenheit(100));
printf("37°C = %.1f°F (should be 98.6)\n", celsiusToFahrenheit(37));
}
/* ============================================================
* EXERCISE 8: IS DIVISIBLE
* ============================================================
* Difficulty: Easy
*
* Task: Return 1 if n is divisible by d, 0 otherwise.
*/
int isDivisible(int n, int d) {
// TODO: Return 1 if n % d == 0, 0 otherwise
// Handle d == 0 case
// Your code here
return 0; // Replace this
}
void exercise_8() {
printf("\n=== Exercise 8: Is Divisible ===\n");
printf("isDivisible(10, 2) = %d (should be 1)\n", isDivisible(10, 2));
printf("isDivisible(10, 3) = %d (should be 0)\n", isDivisible(10, 3));
printf("isDivisible(15, 5) = %d (should be 1)\n", isDivisible(15, 5));
}
/* ============================================================
* EXERCISE 9: COUNT FACTORS
* ============================================================
* Difficulty: Medium
*
* Task: Count how many factors a number has.
*/
int countFactors(int n) {
// TODO: Return count of factors of n
// Your code here
return 0; // Replace this
}
void exercise_9() {
printf("\n=== Exercise 9: Count Factors ===\n");
printf("Factors of 12: %d (should be 6)\n", countFactors(12));
printf("Factors of 7: %d (should be 2)\n", countFactors(7));
printf("Factors of 1: %d (should be 1)\n", countFactors(1));
}
/* ============================================================
* EXERCISE 10: SUM RANGE
* ============================================================
* Difficulty: Medium
*
* Task: Return sum of integers from start to end (inclusive).
*/
int sumRange(int start, int end) {
// TODO: Return start + (start+1) + ... + end
// Your code here
return 0; // Replace this
}
void exercise_10() {
printf("\n=== Exercise 10: Sum Range ===\n");
printf("sum(1, 10) = %d (should be 55)\n", sumRange(1, 10));
printf("sum(5, 5) = %d (should be 5)\n", sumRange(5, 5));
printf("sum(1, 100) = %d (should be 5050)\n", sumRange(1, 100));
}
/* ============================================================
* EXERCISE 11: CIRCLE AREA
* ============================================================
* Difficulty: Medium
*
* Task: Calculate area of a circle given radius.
* Use M_PI from math.h or 3.14159265
*/
double circleArea(double radius) {
// TODO: Return PI * radius * radius
// Your code here
return 0.0; // Replace this
}
void exercise_11() {
printf("\n=== Exercise 11: Circle Area ===\n");
printf("Area (r=1): %.4f\n", circleArea(1));
printf("Area (r=5): %.4f\n", circleArea(5));
printf("Area (r=10): %.4f\n", circleArea(10));
}
/* ============================================================
* EXERCISE 12: IS ARMSTRONG
* ============================================================
* Difficulty: Hard
*
* Task: Check if a 3-digit number is an Armstrong number.
* Armstrong number: sum of cubes of digits = number itself.
* Example: 153 = 1^3 + 5^3 + 3^3 = 153
*/
int isArmstrong(int n) {
// TODO: Return 1 if Armstrong number, 0 otherwise
// Your code here
return 0; // Replace this
}
void exercise_12() {
printf("\n=== Exercise 12: Armstrong Number ===\n");
printf("153: %s (should be Yes)\n", isArmstrong(153) ? "Yes" : "No");
printf("370: %s (should be Yes)\n", isArmstrong(370) ? "Yes" : "No");
printf("123: %s (should be No)\n", isArmstrong(123) ? "Yes" : "No");
}
/* ============================================================
* EXERCISE 13: NTH FIBONACCI
* ============================================================
* Difficulty: Medium
*
* Task: Return the nth Fibonacci number (0-indexed).
* Fib(0)=0, Fib(1)=1, Fib(n)=Fib(n-1)+Fib(n-2)
*/
int fibonacci(int n) {
// TODO: Return nth Fibonacci number
// Your code here
return 0; // Replace this
}
void exercise_13() {
printf("\n=== Exercise 13: Fibonacci ===\n");
printf("Fib(0) = %d (should be 0)\n", fibonacci(0));
printf("Fib(1) = %d (should be 1)\n", fibonacci(1));
printf("Fib(10) = %d (should be 55)\n", fibonacci(10));
printf("Fib(15) = %d (should be 610)\n", fibonacci(15));
}
/* ============================================================
* EXERCISE 14: IS PALINDROME NUMBER
* ============================================================
* Difficulty: Medium
*
* Task: Check if a number is palindrome (reads same forward/backward).
*/
int isPalindromeNumber(int n) {
// TODO: Return 1 if palindrome, 0 otherwise
// Your code here
return 0; // Replace this
}
void exercise_14() {
printf("\n=== Exercise 14: Palindrome Number ===\n");
printf("12321: %s (should be Yes)\n",
isPalindromeNumber(12321) ? "Yes" : "No");
printf("12345: %s (should be No)\n",
isPalindromeNumber(12345) ? "Yes" : "No");
printf("1001: %s (should be Yes)\n",
isPalindromeNumber(1001) ? "Yes" : "No");
}
/* ============================================================
* EXERCISE 15: DIGIT AT POSITION
* ============================================================
* Difficulty: Hard
*
* Task: Return the digit at given position (1 = rightmost).
* Example: digitAt(12345, 2) returns 4
*/
int digitAt(int n, int position) {
// TODO: Return digit at position (1-indexed from right)
// Return -1 if position is invalid
// Your code here
return -1; // Replace this
}
void exercise_15() {
printf("\n=== Exercise 15: Digit at Position ===\n");
printf("digitAt(12345, 1) = %d (should be 5)\n", digitAt(12345, 1));
printf("digitAt(12345, 3) = %d (should be 3)\n", digitAt(12345, 3));
printf("digitAt(12345, 5) = %d (should be 1)\n", digitAt(12345, 5));
printf("digitAt(12345, 6) = %d (should be -1)\n", digitAt(12345, 6));
}
/* ============================================================
* MAIN FUNCTION
* ============================================================ */
int main() {
printf("ââââââââââââââââââââââââââââââââââââââââââââââââââ\n");
printf("â FUNCTION BASICS - EXERCISES â\n");
printf("â Practice writing functions â\n");
printf("ââââââââââââââââââââââââââââââââââââââââââââââââââ\n");
// Uncomment exercises as you complete them:
// exercise_1();
// exercise_2();
// exercise_3();
// exercise_4();
// exercise_5();
// exercise_6();
// exercise_7();
// exercise_8();
// exercise_9();
// exercise_10();
// exercise_11();
// exercise_12();
// exercise_13();
// exercise_14();
// exercise_15();
printf("\n=== Uncomment exercises in main() to run them ===\n");
return 0;
}
/* ============================================================
* ANSWER KEY
* ============================================================
*
* EXERCISE 1:
* void greetByName(char name[]) {
* printf("Hello, %s!\n", name);
* }
*
* EXERCISE 2:
* int power(int base, int exponent) {
* int result = 1;
* for (int i = 0; i < exponent; i++) {
* result *= base;
* }
* return result;
* }
*
* EXERCISE 3:
* int isPositive(int n) {
* return n > 0 ? 1 : 0;
* // Or simply: return n > 0;
* }
*
* EXERCISE 4:
* int absoluteValue(int n) {
* return (n < 0) ? -n : n;
* }
*
* EXERCISE 5:
* float averageOfThree(int a, int b, int c) {
* return (a + b + c) / 3.0f;
* }
*
* EXERCISE 6:
* int isLeapYear(int year) {
* if (year % 400 == 0) return 1;
* if (year % 100 == 0) return 0;
* if (year % 4 == 0) return 1;
* return 0;
* }
*
* EXERCISE 7:
* float celsiusToFahrenheit(float celsius) {
* return celsius * 9.0f / 5.0f + 32;
* }
*
* EXERCISE 8:
* int isDivisible(int n, int d) {
* if (d == 0) return 0; // Avoid division by zero
* return n % d == 0;
* }
*
* EXERCISE 9:
* int countFactors(int n) {
* if (n <= 0) return 0;
* int count = 0;
* for (int i = 1; i <= n; i++) {
* if (n % i == 0) count++;
* }
* return count;
* }
*
* EXERCISE 10:
* int sumRange(int start, int end) {
* int sum = 0;
* for (int i = start; i <= end; i++) {
* sum += i;
* }
* return sum;
* // Or formula: return (end - start + 1) * (start + end) / 2;
* }
*
* EXERCISE 11:
* double circleArea(double radius) {
* return 3.14159265358979 * radius * radius;
* // Or: return M_PI * radius * radius;
* }
*
* EXERCISE 12:
* int isArmstrong(int n) {
* int original = n;
* int sum = 0;
* while (n > 0) {
* int digit = n % 10;
* sum += digit * digit * digit;
* n /= 10;
* }
* return sum == original;
* }
*
* EXERCISE 13:
* int fibonacci(int n) {
* if (n <= 0) return 0;
* if (n == 1) return 1;
* int a = 0, b = 1;
* for (int i = 2; i <= n; i++) {
* int temp = a + b;
* a = b;
* b = temp;
* }
* return b;
* }
*
* EXERCISE 14:
* int isPalindromeNumber(int n) {
* if (n < 0) return 0;
* int original = n;
* int reversed = 0;
* while (n > 0) {
* reversed = reversed * 10 + n % 10;
* n /= 10;
* }
* return reversed == original;
* }
*
* EXERCISE 15:
* int digitAt(int n, int position) {
* if (n < 0) n = -n;
* if (position <= 0) return -1;
* for (int i = 1; i < position; i++) {
* n /= 10;
* }
* if (n == 0) return -1; // Position too large
* return n % 10;
* }
*
* ============================================================
*/