C_Fundamentals_Programming_Manual_Complete
C_Fundamentals_Programming_Manual_Complete
Theory:
- C is a general-purpose programming language developed by Dennis Ritchie.
- It is structured, statically typed, and widely used in system and application development.
Sample Program:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Exercise 1.1:
Write a program to print your name, your course, and your favorite hobby.
Solution:
#include <stdio.h>
int main() {
printf("Name: John Doe\n");
printf("Course: Computer Science\n");
printf("Hobby: Playing Chess\n");
return 0;
}
[Date]
1
SECTION B: C Basics
Example:
Exercise 2.1:
Declare and initialize variables for your age, GPA, and first initial. Print them.
Solution:
#include <stdio.h>
int main() {
int age = 18;
float gpa = 3.75;
char initial = 'J';
printf("Age: %d\nGPA: %.2f\nInitial: %c\n", age, gpa, initial);
return 0;
}
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
Exercise 3.1:
Ask the user to input their name and age, and then display it.
Solution:
[Date]
2
#include <stdio.h>
int main() {
char name[50];
int age;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Hello %s, you are %d years old.\n", name, age);
return 0;
}
Exercise 4.1:
Ask the user for a number and determine if it 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 number\n");
else
printf("Odd number\n");
return 0;
[Date]
3
SECTION E: Looping Constructs
Exercise 5.1:
Write a program to calculate the factorial of a number using a for loop.
Solution:
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
SECTION F: Functions
void greet() {
printf("Hello from function!\n");
}
int main() {
greet();
return 0;
}
[Date]
Exercise 6.1:
Write a function that adds two numbers and returns the result.
4
Solution:
#include <stdio.h>
int main() {
int result = add(3, 5);
printf("Sum: %d\n", result);
return 0;
}
String Example:
Exercise 7.1:
Read 5 integers into an array and find the largest.
Solution:
#include <stdio.h>
int main() {
int arr[5], max;
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] > max)
[Date]
max = arr[i];
5
}
printf("Largest number: %d\n", max);
return 0;
}
Appendix
Installing Code::Blocks:
1. Visit www.codeblocks.org
2. Download “Code::Blocks with MinGW setup.”
3. Install with default options.
4. Open Code::Blocks, go to File > New > Project > Console Application > C.
5. Start coding!
6
Sample Mini Projects
1. Simple Calculator
#include <stdio.h>
int main() {
char op;
float num1, num2;
switch(op) {
case '+': printf("Result: %.2f\n", num1 + num2); break;
case '-': printf("Result: %.2f\n", num1 - num2); break;
case '*': printf("Result: %.2f\n", num1 * num2); break;
case '/':
if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default: printf("Invalid operator.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int marks;
printf("Enter student's marks (0–100): ");
scanf("%d", &marks);
[Date]
7
else if (marks >= 50) printf("Grade: C\n");
else if (marks >= 45) printf("Grade: D\n");
else printf("Grade: F\n");
return 0;
}
#include <stdio.h>
int main() {
char answer;
int score = 0;
printf("\nQ2: 2 + 2 = ?\n");
printf("a) 3\nb) 4\nc) 5\nd) 6\n");
printf("Your answer: ");
scanf(" %c", &answer);
if (answer == 'b' || answer == 'B') score++;
return 0;
}
[Date]