Intro C Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

HKBK College of Engineering

22/1, Opposite Manyata Tech Park, Nagawara, Bangalore-560045


(Approved by AICTE, affiliated to VTU, Accredited by NBA)

Department of Electronics & Communication


Engineering
(Accredited by NBA for the academic years 2022-2025)

INTRODUCTION TO C PROGRAMMING (BESCK204E)

Lab Manual

II Semester ECE
Prepared by
Prof.Shilpa M N
Name:
USN:
Section: Batch:
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

VISION OF THE COLLEGE

To empower students through wholesome education and enable the students to develop into
highly qualified and trained professionals with ethics and emerge as responsible citizen with
broad outlook to build a vibrant nation.

MISSION OF THE COLLEGE

To achieve academic excellence through in-depth knowledge in science, engineering, and


technology through dedication to duty, innovation in teaching and faith in human values.

To enable our students to develop into outstanding professionals with high ethical standards
to face the challenges of the 21st century.

To provide educational opportunities to the deprived and weaker section of the society, to
uplift their socio-economic status.

VISION OF THE DEPARTMENT

To shape the students as disciplined humane engineers who can build a strong, peaceful and
vibrant country and focus on mutual respect, tolerance and professional ethics.

MISSION OF THE DEPARTMENT

To provide the best possible educational experience through excellence in teaching and
research activities for today’s students and professionals of tomorrow.

To hone young minds and train them to be conscientious individuals who will serve the
society as competent professionals in the field of Electronics and Communication
Engineering

Page 2 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program Education Outcomes

PEO-1: Our graduates will possess good knowledge in engineering


fundamentals.
PEO-2: Our graduates will be capable of analysing and designing systems.

PEO-3: Our graduates will be capable of creating innovative products in


multidisciplinary areas.

PEO-4: The graduates will be ethically strong personnel with good


communication and interpersonal skills with high moral values.

PEO-5: They will have a desire for continuous learning and R&D.

Program Specific Outcomes

PSO-1: Professional Skills: An abilit y to understand the basic concepts in


Electronics & Communication Engineering and to apply them to various areas,
like Electronics, Communications, Signal processing, VLSI, Embedded systems
etc., in the design and implementation of complex systems.

PSO-2: Problem-Solving Skills: An ability to solve complex Electronics and


Communication Engineering problems, using latest hardware and software tools,
along with analytical skills to arrive cost effective and appropriate solutions.

PSO-3: An ability to become an entrepreneur or to contribute to industrial


services and / or Govt. organizations in the field of Electronics and
Communication Engg.

PSO-4: An abilit y to work on multidisciplinary teams with efficiency in different


Programming techniques.

Program Outcomes

PO-1: Engineering Knowledge: Apply the knowledge of mathematics, science,


engineering fundamentals, and an engineering specialization to the solution of
complex engineering problems.

PO-2: Problem analysis: Identify, formulate, research literature, and analyse


complex engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.

Page 3 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

PO-3: Design/development of solutions: Design solutions for complex


engineering problems and design system components or processes that meet the
specified needs with appropriate consideration for the public health and safety,
and the cultural, societal, and environmental considerations.

PO-4: Conduct investigations of complex problems: Use research-based


knowledge and research methods including design of experiments, analysis and
interpretation of data, and synthesis of the information to provide valid
conclusions.

PO-5: Modern Tool Usage: Create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including prediction and
modelling to complex engineering activities with an understanding of the
limitations.

PO-6: The Engineer and Society: Apply reasoning informed by the contextual
knowledge to assess societal, health, safety, legal, and cultural issues and the
consequent responsibilit ies relevant to the professional engineering practice.

PO-7: Environment and Sustainabilit y: Understand the impact of the


professional engineering solutions in societal and environmental contexts, and
demonstrate the knowledge of need for sustainable development.

PO-8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilit ies and norms of the engineering practice.

PO-9: Individual and Teamwork: Function effectively as an individual, and as a


member or leader in diverse teams, and in multidisciplinary settings.

PO-10: Communication: Communicate effectively on complex engineering


