C Programming Practice Exercises: Solutions and Explanations14


This post provides comprehensive solutions and detailed explanations for a selection of common C programming practice exercises. Understanding the *why* behind the code is just as important as getting the correct output, so we'll delve into the logic and best practices for each example. These exercises cover fundamental concepts, building a solid foundation for more advanced C programming.

Exercise 1: Hello, World! with a Twist

Problem: Modify the classic "Hello, World!" program to also print your name on a new line.

Solution:#include <stdio.h>
int main() {
printf("Hello, World!");
printf("My name is [Your Name]");
return 0;
}

Explanation: This exercise highlights the use of `printf` for outputting text to the console. The `` character adds a newline, ensuring the name appears on a separate line. Remember to replace "[Your Name]" with your actual name.

Exercise 2: Calculating the Area of a Rectangle

Problem: Write a program that takes the length and width of a rectangle as input from the user and calculates its area.

Solution:#include <stdio.h>
int main() {
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("The area of the rectangle is: %.2f", area);
return 0;
}

Explanation: This introduces user input using `scanf`. The `%f` format specifier is used to read floating-point numbers. The calculated area is then printed using `printf`, with `%.2f` formatting it to two decimal places. Error handling (e.g., checking for non-numeric input) could be added for robustness in a production environment.

Exercise 3: Finding the Largest of Three Numbers

Problem: Write a program that takes three numbers as input and prints the largest of the three.

Solution:#include <stdio.h>
int main() {
int num1, num2, num3, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
largest = num1;
if (num2 > largest) {
largest = num2;
}
if (num3 > largest) {
largest = num3;
}
printf("The largest number is: %d", largest);
return 0;
}

Explanation: This demonstrates basic conditional statements (`if`). The program initializes `largest` to the first number and then uses `if` statements to compare it with the other two, updating `largest` if a larger number is found. A more concise approach could use nested `if-else` statements or the ternary operator for brevity.

Exercise 4: Factorial Calculation

Problem: Write a program to calculate the factorial of a given non-negative integer.

Solution:#include <stdio.h>
int main() {
int num, i, factorial = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.");
} else {
for (i = 1; i

2025-04-06


Previous:Mastering Line Art in Photoshop: A Comprehensive Tutorial for Beginners and Beyond

Next:Mastering Visual FoxPro: A Comprehensive Programming Tutorial