×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

C Programming Question Bank: 120 Questions (Basic to Advanced)



Last Updated on: 15th Aug 2025 23:41:12 PM

This question bank provides 120 C programming questions, progressing from basic to advanced topics, designed to teach essential C concepts through practical programs. Each question includes a description, complete C code, sample output, and a detailed explanation. Topics cover input/output, arithmetic, conditionals, loops, patterns, arrays, matrices, sorting, searching, strings, functions, recursion, structures, pointers, file handling, and dynamic memory allocation. Input methods use scanf for single numeric or character inputs and fgets for strings or robust input handling, ensuring simplicity and reliability. The programs are beginner-friendly, include input validation, and are ideal for mastering C programming.

 

Table of Contents

  1. Basic Input/Output and Arithmetic

  2. Conditional Statements

  3. Loops

  4. Switch Case

  5. Patterns

  6. Arrays

  7. Matrices

  8. Sorting and Searching

  9. Strings

  10. Functions

  11. Recursion

  12. Structures

  13. Pointers

  14. File Handling

  15. Dynamic Memory Allocation

 

Basic Input/Output and Arithmetic :

 

  1. Write a C program to input your name, father's name, college name, and course name from the user and display them.

  2. Write a C program to input two numbers from the user and calculate their addition, subtraction, multiplication, division, and modulus.

  3. Write a C program to input the length and width of a rectangle from the user and calculate its area and perimeter.

  4. Write a C program to input the radius of a circle from the user and calculate its circumference and area.

  5. Write a C program to input temperature in degrees Celsius from the user and convert it to Fahrenheit.

  6. Write a C program to input principal (P), time (T), and rate (R) from the user and calculate simple interest.

  7. Write a C program to input a single-digit number and print its English word equivalent.

  8. Write a C program to input a number and print its multiplication table.

  9. Write a C program to input two numbers, calculate their sum, find the square of the sum, and compute the cube of the squared result.

  10. Write a C program to input marks of five subjects from the user and calculate the total marks and percentage.

 

Conditional Statements :

 

  1. Write a C program to input two numbers and check whether they are equal.

  2. Write a C program to input a number and check whether it is divisible by 5.

  3. Write a C program to input a number and check whether it is positive, negative, or zero.

  4. Write a C program to input a number and check whether it is odd or even.

  5. Write a C program to input an alphabet and check whether it is a vowel or a consonant.

  6. Write a C program to input age and check whether the user is eligible to vote (age >= 18) or drive (age >= 16).

  7. Write a C program to input two numbers and find the largest using the conditional (ternary) operator.

  8. Write a C program to input three numbers and find the greatest among them.

  9. Write a C program to input two numbers and determine which is greater or smaller.

  10. Write a C program to input marks of five subjects, calculate total marks and percentage, and assign a grade based on:

    • Percentage >= 90%: Grade A

    • Percentage >= 80%: Grade B

    • Percentage >= 70%: Grade C

    • Percentage >= 60%: Grade D

    • Percentage >= 40%: Grade E

    • Percentage < 40%: Grade F

  11. Write a C program to input customer ID, name, and electricity units consumed, and calculate the total electricity bill based on:

    • First 50 units: Rs. 0.50/unit

    • Next 100 units: Rs. 0.75/unit

    • Next 100 units: Rs. 1.20/unit

    • Above 250 units: Rs. 1.50/unit

    • Add a 20% surcharge to the bill

  12. Write a C program to input the number of absent days and calculate the total fine based on:

    • 1st week: Rs. 5 per day

    • 2nd week: Rs. 10 per day

    • 3rd week: Rs. 15 per day

    • Beyond 3 weeks: Rs. 20 per day

 

Loops :

 

  1. Write a C program to print numbers from 1 to 10.

  2. Write a C program to calculate the sum of numbers from 1 to 10.

  3. Write a C program to find all even numbers from 1 to 50.

  4. Write a C program to find all odd numbers from 1 to 50.

  5. Write a C program to input the number of terms (N) and calculate the sum of the series 6 + 12 + 18 + … + N terms.

  6. Write a C program to input the number of terms (N) and calculate the sum of the series 4 + 10 + 16 + … + N terms.

  7. Write a C program to input the number of terms and print the Fibonacci series up to N terms (e.g., 0, 1, 1, 2, 3, 5, 8, …).

  8. Write a C program to input a number and check whether it is a palindrome.

  9. Write a C program to input a number and display it in reverse order (e.g., 1345 → 5431).

  10. Write a C program to input a number and check whether it is prime.

  11. Write a C program to print the sum of squares of numbers from 1 to N, where N is input by the user.

  12. Write a C program to print all Armstrong numbers between 1 and 1000.

  13. Write a C program to input a number and print its factors.

 

