INTRODUCTION TO C
PROGRAMMING
LAB MANUAL
BESCK205B
1. C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
#include <stdio.h> int
main()
{ double mass, height, velocity, g = 9.81, mechanicalEnergy;
printf("Enter the mass of the particle (kg): "); scanf("%lf",
&mass); printf("Enter the height of the particle (m): ");
scanf("%lf", &height); printf("Enter the velocity of the particle
(m/s): "); scanf("%lf", &velocity); mechanicalEnergy = (mass * g
* height) + (0.5 * mass * velocity * velocity); printf("The
mechanical energy of the particle is: %.2lf Joules\n",
mechanicalEnergy); return 0;
OUTPUT
2. C Program to convert Kilometers into Meters and Centimeters.
#include <stdio.h> int main() { double km;
printf("Enter distance in kilometers: ");
scanf("%lf", &km); double meters = km *
1000; double centimeters = km * 100000;
printf("Kilometers: %.2lf km\n", km);
printf("Meters: %.2lf m\n", meters);
printf("Centimeters: %.2lf cm\n",
centimeters); return 0;
}
OUTPUT
3. C Program To Check the Given Character is Lowercase or Uppercase or
Special Character. #include <stdio.h> int main()
{ char ch; printf("Enter a
character: "); scanf("%c",
&ch); if (ch >= 'A' && ch
<= 'Z')
{ printf("Uppercase
Letter\n");
} else if (ch >= 'a' && ch <=
'z')
{ printf("Lowercase
Letter\n");
} else { printf("Special
Character\n");
} return
0;
}
OUTPUT
4. Program to balance the given Chemical Equation values x, y, p, q of a simple
chemical equation of the type: The task is to find the values of constants b1, b2,
b3 such that the equation is balanced on both sides and it must be the reduced
form.
#include <stdio.h> int fnGCD(int a, int b) // Function to compute the Greatest
Common Divisor (GCD)
{ if (b == 0) return a;
return fnGCD(b, a %
b);
} int
main(void)
{ int x, y, p, q; int b1, b2, b3; int gcd; printf("Enter
atomicity (x) of Element1: "); scanf("%d", &x);
printf("Enter atomicity (y) of Element2: "); scanf("%d",
&y); printf("Enter atomicity (p) of Element1 in the
compound: "); scanf("%d", &p); printf("Enter atomicity (q)
of Element2 in the compound: "); scanf("%d", &q);
b1 = p * y; b2 = q * x; b3 = x * y; gcd = fnGCD(b1, b2); gcd =
fnGCD(b3, gcd); b1 /= gcd; b2 /= gcd; b3 /= gcd; printf("\nBalanced
Equation in reduced form:\n"); printf("%d(%d) + %d(%d) ==>
%d(%d, %d)\n", b1, x, b2, y, b3, p, q); return 0;
}
5. Implement Matrix multiplication and validate the rules of multiplication.
#include <stdio.h>
int main() {
int m1_rows, m1_cols, m2_rows, m2_cols;
int i, j, k;
// Input dimensions of the first matrix
printf("Enter rows and columns of first matrix (e.g. 2 3): ");
scanf("%d %d", &m1_rows, &m1_cols);
// Input dimensions of the second matrix
printf("Enter rows and columns of second matrix (e.g. 3 2): ");
scanf("%d %d", &m2_rows, &m2_cols);
// Validate the multiplication rule
if (m1_cols != m2_rows) {
printf("Matrix multiplication not possible. Columns of first matrix must
equal rows of second matrix.\n");
return 1;
}
int matrix1[m1_rows][m1_cols];
int matrix2[m2_rows][m2_cols];
int result[m1_rows][m2_cols];
// Input elements of first matrix
printf("Enter elements of first matrix:\n");
for (i = 0; i < m1_rows; i++) {
for (j = 0; j < m1_cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
// Input elements of second matrix
printf("Enter elements of second matrix:\n");
for (i = 0; i < m2_rows; i++) {
for (j = 0; j < m2_cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
// Initialize result matrix with 0
for (i = 0; i < m1_rows; i++) {
for (j = 0; j < m2_cols; j++) {
result[i][j] = 0;
}
}
// Perform matrix multiplication
for (i = 0; i < m1_rows; i++) {
for (j = 0; j < m2_cols; j++) {
for (k = 0; k < m1_cols; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Display the result
printf("Resultant matrix:\n");
for (i = 0; i < m1_rows; i++) {
for (j = 0; j < m2_cols; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}