## Embarking on the C Odyssey: Basic Programming Exercises and Solutions

Welcome, intrepid adventurer, to the exciting world of C programming! This guide offers a treasure trove of basic exercises and solutions, each a stepping stone on your journey to mastering this powerful language. By tackling these challenges, you’ll hone your coding skills, unveil the magic within lines of code, and unlock the boundless possibilities of C.

## 1. Arithmetic Adventures: Mastering Math with Code

Start by flexing your mathematical muscles with these numerical exercises:

**a) Sum of Two Numbers:** Write a program that takes two numbers from the user and prints their sum.

C

`#`**include** <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of the numbers is: %d\n", sum);
return 0;
}

**b) Area of a Rectangle:** Craft a program that calculates the area of a rectangle, taking length and width as input.

C

`#`**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\n", area);
return 0;
}

**c) Average of Three Numbers:** Design a program that accepts three numbers and prints their average.

C

`#`**include** <stdio.h>
int main() {
int num1, num2, num3, average;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
average = (num1 + num2 + num3) / 3;
printf("The average of the numbers is: %d\n", average);
return 0;
}

These exercises illustrate how C handles variables, user input, and operators, laying the foundation for more complex calculations.

## 2. Conditional Conquests: Making Decisions with Code

Dive deeper with conditional statements, allowing your programs to make choices based on conditions:

**a) Positive or Negative:** Write a program that takes a number from the user and tells them if it’s positive, negative, or zero.

C

`#`**include** <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}

**b) Even or Odd:** Design a program that checks if a number is even or odd.

C

`#`**include** <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}

These exercises showcase the power of `if`

statements and logical operators, enabling your programs to react based on user input and data values.

## 3. Looping Legions: Repetition at Your Command

Next, master the art of loops, where repetitive tasks become effortless:

**a) Print Numbers 1 to 10:** Write a program that prints the numbers 1 to 10 using a loop.

C

`#`**include** <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}

**b) Print Sum of Squares:** Design a program that prompts the user for a number and then prints the sum of the squares of all numbers from 1 to that number.

C

`#`**include** <stdio.h>
int main() {
int num, sumOfSquares = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
sumOfSquares += i * i;
}
printf("The sum of squares from 1 to %d is: %d\n", num, sumOfSquares);
return 0;
}

This code uses a loop to iterate from 1 to the user-defined number, squaring each number and adding it to the running sum.

**c) Factorial Calculation:** Write a program that takes a number from the user and calculates its factorial (product of all positive integers less than or equal to itself).

C

`#`**include** <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 0;
}
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("The factorial of %d is: %d\n", num, factorial);
return 0;
}

This code checks for negative input and then uses a loop to multiply the number by all positive integers less than or equal to itself, calculating the factorial.

By tackling these exercises, you’ll master the concept of loops and their power in handling repetitive tasks and calculations.

## Beyond the Basics: Expanding Your C Horizons

These are just the first steps on your C programming journey. Remember, practice makes perfect! Explore online resources, community forums, and challenging projects to refine your skills and delve deeper into advanced C concepts. With dedication and curiosity, you’ll soon be crafting intricate programs and solving real-world problems with the power of C at your fingertips.

**Resources for Further Exploration:**

- “C Programming Exercises and Solutions” by Brian Kernighan and Dennis Ritchie
- “Learn C the Hard Way” by Zed A. Shaw
- Online tutorials and forums dedicated to C programming

Embrace the challenge, adventurer! The world of C programming awaits you, brimming with possibilities and potential. Happy coding!