Basic C Programs with Explanations
1. Hello World Program
```c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
```
Explanation: This is the simplest C program. It prints text to the screen using printf().
2. Add Two Numbers
```c
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b;
printf("Sum = %d", sum);
return 0;
```
Explanation: Adds two integers and displays the result.
3. Check Even or Odd
```c
Basic C Programs with Explanations
#include <stdio.h>
int main() {
int num = 4;
if(num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
```
Explanation: Uses modulus operator to determine if a number is divisible by 2.
4. Factorial of a Number
```c
#include <stdio.h>
int main() {
int i, n = 5;
int fact = 1;
for(i = 1; i <= n; i++) {
fact *= i;
printf("Factorial = %d", fact);
return 0;
```
Basic C Programs with Explanations
Explanation: Calculates factorial using a for loop.
5. Reverse a String
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100] = "hello";
int i, len = strlen(str);
for(i = len - 1; i >= 0; i--)
printf("%c", str[i]);
return 0;
```
Explanation: Prints characters of a string in reverse order.