Programs Studied
0%
Practice · 8 Programs

C Practice Programs

Simple programs to build your C foundations — read each program, understand it line by line, then try writing it yourself.

Print Name Area of Circle Fahrenheit → Celsius Sum of Numbers Simple Interest Even or Odd Swap Two Numbers Largest of Three
Beginner

Getting Started — Input & Output

P1
Print Your Name Using printf() to display text
Beginner
#include main() printf() return 0

The simplest C program. printf() prints text to the terminal. The \n at the end of each string moves the cursor to a new line — without it, everything prints on one line.

print_name.c
C
#include <stdio.h>

int main() {
    printf("Name    : Arjun Sharma\n");
    printf("Roll No : 42\n");
    printf("Course  : B.Tech CSE\n");
    printf("College : IIT Delhi\n");
    return 0;
}
Output
Name : Arjun Sharma
Roll No : 42
Course : B.Tech CSE
College : IIT Delhi
P2
Area of a Circle Using π × r² — float, scanf, printf
Beginner
float scanf() printf() %f / %.2f constants

We define π as a constant using #define. The area formula is π × r × r. We use float for the radius and area because they can be decimal values. %.2f formats the output to 2 decimal places.

💡 #define PI 3.14159 — this is a preprocessor directive. Before compiling, the compiler replaces every occurrence of PI in your code with 3.14159. It's not a variable — it has no memory address.
area_circle.c
C
#include <stdio.h>
#define PI 3.14159   // Preprocessor constant

int main() {
    float radius, area;

    printf("Enter radius of circle: ");
    scanf("%f", &radius);

    area = PI * radius * radius;   // π × r²

    printf("Area of circle = %.2f\n", area);

    return 0;
}
Sample Output
Enter radius of circle: 7
Area of circle = 153.94
P3
Fahrenheit to Celsius Temperature conversion formula
Beginner
float arithmetic type casting formula

The formula is C = (F − 32) × 5 / 9. We write 5.0 / 9 instead of 5 / 9 — because 5 / 9 in integer division gives 0, which would make every answer wrong!

⚠️ Watch out: Writing (f - 32) * 5 / 9 with integer literals causes integer division. Always use 5.0 / 9 or cast: (float)5 / 9 to get the correct decimal result.
temp_convert.c
C
#include <stdio.h>

int main() {
    float fahrenheit, celsius;

    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);

    celsius = (fahrenheit - 32) * 5.0 / 9;   // 5.0 avoids integer division

    printf("%.2f°F = %.2f°C\n", fahrenheit, celsius);

    return 0;
}
Sample Output
Enter temperature in Fahrenheit: 98.6
98.60°F = 37.00°C
P4
Sum & Average of Numbers Adding multiple user inputs
Beginner
int float multiple inputs arithmetic type casting

We take 5 numbers as input, add them together to find the sum, then divide by 5 to get the average. Notice we cast sum to float before dividing — otherwise integer division would truncate the decimal.

sum_average.c
C
#include <stdio.h>

int main() {
    int   a, b, c, d, e;
    int   sum;
    float average;

    printf("Enter 5 numbers: ");
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);

    sum     = a + b + c + d + e;
    average = (float)sum / 5;   // cast to float before dividing

    printf("Sum     = %d\n", sum);
    printf("Average = %.2f\n", average);

    return 0;
}
Sample Output
Enter 5 numbers: 10 20 30 40 50
Sum = 150
Average = 30.00
intermediate programs
Intermediate

Logic & Decision Making

P5
Simple Interest Calculator Formula: (P × R × T) / 100
Intermediate
float multiple variables formula printf formatting

Simple Interest is calculated using the formula SI = (Principal × Rate × Time) / 100. We use float for all values since money and rates are usually decimal. The final amount is Principal + Interest.

simple_interest.c
C
#include <stdio.h>

int main() {
    float principal, rate, time;
    float interest, total;

    printf("Enter Principal amount : "); scanf("%f", &principal);
    printf("Enter Rate of interest : "); scanf("%f", &rate);
    printf("Enter Time (in years)  : "); scanf("%f", &time);

    interest = (principal * rate * time) / 100;
    total    = principal + interest;

    printf("\n--- Result ---\n");
    printf("Simple Interest = %.2f\n", interest);
    printf("Total Amount    = %.2f\n", total);

    return 0;
}
Sample Output
Enter Principal amount : 10000
Enter Rate of interest : 5
Enter Time (in years) : 3

--- Result ---
Simple Interest = 1500.00
Total Amount = 11500.00
P6
Even or Odd? Using the modulus operator %
Intermediate
if / else modulus % conditionals

The % (modulus) operator gives the remainder after division. If a number divided by 2 gives remainder 0, it is even. Otherwise it's odd. This is your first look at conditional logic — the if / else statement.

💡 if / else syntax: The condition goes inside ( ). If the condition is true, the block inside { } executes. The else block runs when the condition is false. Exactly one of the two blocks will always run.
even_odd.c
C
#include <stdio.h>

int main() {
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num % 2 == 0) {           // remainder is 0 → even
        printf("%d is Even\n", num);
    } else {                         // otherwise → odd
        printf("%d is Odd\n", num);
    }

    return 0;
}
Sample Output
Enter an integer: 14
14 is Even

Enter an integer: 7
7 is Odd
P7
Swap Two Numbers Using a temporary variable
Intermediate
temp variable assignment int

To swap two variables, we use a temporary variable as a holding place. Think of it like swapping two drinks — you need an empty cup to temporarily hold one while you move the other. Without temp, one value would be permanently overwritten.

💡 The swap trick works in 3 steps:
1. Save a into temp
2. Copy b into a
3. Copy temp (original a) into b
swap.c
C
#include <stdio.h>

int main() {
    int a, b, temp;

    printf("Enter first number  : "); scanf("%d", &a);
    printf("Enter second number : "); scanf("%d", &b);

    printf("Before swap: a = %d, b = %d\n", a, b);

    temp = a;    // Step 1: save a
    a    = b;    // Step 2: a gets b's value
    b    = temp; // Step 3: b gets original a

    printf("After  swap: a = %d, b = %d\n", a, b);

    return 0;
}
Sample Output
Enter first number : 25
Enter second number : 60
Before swap: a = 25, b = 60
After swap: a = 60, b = 25
P8
Largest of Three Numbers Nested if / else if logic
Intermediate
if / else if / else comparison operators logical &&

We check three conditions using else if — a chain of conditions where exactly one block runs. The && operator means AND — both conditions on either side must be true for the overall condition to be true.

💡 Logical AND &&a >= b && a >= c is true only when both conditions are true at the same time. If either one is false, the whole expression is false and the next else if is checked.
largest.c
C
#include <stdio.h>

int main() {
    int a, b, c;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b && a >= c) {
        printf("Largest = %d\n", a);
    } else if (b >= a && b >= c) {
        printf("Largest = %d\n", b);
    } else {
        printf("Largest = %d\n", c);
    }

    return 0;
}
Sample Output
Enter three numbers: 45 82 37
Largest = 82
knowledge check
Quiz

Test Your Understanding

Question 1 of 4

In the area of circle program, why do we use #define PI 3.14159 instead of a variable?

Question 2 of 4

Why must we write 5.0 / 9 (not 5 / 9) in the Fahrenheit conversion?

Question 3 of 4

What does num % 2 == 0 check in the even/odd program?

Question 4 of 4

In the swap program, what would happen if we skipped the temp variable and wrote a = b; b = a;?

Checklist

Mark What You Can Write Yourself

Practice Programs — Self-Assessment