0% found this document useful (0 votes)
3 views

C_Programming

The document covers the fundamentals of C programming across four chapters, including basic program structure, control structures like if-else statements and loops, and the use of functions. It provides code examples for each concept, such as a Hello World program, switch case, for loops, and recursive functions. The content is designed to help beginners understand and implement basic programming techniques in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C_Programming

The document covers the fundamentals of C programming across four chapters, including basic program structure, control structures like if-else statements and loops, and the use of functions. It provides code examples for each concept, such as a Hello World program, switch case, for loops, and recursive functions. The content is designed to help beginners understand and implement basic programming techniques in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Programming: Chapters 1 to 4

Chapter 1: Introduction to C Programming

This chapter introduces the basic structure of a C program, variables, input/output functions, and
arithmetic operations.

1. Basic Hello World Program

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

Chapter 2: Structured Program Development

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;
}

Chapter 3: Program Control Structures

This chapter covers loops, switch case, do-while, break, and continue.
1. Switch Case

#include <stdio.h>

int main() {
int choice;

printf("1. Start Game\n");


printf("2. View Score\n");
printf("3. Exit\n");
printf("Enter choice: ");
scanf("%d", &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);

for (int i = 1; i <= N; i++) {


printf("%d ", i);
}

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;
}

4. Break and Continue

#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.

1. Function to Add Two Numbers

#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);

int result = add(x, y); // Function call


printf("Sum: %d\n", result);
return 0;
}

2. Recursive Function for Factorial

#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);

printf("Factorial of %d is %d\n", num, factorial(num));


return 0;
}

You might also like