2024’s Top 14 C Program Examples: Master the Essentials

c program examples

Dear friends, lets explore some c program examples

C Program Examples 1 : Input day number of week and print name of the day using switch( )

#include <stdio.h>

int main() {
    int dayNumber;

    // Prompt the user to enter the day number
    printf("Enter the day number (1-7): ");
    scanf("%d", &dayNumber);

    // Use a switch statement to determine the day name
    switch (dayNumber) {
        case 1:
            printf("Sunday\n");
            break;
        case 2:
            printf("Monday\n");
            break;
        case 3:
            printf("Tuesday\n");
            break;
        case 4:
            printf("Wednesday\n");
            break;
        case 5:
            printf("Thursday\n");
            break;
        case 6:
            printf("Friday\n");
            break;
        case 7:
            printf("Saturday\n");
            break;
        default:
            printf("Invalid day number. Please enter a number between 1 and 7.\n");
            break;
    }

    return 0;
}

Output:

Enter the day number (1-7): 6
Friday

Enter the day number (1-7): 8
Invalid day number. Please enter a number between 1 and 7.

C Program Examples 2 : Input day number of week and decide whether working day or week end, using switch( )

#include <stdio.h>

int main() {
    int dayNumber;

    // Prompt the user to enter the day number
    printf("Enter the day number (1-7): ");
    scanf("%d", &dayNumber);

    // Use a switch statement to determine whether it's a working day or weekend
    switch (dayNumber) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("It's a working day.\n");
            break;
        case 6:
        case 7:
            printf("It's the weekend!\n");
            break;
        default:
            printf("Invalid day number. Please enter a number between 1 and 7.\n");
            break;
    }

    return 0;
}

Output:

Enter the day number (1-7): 5
It's a working day.

C Program Examples 3 : Input marks and assign grade to a student using switch( )

#include <stdio.h>

int main() {
    int marks;

    // Prompt the user to enter the marks
    printf("Enter the marks (0-100): ");
    scanf("%d", &marks);

    // Use a switch statement to assign a grade
    switch (marks / 10) {
        case 10:
        case 9:
            printf("Grade: A\n");
            break;
        case 8:
            printf("Grade: B\n");
            break;
        case 7:
            printf("Grade: C\n");
            break;
        case 6:
            printf("Grade: D\n");
            break;
        case 5:
        case 4:
            printf("Grade: E\n");
            break;
        default:
            printf("Grade: F (Fail)\n");
            break;
    }

    return 0;
}

Output:

Enter the marks (0-100): 95
Grade: A

C Program Examples 4 : Calculate Sum of 1/2 + 2/3 + 3/4 … + n terms

#include <stdio.h>

int main() {
    int n;
    float sum = 0.0;

    // Prompt the user to enter the number of terms (n)
    printf("Enter the number of terms (n): ");
    scanf("%d", &n);

    if (n < 1) {
        printf("Invalid input. Please enter a positive integer for n.\n");
    } else {
        for (int i = 1; i <= n; i++) {
            sum += (float)i / (i + 1);
        }

        // Display the sum of the series
        printf("Sum of the series: %.2f\n", sum);
    }

    return 0;
}

Output:

Enter the number of terms (n): 5
Sum of the series: 3.55

C Program Examples 5 : Calculate Sum of S = x/1 + x/2 + x/3 … + x/n

#include <stdio.h>

int main() {
    int a, n, x;
    float s;

    printf("\nEnter value for x and n: ");
    scanf("%d%d", &x, &n);
    
    s = 0.0;
    a = 1;
    
    while (a <= n) {
        s = s + x / (float)(a);
        a = a + 1;
    }
    
    printf("\nSum of the series is %.2f", s);
    
    return 0;
}

Output:

Enter value for x and n: 3 5
Sum of the series is 6.85

Note that, if input is x=3 and n=5

s = 3/1 + 3/2 + 3/3 + 3/4 + 3/5
  = 3 + 1.5 + 1 + 0.75 + 0.6
  = 6.85

C Program Examples 6 : Generate Fibonacci series up to n terms

#include <stdio.h>

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

    // Prompt the user to enter the number of terms (n)
    printf("Enter the number of terms in the Fibonacci series: ");
    scanf("%d", &n);

    printf("Fibonacci Series up to %d terms: ", n);

    // Special case for the first two terms
    if (n >= 1) {
        printf("%d", first);
    }
    if (n >= 2) {
        printf(", %d", second);
    }

    for (i = 3; i <= n; i++) {
        // Calculate the next term by adding the previous two terms
        next = first + second;

        // Print the next term
        printf(", %d", next);

        // Update the values of first and second for the next iteration
        first = second;
        second = next;
    }

    printf("\n");
    return 0;
}