activities with the engineering community and with society at larg e, such as,
being able to comprehend and write effective reports and design
documentation, make effective presentations, and give and receive clear
instructions.
PO-11: Project Management and Finance: Demonstrate knowledge and
understanding of the engineering and management principles and apply these to
one‘s own work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
PO-12: Life-long learning: Recognise the need for and have the preparation and
abilit y to engage in independent and life-long learning in the broadest context of
technological change.

Page 4 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Introduction to C Programming
Course Code: BESCK104E/204E CIE Marks 50

Course Type (Theory/Practical /Integrated) Integrated SEE Marks 50 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 Objectives:

CLO 1. Elucidate the basic architecture and functionalities of a computer.

CLO 2. Apply programming constructs of C language to solve the real-world problems.

CLO 3. Explore user-defined data structures like arrays, structures and pointers in
implementing solutions to problems.

CLO 4. Design and Develop Solutions to problems using modular programming constructs
such as functions and procedures.

LAB Programs

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


2. write a C Program to convert Kilometres into Meters and Centimetres.
3. write a C Program to Check the Given Character is Lowercase or Uppercase or Special
Character.
4. write a C 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 b 1, b2, b3 such that
the equation is balanced on both sides, and it must be the reduced form.
5. write a C Program to Implement Matrix multiplication and validate the rules of
multiplication.
6. write a C Program to 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.
7. Write a C Program to Sort the given set of N numbers using Bubble sort.
8. Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.

Page 5 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

9. write a C Program to 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.
10. Develop a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of N real numbers.
Course Outcomes (Course Skill Set)
At the end of the course the student will be able to:
CO1. Elucidate the basic architecture and functionalities of a computer and recognize the
hardware parts.
CO 2. Apply programming constructs of C language to solve the real-world problem.
CO 3. Explore user-defined data structures like arrays in implementing solutions to
problems like searching and sorting.
CO 4. Explore user-defined data structures like structures, unions, and pointers in
implementing solutions.
CO 5. Design and Develop Solutions to problems using modular programming constructs
using functions.

Page 6 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

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

Program:
#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);
}
OUTPUT:

Page 7 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 2

Write a C Program to convert Kilometers into Meters and Centimeters.

Program:
#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;
}

OUTPUT:

Page 8 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

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

PROGRAM

#include <stdio.h>
int main(void)
{
char ch;
printf("Enter any character: \n");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')
{
printf(" '%c' is uppercase alphabet.", ch);
}
else
if(ch >= 'a' && ch <= 'z')
{
printf(" '%c' is lowercase alphabet.", ch);
}
else
if(ch >= '0' && ch <= '9')
{
printf(" '%c' is digit.", ch);
}
else
printf(" '%c' is special character.", ch);
return 0;
}
OUTPUT:

Page 9 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 4

Write a C 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.
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int k,l,m,n;
printf (“enter the number of reactants x and y \n”);
scanf(“%d%d”, &k,&i);
printf(“enter the number of products p and q\n”);
scanf(“%d%d”,&m,&n);
balance(k,l,m,n);
}
int 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/y1;
b3=1;
}
else
{
p = p*y;
q = q*x;
b3 = x*y;
temp = gcd(p,gcd(q,b3));
b1=p/temp;
b2=q/temp;

Page 10 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

b3=b3/temp;
}
printf(“the coefficients are b1=%d,b2=%d, b3=%d”, b1,b2,b3);
}
int gcd(int a, int b)
{
int hcf;
for(int i=1;i<=a && i<=b;i++)
{
if(a%i==0 && b%i==0)
{
hcf=i;
}
}
return hcf;
}
OUTPUT:

Page 11 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 5
Write a C program to Implement Matrix multiplication and validate the rules of
multiplication.

PROGRAM
#include<stdio.h>
int main(void)
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Please insert the number of rows and columns for first matrix \n ");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
tot = tot + fst[c][k] * sec[k][d];
}

Page 12 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the matrices is: \n ");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}
OUTPUT:

Page 13 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 6
Write a C Program to compute Sin(x)/ Cos(x) using Taylor series approximation.
Compare your result with the built- in Library function. Print both the results with
appropriate messages.
Program:
#include<stdio.h>
#include<math.h>
void main()
{
float x,sum,nume,deno,term;
int degree, i;
printf("Enter degree:\n");
scanf("%d",&degree);
x=degree*(3.142/180);
sum=0;
nume=x;
deno=1;
i=1;
do
{
term=nume/deno;
sum=sum+term;
i=i+2;
deno=deno*(i-1)*i;
nume=-nume*x*x;
}while(fabs(term)>0.00001);
printf("Sin %d using Taylor's Series:%f\n",degree,sum);
printf("Sin %d using built-in Library function:%f\n",degree,sin(x));
getch();
}

OUTPUT:

Page 14 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 7
Write a C Program to Sort the given set of N numbers using Bubble Sort.
Program:

void main()

intn,i,j,a[10],t;

printf("Enter n\n");

scanf("%d",&n);

printf("Enter elements\n");

for (i=0;i<=n;i++)

scanf("%d",&a[i]);

for (i=0;i<n-1;i++)

for (j=0;j<n-1;j++)

if (a[j]>a[j+1])

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

printf("Sorted Array\n");

for (i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
OUTPUT:

Page 15 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 8
Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
Program:

#include <stdio.h>

#include<conio.h>

intlen_str(char s[50]);

intcomp_str(char s1[50], char s2[50]) ;

voidconcat_str(char s1[50],char s2[50]);

void main()

char s1[50],s2[50],len,c;

clrscr();

printf("enter the 1st string\n");

scanf("%s",s1);

printf("enter the 2nd string\n");

scanf("%s",s2);

len=len_str(s1);

printf("length of the string1 = %d\n",len);

len=len_str(s2);

printf("length of the string2 = %d\n",len);

c=comp_str(s1,s2);

if(c==0)

printf("strings are equal\n");

else

printf("strings are not equal\n");

Page 16 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

concat_str(s1,s2);

getch();

intlen_str(char s[50])

int len = 0, i;

for (i = 0; s[i] != '\0'; i++)

len++;

return len;

int comp_str(char s1[50], char s2[50])

int len1, len2, i;

len1 = len_str(s1);

len2 = len_str(s2);

if (len1 != len2)

return 1;

for (i = 0; i < len1; i++)

if (s1[i] !=s2[i])

return 1;

return 0;

Page 17 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Void concat_str(char s1[50],char s2[50])

int i=0,j;

j=len_str(s1);

while(s2[i]!='\0')

s1[j]=s2[i];

j++;

i++;

s1[j]='\0';

printf("concatination of string1 and string2 %s \n",s1);

OUTPUT:

Page 18 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 9
Write a C Program to Implement structures to read, write and compute average marks and
students scoring above and below the average marks for a class of N students.
PROGRAM
#include <stdio.h>

#include<stdio.h>

void main()

struct stud

char name[25];

char usn[25];

int marks;

};

struct stud s[25];

int n,i,sum;

float average;

sum=0;

printf("\nenter number of students :");

scanf("%d",&n);

printf("enter the name usn and marks of %d students\n",n);

for(i=0;i<n;i++)

scanf("%s%s%d",s[i].name,s[i].usn,&s[i].marks);

sum=sum+s[i].marks;

Page 19 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

average=sum/(float)n;

printf("average marks=%f",average);

printf(" \nDetails of students scoring above average marks\n");

for(i=0;i<n;i++)

if (s[i].marks >= average)

printf("name=%s \t usn=%s \t marks= %d\n",s[i].name,s[i].usn,s[i].marks);

printf(" \nDetails of students scoring below average marks\n");

for(i=0;i<n;i++)

if (s[i].marks < average)

printf("name=%s \t usn=%s \t marks= %d\n",s[i].name,s[i].usn,s[i].marks);

OUTPUT:

Page 20 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

Program 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.
PROGRAM
#include<stdio.h>

#include<math.h>

void main()

float a[10],*ptr,mean,std,sum=0,sumstd=0;

intn,i;

printf("Enter the no 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++)

Page 21 of 22
HKBK College of Engineering BESCK104E/204E Introduction to C Programming

sumstd=sumstd+pow((*ptr-mean),2);

ptr++;

std=sqrt(sumstd/n);

printf("sum=%f\n",sum);

printf("Mean=%f\n",mean);

printf("standard deviation=%f\n",std);

getch();

OUTPUT:

Page 22 of 22

You might also like