22ESC145/245: Introduction to C Programming
Course Title
Introduction to C Programming
Lab Assignments
Course Code: 22ESC145/245
Course Type: Integrated
2022-23 ODD/EVEN Semester
SGBIT, Belagavi 1
22ESC145/245: Introduction to C Programming
SGBIT, Belagavi 2
22ESC145/245: Introduction to C Programming
INDEX
Sl. No Experiments
1 C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
2 C Program to convert Kilometers into Meters and Centimeters.
C Program To Check the Given Character is Lowercase or Uppercase or Special
3
Character.
Program to balance the given Chemical Equation values x, y, p, q of a simple
4 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.
5 Implement Matrix multiplication and validate the rules of multiplication.
Compute sin(x)/cos(x) using Taylor series approximation. Compare you result with
6
the built-in library function. Print both the results with appropriate inferences.
7 Sort the given set of Nnumbers using Bubblesort.
Write functions to implement string operations such as compare, concatenate, string
8
length. Convince the parameter passing techniques.
Implement structures to read, write and compute average-marks and the students
9
scoring above and below the average marks for a class of N students.
Develop a program using pointers to compute the sum, mean and standard deviation
10
of all elements stored in an array of N real numbers.
SGBIT, Belagavi 3
22ESC145/245: Introduction to C Programming
1. C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
#include <stdio.h>
int main(void)
{
float m,h,v,p,k,e;
printf("Enter Mass of the body\n");
scanf("%f",&m );
printf("Enter displacement of the body\n");
scanf("%f",&h );
printf("Enter velocity of the body\n");
scanf("%f",&v );
p=m*9.8*h; //To calculate Potential energy
k=0.5*m*(v*v); //To calculate Kinetic energy
e=p+k;
printf("Potential energy of the body = %f\n",p );
printf("Kinetic energy of the body = %f\n",k );
printf("Mechanical energy of a body = %f\n" , e);
}
SGBIT, Belagavi 4
22ESC145/245: Introduction to C Programming
2. C Program to convert Kilometers into Meters and Centimeters.
int main()
{
float km, cm, mm, m;
printf("Enter distance in Kelometer\n");
scanf("%f", &km);
m = km * 1000.0;
cm = km * 100000.0;
mm = km * 1000000.0;
printf("Distance in Meter is %f\n", m);
printf("Distance in Centimeter is %f\n", cm);
printf("Distance in Milimeter is %f\n", mm);
return 0;
}
SGBIT, Belagavi 5
22ESC145/245: Introduction to C Programming
3 C Program To Check the Given Character is Lowercase or Uppercase or Special
Character.
void main()
{
char a;
printf("Press Any Key : ");
scanf("%c",&a);
if(a>=65 && a<90)
{
printf("An Upper Case Letter");
}
else
{
if(a>=97 && a<=122)
{
printf("A lower Case Letter");
}
else
{
if(a>=48 && a<=57)
{
printf("A Digit");
}
else
{
printf("A Special Character\n\n");
}
}
}
}
SGBIT, Belagavi 6
22ESC145/245: Introduction to C Programming
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 main()
{
int x, y, p, q;
printf ("Enter x,y,p,q values: \n");
scanf ("%d%d%d%d",&x,&y,&p,&q);
balance(x, y, p, q);
}
void balance(int x, int y, int p, int q)
{
// Variable declaration
int b1, b2, b3;
if (p % x == 0 && q % y == 0) {
b1 = p / x;
b2 = q / y;
b3 = 1;
}
else {
p = p * y;
q = q * x;
b3 = x * y;
// temp variable to store gcd
int temp = gcd(p, gcd(q, b3));
// Computing GCD
b1 = p / temp;
b2 = q / temp;
SGBIT, Belagavi 7
22ESC145/245: Introduction to C Programming
b3 = b3 / temp;
}
printf ("b1 : %d b2 : %d, b3 : %d",b1,b2,b3);
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
5. Implement Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
printf("Enter the value of N (N <= 10): ");
scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
printf("Enter the elements of Matrix-B: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("The product of the two matrices is: \n");
SGBIT, Belagavi 8
22ESC145/245: Introduction to C Programming
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
6. Compute sin(x)/cos(x) using Taylor series approximation. Compare you result with the
built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#include<math.h>
#include<conio.h>
int fac(int x);
void main()
{
float x,Q,sum=0;
int i,j,limit;
clrscr();
printf("Enter the value of x of sinx series: ");
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&limit);
Q=x;
x = x*(3.1415/180);
for(i=1,j=1;i<=limit;i++,j=j+2)
{
if(i%2!=0)
{
sum=sum+pow(x,j)/fac(j);
}
else
sum=sum-pow(x,j)/fac(j);
}
printf("Sin(%0.1f): %f",Q,sum);
getch();
SGBIT, Belagavi 9
22ESC145/245: Introduction to C Programming
int fac(int x)
{
int i,fac=1;
for(i=1;i<=x;i++)
fac=fac*i;
return fac;
}
7. Sort the given set of N numbers using Bubble sort.
#include <stdio.h>
int main() {
int arr[100],n,i;
printf ("\nEnter the number of elements: ");
scanf ("%d", &n);
printf ("\nEnter the Array elements: ");
for (i=0; i<n;i++)
scanf ("%d", &arr[i]);
//array of size 5
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
//checking and swapping adjacent elements when left_elem > right_elem
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\n Array after implementing Bubble sort: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
SGBIT, Belagavi 10
22ESC145/245: Introduction to C Programming
8. Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
#include <stdio.h>
#include <string.h>
int main()
{
int length(char str[100]);
int compare(char s1[100], char s2[100]);
void concat(char s1[100], char s2[100]);
int option, result;
char str[100], s1[100], s2[100];
do
{
printf(“1.String length \n”);
printf(“2.string comparision \n”);
printf(“3.string concatenation \n”);
printf(“4.quit \n”);
printf(“enter your choice \n”);
scanf(“%d”, &option);
switch (option)
{
case 1:
printf(“enter string \n”);
scanf(“%s”, &str);
result = length(str);
printf(“the length of string= %d\n”, result);
break;
case 2:
printf(“enter 1st string\n”);
scanf(“%s”, &s1);
printf(“enter 2nd string\n”);
SGBIT, Belagavi 11
22ESC145/245: Introduction to C Programming
scanf(“%s”, &s2);
result = compare(s1, s2);
if (result == 0)
{
printf(“strings are equal \n”);
}
else
{
printf(“strings are not equal \n”);
break;
}
case 3:
printf(“enter two strings\n”);
scanf(“%s%s”, s1, s2);
concat(s1, s2);
printf(“result=%s \n”, s1);
break;
}
} while (option <= 3);
return 0;
}
int length(char str[100])
{
int i = 0;
while (str[i] != ‘\0’)
i++;
return (i);
}
int compare(char s1[100], char s2[100])
{
int i = 0;
while (s1[i] != ‘\0’)
{
if (s1[i] > s2[i])
return (1);
else if (s1[i] < s2[i])
return (-1);
i++;
}
return 0;
}
void concat(char s1[100], char s2[100])
{
int i, j;
i = 0;
SGBIT, Belagavi 12
22ESC145/245: Introduction to C Programming
while (s1[i] != ‘\0’)
i++;
for (j = 0; s2[j] != ‘\0’; i++, j++)
s1[i] = s2[j];
s1[i] = ‘\0’;
}
9. Implement structures to read, write and compute average-marks and the students scoring
above and below the average marks for a class of N students.
#include<stdio.h>
#include<conio.h>
struct student{
int marks;
}st[10];
void main()
{
int i,n;
float total=0,avgmarks;
//clrscr();
printf("\nEnter the number of students in class(<=10):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter student %d marks :",i+1);
scanf("%d",&st[i].marks);
}
for(i=0;i<n;i++)
{
total = total + st[i].marks;
}
avgmarks=total/n;
printf("\nAverage marks = %.2f",avgmarks);
for(i=0;i<n;i++)
{
if(st[i].marks>=avgmarks)
{
printf("\n student %d marks = %d above average",i+1,st[i].marks);
}
else
SGBIT, Belagavi 13
22ESC145/245: Introduction to C Programming
{
printf("\n student %d marks = %d below average",i+1,st[i].marks);
}
}
getch();
}
10. Develop a program using pointers to compute the sum, mean and standard deviation of
all elements stored in an array of N real numbers.
#include <stdio.h>
#include <math.h>
int main()
{
float a[10], *ptr, mean, sum = 0, var, sumstd = 0, std;
int n, i;
printf(“Enter the number of elements\n”);
scanf(“%d”, &n);
printf(“Enter the array elements\n”);
for (i = 0; i < n; i++)
{
scanf(“%f”, &a[i]);
}
ptr = a;
for (i = 0; i < n; i++)
{
sum = sum + (*ptr);
ptr++;
}
mean = sum / n;
ptr = a;
for (i = 0; i < n; i++)
{
sumstd = sumstd + pow((*ptr – mean), 2);
ptr++;
}
var = sumstd / n;
std = sqrt(var);
printf(“Sum = %f \n”, sum);
printf(“Mean = %f \n”, mean);
printf(“Variance = %f \n”, var);
SGBIT, Belagavi 14
22ESC145/245: Introduction to C Programming
printf(“Standard Deviation = %f”, std);
}
SGBIT, Belagavi 15