C Programs: 12 Exciting Examples for Your Coding Journey

C Programs

Dear friends , Let’s continue to explore more C programs and their corresponding output.

C Programs 1 : Print following Floyd’s triangle

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

#include <stdio.h>

int main() {
    int n, i, j, num = 1;

    // Input the number of rows for the Floyd's triangle
    printf("Enter the number of rows for Floyd's triangle: ");
    scanf("%d", &n);

    // Loop to iterate through each row
    for (i = 1; i <= n; i++) {
        // Loop to print each number in the row
        for (j = 1; j <= i; j++) {
            // Print 1 if the sum of row number and column number is even, else print 0
            if ((i + j) % 2 == 0) {
                printf("1 ");
            } else {
                printf("0 ");
            }
        }
        printf("\n"); // Move to the next row
    }

    return 0;
}

Output:

Enter the number of rows for Floyd's triangle: 5
1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 

C Programs 2 : Print following Floyd’s triangle

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

#include <stdio.h>

int main() {
    int n, num = 1;

    // Input the number of rows for Floyd's triangle
    printf("Enter the number of rows for Floyd's triangle: ");
    scanf("%d", &n);

    // Loop to iterate through each row
    for (int i = 1; i <= n; i++) {
        // Loop to print each number in the row
        for (int j = 1; j <= i; j++) {
            // Print the current number and increment it
            printf("%d\t", num);
            num++;
        }
        printf("\n"); // Move to the next row
    }

    return 0;
}

Output:

Enter the number of rows for Floyd's triangle: 5
1	
2	 3	
4	 5	6	
7	 8	9	 10	
11 12	13 14	15	

C Programs 3 : Print following center aligned triangle

1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9

#include <stdio.h>

int main() {
    int n = 5;  // Number of rows in the triangle

    int max_width = 2 * n - 1; // Maximum width of the triangle

    for (int i = 1; i <= n; i++) {
        int num = 1;  // Initialize the first number for each row
        int spaces = (max_width - 2 * i + 1) / 2; // Calculate the number of spaces to center-align the row

        // Print leading spaces
        for (int j = 0; j < spaces; j++) {
            printf("  ");
        }

        // Print numbers for the current row
        for (int j = 0; j < 2 * i - 1; j++) {
            printf("%2d", num);
            num++;
        }

        // Print trailing spaces and move to the next line
        for (int j = 0; j < spaces; j++) {
            printf("  ");
        }
        printf("\n");
    }

    return 0;
}

Output:

         1        
       1 2 3      
     1 2 3 4 5    
   1 2 3 4 5 6 7  
 1 2 3 4 5 6 7 8 9

C Programs 4 : Input text and count number of characters, words and lines

#include <stdio.h>

int main() {
    char letter = 'a';  // Start with the letter 'a'

    printf("Lowercase alphabets from 'a' to 'z':\n");

    while (letter <= 'z') {
        printf("%c ", letter);  // Print the current letter
        letter++;  // Move to the next letter
    }

    printf("\n");

    return 0;
}

Output:

Lowercase alphabets from 'a' to 'z':
a b c d e f g h i j k l m n o p q r s t u v w x y z 

C Programs 5 : Print alphabets in Upper case A B C D …Z

#include <stdio.h>

int main() {
    char letter = 'A';  // Start with the letter 'A'

    printf("Uppercase alphabets from 'A' to 'Z':\n");

    while (letter <= 'Z') {
        printf("%c ", letter);  // Print the current letter
        letter++;  // Move to the next letter
    }

    printf("\n");

    return 0;
}

Output:

Uppercase alphabets from 'A' to 'Z':
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

C Programs 6 : Print lowercase as well as uppercase alphabets in reverse order

Z Y X . . . C B A
z y x . . . c b a

#include <stdio.h>

int main() {
    char lowercase = 'a'; // Starting with 'a'
    char uppercase = 'A'; // Starting with 'A'

    printf("Uppercase alphabets in reverse order:\n");

    // Print uppercase alphabets in reverse order
    for (int i = 25; i >= 0; i--) {
        printf("%c ", uppercase + i); // Print the uppercase letter
    }

    printf("\n\nLowercase alphabets in reverse order:\n");

    // Print lowercase alphabets in reverse order
    for (int i = 25; i >= 0; i--) {
        printf("%c ", lowercase + i); // Print the lowercase letter
    }

    printf("\n");

    return 0;
}

Output:

Uppercase alphabets in reverse order:
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A 

Lowercase alphabets in reverse order:
z y x w v u t s r q p o n m l k j i h g f e d c b a 

C Programs 7 : Calculate power of a number

#include <stdio.h>

// Function to calculate the power
double power(double base, int exponent) {
    double result = 1.0;

    // For positive exponent
    while (exponent > 0) {
        result *= base;
        exponent--;
    }

    // For negative exponent
    while (exponent < 0) {
        result /= base;
        exponent++;
    }

    return result;
}

int main() {
    double base;
    int exponent;

    printf("Enter the base number: ");
    scanf("%lf", &base);

    printf("Enter the exponent: ");
    scanf("%d", &exponent);

    double result = power(base, exponent);

    printf("%.3lf raised to the power of %d is %.3lf\n", base, exponent, result);

    return 0;
}

Output:

Enter the base number: 2
Enter the exponent: 5
2.000 raised to the power of 5 is 32.000

C Programs 8 : Calculate sum of digits in a number

#include <stdio.h>

// Function to calculate the sum of digits
int sumOfDigits(int number) {
    int sum = 0;

    while (number > 0) {
        int digit = number % 10; // Extract the last digit
        sum += digit; // Add the digit to the sum
        number /= 10; // Remove the last digit
    }

    return sum;
}

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    int result = sumOfDigits(num);

    printf("Sum of digits in %d is %d\n", num, result);

    return 0;
}

Output:

Enter a number: 12345
Sum of digits in 12345 is 15

C Programs 9 : Input a number and calculate sum of product of its digits

#include <stdio.h>

// Function to calculate the sum of the product of digits
int sumOfProductOfDigits(int number) {
    int sum = 0;
    int product = 1;

    while (number > 0) {
        int digit = number % 10; // Extract the last digit
        sum += digit; // Add the digit to the sum
        product *= digit; // Multiply the digit to the product
        number /= 10; // Remove the last digit
    }

    printf("Sum of digits = %d\n", sum);
    printf("Product of digits = %d\n", product);

    return sum;
}

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    int result = sumOfProductOfDigits(num);

    return 0;
}

Output:

Enter a number: 12345
Sum of digits = 15
Product of digits = 120

C Programs 10 : Print odd number in the range 1 … n. Also calculate their sum and average

#include <stdio.h>

int main() {
    int n;
    printf("Enter a positive integer (n): ");
    scanf("%d", &n);

    int sum = 0;   // Initialize the sum to 0
    int count = 0; // Initialize the count of odd numbers to 0

    printf("Odd numbers in the range 1 to %d are: ", n);

    for (int i = 1; i <= n; i++) {
        if (i % 2 == 1) { // Check if the number is odd
            printf("%d ", i);
            sum += i; // Add the odd number to the sum
            count++;  // Increment the count of odd numbers
        }
    }

    if (count > 0) {
        double average = (double)sum / count; // Calculate the average
        printf("\nSum of odd numbers: %d\n", sum);
        printf("Average of odd numbers: %.2lf\n", average);
    } else {
        printf("\nNo odd numbers in the range.\n");
    }

    return 0;
}

Output:

Enter a positive integer (n): 10
Odd numbers in the range 1 to 10 are: 1 3 5 7 9 
Sum of odd numbers: 25
Average of odd numbers: 5.00

C Programs 11 : Print Chess Board

#include <stdio.h>
#include <conio.h>

int main() {
    int r, c;
    char chess[8][8];

    clrscr();

    for (r = 0; r < 8; r++) {
        for (c = 0; c < 8; c++) {
            if ((r + c) % 2 == 0)
                chess[r][c] = 219;  // ASCII code for a solid block character
            else
                chess[r][c] = 32;   // ASCII code for a space character
            printf("%c ", chess[r][c]);
        }
        printf("\n");
    }

    getch();
    return 0;
}

Output:

█   █   █   █   █   █   █   █
  █   █   █   █   █   █   █   █
█   █   █   █   █   █   █   █
  █   █   █   █   █   █   █   █
█   █   █   █   █   █   █   █
  █   █   █   █   █   █   █   █
█   █   █   █   █   █   █   █
  █   █   █   █   █   █   █   █

C Programs 12 : Input a 3 digit no. & check whether palindrome

Note: A palindrome is a number that can be read the same way both forwards and backwards. For example: 565

#include <stdio.h>

int main() {
    int num, original, reverse = 0;

    printf("Enter a 3-digit number: ");
    scanf("%d", &num);

    original = num; // Store the original number for comparison

    // Check if the number is a 3-digit number
    if (num >= 100 && num <= 999) {
        while (num > 0) {
            int digit = num % 10;
            reverse = reverse * 10 + digit;
            num = num / 10;
        }

        if (original == reverse) {
            printf("%d is a palindrome.\n", original);
        } else {
            printf("%d is not a palindrome.\n", original);
        }
    } else {
        printf("Input is not a 3-digit number.\n");
    }

    return 0;
}

Output:

Enter a 3-digit number: 121
121 is a palindrome.

Enter a 3-digit number: 123
123 is not a palindrome.

Enter a 3-digit number: 1234
Input is not a 3-digit number.

Leave a Reply

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