Switch Case :

 

  1. Write a C program to input a week number (1–7) and display the corresponding weekday using a switch case.

  2. Write a C program to input an alphabet and check whether it is a vowel or consonant using a switch case.

  3. Write a C program to input a number and check whether it is positive, negative, or zero using a switch case.

  4. Write a C program to input a number and check whether it is even or odd using a switch case.

  5. Write a C program to create a simple calculator for addition, subtraction, multiplication, and division using a switch case.

  6. Write a C program to create a menu-driven program for arithmetic operations (addition, subtraction, multiplication, division) using a switch case.

  7. Write a C program to create a menu-driven program that calculates and displays the area of a rectangle, triangle, or circle based on user choice, with an option to exit.

 

Patterns :

 

  1. Write a C program to display a right-angle triangle pattern with numbers:

    1
    12
    123
    1234
    12345
  2. Write a C program to display a pattern of repeating numbers:

    1
    22
    333
    4444
    55555
  3. Write a C program to display a pattern of repeating numbers in reverse:

    55555
    4444
    333
    22
    1
  4. Write a C program to display a pattern of sequential numbers:

    1
    2 3
    4 5 6
    7 8 9 10
  5. Write a C program to display a pyramid pattern with repeating numbers:

        1
       2 2
      3 3 3
     4 4 4 4
    5 5 5 5 5
  6. Write a C program to display a pyramid pattern with stars:

        *
       * *
      * * *
     * * * *
    * * * * *
  7. Write a C program to display a reverse pyramid pattern with stars:

    * * * * *
     * * * *
      * * *
       * *
        *
  8. Write a C program to display a right-aligned pyramid pattern with stars:

        * * * * *
         * * * *
          * * *
           * *
            *
  9. Write a C program to display the alphabet (a–z) in reverse order, printing alternate characters (e.g., z, x, v, …).

  10. Write a C program to display a diamond pattern with stars for a given number of rows.

 

Arrays :

 

  1. Write a C program to input 10 numbers into an array and calculate their sum.

  2. Write a C program to input 10 numbers into an array and calculate their product.

  3. Write a C program to input 5 numbers into an array and calculate the square of each number.

  4. Write a C program to input 10 integers into an array and separate even and odd numbers.

  5. Write a C program to input 10 numbers into an array and print all negative elements.

  6. Write a C program to input 10 numbers into an array and find the largest element.

  7. Write a C program to input 10 numbers into an array and find the smallest element.

  8. Write a C program to input 10 numbers into an array and find the second largest element.

  9. Write a C program to input 10 numbers into an array and find the second smallest element.

  10. Write a C program to input 10 integers into an array and copy them into another array in reverse order.

  11. Write a C program to input 10 numbers into two arrays and calculate the sum of corresponding elements in a third array.

  12. Write a C program to input marks of 5 subjects into an array and calculate total marks and percentage.

  13. Write a C program to count the total number of elements in an array.

  14. Write a C program to input 10 numbers into an array, input another number, and check if it exists in the array.

  15. Write a C program to input 10 numbers into an array and calculate their average.

 

Matrices :

 

  1. Write a C program to input a 3x3 matrix and display it.

  2. Write a C program to input two 3x3 matrices and calculate their sum.

  3. Write a C program to input two 3x3 matrices and check whether they are equal.

  4. Write a C program to input a 3x3 matrix and calculate its transpose.

  5. Write a C program to input two 3x3 matrices and perform matrix multiplication.

 

Sorting and Searching :

 

  1. Write a C program to sort an array of 10 numbers using bubble sort.

  2. Write a C program to sort an array of 10 numbers using insertion sort.

  3. Write a C program to sort an array of 10 numbers using selection sort.

  4. Write a C program to perform a linear search on an array of 10 numbers.

  5. Write a C program to perform a binary search on a sorted array of 10 numbers.

 

Strings :

 

  1. Write a C program to input a string and find its length.

  2. Write a C program to input a string and copy it to another string.

  3. Write a C program to input two strings and concatenate them.

  4. Write a C program to input two strings and compare them.

  5. Write a C program to input a string and convert it from lowercase to uppercase.

  6. Write a C program to input a string and convert it from uppercase to lowercase.

  7. Write a C program to input a string and toggle the case of each character.

  8. Write a C program to input a string and find its reverse.

  9. Write a C program to input a string and reverse the order of its words.

  10. Write a C program to input a string and count the number of vowels and consonants.

  11. Write a C program to input a string and check if it is a palindrome.

 