Output:

Enter the number of terms in the Fibonacci series: 10
Fibonacci Series up to 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

C Program Examples 7 : To find Pythagorean triplets in the range 1 to 25

Note:
Pythagorean triplet is a set of three numbers that satisfy the Pythagorean theorem.
For Example:
3 4 5 = 9 + 16 = 25
5 12 13 = 25 +144 = 169

#include <stdio.h>

int main() {
    printf("Pythagorean Triplets in the range 1 to 25:\n");

    for (int a = 1; a <= 25; a++) {
        for (int b = a; b <= 25; b++) {
            for (int c = b; c <= 25; c++) {
                if (a * a + b * b == c * c) {
                    printf("%d, %d, %d\n", a, b, c);
                }
            }
        }
    }

    return 0;
}

Output:

Pythagorean Triplets in the range 1 to 25:
3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
12, 16, 20
15, 20, 25

C Program Examples 8 : Sine Series

#include <stdio.h>
#include <math.h>

int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}

double power(double base, int exponent) {
    if (exponent == 0) return 1.0;
    return base * power(base, exponent - 1);
}

int main() {
    double x, sum = 0.0;
    int n;

    // Prompt the user to enter the angle in radians
    printf("Enter the angle (in radians): ");
    scanf("%lf", &x);

    // Prompt the user to enter the number of terms
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        int sign = (i % 2 == 0) ? 1 : -1; // Alternate signs
        double term = (sign * power(x, 2 * i + 1)) / factorial(2 * i + 1);
        sum += term;
    }

    // Display the result
    printf("Sine of %.2lf radians (approximated with %d terms) is: %.6lf\n", x, n, sum);

    return 0;
}

Output:

Enter the angle (in radians): 1.047
Enter the number of terms: 10
Sine of 1.05 radians (approximated with 10 terms) is: 0.865927

C Program Examples 9 : Print ***** (5 Stars)

#include <stdio.h>

int main() {
    // Use a loop to print five asterisks
    for (int i = 0; i < 5; i++) {
        printf("*");
    }
    
    // Print a newline to end the line
    printf("\n");

    return 0;
}

Output:

*****

C Program Examples 10 : Print ***** in 5 lines

#include <stdio.h>

int main() {
    // Use two nested loops to print five lines of five asterisks
    for (int i = 0; i < 5; i++) { // This outer loop controls the lines
        for (int j = 0; j < 5; j++) { // This inner loop controls the stars in each line
            printf("*");
        }
        // Print a newline to move to the next line
        printf("\n");
    }

    return 0;
}

Output:

*****
*****
*****
*****
*****

C Program Examples 11 : Print left aligned triangle of stars in increasing order

#include <stdio.h>

int main() {
    int rows;

    // Prompt the user to enter the number of rows for the triangle
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) { // Loop through each row
        for (int j = 1; j <= i; j++) { // Loop to print stars in each row
            printf("*");
        }
        // Move to the next line for the next row
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 5
*
**
***
****
*****

C Program Examples 12 : Print left aligned triangle of stars , in decreasing order

#include <stdio.h>

int main() {
    int rows;

    // Prompt the user to enter the number of rows for the triangle
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = rows; i >= 1; i--) { // Loop through each row in decreasing order
        for (int j = 1; j <= i; j++) { // Loop to print stars in each row
            printf("*");
        }
        // Move to the next line for the next row
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 5
*****
****
***
**
*

C Program Examples 13 : To Print left aligned triangle of stars, in decreasing order for n rows

#include <stdio.h>

int main() {
    int n;

    // Prompt the user to enter the number of rows for the triangle
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) { // Loop through each row in decreasing order
        for (int j = 1; j <= i; j++) { // Loop to print stars in each row
            printf("*");
        }
        // Move to the next line for the next row
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 7
*******
******
*****
****
***
**
*

C Program Examples 14 : Print center aligned triangle of stars, in increasing order

#include <stdio.h>

int main() {
    int n;

    // Prompt the user to enter the number of rows for the triangle
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) { // Loop through each row
        // Print spaces to center-align the stars
        for (int space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print stars in increasing order
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        // Move to the next line for the next row
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 5
    *
   ***
  *****
 *******
*********

One thought on “2024’s Top 14 C Program Examples: Master the Essentials

Leave a Reply

Your email address will not be published. Required fields are marked *