C All Exercises & Assignments

Write a C program to convert the obtained marks into percentage

Description: Take the obtained marks and the total marks as input. Print the percentage.

Hint:

  • Percentage = (obtained marks / total marks) x 100.

C program to calculate the percentage

#include <stdio.h>

int main(void) {
    float obtained, total, percentage;

    printf("Enter obtained marks and total marks: ");
    scanf("%f %f", &obtained, &total);

    percentage = obtained / total * 100;
    printf("Percentage = %.2f%%\n", percentage);
    return 0;
}

Output:

Output of the above program is

Enter obtained marks and total marks: 432 500
Percentage = 86.40%

How it works

  • 432 / 500 * 100 is 86.4.
  • To print the percent sign with printf() we write %%.

Write a C program to calculate the total marks of three subjects

Description: Take the marks of three subjects and print the total and the average.

Hint:

  • Total = sum of the three marks. Average = total / 3.

C program to calculate the total marks

#include <stdio.h>

int main(void) {
    int maths, science, english, total;

    printf("Enter marks of 3 subjects: ");
    scanf("%d %d %d", &maths, &science, &english);

    total = maths + science + english;
    printf("Total = %d\n", total);
    printf("Average = %.2f\n", total / 3.0);
    return 0;
}

Output:

Output of the above program is

Enter marks of 3 subjects: 78 85 91
Total = 254
Average = 84.67

How it works

  • The marks are added to get the total.
  • We divide by 3.0 to get a decimal average.

Write a C program to print your name, city and mobile number

Description: Print your name, city and mobile number on separate lines.

Hint:

  • Use printf() and the new line character \n.

C program to print name, city and mobile number

#include <stdio.h>

int main(void) {
    printf("Name: Robin Sharma\n");
    printf("City: Delhi\n");
    printf("Mobile: 9876543210\n");
    return 0;
}

Output:

Output of the above program is

Name: Robin Sharma
City: Delhi
Mobile: 9876543210

How it works

  • Each printf() prints one line of text.
  • \n moves the cursor to the next line.

Read C printf and scanf and Escape Sequences.

Write a C program to check whether two integers are equal or not

Description: Take two integers as input. Print if they are equal or not.

Hint:

  • Use the == operator inside an if statement.

C program to check if two integers are equal

#include <stdio.h>

int main(void) {
    int a, b;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    if (a == b) {
        printf("The numbers are equal\n");
    } else {
        printf("The numbers are not equal\n");
    }
    return 0;
}

Output:

Output of the above program is

Enter two integers: 15 15
The numbers are equal

How it works

  • a == b is true when both values are the same.
  • Do not write a = b in the condition. It assigns a value and is always true.

Read C If Else Statement.

Write a C program to check whether a year is a leap year or not

Description: Take a year as input. Print if it is a leap year or not.

Hint:

  • A year is a leap year if it is divisible by 400, or it is divisible by 4 but not by 100.

C program to check for a leap year

#include <stdio.h>

int main(void) {
    int year;

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

    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        printf("%d is a leap year\n", year);
    } else {
        printf("%d is not a leap year\n", year);
    }
    return 0;
}

Output:

Output of the above program is

Enter a year: 2024
2024 is a leap year

How it works

  • 2024 is divisible by 4 and not by 100, so it is a leap year.
  • 1900 is divisible by 100 but not by 400, so it is not. The year 2000 is divisible by 400, so it is.

Read C Operators for the logical operators.

Write a C program to calculate the profit or loss on a product

Description: Take the cost price and selling price of a product. Print the profit or the loss amount. If both are the same, print “No profit, no loss”.

Hint:

  • Profit = selling price – cost price (when selling price is more). Loss = cost price – selling price.

C program to calculate profit or loss

#include <stdio.h>

int main(void) {
    float cost, sell;

    printf("Enter cost price and selling price: ");
    scanf("%f %f", &cost, &sell);

    if (sell > cost) {
        printf("Profit = %.2f\n", sell - cost);
    } else if (cost > sell) {
        printf("Loss = %.2f\n", cost - sell);
    } else {
        printf("No profit, no loss\n");
    }
    return 0;
}

Output:

Output of the above program is

Enter cost price and selling price: 500 650
Profit = 150.00

How it works

  • If the selling price is more than the cost price, the difference is profit.
  • If it is less, the difference is loss. Otherwise there is neither.

Read Else If Ladder.

Write a C program to find in which quadrant a coordinate point lies

Description: Take the x and y coordinates of a point. Print the quadrant in which it lies.

Hint:

  • Quadrant 1: x > 0 and y > 0. Quadrant 2: x < 0 and y > 0. Quadrant 3: x < 0 and y < 0. Quadrant 4: x > 0 and y < 0.

C program to find the quadrant of a point

#include <stdio.h>

int main(void) {
    int x, y;

    printf("Enter x and y: ");
    scanf("%d %d", &x, &y);

    if (x > 0 && y > 0) {
        printf("First quadrant\n");
    } else if (x < 0 && y > 0) {
        printf("Second quadrant\n");
    } else if (x < 0 && y < 0) {
        printf("Third quadrant\n");
    } else if (x > 0 && y < 0) {
        printf("Fourth quadrant\n");
    } else {
        printf("The point is on an axis or at the origin\n");
    }
    return 0;
}

Output:

Output of the above program is

Enter x and y: -3 -7
Third quadrant

How it works

  • Both values are negative, so the point is in the third quadrant.
  • If x or y is zero, the point is on an axis, so the last else runs.

Write a C program to print the multiplication table of a number

Description: Take a number as input and print its multiplication table from 1 to 10.

Hint:

  • Use a for loop from 1 to 10 and print n * i in each round.

C program to print a multiplication table

#include <stdio.h>

int main(void) {
    int n;

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

    for (int i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", n, i, n * i);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

How it works

  • The loop variable i goes from 1 to 10.
  • In each round the program prints the number, the multiplier and the product.

Read C For Loop.

Write a C program to find the sum of first n natural numbers

Description: Take a number n as input. Print the sum of numbers from 1 to n.

Hint:

  • Keep a variable sum = 0 and add each number to it in a loop.

C program to find the sum of natural numbers

#include <stdio.h>

int main(void) {
    int n, sum = 0;

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

    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("Sum of 1 to %d = %d\n", n, sum);
    return 0;
}

Output:

Output of the above program is

Enter n: 10
Sum of 1 to 10 = 55

How it works

  • The loop adds 1, 2, 3 … up to n into sum.
  • There is also a formula: n * (n + 1) / 2, which gives 55 for n = 10 without a loop.

Write a C program to find the factorial of a number

Description: Take a number as input and print its factorial. Factorial of n is 1 x 2 x 3 x ... x n.

Hint:

  • Start with fact = 1 and multiply it by each number from 1 to n. Use unsigned long long for bigger values.

C program to find the factorial

#include <stdio.h>

int main(void) {
    int n;
    unsigned long long fact = 1;

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

    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    printf("Factorial of %d = %llu\n", n, fact);
    return 0;
}

Output:

Output of the above program is

Enter a number: 5
Factorial of 5 = 120

How it works

  • fact becomes 1, 2, 6, 24 and finally 120.
  • Factorial of 0 is 1, and our loop gives 1 for 0, so it is correct.
  • The type unsigned long long holds values up to 20! only.

Read C Recursion for the recursive way.

Write a C program to print the Fibonacci series

Description: Take the number of terms as input. Print that many terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8 …

Hint:

  • Each term is the sum of the two terms before it.

C program to print the Fibonacci series

#include <stdio.h>

int main(void) {
    int n, a = 0, b = 1, next;

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

    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

Output:

Output of the above program is

Enter number of terms: 8
0 1 1 2 3 5 8 13

How it works

  • We start with a = 0 and b = 1.
  • In each round we print a, then move forward: a takes the value of b, and b takes the new sum.

Write a C program to reverse a number

Description: Take a number as input and print it in reverse order, like 1234 as 4321.

Hint:

  • Take the last digit with % 10, add it to the reversed number, and remove it with / 10.

C program to reverse a number

#include <stdio.h>

int main(void) {
    int n, reversed = 0;

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

    while (n != 0) {
        int digit = n % 10;
        reversed = reversed * 10 + digit;
        n /= 10;
    }
    printf("Reversed number = %d\n", reversed);
    return 0;
}

Output:

Output of the above program is

Enter a number: 1234
Reversed number = 4321

How it works

  • Round 1: digit 4, reversed = 4, n = 123.
  • Round 2: digit 3, reversed = 43, n = 12.
  • It continues until n is 0, and the answer is 4321.

Write a C program to check whether a number is a palindrome

Description: Take a number as input. Print if it is a palindrome (it reads the same from both sides), like 121.

Hint:

  • Reverse the number, and compare it with the original.

C program to check a palindrome number

#include <stdio.h>

int main(void) {
    int n, original, reversed = 0;

    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome\n", original);
    } else {
        printf("%d is not a palindrome\n", original);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 121
121 is a palindrome

How it works

  • We keep a copy in original, because the loop changes n.
  • 121 reversed is 121, so both are equal.

Read Reverse a Number.

Write a C program to check whether a number is prime or not

Description: Take a number as input. Print if it is a prime number. A prime number has only two factors, 1 and itself.

Hint:

  • Try to divide the number by 2, 3, 4 … Stop when a divisor is found, or when the divisor is more than the square root.

C program to check a prime number

#include <stdio.h>

int main(void) {
    int n, isPrime = 1;

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

    if (n < 2) {
        isPrime = 0;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            isPrime = 0;
            break;
        }
    }

    if (isPrime) {
        printf("%d is a prime number\n", n);
    } else {
        printf("%d is not a prime number\n", n);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 29
29 is a prime number

How it works

  • Numbers less than 2 are not prime.
  • If n has a divisor, it has one which is not more than its square root. So the loop stops at i * i <= n.
  • break ends the loop as soon as a divisor is found.

Write a C program to check whether a number is an Armstrong number

Description: Take a 3 digit number as input. Print if it is an Armstrong number. For example, 153 is, because 13 + 53 + 33 = 153.

Hint:

  • Take each digit with % 10, add its cube to a sum, and compare the sum with the original number.

C program to check an Armstrong number

#include <stdio.h>

int main(void) {
    int n, original, digit, sum = 0;

    printf("Enter a 3 digit number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0) {
        digit = n % 10;
        sum += digit * digit * digit;
        n /= 10;
    }

    if (sum == original) {
        printf("%d is an Armstrong number\n", original);
    } else {
        printf("%d is not an Armstrong number\n", original);
    }
    return 0;
}

Output:

Output of the above program is

Enter a 3 digit number: 153
153 is an Armstrong number

How it works

  • The digits of 153 are 3, 5 and 1. Their cubes are 27, 125 and 1, and the sum is 153.
  • For numbers with a different count of digits, the power is the count of digits, not 3.

Write a C program to count the digits in a number

Description: Take a number as input and print how many digits it has.

Hint:

  • Divide the number by 10 in a loop, and count how many times you can do it before it becomes 0.

C program to count the digits of a number

#include <stdio.h>

int main(void) {
    int n, count = 0;

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

    if (n == 0) {
        count = 1;
    }
    while (n != 0) {
        n /= 10;
        count++;
    }
    printf("Number of digits = %d\n", count);
    return 0;
}

Output:

Output of the above program is

Enter a number: 45678
Number of digits = 5

How it works

  • 45678 becomes 4567, 456, 45, 4 and finally 0. That is 5 divisions, so 5 digits.
  • The number 0 has one digit, so we handle it separately.

Write a C program to print a pyramid star pattern

Description: Print a pyramid of stars with 4 rows.

Hint:

  • In row i print rows - i spaces first, and then 2 * i - 1 stars.

C program to print a pyramid pattern

#include <stdio.h>

int main(void) {
    int rows = 4;

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

Output:

Output of the above program is

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

How it works

  • The first inner loop prints the spaces on the left, so the pyramid is in the centre.
  • The second inner loop prints the stars: 1, 3, 5, 7 in the four rows.

Read C Nested Loops.

Write a C program to find the sum and average of array elements

Description: Take 5 numbers and store them in an array. Print the sum and the average.

Hint:

  • Use a loop to read each element and add it to a sum variable.

C program to find the sum and average of an array

#include <stdio.h>

int main(void) {
    int a[5], sum = 0;

    printf("Enter 5 numbers: ");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
        sum += a[i];
    }
    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", sum / 5.0);
    return 0;
}

Output:

Output of the above program is

Enter 5 numbers: 10
20
30
40
55
Sum = 155
Average = 31.00

How it works

  • The loop reads the numbers into a[0] to a[4], and adds them to sum.
  • We divide by 5.0 to get the decimal average.

Read C Arrays.

Write a C program to find the largest element in an array

Description: Take 5 numbers in an array and print the largest one.

Hint:

  • Assume the first element is the largest. Then compare the others, and update the value if a bigger one is found.

C program to find the largest element in an array

#include <stdio.h>

int main(void) {
    int a[5], max;

    printf("Enter 5 numbers: ");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
    }

    max = a[0];
    for (int i = 1; i < 5; i++) {
        if (a[i] > max) {
            max = a[i];
        }
    }
    printf("Largest = %d\n", max);
    return 0;
}

Output:

Output of the above program is

Enter 5 numbers: 23
91
45
67
12
Largest = 91

How it works

  • max starts as 23. When 91 is found, it is bigger, so max becomes 91. No later value is bigger.
  • For the smallest element, change > to <.

Write a C program to print the elements of an array in reverse order

Description: Take 5 numbers in an array and print them in reverse order.

Hint:

  • Start the loop from the last index (size – 1) and go down to 0.

C program to print an array in reverse order

#include <stdio.h>

int main(void) {
    int a[5];

    printf("Enter 5 numbers: ");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
    }

    printf("Reverse order: ");
    for (int i = 4; i >= 0; i--) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

Output:

Output of the above program is

Enter 5 numbers: 1
2
3
4
5
Reverse order: 5 4 3 2 1

How it works

  • The second loop starts at index 4 and goes down to index 0.
  • The array itself is not changed. We only print it in reverse.