0% found this document useful (0 votes)
9 views

ESC LAB C Manual

Uploaded by

radhikasn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ESC LAB C Manual

Uploaded by

radhikasn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

National Education Society (R.

)
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

1|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

Code::Blocks is a free, open-source, cross-platform IDE. Using a plugin


architecture, its capabilities, and features are defined by the provided plugins.
Currently, Code::Blocks is oriented towards C/C++/Fortran.

Lab Assignments:

1. C Program to find the Mechanical Energy of a particle using E = mgh+1/2 mv2.

2. C Program to convert Kilometers into Meters and Centimeters.

3. C Program To Check the Given Character is Lowercase or Uppercase or Special

Character.

4. Implement Matrix multiplication and validate the rules of multiplication.

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.

6. Sort the given set of N numbers using Bubble Sort.

7. Write functions to implement string operations such as compare, concatenate, and

string length. Convince the parameter passing techniques.

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.

9. Develop a program using pointers to compute the sum, mean, and standard deviation

of all elements stored in an array of N real numbers.

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

equation is balanced on both sides and it must be the reduced form.

2|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
1. C Program to find the Mechanical Energy of a particle using E = mgh+1/2 mv2.

Note: Mechanical Energy = Potential Energy + Kinetic Energy


#include <stdio.h>
#define G 9.8
void main()
{
float m,h,v;
float pe,ke, te;

printf("Enter the mass of the particle\n");


scanf("%f",&m);

printf("Enter the height of the particle\n");

printf("Enter the velocity of the particle\n");


scanf("%f",&v);

pe=m*G*h;
pe =(m*v*v)/2;
te= pe+ pe;

printf("The total energy of the particle is %f\n", te);


}
Output
Enter the mass of the particle
0.3
Enter the height of the particle
1.23
Enter the velocity of the particle
15.7
The total energy of the particle is 73.946999

2. C Program to convert Kilometers into Meters and Centimeters.

#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;

3|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
}
Output
Enter distance between two cities in km
5
Total km in meters
5000
Total km in centimeters
500000

3. C Program To Check the Given Character is Lowercase or Uppercase or Special

Character.

#include<stdio.h>
#include<ctype.h>

void main()
{
char ch;

printf("Enter the character\n");


scanf("%c",&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

4|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
5
The character entered is a digit
Enter the character
$
The character entered may be a special character

4. Implement Matrix multiplication and validate the rules of multiplication.

#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;

printf("Enter the order of the matrix A\n");


scanf("%d %d",&r1,&c1);

printf("Enter the order of the matrix B\n");


scanf("%d %d",&r2,&c2);

if(c1!=r2)
{
printf("Matrix Multiplication is not possible\n");
exit(0);
}

r=r1;
c=c2;

printf("Enter the elements of the matrix A\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter the elements of the matrix B\n");


for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}

5|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
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
prod[i][j]=0;
for(k=0;k<r2;k++)
{
prod[i][j]=prod[i][j]+(a[i][k]*b[k][j]);
}
}
}

printf("The resultant Matrix is\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",prod[i][j]);
}
printf("\n");
}
}

Enter the order of the matrix A


22
Enter the order of the matrix B
22
Enter the elements of the matrix A
12 34 12 56
Enter the elements of the matrix B
12 45 23 11
The resultant Matrix is
926 914
1432 1156

Enter the order of the matrix A


23
Enter the order of the matrix B
32
Enter the elements of the matrix A
124345
Enter the elements of the matrix B
345678
The resultant Matrix is
41 48
64 76

6|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

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>

int findFact(int num);

void main()
{
int i,n,sign;
float x,sinx=0;

printf("Enter the value of x in radians");


scanf("%f",&x);

printf("Enter the number of terms to be added\n");


scanf("%d",&n);

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));
}

int findFact(int num)


{
int ft=1,i;
for(i=1;i<=num;i++)
{
ft=ft*i;
}
return(ft);
}
Enter the value of x in radians 0.2
Enter the number of terms to be added
5
The value of sinx using the series is 0.198669
The value of sinx using the builtin function is 0.198669

