0% found this document useful (0 votes)
4 views3 pages

Basic C Programs Explained

The document provides basic C programming examples along with explanations for each program. It includes a 'Hello World' program, addition of two numbers, checking if a number is even or odd, calculating the factorial of a number, and reversing a string. Each example is accompanied by code snippets and a brief explanation of its functionality.

Uploaded by

kraishwarya334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Basic C Programs Explained

The document provides basic C programming examples along with explanations for each program. It includes a 'Hello World' program, addition of two numbers, checking if a number is even or odd, calculating the factorial of a number, and reversing a string. Each example is accompanied by code snippets and a brief explanation of its functionality.

Uploaded by

kraishwarya334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like