Basic C Programs C Programming Examples

Let's examine a basic illustration demonstrating how to display "Hello World!" in the C programming language.

Example

Example

#include <stdio.h>

int main() {

    printf("Hello, World!\n");

    return 0;

}

Output:

Output

Hello World!

Let's see the list of C Programming Examples.

1) Fibonacci Series in C

To generate the Fibonacci series without using recursion, a loop can be utilized to calculate and print each Fibonacci number iteratively. On the other hand, when using recursion, a function can be created to call itself to calculate and print the Fibonacci numbers.

Without using recursion:

Example

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (int i = 0; i < n; i++) {
        if (i <= 1)
            next = i;
        else {
            next = first + second;
            first = second;
            second = next;
        }
        printf("%d, ", next);
    }

    return 0;
}

Using recursion:

Example

#include <stdio.h>

int fibonacci(int n) {
    if (n <= 1)
        return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int n;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");
    for (int i = 0; i < n; i++) {
        printf("%d, ", fibonacci(i));
    }

    return 0;
}

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

2) Prime Number Program in C

Write a c program to check prime number.

Input: 44

Output: not prime number

Input: 7

Output: prime number

3) Palindrome Program in C

Write a c program to check palindrome number.

Input: 329

Output: not palindrome number

Input: 12321

Output: palindrome number

4) Factorial Program in C

Write a c program to print factorial of a number.

Input: 5

Output: 120

Input: 6

Output: 720

5) Armstrong Number in C

Write a c program to check armstrong number.

Input: 153

Output: armstrong

Input: 22

Output: not armstrong

6) Sum of Digits Program in C

Write a c program to print sum of digits.

Input: 234

Output: 9

Input: 12345

Output: 15

7) Reverse Number Program in C

Write a c program to reverse given number.

Input: 123

Output: 321

8) C Program to swap two numbers without using third variable

To swap two numbers in a C program without utilizing a third variable, you can employ bitwise XOR operation. Here is an example of how you can achieve this:

Example

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

    num1 = num1 ^ num2;
    num2 = num1 ^ num2;
    num1 = num1 ^ num2;

    printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

Input: a=10 b=20

Output: a=20 b=10

9) C Program to Print "hello" without using semicolon

Create a C program that displays the word "hello" without the use of a semicolon.

10) Assembly Program in C

Write a C program that demonstrates the addition of two numbers by incorporating assembly code.

Example

#include <stdio.h>

int main() {
    int num1 = 10, num2 = 20, sum;

    // Assembly code to add two numbers
    __asm {
        mov eax, num1   // Move num1 into eax register
        add eax, num2   // Add num2 to eax register
        mov sum, eax    // Move the result to sum variable
    }

    printf("The sum of %d and %d is: %d\n", num1, num2, sum);
    
    return 0;
}

11) C Program without main function

To print "Hello" without using the main function in a C program, you can utilize the ```

include <stdio.h>

int main {

printf("Hello, World!\n");

return 0;

}

Example


include <stdio.h>

void ```

include <stdio.h>

int main {

printf("Hello, World!\n");

return 0;

}

Example

    printf("Hello");
}

int main() {
    return 0;
}

12) Matrix Multiplication in C

Create a C program to display the multiplication result of two matrices.

Input:

Example

first matrix elements:

1 1 1

2 2 2

3 3 3

second matrix elements

1 1 1

2 2 2

3 3 3

Output:

Output

multiplication of the matrix:

6 6 6

12 12 12

18 18 18

13) C Program to Convert Decimal to Binary

Create a C program that converts a decimal number into its binary equivalent.

Here is the code snippet:

Example

#include <stdio.h>

void decimalToBinary(int decimalNum) {
    int binaryNum[32];
    int i = 0;

    while (decimalNum > 0) {
        binaryNum[i] = decimalNum % 2;
        decimalNum = decimalNum / 2;
        i++;
    }

    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binaryNum[j]);
    }
}

int main() {
    int decimalNum;
    printf("Enter a decimal number: ");
    scanf("%d", &decimalNum);

    printf("Binary representation: ");
    decimalToBinary(decimalNum);

    return 0;
}

This C program takes a decimal number as input and converts it into binary representation.

Input: 5

Output: 101

Input: 20

Output: 10100

14) C Program to Print Alphabet Triangle

Write a c program to print alphabet triangle.

Output:

Output

A

    ABA

   ABCBA

  ABCDCBA

 ABCDEDCBA

15) C Program to Print Number Triangle

Write a c program to print number triangle.

Input: 7

Output:

Output

enter the range= 6

      1

     121

    12321

   1234321

  123454321

 12345654321

16) C Program to generate Fibonacci Triangle

Write a c program to generate fibonacci triangle.

Input: 5

Output:

Output

1

1	1	

1	1	2	

1	1	2	3	

1	1	2	3	5

17) C Program to convert Number in Characters

Create a C program that converts a number into its corresponding characters.

Input: 5

Output: five

Input: 203

Output: two zero three

18) Linear Search Program in C

Create a C program that executes the Linear Search algorithm.

Example

#include <stdio.h>

int linearSearch(int arr[], int n, int x) {
    for(int i = 0; i < n; i++) {
        if(arr[i] == x) {
            return i;
        }
    }
    return -1;
}

int main() {
    int arr[] = {12, 34, 45, 9, 8, 23, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 23;
    int result = linearSearch(arr, n, x);
    if(result == -1) {
        printf("Element not found");
    } else {
        printf("Element found at index: %d", result);
    }
    return 0;
}

19) Pattern Programs in C

Create a C program to display various patterns using loops.

20) Bubble Sort Program in C

Write a C program to arrange elements in an array using the bubble sort algorithm.

Input Required

This code uses input(). Please provide values below: