C Programming Examples.

Welcome to the world of C programming! If you are just starting your journey in the realm of programming or looking to sharpen your coding skills, you have come to the right place. This article page is dedicated to providing you with a diverse collection of C program examples to practice and master the language.

C Programming Examples List

C Programs - Basic.

👉 Hello World Program in C.

👉 C Program Input/Output Operation.

👉 C Program to Add Two Integer Numbers.

👉 C Program to Find the ASCII value of a Character.

👉 C Program to Swap Two Numbers

👉 C Program to Find Quotient and Remainder.

👉 C Program to Calculate Fahrenheit to Celsius and vice-versa.

👉 C program to Add Two Complex Numbers.

👉 C Program to Find the Size of int, float, char, and double.

👉 C Program to Calculate Simple Interest.

👉 C Program to Calculate Compound Interest.

👉 C Program to Find Area of a Circle.


C Program - Control Follow.

👉 C Program to Find Greatest Number Among Three.

👉 C Program to Check Whether the Number is Even or Odd.

👉 C Program to Count Vowel and Consonant.

👉 C Program to Check Leap Year.

👉 C Program to Print Sum of Natural Numbers.

👉 C Program to Find Factorial of a Number.

👉 C Program to Print Fabanocci Series.

👉 C Program to Check Armstrong Number.

👉 C Program to Check Palindrome Number.

👉 C Program to Check if a number is prime or not.

👉 C Program to Find GCD for two numbers.

👉 C Program to Reverse a Number.

👉 C Program to Create a Calculator.

👉 C Program to Generate Multiplication Table.


Conversion Programs in C.

👉 C Program to Convert Binary to Octal.

👉 C Program to Convert Octal to Binary.

👉 C Program to Convert Binary to Decimal.

👉 C Program to Convert Octal to Decimal.


Patterns Programs in C.

👉 C Program to Print Pyramid Patterns.

👉 C Program to Print Dymond Patterns.


C Program - Arrays.

👉 C Program to Find Sum of Array Elements.

👉 C Program to Find the Largest Element in an array.

👉 C Program to Find the Smallest Element in an Array.

👉 C Program to Reverse the Elements of an Array.

👉 C Program to Search for a Given Element in an Array.

👉 C Program to Count Number of Odd and Even Elements in an Array.

👉 C Program to Find and Display duplicate elements in an array.

👉 C Program to Calculate Average of All the Elements of an Array.


C Program - String.

👉 C Program to Find Length of a String.

👉 C Program to Copy One String to Another String.

👉 C Program to concatenate two strings.

👉 C Program to Compare Two Strings.

👉 C Program to Reverse Given String.

C Program to Check Palindrome Number.

In this C programming tutorial, we will learn how to check if the given number is a palindrome number or not. Before moving to the code section let us understand what is Palindrome number?

Palindrome Number.

A palindrome number is a number that reads the same backward as forward. In other words, the digits of a palindrome number remain unchanged when read from left to right and from right to left. 

Example: 121, 454, and 12321 are palindrome numbers.

Algorithm to Check Palindrome Number.

Step 1: Input the number from the user.
Step 2: Create a copy of the original number to compare later.
Step 3: Initialize variables to store the reverse of the number and the remainder.
Step 4: Use a loop to reverse the digits of the number:
  • a) Extract the last digit of the number using the modulus (%) operator.
  • b) Add the last digit to the reversed number after moving the previous digits one place left using multiplication (*= 10).
  • c) Remove the last digit from the original number using integer division ( /= 10).
  • d) Repeat the above steps until the original number becomes 0.
Step 5: Compare the reversed number with the copy of the original number.
Step 6: If the reversed number is equal to the original number, it is a palindrome. Otherwise, it is not.

Program to Check Palindrome Number.

// c code implementation to check palindrome number
#include <stdio.h>

int main() {
    int num, originalNum, reversedNum = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;         
        reversedNum = reversedNum * 10 + remainder;   
        num /= 10;                    
    }

    // Compare with the original number
    if (reversedNum == originalNum) {
        printf("%d is a palindrome number.\n", originalNum);
    } else {
        printf("%d is not a palindrome number.\n", originalNum);
    }

    return 0;
}
Output:
Enter an integer: 12321
12321 is a palindrome number.

Time Complexity: O(n) where n is the number of digits present in the given number.
Space Complexity: O(1) no extra space is required to solve this problem.

Similar articles:

C Program to Print Diamond Patterns.

In this C programming tutorial, we will explore how to print diamond patterns using loops. 


The diamond pattern consists of two inverted right-angled triangles joined at the base. These patterns are a classic example of nested loops, where we use loops inside loops to control the pattern's structure.


Here we are going to print two different types of Diamond Patterns:

Pattern 1: Diamond Pattern with Asterisks.

C Code:

//C program to print diamond patterns
#include <stdio.h>

int main() {
    int rows, i, j, space;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper half of the diamond
    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (i = rows - 1; i >= 1; i--) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Pattern 2: Hollow Diamond Pattern.

C Code:

// C program to print hollow diamond shape
#include <stdio.h>

int main() {
    int rows, i, j, space;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper half of the diamond
    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (i = rows - 1; i >= 1; i--) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 6
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *

C Program to Print Pyramid Patterns.

Patterns help us understand valuable insights into the logic and control flow required in programming. In this C programming tutorial, we are going to learn how to print different kinds of Pyramid patterns.

Here we are going to see 6 different kinds of Pyramid patterns with a C code for each of them:

Pattern 1: Left-aligned Pyramid Pattern.

Below is the C program to print a left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    printf("\nPattern 1:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
Pattern 1:
* 
* * 
* * * 
* * * * 
* * * * * 


Pattern 2: Inverted Left-aligned Pyramid Pattern.

Below is the C program to print an Inverted left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
* * * * * 
* * * * 
* * * 
* * 
* 

Pattern 3: Center-aligned Pyramid Pattern.

Below is the C program to print a Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

Pattern 4: Inverted Center-aligned Pyramid Pattern.

Below is the C program to print an Inverted Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 6
* * * * * * * * * * * 
  * * * * * * * * * 
    * * * * * * * 
      * * * * * 
        * * * 
          * 


Pattern 5: Right-aligned Pyramid Pattern.

Below is the C program to print a Right-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    *
   **
  ***
 ****
*****


Pattern 6: Number Pyramid Pattern.

Below is the C program to print a Number pyramid pattern.
#include <stdio.h>

int main() {
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (int j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    1
   121
  12321
 1234321
123454321

C Program to Convert Octal to Decimal.

In this C program tutorial, we are going to learn how to convert an Octal to a Decimal number. Let's understand the process with an example. 

Step-by-step Algorithm:

Step 1: Input the octal number from the user.
Step 2: Initialize the decimalNum variable to 0, and base variable to 1. The decimalNum will store the final decimal equivalent, and the base is used to keep track of the current place value (powers of 8).
Step 3: Convert octal to decimal using a while loop. In each iteration, we extract the last digit (remainder) of the octal number using the modulus operator %. Then, we multiply the digit with the current base value and add it to the decimalNum. We update the octalNum by removing the last digit (integer division by 10) and increasing the base by multiplying it by 8, which represents the next place value (8 raised to the power of the current position).
Step 4: Output the decimal equivalent of the octal number.

C Code:

//C program to Convert Octal to Decimal.
#include <stdio.h>

int main() {
    int octalNum, decimalNum = 0, base = 1, remainder;

    printf("Enter an octal number: ");
    scanf("%d", &octalNum);

    // Convert octal to decimal
    while (octalNum != 0) {
        // Extract the last digit
        remainder = octalNum % 10; 
        // Multiply digit with base and add to decimalNum
        decimalNum += remainder * base;
        // Remove the last digit
        octalNum /= 10; 
        // Increase base by a power of 8 for next digit
        base *= 8; 
    }

    printf("Decimal equivalent: %d\n", decimalNum);

    return 0;
}
Output:
Enter an octal number: 127
Decimal equivalent: 87

Time Complexity: O(log N)
Space Complexity: O(1)

How To Create a Sticky Header on Scroll?

A sticky header is a popular web design technique that keeps the header fixed at the top of the page, even as users scroll down the content. This provides a seamless user experience, making it easier for visitors to access important navigation options without having to scroll back to the top of the page. 

Here are steps that you can follow to Create a Sticky Header for your Webpage:


Step 1: Add the necessary HTML section.

To create a sticky header, we first need to set up the HTML structure. The header element typically contains the website logo, navigation menu, and other crucial elements. We can use the <header> tag to enclose these elements and ensure it's placed at the top of the <body> element.

HTML Code:

<!DOCTYPE html>
<html>
  <head>
    <title>Sticky Header Example</title>
  </head>
  <body>
    <header>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <h1>Welcome to Our Website</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non
        sapien vitae elit blandit bibendum a eu metus.
      </p>
      <!-- Add more content here -->
    </main>

    <footer>
      <p>&copy; 2023 Your Website. All rights reserved.</p>
    </footer>
  </body>
</html>

Step 2: Add the necessary CSS Style to fix the header on Scroll.

To make the header stick to the top of the page, we'll use CSS positioning. We can set the header's position to "fixed" to ensure it remains fixed relative to the viewport while users scroll down.

CSS Code:
header {
    position: fixed;
    top: 0;
    width: 100%;
    background-color: #ffffff;
    /* Add other styles such as height, 
    padding, and box-shadow as per your design */
}

If you want more control over the sticky header behavior, JavaScript can be used to add or remove the "sticky" class dynamically based on the user's scrolling behavior.

Javascript Code:
<script>
window.addEventListener('scroll', function() {
    const header = document.querySelector('header');
    if (window.scrollY > 0) {
        header.classList.add('sticky');
    } else {
        header.classList.remove('sticky');
    }
});
</script>

Below is the complete working example of a Sticky Header:
<!DOCTYPE html>
<html>
  <head>
    <title>Sticky Header Example</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        font-family: Arial, sans-serif;
      }

      header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        background-color: #333;
        padding: 10px 20px;
        color: #fff;
      }

      nav ul {
        list-style: none;
        margin: 0;
        padding: 0;
      }

      nav li {
        display: inline-block;
        margin-right: 20px;
      }

      nav li a {
        text-decoration: none;
        color: #fff;
        font-size: 18px;
      }

      main {
        padding: 100px 20px;
        text-align: center;
      }

      h1 {
        font-size: 36px;
        margin-bottom: 20px;
      }

      footer {
        text-align: center;
        background-color: #333;
        color: #fff;
        padding: 10px;
      }

      /* Sticky header styles */
      header.sticky {
        position: fixed;
        top: 0;
      }
    </style>
  </head>
  <body>
    <header>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <h1>Welcome to Our Website</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non
        sapien vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit
        amet, consectetur adipiscing elit. Phasellus non sapien vitae elit
        blandit bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur
        adipiscing elit. Phasellus non sapien vitae elit blandit bibendum a eu
        metus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Phasellus non sapien vitae elit blandit bibendum a eu metus. Lorem ipsum
        dolor sit amet, consectetur adipiscing elit. Phasellus non sapien vitae
        elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
        consectetur adipiscing elit. Phasellus non sapien vitae elit blandit
        bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur adipiscing
        elit. Phasellus non sapien vitae elit blandit bibendum a eu metus. Lorem
        ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non sapien
        vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
        consectetur adipiscing elit.
      </p>
      <h3>Coding is Great</h3>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non
      sapien vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Phasellus non sapien vitae elit blandit
      bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur adipiscing
      elit. Phasellus non sapien vitae elit blandit bibendum a eu metus. Lorem
      ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non sapien
      vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Phasellus non sapien vitae elit blandit
      bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur adipiscing
      elit. Phasellus non sapien vitae elit blandit bibendum a eu metus. Lorem
      ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non sapien
      vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Phasellus non sapien vitae elit blandit
      bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur adipiscing
      elit. Phasellus non sapien vitae elit blandit bibendum a eu metus. Lorem
      ipsum dolor sit amet, consectetur adipiscing elit. Phasellus non sapien
      vitae elit blandit bibendum a eu metus. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Phasellus non sapien vitae elit blandit
      bibendum a eu metus. Lorem ipsum dolor sit amet, consectetur adipiscing
      elit.
    </main>

    <footer>
      <p>&copy; 2023 Your Website. All rights reserved.</p>
    </footer>
  </body>
  <script>
    // JavaScript to make the header sticky
    window.addEventListener("scroll", function () {
      const header = document.querySelector("header");
      if (window.scrollY > 0) {
        header.classList.add("sticky");
      } else {
        header.classList.remove("sticky");
      }
    });
  </script>
</html>
Output:
Sticky Header Example

Pro-Tip: For simple cases, where you need the header to stick only during a certain scroll range, the CSS position: sticky property can be used.

CSS Code:

header {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    width: 100%;
    background-color: #ffffff;
    /* Add other styles as per your design */
}

C Program to Convert Binary to Decimal.

Write a C program to convert a binary number to its decimal equivalent. 
Let's understand the processing of converting a binary number to a decimal number with one example.

Example: 
Input: binaryNum = 10101
Output: 22

Explanation:
remainder = 10101 % 10 = 1, 
decimal = 1 * 1 = 1, base = 1 * 2 = 2, 
binaryNum = 1010

remainder = 1010 % 10 = 0, 
decimal = 0 * 2 + 1 * 2 = 2, base = 2 * 2 = 4, 
binaryNum = 101

remainder = 101 % 10 = 1, 
decimal = 1 * 4 + 2 * 1 = 6, base = 4 * 2 = 8, 
binaryNum = 10

remainder = 10 % 10 = 0, 
decimal = 0 * 8 + 6 * 1 = 6, base = 8 * 2 = 16, 
binaryNum = 1

remainder = 1 % 10 = 1, 
decimal = 1 * 16 + 6 * 1 = 22, base = 16 * 2 = 32, 
binaryNum = 0

Decimal equivalent = 22

Steps-by-step Algorithm:

Step 1: Take input of a binary number from the user.
Step 2: Initialize a variable decimal to store the decimal equivalent, initially set to 0.
Step 3: Initialize a variable base to store the current place value, initially set to 1.
Step 4: Extract the rightmost digit of the binary number using the modulo operator %.
Step 5: Multiply the extracted digit by base and add it to the decimal variable.
Step 6: Divide the binary number by 10 to remove the rightmost digit.
Step 7: Update the base value by multiplying it by 2, as we are moving to the next higher place value (binary is base-2 system).
Step 8: Repeat steps 4 to 7 until the binary number becomes 0.
Step 9: The value of the decimal variable will now be the decimal equivalent of the input binary number.

C code:
//C program to convert binary to decimal number
#include <stdio.h>

int main() {

    long long binaryNum;
    int decimal = 0, base = 1, remainder;

    printf("Enter a binary number: ");
    scanf("%lld", &binaryNum);

    //converting binary to decimal
    while (binaryNum != 0) {
        remainder = binaryNum % 10;
        decimal += remainder * base;
        base *= 2;
        binaryNum /= 10;
    }

    printf("Decimal equivalent: %d\n", decimal);

    return 0;
}
Output:
Enter a binary number: 11011
Decimal equivalent: 27

Time Complexity: The time complexity of the above code is O(log N), where N is the decimal equivalent of the binary number. 

Space Complexity: The space complexity of the above code is O(1), which means it uses a constant amount of additional space that does not depend on the input.

Related articles:

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson