manual
manual
Course Title
Introduction to C Programming
Lab Assignments
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
#include <stdio.h>
int main(void)
{
float m,h,v,p,k,e;
e=p+k;
SGBIT, Belagavi 4
22ESC145/245: Introduction to C Programming
int main()
{
float km, cm, mm, m;
m = km * 1000.0;
cm = km * 100000.0;
mm = km * 1000000.0;
return 0;
}
SGBIT, Belagavi 5
22ESC145/245: Introduction to C Programming
void main()
{
char a;
printf("Press Any Key : ");
scanf("%c",&a);
SGBIT, Belagavi 6
22ESC145/245: Introduction to C Programming
#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;
// Computing GCD
b1 = p / temp;
b2 = q / temp;
SGBIT, Belagavi 7
22ESC145/245: Introduction to C Programming
b3 = b3 / temp;
}
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
SGBIT, Belagavi 8
22ESC145/245: Introduction to C Programming
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;
}
#include <stdio.h>
int main() {
int arr[100],n,i;
printf ("\nEnter the number of elements: ");
scanf ("%d", &n);
//array of size 5
}
}
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
#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]);
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
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
SGBIT, Belagavi 15