Getting Started — Input & Output
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.
#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; }
Roll No : 42
Course : B.Tech CSE
College : IIT Delhi
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.
PI in your code with 3.14159. It's not a variable — it has no memory address.
#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; }
Area of circle = 153.94
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!
(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.
#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; }
98.60°F = 37.00°C
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.
#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; }
Sum = 150
Average = 30.00
Logic & Decision Making
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.
#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; }
Enter Rate of interest : 5
Enter Time (in years) : 3
--- Result ---
Simple Interest = 1500.00
Total Amount = 11500.00
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 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.
#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; }
14 is Even
Enter an integer: 7
7 is Odd
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.
1. Save
a into temp2. Copy
b into a3. Copy
temp (original a) into b
#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; }
Enter second number : 60
Before swap: a = 25, b = 60
After swap: a = 60, b = 25
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.
&& — 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.
#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; }
Largest = 82
Test Your Understanding
In the area of circle program, why do we use #define PI 3.14159 instead of a variable?
Why must we write 5.0 / 9 (not 5 / 9) in the Fahrenheit conversion?
What does num % 2 == 0 check in the even/odd program?
In the swap program, what would happen if we skipped the temp variable and wrote a = b; b = a;?
Mark What You Can Write Yourself
- P1 — Print name and details using printf() printf
- P2 — Calculate area of a circle with #define PI #define
- P3 — Convert Fahrenheit to Celsius correctly 5.0/9
- P4 — Take 5 inputs and compute sum + average casting
- P5 — Simple interest calculator with 3 inputs formula
- P6 — Check even or odd using modulus % if/else
- P7 — Swap two numbers using temp variable temp
- P8 — Find largest of 3 numbers with else if &&
- I completed all 4 quiz questions