Skip to content
Laviyx Learn

Try

C Basics Practice

Practise C variables, operators, input and output, conditions, loops and functions with short programs.

Beginner10 problemsUpdated

Review first: Variables, Operators, Input and Output, if, else if and else, Loops: for, while, do-while, Functions

  1. 1.Simple interest

    Beginner

    Read a principal amount (whole rupees), a yearly rate (a decimal) and a number of years, then print the simple interest and the total amount, each with two decimal places. Simple interest is principal × rate × years / 100.

    Show hint

    Read with scanf("%d %lf %d", ...). Remember that double needs %lf in scanf but %f in printf.

    Show solution
    C
    #include <stdio.h>
    
    int main(void) {
        int principal, years;
        double rate;
        if (scanf("%d %lf %d", &principal, &rate, &years) != 3) return 1;
        double interest = principal * rate * years / 100.0;
        printf("Interest: %.2f\n", interest);
        printf("Total: %.2f\n", principal + interest);
        return 0;
    }
    Output
    25000
    7.5
    3
    Interest: 5625.00
    Total: 30625.00
  2. 2.Grade from marks

    Beginner

    Write char grade(int marks) that returns 'A' for 90 and above, 'B' for 75–89, 'C' for 50–74 and 'F' below 50. Print the grade for 95, 75, 62 and 49.

    Show hint

    An if / else if chain checked from the highest band down needs only one comparison per band.

    Show solution
    C
    #include <stdio.h>
    
    char grade(int marks) {
        if (marks >= 90) return 'A';
        else if (marks >= 75) return 'B';
        else if (marks >= 50) return 'C';
        return 'F';
    }
    
    int main(void) {
        int tests[] = {95, 75, 62, 49};
        for (int i = 0; i < 4; i++) {
            printf("%d -> %c\n", tests[i], grade(tests[i]));
        }
        return 0;
    }
    Output
    95 -> A
    75 -> B
    62 -> C
    49 -> F
  3. 3.Leap years

    Beginner

    Write int is_leap(int year): a year is a leap year if it is divisible by 4, except century years, which must be divisible by 400. Print the leap years among 1900, 2000, 2024 and 2026.

    Show hint

    Combine the rules with && and ||: (y % 4 == 0 && y % 100 != 0) || y % 400 == 0.

    Show solution
    C
    #include <stdio.h>
    
    int is_leap(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    
    int main(void) {
        int years[] = {1900, 2000, 2024, 2026};
        for (int i = 0; i < 4; i++) {
            printf("%d: %s\n", years[i], is_leap(years[i]) ? "leap" : "not leap");
        }
        return 0;
    }
    Output
    1900: not leap
    2000: leap
    2024: leap
    2026: not leap
  4. 4.Multiplication table

    Beginner

    Read a number n and print its multiplication table from 1 to 5, aligned like 7 x 1 = 7.

    Show hint

    Use a for loop from 1 to 5 and a width in the format: %2d pads to two characters.

    Show solution
    C
    #include <stdio.h>
    
    int main(void) {
        int n;
        if (scanf("%d", &n) != 1) return 1;
        for (int i = 1; i <= 5; i++) {
            printf("%d x %d = %2d\n", n, i, n * i);
        }
        return 0;
    }
    Output
    7
    7 x 1 =  7
    7 x 2 = 14
    7 x 3 = 21
    7 x 4 = 28
    7 x 5 = 35
  5. 5.Sum and count of digits

    Beginner

    Write a program that prints the number of digits and the sum of the digits of 908172, using a while loop with % 10 and / 10.

    Show hint

    n % 10 is the last digit and n / 10 removes it. Stop when n becomes 0.

    Show solution
    C
    #include <stdio.h>
    
    int main(void) {
        int n = 908172, count = 0, sum = 0;
        while (n > 0) {
            sum += n % 10;
            count++;
            n /= 10;
        }
        printf("%d digits, sum %d\n", count, sum);
        return 0;
    }
    Output
    6 digits, sum 27
  6. 6.FizzBuzz

    Beginner

    Print the numbers 1 to 15 on one line, separated by spaces, but print Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both.

    Show hint

    Check the "both" case (i % 15 == 0) first, otherwise multiples of 15 would print only Fizz.

    Show solution
    C
    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 15; i++) {
            if (i % 15 == 0) printf("FizzBuzz");
            else if (i % 3 == 0) printf("Fizz");
            else if (i % 5 == 0) printf("Buzz");
            else printf("%d", i);
            printf(i < 15 ? " " : "\n");
        }
        return 0;
    }
    Output
    1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
  7. 7.Right-angled triangle of stars

    Beginner

    Read a height h and print a right-angled triangle of * characters with h rows, the first row having one star.

    Show hint

    Use nested loops: the outer loop chooses the row, and the inner loop prints as many stars as the row number.

    Show solution
    C
    #include <stdio.h>
    
    int main(void) {
        int h;
        if (scanf("%d", &h) != 1) return 1;
        for (int row = 1; row <= h; row++) {
            for (int i = 0; i < row; i++) putchar('*');
            putchar('\n');
        }
        return 0;
    }
    Output
    4
    *
    **
    ***
    ****
  8. 8.GCD and LCM

    Intermediate

    Write int gcd(int a, int b) using Euclid's algorithm with a loop, and use it to print the GCD and LCM of 84 and 120.

    Show hint

    Repeat t = b; b = a % b; a = t; until b is 0. Then lcm = a / gcd * b (dividing first avoids overflow).

    Show solution
    C
    #include <stdio.h>
    
    int gcd(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
    
    int main(void) {
        int a = 84, b = 120;
        int g = gcd(a, b);
        printf("gcd %d, lcm %d\n", g, a / g * b);
        return 0;
    }
    Output
    gcd 12, lcm 840
  9. 9.Count primes with a helper function

    Intermediate

    Write int is_prime(int n) and use it to print how many primes there are below 100 and the largest one.

    Show hint

    Test divisors d while d * d <= n. Numbers below 2 are not prime.

    Show solution
    C
    #include <stdio.h>
    
    int is_prime(int n) {
        if (n < 2) return 0;
        for (int d = 2; d * d <= n; d++) {
            if (n % d == 0) return 0;
        }
        return 1;
    }
    
    int main(void) {
        int count = 0, largest = 0;
        for (int n = 2; n < 100; n++) {
            if (is_prime(n)) {
                count++;
                largest = n;
            }
        }
        printf("%d primes below 100, largest %d\n", count, largest);
        return 0;
    }
    Output
    25 primes below 100, largest 97