C Programs

Variables and Expressions


Programs at a Glance

This section lists the programs covered under Variables and Expressions. Click on a program title to jump directly to its detailed explanation.

  1. Swap two numbers using a third variable
  2. Simple and compound interest
  3. Evaluate expressions (y = m·x + c, displacement)

Swapping of Two Numbers Using a Third Variable


Problem Statement

Write a C program to swap (exchange) the values of two variables using a third temporary variable.

Algorithm
  1. Start
  2. Take two input numbers, a and b
  3. Store the value of a in a temporary variable temp
  4. Copy the value of b into a
  5. Copy the value stored in temp (original a) into b
  6. Display the swapped values of a and b
  7. Stop
C Program

#include <stdio.h>

int main() {
    int a, b, temp;  // Declare three integer variables

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    
    printf("Before swapping: a = %d, b = %d\n", a, b);  
    
    temp = a;   // Store value of a in temp    
    a = b;      // Now copy value of b into a 
    b = temp;   // Finally, assign old value of a (stored in temp) into b
    
    printf("After swapping: a = %d, b = %d\n", a, b);  
    return 0;   // End of program
}
Sample Output

Enter two numbers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
Detailed Explanation

The temporary variable temp acts like a storage box that keeps the first value safe while we overwrite a. Without using temp, we would lose the original value of a after assigning a = b. After copying, we restore the saved value into b, completing the swap.

Think of it like swapping water between two glasses: you need a third empty glass to hold one temporarily before exchanging.

Simple Interest and Compound Interest


Problem Statement

Write a C program to calculate Simple Interest and Compound Interest for a given Principal, Time, and Rate.

Formula:

Let:

  • P = Principal (initial amount of money)
  • T = Time (usually in years)
  • R = Rate of interest per year (in percent)
  • Simple Interest (SI): SI = (P × T × R) / 100
  • Amount in Compound Interest: A = P (1 + R/100)T
  • Compound Interest (CI): CI = A − P
Algorithm
  1. Start
  2. Read Principal P, Time T (in years), and Rate R (percent per year)
  3. Calculate Simple Interest using SI = (P × T × R) / 100
  4. Calculate Amount in Compound Interest using A = P (1 + R/100)T
  5. Calculate Compound Interest using CI = A − P
  6. Display Simple Interest and Compound Interest
  7. Stop
C Program

#include <stdio.h>
#include <math.h>   // Needed for pow() function

int main() {
    float P, T, R;      // Input: Principal, Time (years), Rate (% per year)
    float SI, CI;       // Output: Simple Interest, Compound Interest
    float A;            // Amount for compound interest

    printf("Enter Principal, Time (in years) and Rate (in %%):\n");
    scanf("%f %f %f", &P, &T, &R);
    
    SI = (P * T * R) / 100;  // Formaula :  Simple Interest

    A = P * pow((1 + R / 100), T); // Amount for Compound Interest 
    
    CI = A - P;  // Compound Interest : CI = A - P

    // Display results
    printf("Simple Interest = %.2f\n", SI);
    printf("Compound Interest = %.2f\n", CI);

    return 0;
}
    
Sample Output

Enter Principal, Time (in years) and Rate (in %):
1000 2 5
Simple Interest = 100.00
Compound Interest = 102.50
    
Explanation

In Simple Interest, interest is calculated only on the original principal for all years, so the increase in money is linear. In Compound Interest, each year the interest is added to the principal, and the next year's interest is calculated on this new amount, so the money grows faster.

The function pow(x, y) from the math library calculates x raised to the power y, which is used here to compute (1 + R/100)T for the compound interest formula. The format specifier %.2f is used to display the results with 2 decimal places, which is suitable for financial values.

Evaluation of Expressions


Problem Statement

Write a C program to evaluate the following expressions:

  • y = m × x + c   (equation of a straight line)
  • s = u·t + ½·a·t²   (displacement in uniformly accelerated motion)
Meaning of symbols
  • m = slope of the line, x = input value, c = intercept
  • y = value of the line for the given x
  • u = initial velocity, a = acceleration, t = time
  • s = displacement after time t
Algorithm
  1. Start
  2. Read values of m, x and c
  3. Calculate y using y = m * x + c
  4. Display the value of y
  5. Read values of u, a and t
  6. Calculate displacement s using s = u * t + 0.5 * a * t * t
  7. Display the value of s
  8. Stop
C Program

#include <stdio.h>

int main() {
    float m, x, c, y;   // Variables for line equation y = m*x + c
    float u, a, t, s;   // Variables for displacement s = u*t + 0.5*a*t*t

    printf("Enter m, x and c:\n");
    scanf("%f %f %f", &m, &x, &c);

    y = m * x + c;   
    printf("Value of y = %.2f\n", y);

    printf("Enter u, a and t:\n");
    scanf("%f %f %f", &u, &a, &t);

    s = u * t + 0.5f * a * t * t;
    printf("Displacement s = %.2f\n", s);

    return 0;
}
    
Sample Output

Enter m, x and c:
2 3 4
Value of y = 10.00
Enter u, a and t:
5 2 3
Displacement s = 24.00
    
Explanation

The mathematical formulas from algebra and physics are written in C by replacing symbols with variables and using the correct operators. Multiplication is written with *, and powers like t² are written as t * t.

For the line equation, y = m * x + c gives the value of y for the given x. For the displacement equation, s = u * t + 0.5f * a * t * t calculates how far an object moves when it starts with velocity u and moves with constant acceleration a for time t.