This section lists the programs covered under Variables and Expressions. Click on a program title to jump directly to its detailed explanation.
Write a C program to swap (exchange) the values of two variables using a third temporary variable.
a and ba in a temporary variable tempb into atemp (original a) into ba and b
#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
}
Enter two numbers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
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.
Write a C program to calculate Simple Interest and Compound Interest for a given Principal, Time, and Rate.
Let:
P, Time T (in years), and Rate R (percent per year)SI = (P × T × R) / 100A = P (1 + R/100)TCI = A − P
#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;
}
Enter Principal, Time (in years) and Rate (in %):
1000 2 5
Simple Interest = 100.00
Compound Interest = 102.50
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.
Write a C program to evaluate the following expressions:
xtm, x and cy using y = m * x + cyu, a and ts using s = u * t + 0.5 * a * t * ts
#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;
}
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
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.