ESC LAB C Manual
ESC LAB C Manual
)
J N N College of Engineering, Shivamogga
Department of Electronics & Telecommunication Engineering
Course Title: Introduction to C Programming CIE Marks 50
Course Code: BESCK104E SEE Marks 50
Course Type: Integrated Total Marks 100
Teaching Hours/Week (L:T:P:S) 2:0:2:0 Exam Hours 03
Total Hours of Pedagogy 40 hours Credits 03
Course Outcomes:
At the end of the course the student will be able to:
CO1. Elucidate the basic architecture and functionalities of a computer
and also recognize the hardware parts.
CO2. Apply programming constructs of C language to solve the real-
world problem
CO3. Explore user-defined data structures like arrays in implementing
solutions to
problems like searching and sorting
CO4 Explore user-defined data structures like structures, unions, and
pointers in implementing solutions
CO5. Design and Develop Solutions to problems using modular
programming constructs using functions
Prepared by
Mr. Anand Raj S N
Assistant Professor, Department of ETE,
JNNCE Shivamogga
Lab Assignments:
Character.
5. Compute sin(x)/cos(x)using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
8. Implement structure stored, write, and compute average marks and the students scoring
9. Develop a program using pointers to compute the sum, mean, and standard deviation
10. 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
pe=m*G*h;
pe =(m*v*v)/2;
te= pe+ pe;
#include <stdio.h>
int main(void)
{
int a,b,c;
printf("Enter distance between two cities in km\n");
scanf("%d",&a);
b=a*1000; //To convert in meters
c=a*100000;//To convert in centimeters
printf("Total km in meters=%d\n",b );
printf("Total km in centimeters=%d",c );
return 0;
Character.
#include<stdio.h>
#include<ctype.h>
void main()
{
char ch;
if(isalpha(ch))
{
printf("The character entered is an alphabet\n");
}
else if(isdigit(ch))
{
printf("The character entered is a digit\n");
}
else if(isspace(ch))
{
printf("The character entered is space\n");
}
else
{
printf("The character entered may be a special character\n");
}
}
Enter the character
D
The character entered is an alphabet
Enter the character
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],prod[10][10];
int i,j,k,r1,c1,r2,c2,r,c;
if(c1!=r2)
{
printf("Matrix Multiplication is not possible\n");
exit(0);
}
r=r1;
c=c2;
5. Compute sin(x)/ cos(x) using Taylor series approximation. Compare your result
with the built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#include<math.h>
void main()
{
int i,n,sign;
float x,sinx=0;
for(i=0;i<n;i++)
{
sign=(int)pow(-1,i);
sinx=sinx+sign*pow(x,2*i+1)/findFact(2*i+1);
}
printf("The value of sinx using the series is %f\n",sinx);
printf("The value of sinx using the builtin function is %f\n",sin(x));
}
#include<stdio.h>
void main()
{
int arr[100];
int len,i,j,temp;
for(i=0;i<len-1;i++)
{
for(j=0;j<len-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<len;i++)
{
printf("%d\t",arr[i]);
}
}
Enter the length of the array
6
Enter the elements of the array
34 2 89 45 12 90
The sorted array is
2 12 34 45 89 90
#include<stdio.h>
void main()
{
char str[40];
int i=0,length=0;
while(str[i]!='\0')
{
i++;
length++;
}
printf("The length of the string is %d\n",length);
}
Enter the string
Hello world
The length of the string is 11
#include <stdio.h>
void main()
{
char str1[40],str2[40], str3[40];
int i=0,j=0,k=0;
while(str1[i]!='\0')
{
str3[k]=str1[i];
i=i+1;
k=k+1;
9|P a g e Anand Raj S N, Assistant Professor,
Dept. Of ETE, JNNCE, Shivamogga
National Education Society (R.)
J N N College of Engineering, Shivamogga
Department of Electronics & Telecommunication Engineering
}
while(str2[j]!='\0')
{
str3[k]=str2[j];
j=j+1;
k=k+1;
}
str3[k]='\0';
printf("The concatenated string is %s\n",str3);
}
Enter the first string
hello
Enter the second string
world
The concatenated string is hello world
void main()
{
char str1[40],str2[40];
int i=0;
int len1,len2,diff=0;
len1=strlen(str1);
len2=strlen(str2);
if(len1!=len2)
{
printf("The strings should be of same length\n");
exit(1);
}
while(str1[i]!='\0')
{
if(str1[i]!=str2[i])
{
diff=1;
if(diff==0)
{
printf("Both strings are same\n");
}
else
{
if(str1[i]>str2[i])
{
printf("String 1 is greater than string 2\n");
}
else
{
printf("String 1 is lesser than string 2\n");
}
}
}
Enter the first string
hello
Enter the second string
world
String 1 is lesser than string 2
8. Implement structure stored, write, and compute average marks and the students
scoring above and below the average marks for a class of N students.
#include<stdio.h>
void main()
{
int i,n;
float sum=0,class_avg;
struct stud
{
int roll_no;
char name[40];
float marks;
};
for(i=0;i<n;i++)
{
printf("Enter the roll no of student %d\n",i);
scanf("%d",&st[i].roll_no);
printf("Enter the name of student %d\n",i);
scanf("%s",&st[i].name);
printf("Enter the marks of student %d\n",i);
scanf("%f",&st[i].marks);
}
for(i=0;i<n;i++)
{
sum=sum+st[i].marks;
}
class_avg=sum/n;
9. Develop a program using pointers to compute the sum, mean, and standard
#include<stdio.h>
#include<math.h>
void main()
{
int arr[10];
int i,n=5,sum;
int *ptr;
float mean,sddv,var;
ptr=arr;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+ *(ptr+i);
}
printf("The sum of the elements in the array is %d\n",sum);
mean=sum/(float)n;
printf("The average of the elements in the array is %f\n",mean);
var=0;
for(i=0;i<n;i++)
{
var=var+pow(*(ptr+i)-mean,2);
}
sddv=sqrt(var/n);
printf("The SD of the elements in the array is %f\n",sddv);
}
Enter the value of n
4
Enter the elements of the array
67 78 89 56
The sum of the elements in the array is 290
The average of the elements in the array is 72.500000
The SD of the elements in the array is 12.298374
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>
void main()
temp=gcd(p,gcd(q,b3));
b1=p/temp;
b2=q/temp;
b3=b3/temp;
}