Program 1: Hello World in C
Algorithm
1. Start the program.
2. Include the stdio.h header file to use printf().
3. Define the main() function.
4. Use printf() to display "Hello, World!" on the screen.
5. Return 0 to indicate successful execution.
6. End the program
Code
#include <stdio.h> // Header file for input/output functions
int main() {
// Printing message to the screen
printf("Hello, World!\n");
return 0; // Program executed successfully
}
Output
Hello, World!
Explanation
#include <stdio.h> → Standard Input Output library needed for printf().
int main() → Entry point of every C program.
printf("Hello, World!\n"); → Prints the text on screen. \n moves the cursor to the next line.
return 0; → Tells the operating system that the program finished successfully.
#include <stdio.h>
int main() {
int num1, num2; // Variables to store input numbers
int sum, diff, prod;
float div;
// Taking input from user
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Performing operations
sum = num1 + num2;
diff = num1 - num2;
prod = num1 * num2;
// To avoid division by zero
if (num2 != 0) {
div = (float)num1 / num2; // Typecasting to float
} else {
div = 0; // Assign 0 if denominator is zero
}
// Displaying results
printf("Sum = %d\n", sum);
printf("Difference = %d\n", diff);
printf("Product = %d\n", prod);
if (num2 != 0)
printf("Division = %.2f\n", div);
else
printf("Division not possible (denominator is zero)\n");
return 0;
}
Explanation of Program 2
1. Header File
#include <stdio.h>
This line includes the Standard Input/Output library.
Without this, functions like printf() and scanf() won’t work.
2. Main Function
int main() {
Every C program starts execution from the main() function.
int means this function will return an integer value to the operating system.
3. Variable Declaration
int num1, num2;
int sum, diff, prod;
float div;
num1, num2 → store the numbers entered by the user.
sum, diff, prod → store results of addition, subtraction, multiplication.
div → declared as float to store division result with decimal values.
4. Taking Input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf() displays a message asking the user for input.
scanf() reads the two integers typed by the user and stores them in num1 and num2.
& is used because we need the address of the variables to store input values.
5. Performing Arithmetic Operations
sum = num1 + num2; // Addition
diff = num1 - num2; // Subtraction
prod = num1 * num2; // Multiplication
These statements calculate the results and store them in respective variables
6. Division with Check
if (num2 != 0) {
div = (float)num1 / num2;
} else {
div = 0;
}
Division is only valid if the denominator (num2) is not zero.
(float)num1 → typecasting is used to convert integer to float so division can produce decimal
values.
If num2 is zero, we print a warning instead of dividing.
7. Displaying Results
printf("Sum = %d\n", sum);
printf("Difference = %d\n", diff);
printf("Product = %d\n", prod);
if (num2 != 0)
printf("Division = %.2f\n", div);
else
printf("Division not possible (denominator is zero)\n");
\n is used to move the cursor to the next line after each output.
%.2f → prints the division result up to 2 decimal places.
If num2 is zero, a message is shown instead of the result.
8. Return Statement
return 0;
This ends the program and returns 0 to indicate successful execution.
The program takes two numbers as input, calculates addition, subtraction, multiplication, and
division, and displays the results. It also carefully handles the division by zero case.
Lab Program – Find Area of Circle
1. Aim
To write a C program to find the area of a circle using the formula:
Area=π×r2\text{Area} = \pi \times r^2Area=π×r2
2. Algorithm
1. Start
2. Declare variables radius, area.
3. Read the value of radius from the user.
4. Calculate area using formula:
area=π×radius×radius\text{area} = \pi \times radius \times radiusarea=π×radius×radius
5. Print the value of area.
6. Stop
#include <stdio.h> // standard input-output library
#define PI 3.14159 // define constant value of PI
int main() {
float radius, area;
// Step 1: Input radius
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Step 2: Calculate area
area = PI * radius * radius;
// Step 3: Output result
printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);
return 0;
}
4. Dry Run (Example)
Input: radius = 5
area = PI * radius * radius
= 3.14159 * 5 * 5
= 78.53975
Output: The area of the circle with radius 5.00 is: 78.54
5. Flowchart
┌──────────┐
│ Start │
└────┬─────┘
│
▼
┌───────────────┐
│ Input radius │
└──────┬────────┘
│
▼
┌────────────────────┐
│ area = π * r * r │
└──────┬────────────┘
│
▼
┌───────────────────┐
│ Print area │
└──────┬────────────┘
│
▼
┌──────────┐
│ Stop │
└──────────┘
6. Sample Output
Enter the radius of the circle: 7
The area of the circle with radius 7.00 is: 153.94