Functions :

 

  1. Write a C program to find the cube of a number using a function.

  2. Write a C program to calculate the diameter, circumference, and area of a circle using functions.

  3. Write a C program to check whether a number is even or odd using a function.

  4. Write a C program to input two numbers into variables A and B and swap their contents using a function.

  5. Write a C program to input two numbers and swap them without using a temporary variable using a function.

  6. Write a C program to input a salary, increase it by 10%, and display the total salary using a function.

  7. Write a C program to find the GCD of two numbers using a function.

 

Recursion :

 

  1. Write a C program to calculate the factorial of a number using recursion.

  2. Write a C program to print all natural numbers from 1 to N using recursion.

  3. Write a C program to calculate the sum of digits of a number using recursion.

  4. Write a C program to find the power of a number using recursion.

  5. Write a C program to generate the Fibonacci series up to N terms using recursion.

 

Structures :

 

  1. Write a C program to define a structure "Student" with members name, age, and total marks, input data for two students, display their information, and calculate the average of total marks.

  2. Write a C program to define a structure "Time" with members hours, minutes, and seconds, input two times, add them, and display the result in proper time format.

  3. Write a C program to define a structure "Book" with members title, author, and price, input details for three books, and display their information.

  4. Write a C program to define a structure "Circle" with member radius, input data for a circle, and calculate its area.

  5. Write a C program to define a structure "Employee" with members employee ID, name, and salary, input data for three employees, find the highest salary, and display their information.

  6. Write a C program to define a structure "Date" with members day, month, and year, input a date, and display it.

 

Pointers :

 

  1. Write a C program to add two numbers using pointers.

  2. Write a C program to swap two numbers using pointers.

  3. Write a C program to find the length of a string using pointers.

  4. Write a C program to reverse an array using pointers.

  5. Write a C program to reverse a string using pointers.

  6. Write a C program to find the sum of elements in an array using pointers.

 

File Handling :

 

  1. Write a C program to input a string from the user and write it to a file.

  2. Write a C program to read a string from a file and display it.

  3. Write a C program to input 10 numbers, write them to a file, and read them back to display.

  4. Write a C program to copy the contents of one text file to another.

  5. Write a C program to count the number of lines, words, and characters in a text file.

  6. Write a C program to append a string to an existing file and display the updated content.

 

Dynamic Memory Allocation :

 

  1. Write a C program to dynamically allocate memory for an array of N integers, input values, and calculate their sum.

  2. Write a C program to dynamically allocate memory for a string, input the string, and find its length.

 


 

                                                 Coding Part :

 

Basic Input/Output and Arithmetic

 

1. Input and Display Personal Information

Description: Write a C program to input the user's name, father's name, college name, and course name and display them in a formatted manner.

Code:

#include <stdio.h>
#include <string.h>

int main() {
    char name[50], fatherName[50], college[50], course[50];
    printf("Enter your name: ");
    fgets(name, 50, stdin);
    name[strcspn(name, "\n")] = '\0';
    printf("Enter father's name: ");
    fgets(fatherName, 50, stdin);
    fatherName[strcspn(fatherName, "\n")] = '\0';
    printf("Enter college name: ");
    fgets(college, 50, stdin);
    college[strcspn(college, "\n")] = '\0';
    printf("Enter course name: ");
    fgets(course, 50, stdin);
    course[strcspn(course, "\n")] = '\0';
    printf("\n--- Personal Information ---\n");
    printf("Name: %s\n", name);
    printf("Father's Name: %s\n", fatherName);
    printf("College: %s\n", college);
    printf("Course: %s\n", course);
    return 0;
}

 

Sample Output:

Enter your name: trisha shetty
Enter father's name: ganesh shetty
Enter college name: Mumbai University
Enter course name: Computer Science

 

--- Personal Information ---
Name: trisha shetty
Father's Name: ganesh shetty
College: Mumbai University
Course: Computer Science

 

Explanation:

  • Variable Declaration: Four character arrays (name, fatherName, college, course) of size 50 store string inputs.

  • Input Handling: fgets(name, 50, stdin) reads up to 49 characters plus a null terminator from standard input (keyboard). The strcspn(name, "\n") function finds the index of the newline character, and name[strcspn(name, "\n")] = '\0' replaces it with a null terminator to remove it for clean display. This is repeated for each input.

  • Output: printf statements format and display the strings with labels in a structured block.

  • Logic Flow: Sequentially reads four strings, removes newlines, and prints them in a formatted manner.

  • Key Learning: Safe string input with fgets, newline handling with strcspn, and formatted output with printf.

 

2. Basic Arithmetic Operations

Description: Write a C program to input two numbers and calculate their addition, subtraction, multiplication, division, and modulus.

Code:

#include <stdio.h>

int main() {
    double num1, num2;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    if (num2 == 0) {
        printf("Error: Division by zero!\n");
    } else {
        printf("Addition: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
        printf("Subtraction: %.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
        printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
        printf("Division: %.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
        printf("Modulus: %d %% %d = %d\n", (int)num1, (int)num2, (int)num1 % (int)num2);
    }
    return 0;
}

 

Sample Output:

Enter first number: 10
Enter second number: 5
Addition: 10.00 + 5.00 = 15.00
Subtraction: 10.00 - 5.00 = 5.00
Multiplication: 10.00 * 5.00 = 50.00
Division: 10.00 / 5.00 = 2.00
Modulus: 10 % 5 = 0

 

Explanation:

  • Uses scanf for simple numeric input.

  • Checks for division by zero to prevent errors.

  • Performs all arithmetic operations with formatted output.

  • Teaches basic arithmetic and error handling.

 

3. Rectangle Area and Perimeter

Description: Write a C program to input the length and width of a rectangle and calculate its area and perimeter.

Code:

#include <stdio.h>

int main() {
    double length, width;
    printf("Enter length of rectangle: ");
    scanf("%lf", &length);
    printf("Enter width of rectangle: ");
    scanf("%lf", &width);
    if (length <= 0 || width <= 0) {
        printf("Error: Length and width must be positive!\n");
    } else {
        printf("Area: %.2f\n", length * width);
        printf("Perimeter: %.2f\n", 2 * (length + width));
    }
    return 0;
}

 

Sample Output:

Enter length of rectangle: 10
Enter width of rectangle: 5
Area: 50.00
Perimeter: 30.00

 

Explanation:

  • Uses scanf for numeric input.

  • Validates that inputs are positive.

  • Calculates area (length * width) and perimeter (2 * (length + width)).

  • Teaches input validation and arithmetic.

 

4. Circle Circumference and Area

Description: Write a C program to input the radius of a circle and calculate its circumference and area.

Code:

#include <stdio.h>
#define PI 3.14159

int main() {
    double radius;
    printf("Enter radius of circle: ");
    scanf("%lf", &radius);
    if (radius <= 0) {
        printf("Error: Radius must be positive!\n");
    } else {
        printf("Circumference: %.2f\n", 2 * PI * radius);
        printf("Area: %.2f\n", PI * radius * radius);
    }
    return 0;
}

 

Sample Output:

Enter radius of circle: 7
Circumference: 43.98
Area: 153.94

 

Explanation:

  • Uses scanf for radius input.

  • Validates that radius is positive.

  • Calculates circumference (2 * π * radius) and area (π * radius^2) using defined constant PI.

  • Teaches constants and mathematical computations.

 

5. Celsius to Fahrenheit Conversion

Description: Write a C program to input temperature in degrees Celsius and convert it to Fahrenheit.

Code:

#include <stdio.h>

int main() {
    double celsius;
    printf("Enter temperature in Celsius: ");
    scanf("%lf", &celsius);
    printf("Temperature in Fahrenheit: %.2f\n", (celsius * 9.0 / 5.0) + 32);
    return 0;
}

 

Sample Output:

Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00

 

Explanation:

  • Uses scanf for numeric input.

  • Converts Celsius to Fahrenheit using the formula: (C * 9/5) + 32.

  • Displays result with two decimal places.

  • Teaches basic arithmetic and input handling.

 

6. Simple Interest Calculation

Description: Write a C program to input principal (P), time (T), and rate (R) and calculate simple interest.

Code:

#include <stdio.h>

int main() {
    double principal, time, rate;
    printf("Enter principal amount: ");
    scanf("%lf", &principal);
    printf("Enter time (years): ");
    scanf("%lf", &time);
    printf("Enter rate of interest: ");
    scanf("%lf", &rate);
    if (principal <= 0 || time <= 0 || rate <= 0) {
        printf("Error: Inputs must be positive!\n");
    } else {
        printf("Simple Interest: %.2f\n", (principal * time * rate) / 100);
    }
    return 0;
}

 

Sample Output:

Enter principal amount: 1000
Enter time (years): 2
Enter rate of interest: 5
Simple Interest: 100.00

 

Explanation:

  • Uses scanf for numeric inputs.

  • Validates that inputs are positive.

  • Calculates simple interest using the formula: (P * T * R) / 100.

  • Teaches input validation and arithmetic.

 

7. Single-Digit Number to Word

Description: Write a C program to input a single-digit number and print its English word equivalent.

Code:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a single-digit number (0-9): ");
    scanf("%d", &num);
    if (num < 0 || num > 9) {
        printf("Error: Please enter a single-digit number!\n");
    } else {
        switch (num) {
            case 0: printf("Zero\n"); break;
            case 1: printf("One\n"); break;
            case 2: printf("Two\n"); break;
            case 3: printf("Three\n"); break;
            case 4: printf("Four\n"); break;
            case 5: printf("Five\n"); break;
            case 6: printf("Six\n"); break;
            case 7: printf("Seven\n"); break;
            case 8: printf("Eight\n"); break;
            case 9: printf("Nine\n"); break;
        }
    }
    return 0;
}

 

Sample Output:

Enter a single-digit number (0-9): 5
Five

 

Explanation:

  • Uses scanf for single-digit input.

  • Validates that the input is between 0 and 9.

  • Uses a switch case to print the word equivalent.

  • Teaches switch statements and input validation.

 

8. Multiplication Table

Description: Write a C program to input a number and print its multiplication table.

Code:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Multiplication Table of %d:\n", num);
    for (int i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", num, i, num * i);
    }
    return 0;
}

 

Sample Output:

Enter a number: 4
Multiplication Table of 4:
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

 

Explanation:

  • Uses scanf for numeric input.

  • Uses a for loop to print the multiplication table from 1 to 10.

  • Displays results in a formatted manner.

  • Teaches loops and basic arithmetic.

 

9. Sum, Square, and Cube

Description: Write a C program to input two numbers, calculate their sum, square the sum, and cube the squared result.

Code:

#include <stdio.h>

int main() {
    double num1, num2, sum, square, cube;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    sum = num1 + num2;
    square = sum * sum;
    cube = square * sum;
    printf("Sum: %.2f\n", sum);
    printf("Square of sum: %.2f\n", square);
    printf("Cube of square: %.2f\n", cube);
    return 0;
}

 

Sample Output:

Enter first number: 3
Enter second number: 4
Sum: 7.00
Square of sum: 49.00
Cube of square: 343.00

 

Explanation:

  • Uses scanf for numeric inputs.

  • Calculates sum, square, and cube sequentially.

  • Displays results with two decimal places.

  • Teaches sequential arithmetic operations.

 

10. Total Marks and Percentage

Description: Write a C program to input marks of five subjects and calculate the total marks and percentage.

Code:

#include <stdio.h>

int main() {
    float marks[5], total = 0, percentage;
    for (int i = 0; i < 5; i++) {
        printf("Enter marks for subject %d (out of 100): ", i + 1);
        scanf("%f", &marks[i]);
        if (marks[i] < 0 || marks[i] > 100) {
            printf("Error: Marks must be between 0 and 100!\n");
            return 1;
        }
        total += marks[i];
    }
    percentage = total / 5;
    printf("Total Marks: %.2f\n", total);
    printf("Percentage: %.2f%%\n", percentage);
    return 0;
}

 

Sample Output:

Enter marks for subject 1 (out of 100): 85
Enter marks for subject 2 (out of 100): 90
Enter marks for subject 3 (out of 100): 78
Enter marks for subject 4 (out of 100): 92
Enter marks for subject 5 (out of 100): 88
Total Marks: 433.00
Percentage: 86.60%

 

Explanation:

  • Uses scanf for float inputs.

  • Validates marks are between 0 and 100.

  • Calculates total and percentage.

  • Teaches arrays, loops, and input validation.

 

Conditional Statements :

 

11. Check Equal Numbers

Description: Write a C program to input two numbers and check whether they are equal.

Code:

#include <stdio.h>

int main() {
    double num1, num2;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    if (num1 == num2) {
        printf("The numbers %.2f and %.2f are equal.\n", num1, num2);
    } else {
        printf("The numbers %.2f and %.2f are not equal.\n", num1, num2);
    }
    return 0;
}

 

Sample Output:

Enter first number: 10
Enter second number: 10
The numbers 10.00 and 10.00 are equal.

 

Explanation:

  • Uses scanf for numeric inputs.

  • Uses if-else to check equality.

  • Displays appropriate message.

  • Teaches basic conditional statements.

 

12. Divisible by 5

Description: Write a C program to input a number and check whether it is divisible by 5.

Code:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 5 == 0) {
        printf("%d is divisible by 5.\n", num);
    } else {
        printf("%d is not divisible by 5.\n", num);
    }
    return 0;
}

 