7|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

6. Sort the given set of N numbers using Bubble Sort.

#include<stdio.h>
void main()
{
int arr[100];
int len,i,j,temp;

printf("Enter the length of the array\n");


scanf("%d",&len);

printf("Enter the elements of the array\n");


for(i=0;i<len;i++)
{
scanf("%d",&arr[i]);
}

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

8|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

7. Write functions to implement string operations such as compare, concatenate, and

string length. Convince the parameter passing techniques.

#include<stdio.h>

void main()
{
char str[40];
int i=0,length=0;

printf("Enter the string\n");


gets(s);

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

C Program to concatenate two strings

#include <stdio.h>
void main()
{
char str1[40],str2[40], str3[40];
int i=0,j=0,k=0;

printf("Enter the first string\n");


gets(str1);

printf("Enter the second string\n");


gets(str2);

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

C Program to compare two strings


#include<stdio.h>
#include<string.h>

void main()
{
char str1[40],str2[40];
int i=0;
int len1,len2,diff=0;

printf("Enter the first string\n");


gets(str1);

printf("Enter the second string\n");


gets(str2);

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;

10 | 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
break;
}
i=i+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;
};

struct stud st[10];

printf("Enter the number of students\n");


11 | 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
scanf("%d",&n);

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;

printf("The Class Average is %f\n",class_avg);

printf("\nthe list of students above average\n");


for(i=0;i<n;i++)
{
if(st[i].marks>class_avg)
{
printf("%d\t %s\t%f\n",st[i].roll_no,st[i].name,st[i].marks);
}
}

printf("\nthe list of students below average\n");


for(i=0;i<n;i++)
{
if(st[i].marks<class_avg)
{
printf("%d\t %s\t%f\n",st[i].roll_no,st[i].name,st[i].marks);
}
}
}
Enter the number of students
5
Enter the roll no of student 0
1
Enter the name of student 0
Darshan
Enter the marks of student 0
23
Enter the roll no of student 1
2
12 | 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
Enter the name of student 1
Krishna
Enter the marks of student 1
24
Enter the roll no of student 2
3
Enter the name of student 2
Rama
Enter the marks of student 2
25
Enter the roll no of student 3
4
Enter the name of student 3
Hari
Enter the marks of student 3
15
Enter the roll no of student 4
5
Enter the name of student 4
lakshmi
Enter the marks of student 4
21
The Class Average is 21.600000
the list of students above average
1 Darshan 23.000000
2 Krishna 24.000000
3 Rama 25.000000

the list of students below average


4 Hari 15.000000
5 lakshmi 21.000000

9. 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>
void main()
{
int arr[10];
int i,n=5,sum;
int *ptr;
float mean,sddv,var;

ptr=arr;

printf("Enter the value of n\n");


13 | 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
scanf("%d",&n);

printf("Enter the elements of the array\n");


for(i=0;i<n;i++)
{
scanf("%d",(ptr+i));
}

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

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 equation is balanced on both sides and it must be the reduced form.

#include <stdio.h>

int gcd(int a, int b);


void balance(int x, int y, int p, int q);

void main()

14 | 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
{
int x = 2, y = 3, p = 4, q = 5;
printf("Enter x y p and q values :");
scanf("%d%d%d%d",&x,&y,&p,&q);
balance(x, y, p, q);
}

int gcd(int a, int b)


{
If(b==0)
{
return a;
}
return gcd(b,a%b);
}

void balance(int x, int y, int p, int q)


{
int b1, b2, b3,temp;
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=gcd(p,gcd(q,b3));
b1=p/temp;
b2=q/temp;
b3=b3/temp;
}

printf("b1 = %d\nb2 = %d\nb3 = %d",b1,b2,b3);


}
Enter x y p and q values :2 3 4 5
b1 = 6
b2 = 5
b3 = 3

15 | P a g e Anand Raj S N, Assistant Professor,


Dept. Of ETE, JNNCE, Shivamogga

You might also like