C Programming Lab Manual
Topic: Variables, Constants, Operators, and Expressions
This document contains 20 basic C programs demonstrating fundamental concepts of
variables, constants, operators, and expressions along with their outputs.
1. Display Variables
AIM: To write a C program to display variables.
Program:
#include <stdio.h>
int main() {
int age = 20;
float height = 5.8;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Age: 20
Height: 5.8
Grade: A
2. Use Constants
AIM: To write a C program to use constants.
Program:
#include <stdio.h>
#define PI 3.14159
int main() {
const int DAYS_IN_WEEK = 7;
printf("Value of PI: %.2f\n", PI);
printf("Days in a week: %d\n", DAYS_IN_WEEK);
return 0;
}
Output:
Value of PI: 3.14
Days in a week: 7
3. Add Two Numbers
AIM: To write a C program to add two numbers.
Program:
#include <stdio.h>
int main() {
int a = 5, b = 10;
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Output:
Sum = 15
4. Simple Interest
AIM: To write a C program to find simple interest.
Program:
#include <stdio.h>
int main() {
float principal = 1000, rate = 5, time = 2;
float interest = (principal * rate * time) / 100;
printf("Simple Interest = %.2f\n", interest);
return 0;
}
Output:
Simple Interest = 100.00
5. Swap Two Numbers
AIM: To write a C program to swap two numbers.
Program:
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output:
After swapping: a = 10, b = 5
6. Area of Circle
AIM: To write a C program to find area of circle.
Program:
#include <stdio.h>
#define PI 3.1416
int main() {
float radius = 3.5;
float area = PI * radius * radius;
printf("Area of Circle = %.2f\n", area);
return 0;
}
Output:
Area of Circle = 38.48
7. Increment and Decrement
AIM: To write a C program to 7. increment and decrement.
Program:
#include <stdio.h>
int main() {
int x = 10;
printf("x = %d\n", x);
printf("x++ = %d\n", x++);
printf("Now x = %d\n", x);
printf("++x = %d\n", ++x);
printf("x-- = %d\n", x--);
printf("Now x = %d\n", x);
return 0;
}
Output:
x = 10
x++ = 10
Now x = 11
++x = 12
x-- = 12
Now x = 11
8. Relational Operators
AIM: To write a C program to demonstrate relational operators.
Program:
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("a < b: %d\n", a < b);
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
return 0;
}
Output:
a < b: 1
a == b: 0
a != b: 1
9. Logical Operators
AIM: To write a C program to 9. logical operators.
Program:
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("(x < y) && (y > 0): %d\n", (x < y) && (y > 0));
printf("(x > y) || (y > 0): %d\n", (x > y) || (y > 0));
printf("!(x > y): %d\n", !(x > y));
return 0;
}
Output:
(x < y) && (y > 0): 1
(x > y) || (y > 0): 1
!(x > y): 1
10. Arithmetic Expression
AIM: To write a C program to demonstrate arithmetic expression.
Program:
#include <stdio.h>
int main() {
int a = 4, b = 2, c = 3;
int result = (a + b) * c - (a / b);
printf("Result = %d\n", result);
return 0;
}
Output:
Result = 16
11. Square and Cube
AIM: To write a C program to find the square and cube.
Program:
#include <stdio.h>
int main() {
int num = 4;
int square = num * num;
int cube = num * num * num;
printf("Square = %d\n", square);
printf("Cube = %d\n", cube);
return 0;
}
Output:
Square = 16
Cube = 64
12. Perimeter of Rectangle
AIM: To write a C program to 12. perimeter of rectangle.
Program:
#include <stdio.h>
int main() {
float length = 6.5, breadth = 4.2;
float perimeter = 2 * (length + breadth);
printf("Perimeter of Rectangle = %.2f\n", perimeter);
return 0;
}
Output:
Perimeter of Rectangle = 21.40
13. Celsius to Fahrenheit
AIM: To write a C program to 13. celsius to fahrenheit.
Program:
#include <stdio.h>
int main() {
float celsius = 37;
float fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
Output:
37.00 Celsius = 98.60 Fahrenheit
14. Average of Three Numbers
AIM: To write a C program to find the average of three numbers.
Program:
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
float average = (a + b + c) / 3.0;
printf("Average = %.2f\n", average);
return 0;
}
Output:
Average = 20.00
15. Compound Assignment
AIM: To write a C program to demonstrate compound assignment.
Program:
#include <stdio.h>
int main() {
int x = 5;
x += 3;
printf("After x += 3: %d\n", x);
x *= 2;
printf("After x *= 2: %d\n", x);
return 0;
}
Output:
After x += 3: 8
After x *= 2: 16
16. Remainder Without %
AIM: To write a C program to demonstrate remainder without %.
Program:
#include <stdio.h>
int main() {
int dividend = 17, divisor = 5;
int quotient = dividend / divisor;
int remainder = dividend - (quotient * divisor);
printf("Remainder = %d\n", remainder);
return 0;
}
Output:
Remainder = 2
17. Maximum Using Ternary
AIM: To write a C program to find the maximum of 3 numbers using ternary operator.
Program:
#include <stdio.h>
int main() {
int a = 15, b = 25;
int max = (a > b) ? a : b;
printf("Maximum = %d\n", max);
return 0;
}
Output:
Maximum = 25
18. Area of Triangle
AIM: To write a C program to find the area of triangle.
Program:
#include <stdio.h>
int main() {
float base = 5.0, height = 8.0;
float area = 0.5 * base * height;
printf("Area of Triangle = %.2f\n", area);
return 0;
}
Output:
Area of Triangle = 20.00
19. Type Casting
AIM: To write a C program to demonstrate type casting.
Program:
#include <stdio.h>
int main() {
int total = 5, count = 2;
float average = (float) total / count;
printf("Average = %.2f\n", average);
return 0;
}
Output:
Average = 2.50
20. Volume of Cylinder
AIM: To write a C program to 20. volume of cylinder.
Program:
#include <stdio.h>
#define PI 3.1416
int main() {
float radius = 3.0, height = 7.0;
float volume = PI * radius * radius * height;
printf("Volume of Cylinder = %.2f\n", volume);
return 0;
}
Output:
Volume of Cylinder = 197.92