Sample Output:

Enter a number: 25
25 is divisible by 5.

 

Explanation:

  • Uses scanf for integer input.

  • Checks divisibility using modulus operator.

  • Displays result based on condition.

  • Teaches modulus and conditionals.

 

13. Positive, Negative, or Zero

Description: Write a C program to input a number and check whether it is positive, negative, or zero.

Code:

#include <stdio.h>

int main() {
    double num;
    printf("Enter a number: ");
    scanf("%lf", &num);
    if (num > 0) {
        printf("%.2f is positive.\n", num);
    } else if (num < 0) {
        printf("%.2f is negative.\n", num);
    } else {
        printf("%.2f is zero.\n", num);
    }
    return 0;
}

 

Sample Output:

Enter a number: -7
-7.00 is negative.

 

Explanation:

  • Uses scanf for numeric input.

  • Uses nested if-else to check number type.

  • Displays result with two decimal places.

  • Teaches nested conditionals.

 

14. Odd or Even

Description: Write a C program to input a number and check whether it is odd or even.

Code:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
    return 0;
}

 

Sample Output:

Enter a number: 8
8 is even.

 

Explanation:

  • Uses scanf for integer input.

  • Uses modulus operator to check evenness.

  • Displays result.

  • Teaches basic conditional logic.

 

15. Vowel or Consonant

Description: Write a C program to input an alphabet and check whether it is a vowel or a consonant.

Code:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch;
    printf("Enter an alphabet: ");
    scanf(" %c", &ch);
    ch = tolower(ch);
    if (isalpha(ch)) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            printf("%c is a vowel.\n", ch);
        } else {
            printf("%c is a consonant.\n", ch);
        }
    } else {
        printf("Error: Please enter an alphabet!\n");
    }
    return 0;
}

 

Sample Output:

Enter an alphabet: E
e is a vowel.

 

Explanation:

  • Uses scanf for character input.

  • Converts to lowercase and validates alphabet using isalpha.

  • Checks for vowels using conditionals.

  • Teaches character handling.

 

16. Voting and Driving Eligibility

Description: Write a C program to input age and check whether the user is eligible to vote (age >= 18) or drive (age >= 16).

Code:

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    if (age < 0) {
        printf("Error: Age cannot be negative!\n");
    } else {
        if (age >= 18) {
            printf("You are eligible to vote.\n");
        } else {
            printf("You are not eligible to vote.\n");
        }
        if (age >= 16) {
            printf("You are eligible to drive.\n");
        } else {
            printf("You are not eligible to drive.\n");
        }
    }
    return 0;
}

 

Sample Output:

Enter your age: 17
You are not eligible to vote.
You are eligible to drive.

 

Explanation:

  • Uses scanf for integer input.

  • Validates non-negative age.

  • Checks voting and driving eligibility.

  • Teaches multiple condition checks.

 

17. Largest Number Using Ternary Operator

Description: Write a C program to input two numbers and find the largest using the conditional (ternary) operator.

Code:

#include <stdio.h>

int main() {
    double num1, num2;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    double largest = (num1 > num2) ? num1 : num2;
    printf("Largest number: %.2f\n", largest);
    return 0;
}

 

Sample Output:

Enter first number: 15.5
Enter second number: 10.2
Largest number: 15.50

 

Explanation:

  • Uses scanf for numeric inputs.

  • Uses ternary operator to find largest.

  • Displays result.

  • Teaches ternary operator.

 

18. Greatest Among Three Numbers

Description: Write a C program to input three numbers and find the greatest among them.

Code:

#include <stdio.h>

int main() {
    double num1, num2, num3;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    printf("Enter third number: ");
    scanf("%lf", &num3);
    if (num1 >= num2 && num1 >= num3) {
        printf("Greatest number: %.2f\n", num1);
    } else if (num2 >= num1 && num2 >= num3) {
        printf("Greatest number: %.2f\n", num2);
    } else {
        printf("Greatest number: %.2f\n", num3);
    }
    return 0;
}

 

Sample Output:

Enter first number: 10
Enter second number: 25
Enter third number: 15
Greatest number: 25.00

 

Explanation:

  • Uses scanf for numeric inputs.

  • Uses nested if-else to find greatest.

  • Displays result.

  • Teaches complex conditionals.

 

19. Compare Two Numbers

Description: Write a C program to input two numbers and determine which is greater or smaller.

Code:

#include <stdio.h>

int main() {
    double num1, num2;
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);
    if (num1 > num2) {
        printf("%.2f is greater than %.2f.\n", num1, num2);
    } else if (num1 < num2) {
        printf("%.2f is smaller than %.2f.\n", num1, num2);
    } else {
        printf("%.2f is equal to %.2f.\n", num1, num2);
    }
    return 0;
}

 

Sample Output:

Enter first number: 8
Enter second number: 12
8.00 is smaller than 12.00.

 

Explanation:

  • Uses scanf for numeric inputs.

  • Uses if-else to compare numbers.

  • Displays result.

  • Teaches comparison logic.

 

20. Grade Based on Percentage

Description: Write a C program to input marks of five subjects, calculate total marks and percentage, and assign a grade based on specified conditions.

Code:

#include <stdio.h>

int main() {
    float marks[5], total = 0, percentage;
    for (int i = 0; i < 5; i++) {
        printf("Enter marks for subject %d (out of 100): ", i + 1);
        scanf("%f", &marks[i]);
        if (marks[i] < 0 || marks[i] > 100) {
            printf("Error: Marks must be between 0 and 100!\n");
            return 1;
        }
        total += marks[i];
    }
    percentage = total / 5;
    printf("Total Marks: %.2f\n", total);
    printf("Percentage: %.2f%%\n", percentage);
    if (percentage >= 90) {
        printf("Grade: A\n");
    } else if (percentage >= 80) {
        printf("Grade: B\n");
    } else if (percentage >= 70) {
        printf("Grade: C\n");
    } else if (percentage >= 60) {
        printf("Grade: D\n");
    } else if (percentage >= 40) {
        printf("Grade: E\n");
    } else {
        printf("Grade: F\n");
    }
    return 0;
}

 

Sample Output:

Enter marks for subject 1 (out of 100): 95
Enter marks for subject 2 (out of 100): 88
Enter marks for subject 3 (out of 100): 92
Enter marks for subject 4 (out of 100): 87
Enter marks for subject 5 (out of 100): 90
Total Marks: 452.00
Percentage: 90.40%
Grade: A

 

Explanation:

  • Uses scanf for float inputs.

  • Validates marks are between 0 and 100.

  • Calculates total and percentage, assigns grade.

  • Teaches arrays and conditionals.

 

21. Electricity Bill Calculation

Description: Write a C program to input customer ID, name, and electricity units consumed, and calculate the total bill based on specified rates with a 20% surcharge.

Code:

#include <stdio.h>
#include <string.h>

int main() {
    char id[20], name[50];
    float units, bill = 0;
    printf("Enter Customer ID: ");
    fgets(id, 20, stdin);
    id[strcspn(id, "\n")] = '\0';
    printf("Enter Customer Name: ");
    fgets(name, 50, stdin);
    name[strcspn(name, "\n")] = '\0';
    printf("Enter units consumed: ");
    scanf("%f", &units);
    if (units < 0) {
        printf("Error: Units cannot be negative!\n");
        return 1;
    }
    if (units <= 50) {
        bill = units * 0.50;
    } else if (units <= 150) {
        bill = 50 * 0.50 + (units - 50) * 0.75;
    } else if (units <= 250) {
        bill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;
    } else {
        bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;
    }
    bill *= 1.20; // 20% surcharge
    printf("\n--- Electricity Bill ---\n");
    printf("Customer ID: %s\n", id);
    printf("Customer Name: %s\n", name);
    printf("Units Consumed: %.2f\n", units);
    printf("Total Bill: Rs. %.2f\n", bill);
    return 0;
}

 

Sample Output:

Enter Customer ID: C123
Enter Customer Name: Rojas Bankar
Enter units consumed: 200
--- Electricity Bill ---
Customer ID: C123
Customer Name: Rojas Bankar
Units Consumed: 200.00
Total Bill: Rs. 174.00

 

Explanation:

  • Uses fgets for string inputs (ID, name) and scanf for units.

  • Validates units are non-negative.

  • Calculates bill with tiered rates and surcharge.

  • Teaches string handling and complex calculations.

 

22. Fine Based on Absent Days

Description: Write a C program to input the number of absent days and calculate the total fine based on specified rates.

Code:

#include <stdio.h>

int main() {
    int days;
    float fine = 0;
    printf("Enter number of absent days: ");
    scanf("%d", &days);
    if (days < 0) {
        printf("Error: Days cannot be negative!\n");
        return 1;
    }
    if (days <= 7) {
        fine = days * 5;
    } else if (days <= 14) {
        fine = 7 * 5 + (days - 7) * 10;
    } else if (days <= 21) {
        fine = 7 * 5 + 7 * 10 + (days - 14) * 15;
    } else {
        fine = 7 * 5 + 7 * 10 + 7 * 15 + (days - 21) * 20;
    }
    printf("Total Fine: Rs. %.2f\n", fine);
    return 0;
}

 

Sample Output:

Enter number of absent days: 10
Total Fine: Rs. 65.00

 

Explanation:

  • Uses scanf for integer input.

  • Validates days are non-negative.

  • Calculates fine based on weekly tiers.

  • Teaches tiered calculations.

 

Loops

 

23. Print Numbers 1 to 10

Description: Write a C program to print numbers from 1 to 10.

Code:

#include <stdio.h>

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

 

Sample Output:

1 2 3 4 5 6 7 8 9 10

 

Explanation:

  • Uses a for loop to print numbers.

  • Simple output formatting.

  • Teaches basic for loop.

 

24. Sum of Numbers 1 to 10

Description: Write a C program to calculate the sum of numbers from 1 to 10.

Code:

#include <stdio.h>

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

 

Sample Output:

Sum of numbers from 1 to 10: 55

 

Explanation:

  • Uses a for loop to accumulate sum.

  • Displays final sum.

  • Teaches loop accumulation.

 

25. Even Numbers 1 to 50

Description: Write a C program to find all even numbers from 1 to 50.

Code:

#include <stdio.h>

int main() {
    printf("Even numbers from 1 to 50:\n");
    for (int i = 1; i <= 50; i++) {
        if (i % 2 == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
    return 0;
}

 

Sample Output:

Even numbers from 1 to 50:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

 

Explanation:

  • Uses a for loop and modulus to find even numbers.

  • Prints with space separator.

  • Teaches loops and conditionals.

 

26. Odd Numbers 1 to 50

Description: Write a C program to find all odd numbers from 1 to 50.

Code:

#include <stdio.h>

int main() {
    printf("Odd numbers from 1 to 50:\n");
    for (int i = 1; i <= 50; i++) {
        if (i % 2 != 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
    return 0;
}

 

 Sample Output:

Odd numbers from 1 to 50:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

 

Explanation:

  • Uses a for loop and modulus to find odd numbers.

  • Prints with space separator.

  • Teaches loops and conditionals.

 

27. Sum of Series (6 + 12 + 18 + …)

Description: Write a C program to input the number of terms (N) and calculate the sum of the series 6 + 12 + 18 + … + N terms.

Code:

#include <stdio.h>

int main() {
    int N, sum = 0;

    // Taking input for N
    printf("Enter the value of N: ");
    scanf("%d", &N);

    // Calculating sum of the first series: 6 + 12 + 18 + ... + N
    printf("Series 1: 6 + 12 + 18 + ... up to N\n");
    for (int i = 6; i <= N; i += 6) {
        sum = sum + i;
    }
    printf("Sum of series: %d\n", sum);

return 0;
}

 

Sample Output:

Enter number of terms: 5
Sum of series (6 + 12 + 18 + ...): 90

 

Explanation:

  • Uses scanf for integer input.

  • Validates positive N.

  • Calculates sum of arithmetic series.

  • Teaches arithmetic series.

 

28. Sum of Series (4 + 10 + 16 + …)

Description: Write a C program to input the number of terms (N) and calculate the sum of the series 4 + 10 + 16 + … + N terms.

Code:

#include <stdio.h>

int main() {
    int n;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    if (n <= 0) {
        printf("Error: Number of terms must be positive!\n");
        return 1;
    }
    printf("Fibonacci Series: ");
    int a = 0, b = 1, next;
    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

 

Sample Output:

Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

 

Explanation:

  • Uses scanf for integer input.

  • Validates positive N.

  • Generates Fibonacci series iteratively.

  • Teaches sequence generation.

 

30. Palindrome Number

Description: Write a C program to input a number and check whether it is a palindrome.

Code:

#include <stdio.h>

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

    // Taking input for the number
    printf("Enter a number: ");
    scanf("%d", &num);
   
     if (num < 0) {
        printf("Error: Number cannot be negative!\n");
        return 1;
     }

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

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;  // Get the last digit
        reverse = reverse * 10 + remainder;  // Build the reversed number
        num /= 10;  // Remove the last digit
    }

    // Check if the original number is equal to the reversed number
    if (original == reverse) {
        printf("%d is a palindrome.\n", original);
    } else {
        printf("%d is not a palindrome.\n", original);
    }

    return 0;
}

 

Sample Output:

Enter a number: 121
121 is a palindrome.

 

Explanation:

  • Uses scanf for integer input.

  • Validates non-negative number.

  • Reverses number and checks for palindrome.

  • Teaches number manipulation.

 

31. Reverse a Number

Description: Write a C program to input a number and display it in reverse order.

Code:

 

#include <stdio.h>

int main() {
    int num, reversed = 0;
    printf("Enter a number: ");
    scanf("%d", &num);

    while(num != 0) {
        int digit = num % 10;
        reversed = reversed * 10 + digit;
        num = num / 10;
    }

    printf("Reversed number: %d\n", reversed);
    return 0;
}

 

Sample Output:

Enter a number: 1345
Reversed number: 5431

 

Explanation:

  • % 10 gets the last digit.

  • * 10 shifts digits left in the reversed number.

  • / 10 removes the last digit from the original number.

https://grok.com/chat/6155e1ed-87d6-4286-baa1-827f85a7ce2d 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo