C_Programming
C_Programming
This chapter introduces the basic structure of a C program, variables, input/output functions, and
arithmetic operations.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This chapter covers structured programming concepts including if-else statements, loops, and
control structures.
1. If-Else Statement
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive number\n");
} else if (num < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
This chapter covers loops, switch case, do-while, break, and continue.
1. Switch Case
#include <stdio.h>
int main() {
int choice;
switch (choice) {
case 1:
printf("Game started!\n");
break;
case 2:
printf("Your score is 100.\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
return 0;
}
2. For Loop
#include <stdio.h>
int main() {
int N;
printf("Enter N: ");
scanf("%d", &N);
return 0;
}
3. Do-While Loop
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
if (num != 0) {
printf("You entered: %d\n", num);
}
} while (num != 0);
return 0;
}
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
printf("Skipping 5 using continue\n");
continue;
}
if (i == 8) {
printf("Breaking at 8\n");
break;
}
printf("%d ", i);
}
return 0;
}
Chapter 4: Functions
Functions help modularize programs, making them reusable and easier to manage. This section
includes examples of user-defined and recursive functions.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);