Foundation for Beginners PART 1
Preface
Welcome to this introductory C programming exercise/solution guide.This guide is designed for
beginners taking their first steps in C programming
In this initial part, we deliberately focus on the core building blocks of the language, omitting
more advanced features like functions, arrays, and pointers. Our goal is to provide a clear and
uncluttered path to understanding the fundamentals.
Each exercise is accompanied by a detailed solution, often with multiple approaches to
demonstrate different problem-solving techniques. We encourage you to work through the
exercises independently before consulting the solutions. This active learning process will
significantly enhance your understanding and retention.
This section does not cover all aspects of C programming, it provides a crucial foundation for
future learning. By mastering these basic concepts, you'll be well-prepared to tackle more
complex topics and build sophisticated C applications.
Let's begin!
Important Prerequisites!!!
Before starting with these exercises, you should have a basic understanding of C programming
fundamentals including:
● Variables and data types
● Input/output using printf() and scanf()
● Control structures (if, else) and switch{}
● Loops (while, for)
,This book assumes you are familiar with these concepts and focuses on practical exercises to
strengthen your skills. If you're not comfortable with any of these topics, we recommend
reviewing them before proceeding with the exercises.
Compiling and Running Your C Programs
(you can skip this part)
Before diving into C programming, you need to understand how to compile and run your code.
Unlike interpreted languages such as Python, C code must be compiled into machine code
before it can be executed. This section will guide you through this essential process.
Setting Up Your Development Environment
To get started with C programming, you'll need two main components:
1. A text editor or IDE to write your code
2. A C compiler to convert your code into an executable program
For beginners, we recommend:
● Text Editor: Visual Studio Code, Sublime Text, or Notepad++
● Compiler: GCC (GNU Compiler Collection)
Installing a C Compiler
On Windows:
1. Download and install MinGW (Minimalist GNU for Windows)
2. Add MinGW's bin directory to your system's PATH environment variable
3. Open Command Prompt and type gcc --version to verify the installation
On macOS:
1. Open Terminal
2. Install Xcode Command Line Tools by running: xcode-select --install
3. Verify the installation with gcc --version
On Linux:
1. Open Terminal
2. Install GCC using your distribution's package manager:
, ○ For Ubuntu/Debian: sudo apt-get install gcc
○ For Fedora: sudo dnf install gcc
3. Verify with gcc --version
Your First C Program
Let's create and run a simple "Hello, World!" program:
1. Create a new file named hello.c and enter this code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
2. Open your terminal/command prompt in the directory containing your file
3. Compile the program:
bash
gcc hello.c -o hello
This command tells GCC to:
● Compile hello.c (your source file)
● Create an executable named hello (or hello.exe on Windows)
4. Run your program:
● On Windows: hello
● On macOS/Linux: ./hello
Common Compilation Options
GCC provides several useful options:
● -Wall: Enable all warning messages
● -o filename: Specify the output file name
● -g: Include debugging information
● -O2: Enable optimization
,Example with multiple options:
bash
gcc -Wall -g hello.c -o hello
Troubleshooting Common Issues
1. "Command not found: gcc"
○ Solution: Verify compiler installation and PATH settings
2. "stdio.h: No such file or directory"
○ Solution: Check compiler installation and system includes
3. Permission denied when running
○ Solution: On Unix-like systems, use chmod +x ./hello
,Basic exercises
Exercise 1: Hello, World!
Write a C program that prints "Hello, World!" to the screen.
Solution:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Exercise 2: Sum of Two Numbers
Write a program that takes two integers as input and prints their sum.
Solution:
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
,Exercise 3: Even or Odd Number Check
Write a program that checks if a number is even or odd.
Solution:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
Exercise 4: Find the Largest Number
Write a program that takes three numbers as input and prints the largest among them.
Solution:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("Largest: %d\n", a);
else if (b >= a && b >= c)
printf("Largest: %d\n", b);
else
, printf("Largest: %d\n", c);
return 0;
}
Exercise 5: Factorial of a Number
Write a program that calculates the factorial of a given number using a loop.
Solution:
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d is %llu\n", n, factorial);
return 0;
}
,Advance exercises
Exercise 1
A hardware store sells Ardouinos with a 5% VAT and Intel i7-9700K CPUs at 6.5%. Write a
program that enters the price excluding tax of the hardware, enters the type of hardware on
the keyboard and displays the VAT rate and the price including tax of the hardware. To
make the program easier, the Ardouino is represented by the character “A”, and the CPU by
the character “C”
Solution
#include <stdio.h> // Standard Input/Output library
int main() {
char hardwareType;
float priceExcludingVAT, VATRate, priceIncludingVAT;
// Prompt the user to enter the type of hardware and its price
printf("Enter the hardware type ('A' for Arduino, 'C' for CPU): ");
scanf(" %c", &hardwareType);
printf("Enter the price excluding VAT: ");
scanf(" %f", &priceExcludingVAT);
// Determine the VAT rate based on the hardware type, || = or
if (hardwareType == 'A' || hardwareType == 'a') {
VATRate = 5.0;
} else if (hardwareType == 'C' || hardwareType == 'c') {
VATRate = 6.5;
} else {
printf("Invalid hardware type!\n");
return 1; // Exit the program with an error
}
// priceIncludingVAT = priceExcludingVAT + (priceExcludingVAT *VATRate / 100);
//the line under it, is the same
priceIncludingVAT = priceExcludingVAT * (1 + VATRate / 100);
// Display the VAT rate and the price including VAT
, printf("VAT Rate: %.1f%%\n", VATRate);
printf("Price including VAT: %.2f\n", priceIncludingVAT);
return 0;
}
Exercise 2
Write a program in C that reads two integers a and b and gives the user the choice: 1. to
know if the sum a + b is even. 2. to know if the product a*b is even. 3. to know the sign of
the sum a + b. 4. to know the sign of the product a*b”.
Solution 1
#include<stdio.h>
#include<conio.h>
int main(void) {
int a, b;
char choice;
printf("Enter 2 values: ");
scanf(" %d %d", &a, &b);
// note: so yeah...just click "ENTER" to write-
// the next value in case you wanted to test it at your own peace.
printf("Press\n");
printf("1 if the sum is even\n");
printf("2 if the product is even\n");
printf("3 to check the sign of the sum\n");
printf("4 to check the sign of the product\n");
getchar(); // get a character [letter] as “a”, “b”...
choice= getchar();
switch (choice) {
case '1':
if ((a + b) % 2 == 0) {
printf("The sum is even\n");
} else {
, printf("The sum is odd\n");
}
break;
case '2':
if ((a * b) % 2 == 0) {
printf("The product is even\n");
} else {
printf("The product is odd\n");
}
break;
case '3':
if (a + b >= 0) {
printf("The sum is positive\n");
} else {
printf("The sum is negative\n");
}
break;
case '4':
if (a * b >= 0) {
printf("The product is positive\n");
} else {
printf("The product is negative\n");
}
break;
Solution 2
#include <stdio.h>
int main(void) {
int a, b;
char choice;
// Prompt the user to enter two integer values.
printf("Enter two values: ");
// Read two integers from the user. Check if exactly two integers were