Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem...

51
MPES Program L L L L PROGRAMM USIN B.Te Dr.De MALIN MALIN MALIN MALIN EDUCATIO (Approved by A (Integr Pulladigunta (Vil), V mming for Problem Solving using C Lab Manual LAB MANUAL LAB MANUAL LAB MANUAL LAB MANUAL MING FOR PROBLEM S NG C LAB (ES1102) (C11 ech - I YEAR - I SEMESTER - ECE R19 Prepared By evireddy Srinivasa Kum Professor in CSE NENI PERUMAL NENI PERUMAL NENI PERUMAL NENI PERUMAL ONAL SOCIETY’S GROUP OF INSTITUT AICTE, New Delhi & Affiliated to JNTUK, Kakinada) rated Campus: Engineering, MBA & B.Pharmacy) Vatticherukuru (Md), Prathipadu Road, Guntur Page 1 SOLVING 18) mar LLU LLU LLU LLU TIONS ) ) 522 017 A.P.

Transcript of Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem...

Page 1: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

LAB MANUALLAB MANUALLAB MANUALLAB MANUAL

PROGRAMMING FOR PROBLEM SOLVING

USING C LAB (ES1102)B.Tech

Dr.Devireddy Srinivasa Kumar

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu

ogramming for Problem Solving using C Lab Manual

LAB MANUALLAB MANUALLAB MANUALLAB MANUAL

PROGRAMMING FOR PROBLEM SOLVING

USING C LAB (ES1102) (C118)B.Tech - I YEAR - I SEMESTER - ECE R19

Prepared By

Dr.Devireddy Srinivasa Kumar

Professor in CSE

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu Road, Guntur –

Page 1

PROGRAMMING FOR PROBLEM SOLVING

(C118)

Dr.Devireddy Srinivasa Kumar

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLU EDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

– 522 017 A.P.

Page 2: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

MMMMALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu

-----------------------------------------------------------------------------------------------------------------------------

VISION:

To Emerge as Premier Institute of Quality

Employability, Social and Ethical Values

MISSION:

IM1: Provide student centric

Methodologies with Modern Infrastructure.

IM2: Promote Technical

with career skills

IM3: Cultivate

IM4: Provide academic facilities

needs

ogramming for Problem Solving using C Lab Manual

ALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu Road, Guntur

-----------------------------------------------------------------------------------------------------------------------------

INSTITUTE VISION MISSION

To Emerge as Premier Institute of Quality

Employability, Social and Ethical Values.

Provide student centric Teaching Learning

Methodologies with Modern Infrastructure.

romote Technical courses to equip the

career skills

Cultivate idealistic minds

rovide academic facilities catering industry and society

Page 2

ALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLUALINENI PERUMALLU EDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Road, Guntur – 522 017 A.P.

--------------------------------------------------------------------------------------------------------------------------------------------------

INSTITUTE VISION MISSION

To Emerge as Premier Institute of Quality Education,

Teaching Learning

Methodologies with Modern Infrastructure.

equip the stakeholders

industry and society

Page 3: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu Road,

-----------------------------------------------------------------------------------------------------------------------------

1. ENGINEERING KNOWLEDGE

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

engineering problems.

2. PROBLEM ANALYSIS

engineering problems reaching substantiated conclusions using

natural sciences, and engineering sciences.

3. DESIGN/DEVELOPMENT OF SOLUTIONS

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.

4. CONDUCT INVESTIGATIONS OF COMPLEX PROBLEMS

knowledge and research methods including design of experiments, analysis and inter

of data, and synthesis of the information to provide valid conclusions.

5. MODERN TOOL USAGE

modern engineering and IT tools including prediction and modelling to complex engineer

activities with an understanding of the limitations.

6. THE ENGINEER AND SOCIETY

knowledge to assess societal, health, safety, legal and cultural issues and the consequent

responsibilities relevant to the pr

7. ENVIRONMENT AND SUSTAINABILITY

engineering solutions in societal and environmental contexts, and demonstrate the knowledge

of, and need for sustainable development.

8. ETHICS: Apply ethical principles and commit to professional ethics and responsibilities

and norms of the engineering practice.

9. INDIVIDUAL AND TEAM WORK

member or leader in diverse teams, and in

10. COMMUNICATION

the engineering community and with society at large, such as, being able to comprehend and

write effective reports and design documentation, ma

receive clear instructions.

11. PROJECT MANAGEMENT AND FINANCE

understanding of the engineering and management principles and apply these to one’s own

work, as a member and

environments.

12. LIFE-LONG LEARNING

to engage in independent and life

ogramming for Problem Solving using C Lab Manual

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Pulladigunta (Vil), Vatticherukuru (Md), Prathipadu Road, Guntur

-----------------------------------------------------------------------------------------------------------------------------

PROGRAM OUTCOMES

ENGINEERING KNOWLEDGE: Apply the knowledge of mathematics, science,

fundamentals, and an engineering specialization to the solution of complex

PROBLEM ANALYSIS: Identify, formulate, research literature, and analyze complex

problems reaching substantiated conclusions using first principles of mathematics,

sciences, and engineering sciences.

DESIGN/DEVELOPMENT OF SOLUTIONS: Design solutions for complex engineering

design system components or processes that meet the specified needs with

sideration for the public health and safety, and the cultural, societal, and

considerations.

CONDUCT INVESTIGATIONS OF COMPLEX PROBLEMS

research methods including design of experiments, analysis and inter

synthesis of the information to provide valid conclusions.

MODERN TOOL USAGE: Create, select, and apply appropriate techniques, resources, and

engineering and IT tools including prediction and modelling to complex engineer

with an understanding of the limitations.

THE ENGINEER AND SOCIETY: Apply reasoning informed by the contextual

societal, health, safety, legal and cultural issues and the consequent

to the professional engineering practice.

ENVIRONMENT AND SUSTAINABILITY: Understand the impact of the professional

solutions in societal and environmental contexts, and demonstrate the knowledge

for sustainable development.

Apply ethical principles and commit to professional ethics and responsibilities

norms of the engineering practice.

INDIVIDUAL AND TEAM WORK: Function effectively as an individual, and as a

in diverse teams, and in multidisciplinary settings.

COMMUNICATION: Communicate effectively on complex engineering activities with

engineering community and with society at large, such as, being able to comprehend and

effective reports and design documentation, make effective presentations,

PROJECT MANAGEMENT AND FINANCE: Demonstrate knowledge and

engineering and management principles and apply these to one’s own

leader in a team, to manage projects and in multidisciplinary

LONG LEARNING: Recognize the need for, and have the prepa

independent and life-long learning in the broadest context of technological change.

Page 3

MALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLUMALINENI PERUMALLU EDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)

(Integrated Campus: Engineering, MBA & B.Pharmacy)

Guntur – 522 017 A.P.

------------------------------------------------------------------------------------------------------------------------------------------------

: Apply the knowledge of mathematics, science,

fundamentals, and an engineering specialization to the solution of complex

: Identify, formulate, research literature, and analyze complex

first principles of mathematics,

: Design solutions for complex engineering

design system components or processes that meet the specified needs with

sideration for the public health and safety, and the cultural, societal, and

CONDUCT INVESTIGATIONS OF COMPLEX PROBLEMS: Use research-based

research methods including design of experiments, analysis and interpretation

: Create, select, and apply appropriate techniques, resources, and

engineering and IT tools including prediction and modelling to complex engineering

: Apply reasoning informed by the contextual

societal, health, safety, legal and cultural issues and the consequent

: Understand the impact of the professional

solutions in societal and environmental contexts, and demonstrate the knowledge

Apply ethical principles and commit to professional ethics and responsibilities

: Function effectively as an individual, and as a

: Communicate effectively on complex engineering activities with

engineering community and with society at large, such as, being able to comprehend and

effective presentations, give and

: Demonstrate knowledge and

engineering and management principles and apply these to one’s own

manage projects and in multidisciplinary

: Recognize the need for, and have the preparation and ability

long learning in the broadest context of technological change.

Page 4: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

MALINENI PERUMALLU EDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

Department of -----------------------------------------------------------------------------------------------------------------------------

B.Tech ----

PROGRAMMING FOR PROBLEM SOLVING USING C LAB (ES1102)

----------------------------------------------------------------------------------------------------------------------

Course Objectives:

1) Apply the principles of C language in problem solving.

2) To design flowcharts, algorithms and knowing how to deb

3) To design & develop of C programs using arrays, strings pointers & functions.

4) To review the file operations, pre

------------------------------------------------------------------------------------------------------

Exercise 1:

1. Write a C program to print a block F using hash (#), where the F has a height of six characters

and width of five and four characters.

2. Write a C program to compute the perimeter and area of a rectangle with a height of

and width of 5 inches.

3. Write a C program to display multiple variables.

Exercise 2:

1. Write a C program to calculate the distance between the two points.

2. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and

p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the

sum of p and q print "Correct values", otherwise pr

Exercise 3:

1. Write a C program to convert a string to a

2. Write a program in C which is a Menu

geometrical shape.

3. Write a C program to calculate the factorial of a g

Exercise 4:

1. Write a program in C to display the n terms of even natural number and their sum.

2. Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 +

1/4 + 1/5 ... 1/n terms.

3. Write a C program to check whether a given number is an Armstrong number or not.

Exercise 5:

1. Write a program in C to print all unique elements in an array.

2. Write a program in C to separate odd and even integers in separate arrays.

3. Write a program in C to sor

Exercise 6:

1. Write a program in C for multiplication of two square Matrices.

2. Write a program in C to find transpose of a given matrix.

Exercise 7:

1. Write a program in C to search an element in a row wis

2. Write a program in C to print individual characters of string in reverse order.

ogramming for Problem Solving using C Lab Manual

MALINENI PERUMALLUEDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

Department of Electronics and Communication Engineering-----------------------------------------------------------------------------------------------------------------------------

I YEAR ----- I SEMESTER ----- ECE ----

PROGRAMMING FOR PROBLEM SOLVING USING C LAB (ES1102)

----------------------------------------------------------------------------------------------------------------------

1) Apply the principles of C language in problem solving.

2) To design flowcharts, algorithms and knowing how to debug programs.

3) To design & develop of C programs using arrays, strings pointers & functions.

4) To review the file operations, pre-processor commands.

------------------------------------------------------------------------------------------------------

1. Write a C program to print a block F using hash (#), where the F has a height of six characters

and width of five and four characters.

2. Write a C program to compute the perimeter and area of a rectangle with a height of

3. Write a C program to display multiple variables.

1. Write a C program to calculate the distance between the two points.

2. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and

p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the

sum of p and q print "Correct values", otherwise print "Wrong values".

1. Write a C program to convert a string to an integer.

2. Write a program in C which is a Menu-Driven Program to compute the area of the various

3. Write a C program to calculate the factorial of a given number.

1. Write a program in C to display the n terms of even natural number and their sum.

2. Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 +

m to check whether a given number is an Armstrong number or not.

1. Write a program in C to print all unique elements in an array.

2. Write a program in C to separate odd and even integers in separate arrays.

3. Write a program in C to sort elements of array in ascending order.

1. Write a program in C for multiplication of two square Matrices.

2. Write a program in C to find transpose of a given matrix.

1. Write a program in C to search an element in a row wise and column wise sorted matrix.

2. Write a program in C to print individual characters of string in reverse order.

Page 4

MALINENI PERUMALLU EDUCATIONAL SOCIETY’S GROUP OF INSTITUTIONS

Electronics and Communication Engineering

---------------------------------------------------------------------------------------------------------------------------------

---- R19

PROGRAMMING FOR PROBLEM SOLVING USING C LAB (ES1102) (C118)

----------------------------------------------------------------------------------------------------------------------

ug programs.

3) To design & develop of C programs using arrays, strings pointers & functions.

----------------------------------------------------------------------------------------------------------------------

1. Write a C program to print a block F using hash (#), where the F has a height of six characters

2. Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches

2. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and

p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the

Driven Program to compute the area of the various

1. Write a program in C to display the n terms of even natural number and their sum.

2. Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 +

m to check whether a given number is an Armstrong number or not.

2. Write a program in C to separate odd and even integers in separate arrays.

e and column wise sorted matrix.

2. Write a program in C to print individual characters of string in reverse order.

Page 5: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 5

Exercise 8:

1. Write a program in C to compare two strings without using string library functions.

2. Write a program in C to copy one string to another string.

Exercise 9:

1. Write a C Program to Store Information Using Structures with Dynamically Memory

Allocation

2. Write a program in C to demonstrate how to handle the pointers in the program.

Exercise 10:

1. Write a program in C to demonstrate the use of & (address of) and *(value at address) operator.

2. Write a program in C to add two numbers using pointers.

Exercise 11:

1. Write a program in C to add numbers using call by reference.

2. Write a program in C to find the largest element using Dynamic Memory Allocation.

Exercise 12:

1. Write a program in C to swap elements using call by reference.

2. Write a program in C to count the number of vowels and consonants in a string using a pointer.

Exercise 13:

1. Write a program in C to show how a function returning pointer.

2. Write a C program to find sum of n elements entered by user. To perform this program, allocate

memory dynamically using malloc( ) function.

Exercise 14:

1. Write a C program to find sum of n elements entered by user. To perform this program, allocate

memory dynamically using calloc( ) function. Understand the difference between the above two

programs

2. Write a program in C to convert decimal number to binary number using the function.

Exercise 15:

1. Write a program in C to check whether a number is a prime number or not using the function.

2. Write a program in C to get the largest element of an array using the function.

Exercise 16:

1. Write a program in C to append multiple lines at the end of a text file.

2. Write a program in C to copy a file in another name.

3. Write a program in C to remove a file from the disk.

Course Outcomes:

By the end of the Lab, the student

1) Gains Knowledge on various concepts of a C language.

2) Able to draw flowcharts and write algorithms.

3) Able design and development of C problem solving skills.

4) Able to design and develop modular programming skills.

5) Able to trace and debug a program

Page 6: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 6

Exercise 1.1) Write a C program to print a block F using hash (#), where the F has a height of six

characters and width of five and four characters.

Pictorial Presentation:

#####

#

####

#

#

#

#include <stdio.h>

#include <conio.h>

void main()

{

printf("#####\n");

printf("#\n";

printf("#\n");

printf("####\n");

printf("#\n");

printf("#\n");

printf("#\n");

getch();

}

OUTPUT

#####

#

####

#

#

#

Page 7: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 7

Exercise 1.2) Write a C program to compute the perimeter and area of a rectangle with a

height of 7 inches and width of 5 inches.

Perimeter of a rectangle

A perimeter is a path that surrounds a two-dimensional shape. The word comes from the Greek

peri (around) and meter (measure). The perimeter can be used to calculate the length of fence

required to surround a yard or garden. For rectangles or kites which have only two different side

lengths, say x and y, the perimeter is equal to 2x + 2y

Area of a rectangle

The area of a two-dimensional figure describes the amount of surface the shape covers. You

measure area in square units of a fixed size, square units of measure are square inches, square

centimeters, or square miles etc. The formula for the area of a rectangle uses multiplication: length

• width = area. A rectangle with four sides of equal length is a square.

Page 8: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

/* height and width of a rectangle in inches */

#include <stdio.h>

#include <conio.h>

void main()

{

int height, width;

int area, perimeter;

height=7;

width=5;

clrscr();

perimeter=2*(height

printf("Perimeter of the rectangle = %d inches

area= height * width

printf("Area of the rectangle = %d square inches

getch();

}

ogramming for Problem Solving using C Lab Manual

/* height and width of a rectangle in inches */

height + width);

("Perimeter of the rectangle = %d inches\n", perimeter);

width;

("Area of the rectangle = %d square inches\n", area);

OUTPUT:

Perimeter of rectangle = 24 inches

Area of the rectangle = 35Square inches

Page 8

Page 9: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 9

Exercise 1.3) Write a C program to display multiple variables

consider the following variable declarations :

int a = 125, b = 12345;

long ax = 1234567890;

short s = 4043;

float x = 2.13459;

double dx = 1.1415927;

char c = 'W';

unsigned long ux = 2541567890;

Find and print

a+ c, x + c,dx + x, ((int) dx) + ax, a + x, s + b, ax + b, s + c, ax + c, ax + ux

//----------------------------- program-------------------------------------------------------

#include <stdio.h>

#include <conio.h>

void main()

{

int a =125, b =12345;

long ax =1234567890;

short s =4043;

float x =2.13459;

double dx =1.1415927;

char c ='W';

unsigned long ux =2541567890;

clrscr();

printf("a = %d\n",a);

printf("c = %c\n",c);

printf("ASCII value of c = %d\n",c);

printf("a + c = %d\n", a + c);

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

printf("x + c = %f\n", x + c);

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

printf("dx + x = %f\n", dx + x);

printf("ax = %ld\n",ax);

printf("((int) dx) + ax = %ld\n",((int) dx)+ ax);

printf("a + x = %f\n", (float)(a + x));

printf("s = %d\n",s);

printf("b = %d\n",b);

printf("s + b = %d\n", s + b);

printf("ax + b = %ld\n", ax + b);

printf("s + c = %hd\n", s + c);

printf("ax + c = %ld\n", ax + c);

printf("ux = %lu\n",ux);

printf("ax + ux = %lu\n", ax + ux);

getch();

}

Page 10: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 10

OUTPUT

a = 125

c= W

ASCII value of c is 87

a + c = 212

x = 2.13459

x + c = 89.134590

dx = 1.1415927

dx + x = 3.276183

ax = 1234567890

((int) dx) + ax = 1234567891

a + x = 127.134590

s = 4043

s + b = 16388

b =12345

ax + b = 1234580235

s + c = 4130

ax + c = 1234567977

ux =2541567890

ax + ux = 3776135780

Page 11: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

Exercise 2.1) Write a C program to calculate the distance between the two points.

Assume the two points are (x1,y1)

x1, y1, x2, y2 are all double values.

ogramming for Problem Solving using C Lab Manual

Exercise 2.1) Write a C program to calculate the distance between the two points.

Assume the two points are (x1,y1) and (x2,y2)

x1, y1, x2, y2 are all double values.

Page 11

Exercise 2.1) Write a C program to calculate the distance between the two points.

Page 12: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 12

//-------------------------------------------------------------

//program to calculate distance between two points

// Assume the two points are (x1,y1) and (x2,y2)

// x1, y1, x2, y2 are all double values.

//-------------------------------------------------------------

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

float x1, y1, x2, y2, dist, res;

clrscr();

printf("Input x1: ");

scanf("%f", &x1);

printf("Input y1: ");

scanf("%f", &y1);

printf("Input x2: ");

scanf("%f", &x2);

printf("Input y2: ");

scanf("%f", &y2);

dist = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));

res = sqrt(dist);

printf("Distance between two points: %.4f", res);

printf("\n");

getch();

}

OUTPUT Ex 1:

Input x1: 25

Input y1: 15

Input x2: 35

Input y2: 10

Distance between two points: 11.1803

OUTPUT Ex 2 :

Input x1: 30

Input y1: 25

Input x2: 50

Input y2: 75

Distance between two points: 53.8516

Page 13: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 13

Exercise 2.2) Write a C program that accepts 4 integers p, q, r, s from the user where q, r

and s are positive and p is even. If q is greater than r and s is greater than p and if the sum

of r and s is greater than the sum of p and q print "Correct values", otherwise print "Wrong

values".

#include <stdio.h>

#include <conio.h>

void main()

{

int p, q, r, s;

clrscr();

printf("Input the first integer: ");

scanf("%d", &p);

printf("Input the second integer: ");

scanf("%d", &q);

printf("Input the third integer: ");

scanf("%d", &r);

printf("Input the fourth integer: ");

scanf("%d", &s);

if((q>r)&&(s>p)&&((r+s)>(p+q)) &&(r>0)&&(s>0)&&(p%2==0))

{

printf("Correct values\n");

}

else

{

printf("Wrong values\n");

}

getch();

}

OUTPUT ex1)

Input the first integer : 25

Input the second integer: 35

Input the third integer : 15

Input the fourth integer : 46

Wrong values

OUTPUT ex2)

Input the first integer : 2

Input the second integer : 5

Input the third integer : 4

Input the fourth integer : 8

Correct values

Page 14: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 14

Exercise 3.1)Write a C program to convert a string to an integer.

//----------------------------------------------------

// C Program to convert string to integer

//-----------------------------------------------------

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

{

char str[20];

clrscr();

printf("Enter a String for Integer conversion \n");

gets(str);

printf("Integer: %d \n", atoi(str));

getch();

}

Output:

Ex1) Enter a string for integer conversion

456hagshdjdfhj

Integer : 456

Ex2) Enter a string for integer conversion

-578hagshdjdfhj

Integer : -578

Page 15: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

Exercise 3.2) Write a program in C which is a Menu

various geometrical shape.

ogramming for Problem Solving using C Lab Manual

2) Write a program in C which is a Menu-Driven Program to compute the area of the

Page 15

Driven Program to compute the area of the

Page 16: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 16

#include <stdio.h>

#include <conio.h>

void main ()

{

int choice,r,l,w,b,h;

float area;

clrscr();

printf("Input 1 for area of circle\n");

printf("Input 2 for area of rectangle\n");

printf("Input 3 for area of triangle\n");

printf("Input your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1: printf("Input radius of the circle: ");

scanf("%d",&r);

area=3.14*r*r;

break;

case 2: printf("Input length and width of the rectangle: ");

scanf("%d%d",&l,&w);

area=l*w;

break;

case 3: printf("Input the base and height of the triangle:");

scanf("%d%d",&b,&h);

area=.5*b*h;

break;

}

printf("The area is : %f\n",area);

getch();

}

OUTPUT Ex 1) Input 1 for area of circle

Input 2 for area of rectangle

Input 3 for area of triangle

Input your choice: 1

Input radius of the circle: 5

The area is: 78.500000

OUTPUT Ex 2) Input 1 for area of circle

Input 2 for area of rectangle

Input 3 for area of triangle

Input your choice: 2

Input length and width of the rectangle: 5 4

The area is: 20.000000

Page 17: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programmin

Exercise 3.3) Write a C program to calculate the factorial of a given number.

Ex ) num=5 factorial = 1*2*3*4*5 = 120

#include <stdio.h>

#include <conio.h>

voidmain()

{

}

OUTPUT

num=5, i=1, f=1

ogramming for Problem Solving using C Lab Manual

3) Write a C program to calculate the factorial of a given number.

Ex ) num=5 factorial = 1*2*3*4*5 = 120

#include <stdio.h>

#include <conio.h>

oidmain()

int i,f=1,num;

clrscr();

printf("Input the number : ");

scanf("%d",&num);

for(i=1;i<=num;i++)

f=f*i;

printf("The Factorial of %d is: %d\n",num,f);

getch();

OUTPUT: Input the number : 5

The Factorial of 5 is: 120

is i<=num Factorialf=f*i

is 1<=5 Yes f=1*1 f=1

is 2<=5 Yes f=1*2 f= 2

is 3<=5 yes f=2*3 f=6

is 4<=5 yes f=6*4 f=24

is 5<=5 yes f=24*5 f=120

is 6<=5 No

Page 17

3) Write a C program to calculate the factorial of a given number.

);

i++

1+1=2

2+1=3

3+1=4

4+1=5

5+1=6

Page 18: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 18

Exercise 4.1:Write a program in C to display the n terms of even natural numbers and their sum.

#include <stdio.h>

#include <conio.h>

voidmain()

{

int i,n,sum=0;

clrscr();

printf("Input number of terms : ");

scanf("%d",&n);

printf("\nThe even numbers are :");

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

{

printf("%d ",2*i);

sum+=2*i;

}

printf("\nThe Sum of even Natural Numbers upto %d terms: %d \n",n,sum);

getch();

}

OUTPUT Ex 1):

Input number of terms : 5

The even numbers are :2 4 6 8 10

The Sum of even Natural Numbers upto 5 terms : 30

OUTPUT Ex 2):

Input number of terms : 8

The even numbers are :2 4 6 8 10 12 14 16

The Sum of even Natural Numbers upto 8 terms : 72

Page 19: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 19

Exercise 4.2:Write a program in C to display the n terms of harmonic series and their sum.

1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.

#include <stdio.h>

#include<conio.h>

void main()

{

int i,n;

float s=0.0;

clrscr();

printf("Input the number of terms : ");

scanf("%d",&n);

printf("\n");

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

{

s+=1/(float)i;

}

printf("\nSum of Series upto %d terms : %f \n",n,s);

getch();

}

OUTPUT Ex 1 )

Input the number of terms : 5

Sum of Series upto 5 terms : 2.283334

OUTPUT Ex 2 )

Input the number of terms : 10

Sum of Series upto 10 terms : 2.928968

Page 20: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 20

Exercise 4.3:Write a C program to check whether a given number is an Armstrong number or not.

/*-------------------------------------------------------------------------------------------------------------------

When the sum of the cubes of the individual digits of a number is equal to that number, the

number is called Armstrong number.

For example 153.

Sum of cubes of digits is 13+ 5

3+ 3

3 = 1+125+27 = 153

370 = 33+7

3+0

3 = 27 + 343 + 0 = 370

371 = 33+7

3+1

3 = 27 + 343 + 1 = 371

407 = 43+0

3+7

3 = 64 + 0 + 343 = 407

------------------------------------------------------------------------------------------------------------------*/

#include <stdio.h>

#include <conio.h>

void main()

{

int n,num,r,sum;

clrscr();

printf("Input a number: ");

scanf("%d",&num);

n=num;

sum=0;

while(n>0)

{

r = n % 10;

sum = sum + (r * r * r);

n = n / 10;

}

if (sum==num)

printf("%d is an Armstrong number.\n",num);

else

printf("%d is not an Armstrong number.\n",num);

getch();

}

OUTPUT

Ex1 ) Input a number: 153

153 is an Armstrong number.

Ex2 ) Input a number: 407

407 is an Armstrong number.

Ex3 ) Input a number: 520

520 is not an Armstrong number.

Page 21: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 21

Exercise 5.1: Write a program in C to print all unique elements in an array.

#include <stdio.h>

#include <conio.h>

void main()

{

int arr[100], n,ctr=0;

int i, j, k;

clrscr();

printf("enter n : ");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

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

printf("The unique elements found in the array :\n");

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

{

ctr=0;

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

{

if(arr[i]==arr[j])

{

ctr++;

}

}

if(ctr==1)

{

printf("%d ",arr[i]);

}

}

printf("\n");

getch();

}

OUTPUT:

Ex1) enter n : 6

input 6 elements in the array

8 9 4 5 6 1

The unique elements found in the array:

8 9 4 5 6 1

Ex 2 )enter n : 6

input 6 elements in the array

8 9 4 8 6 1

The unique elements found in the array:

9 4 6 1

Page 22: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 22

Exercise 5.2: Write a program in C to separate odd and even integers in separate arrays.

#include <stdio.h>

#include <conio.h>

void main()

{

int arr[10], evenarr[10], oddarr[10];

int i,j,k,n;

clrscr();

printf("Input size of the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

{

printf("element : ");

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

}

j=0;

k=0;

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

{

if (arr[i]%2 == 0)

{

evenarr[j] = arr[i];

j++;

}

else

{

oddarr[k] = arr[i];

k++;

}

}

printf("\nThe Even Array elements are : \n");

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

{

printf("%d ",evenarr[i]);

}

printf("\nThe Odd Array elements are :\n");

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

{

printf("%d ", oddarr[i]);

}

printf("\n");

getch();

}

Page 23: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 23

OUTPUT

Ex 1)input Size of the array : 6

input 6 elements in the array :

element : 56

element : 78

element : 45

element : 89

element : 98

element : 10

The Even Array elements are :

56 78 98 10

The ODD Array elements are :

45 89

Ex 2)input Size of the array :10

input 10 elements in the array :

element : 89

element : 87

element : 45

element : 68

element : 44

element : 10

element : 59

element : 72

element : 33

element : 28

The Even Array elements are :

68 44 10 72 28

The ODD Array elements are :

89 87 45 59 33

Page 24: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 24

Exercise 5.3: Write a program in C to sort elements of array in ascending order.

#include <stdio.h>

#include <conio.h>

void main()

{

int arr[100];

int n, i, j, tmp;

clrscr();

printf("Input the size of array : ");

scanf("%d", &n);

printf("Enter array elements :\n");

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

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

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

{

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

{

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

{

tmp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = tmp;

}

}

}

printf("\nElements of array in ascending order:\n");

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

{

printf("%d ", arr[i]);

}

printf("\n");

getch();

}

OUTPUT

Ex1 )Input the size of array : 10

Enter array elements

10 9 8 7 6 5 4 3 2 1

Elements of array in ascending order

1 2 3 4 5 6 7 8 9 10

Ex2 ) Input the size of array : 10

Enter array elements

55 76 32 10 98 56 74 48 28 53

Elements of array in ascending order

10 28 32 48 53 55 56 74 76 98

Page 25: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 25

Exercise 6: 1. Write a program in C for multiplication of two Matrices.

/*============================================================

program to multiply two matrices

matrix A of size M x N

matrix B of size P x Q

Resultant matrix C of size M x Q

============================================================*/

#include<stdio.h>

#include <conio.h>

void main()

{

int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;

clrscr();

printf("enter a size of the array A:" );

scanf("%d%d",&m,&n);

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

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

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

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

printf("enter a size of the array B:" );

scanf("%d%d",&p,&q);

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

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

for (j=0;j<q;j++)

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

if (n==p)

{

for (i=0;i<m;i++) /*initialisation of C*/

for (j=0;j<q;j++)

c[i][j]=0;

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

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

for (k=0;k<q;k++)

c[i][k]=c[i][k]+a[i][j]*b[j][k];

printf("The Resultant array\n");

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

{

for(j=0;j<q;j++)

printf("%d ",c[i][j]);

printf("\n");

}

}

else

printf("Matrix multiplication is not possible\n");

getch();

}

Page 26: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 26

OUTPUT

Enter size of array A 3 3

Enter elements of Matrix A

1 2 3

4 5 6

7 8 9

Enter elements of Matrix b

4 3 2

5 6 7

8 7 6

The product of Matrices

38 36 34

89 84 79

140 132 124

Page 27: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 27

Exercise 6: 2. Write a program in C to find transpose of a given matrix.

#include <stdio.h>

#include <conio.h>

void main()

{

int a[50][50],at[50][50],i,j,k=0,r,c;

clrscr();

printf("\n\nTranspose of a Matrix :\n");

printf("---------------------------\n");

printf("\nInput the rows and columns of the matrix : ");

scanf("%d %d",&r,&c);

printf("Input elements in the matrix :\n");

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

for(j=0;j<c;j++)

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

printf("\nThe matrix is :\n");

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

{

for(j=0;j<c;j++)

printf("%d\t",a[i][j]);

printf("\n");

}

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

for(j=0;j<c;j++)

at[j][i]=a[i][j];

printf("\n\nThe transpose of a matrix is : \n");

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

{

for(j=0;j<r;j++)

printf("%d\t",at[i][j]);

printf("\n");

}

getch();

}

OUTPUT

input Rows and Columns of matrix 3 3

Input Elements in the Matrix

1 2 3

4 5 6

7 8 9

The matrix

1 2 3

4 5 6

7 8 9

Transpose matrix

1 4 7

2 5 8

3 6 9

Page 28: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 28

Exercise 7: 1. Write a program in C to search an element in a row wise and column wise sorted

matrix.

#include <stdio.h>

#include <conio.h>

void main()

{

int a[10][10];

int m,n,i,j,x,found,rowpos,colpos;

clrscr();

printf("enter row and col values ");

scanf("%d%d",&m,&n);

printf("enter matrix rows=%d cols=%d\n",m,n);

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

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

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

printf("enter element to search ");

scanf("%d",&x);

printf("The array in matrix form is : \n");

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

{

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

printf("%d\t", a[i][j]);

printf("\n");

}

printf("The given value for searching is: %d\n",x);

found=0;

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

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

if (a[i][j]==x)

{

found=1;

rowpos=i;

colpos=j;

break;

}

if (found)

printf("%d found at row %d col %d \n",x,rowpos,colpos);

else

printf("%d not found in the array\n",x);

getch();

}

Page 29: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 29

OUTPUT

Enter row and col values 3 3

Enter matrix rows 3 col 3

10 23 45

45 52 58

67 89 92

Enter element to search

89

The array in matrix form

10 23 45

45 52 58

67 89 92

The given value for searching is 89

89 found at row 2 and col 1

Page 30: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 30

Exercise 7:2. Write a program in C to print individual characters of string in reverse order.

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

void main()

{

char ch,str[100]; /* Declares a string of size 100 */

int length,i;

clrscr();

printf("Input the string : ");

i=0;

do

{

ch=getchar();

str[i]=ch;

i++;

} while(ch!='\n');

i--;

str[i]='\0';

printf("string is \n");

puts(str);

length=i;

printf("number of character in the string = %d\n",l);

printf("The characters of the string in reverse are : \n");

for(i=length;i>=0;i--)

{

printf("%c ", str[i]);

}

printf("\n");

getch();

}

OUTPUT

Input the string

rama is a good boy

String is

rama is a good boy

Number of characters in the string = 19

The characters of the string in reverse are

yob doog a si amar

Page 31: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 31

Exercise 8:1. Write a program in C to compare two strings without using string library functions.

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

#define str_size 100 //----------Declare the maximum size of the string

void main()

{

char str1[str_size], str2[str_size];

int flg;

int i;

clrscr();

printf("Input the 1st string : ");

fgets(str1, sizeof str1, stdin);

printf("Input the 2nd string : ");

fgets(str2, sizeof str2, stdin);

i=0;

flg=1;

while ((str1[i]!='\0')&& (str2[i]!='\0'))

{

if (str1[i]!=str2[i])

{

flg=0;

break;

}

i++;

}

printf("string 1 = %s\n",str1);

printf("string 2 = %s\n",str2);

if (flg==1)

printf("strings are equal \n");

else

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

getch();

}

OUTPUT

Ex1) Input the 1st string : rama

Input the 2nd

string : rama

String 1 = rama

String 2 = rama

Strings are equal

Ex2) Input the 1st string : rama

Input the 2nd

string : ravi

String 1 = rama

String 2 = ravi

Strings are not equal

Page 32: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 32

Exercise 8:2. Write a program in C to copy one string to another string.

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

void main()

{

char str1[100], str2[100];

int i;

clrscr();

printf("Copy one string into another string :\n");

printf("---------------------------------------------\n");

printf("Input the string : ");

fgets(str1, sizeof str1, stdin);

/* --------------------------------------Copies string1 to string2 character by character */

i=0;

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

{

str2[i] = str1[i];

i++;

}

//Makes sure that the string is NULL terminated

str2[i] = '\0';

printf("\nThe First string is : %s\n", str1);

printf("The Second string is : %s\n", str2);

printf("Number of characters copied : %d\n\n", i);

getch();

}

OUTPUT Ex1)

Copy one string into another string :

---------------------------------------------

Input the string : rama is a good boy

The first string is : rama is a good boy

The second string is : rama is a good boy

Number of characters copied: 19

OUTPUT Ex2)

Copy one string into another string :

---------------------------------------------

Input the string : sunday

The first string is : sunday

The second string is : sunday

Number of characters copied: 7

Page 33: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 33

Exercise 9:1. Write a C Program to Store Information Using Structures with Dynamically

Memory Allocation

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

struct course

{

char sub[30];

int marks;

};

typedef struct course coursetype;

int main()

{

coursetype *ptr;

int i, n;

clrscr();

printf("Enter number of records: ");

scanf("%d", &n);

ptr = (coursetype *) malloc(n * sizeof(coursetype));

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

{

printf("Enter name of the subject and marks respectively:\n");

scanf("%s%d", &(ptr+i)->sub, &(ptr+i)->marks);

}

printf("Displaying Information:\n");

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

printf("%s\t%d\n", (ptr+i)->sub, (ptr+i)->marks);

getch();

}

OUTPUT

Enter number of records: 2

Enter name of the subject and marks respectively:

Programming

22

Enter name of the subject and marks respectively:

Structure

33

Displaying Information:

Programming 22

Structure 33

Page 34: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 34

Exercise 9:2. Write a program in C to demonstrate how to handle the pointers in the program.

#include <stdio.h>

#include <conio.h>

void main()

{

int *ab;

int m;

clrscr();

m=29;

printf(" Address of m : %p\n",&m);

printf(" Value of m : %d\n\n",m);

ab=&m;

printf(" Address of pointer ab : %p\n",ab);

printf(" Content of pointer ab : %d\n\n",*ab);

m=34;

printf(" Address of pointer ab : %p\n",ab);

printf(" Content of pointer ab : %d\n\n",*ab);

*ab=7;

printf(" Address of m : %p\n",&m);

printf(" Value of m : %d\n\n",m);

getch();

}

OUTPUT

Address of m : FFF4

Value of m : 29

Address of pointer ab : FFF4

Content of pointer ab : 29

Address of pointer ab : FFF4

Content of pointer ab : 34

Address of m : FFF4

Value of m : 7

Page 35: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 35

Exercise 10:1. Write a program in C to demonstrate the use of & (address of) and *(value at

address) operator.

#include <stdio.h>

#include <conio.h>

void main()

{

int m=300;

float fx = 300.60;

char ch = 'z';

int *pt1;

float *pt2;

char *pt3;

clrscr();

pt1= &m;

pt2=&fx;

pt3=&ch;

printf ( " m = %d\n",m);

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

printf ( " ch = %c\n",ch);

printf("\n Using & operator :\n");

printf("-----------------------\n");

printf ( " address of m = %p\n",&m);

printf ( " address of fx = %p\n",&fx);

printf ( " address of ch = %p\n",&ch);

printf("\n Using & and * operator :\n");

printf("---------------------------------\n");

printf ( " value at address of m = %d\n",*(&m));

printf ( " value at address of fx = %f\n",*(&fx));

printf ( " value at address of ch = %c\n",*(&ch));

printf("\n Using only pointer variable :\n");

printf("-------------------------------------\n");

printf ( " address of m = %p\n",pt1);

printf ( " address of fx = %p\n",pt2);

printf ( " address of ch = %p\n",pt3);

printf("\n Using only pointer operator :\n");

printf("--------------------------------------\n");

printf ( " value at address of m = %d\n",*pt1);

printf ( " value at address of fx= %f\n",*pt2);

printf ( " value at address of ch= %c\n\n",*pt3);

getch();

}

Page 36: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 36

OUTPUT

Pointer : Demonstrate the use of & and * operator :

--------------------------------------------------------

m = 300

fx = 300.600006

ch = z

Using &operator :

------------------------------

address of m = FFF4

address of fx = FFF0

address of ch = FFEF

Using & and * operator :

--------------------------------

value at address of m = 300

value at address of fx = 300.600006

value at address of ch = z

Using only pointer variable :

-------------------------------------

address of m = FFF4

address of fx = FFF0

address of ch = FFEF

Using only pointer operator :

------------------------------------

value at address of m = 300

value at address of fx= 300.600006

value at address of ch= z

-------------------------------

Page 37: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 37

Exercise 10:2. Write a program in C to add two numbers using pointers.

#include <stdio.h>

#include <conio.h>

void main()

{

int first, second,*p,*q, sum;

clrscr();

printf("Enter two integers to add\n");

scanf("%d%d",&first, &second);

p = &first;

q = &second;

sum = *p + *q;

printf("First Number = %d\n",*p);

printf("Second Number = %d\n",*q);

printf("Sum of two numbers = %d\n",sum);

getch();

}

OUTPUT Ex1)

Enter two integers to add 10 20

First Number = 10

Second Number = 20

Sum of two numbers = 30

OUTPUT Ex2)

Enter two integers to add 50 70

First Number = 50

Second Number = 70

Sum of two numbers = 120

Page 38: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 38

Exercise 11:1. Write a program in C to add numbers using call by reference.

#include <stdio.h>

#include <conio.h>

long addition(long *p1,long *p2)

{

long sum;

sum = *p1 + *p2;

return sum;

}

void main()

{

long n1, n2, sum;

clrscr();

printf("Pointer:Add two numbers using call by reference:\n");

printf("------------------------------------------------\n");

printf("Input the first number : ");

scanf("%ld",&n1);

printf("Input the second number : ");

scanf("%ld",&n2);

sum = addition(&n1,&n2);

printf("The sum of %ld and %ld is %ld\n\n", n1, n2, sum);

getch();

}

OUTPUT

Pointer : Add two numbers using call by reference:

-----------------------------------------------------

Input the first number : 5

Input the second number : 6

The sum of 5 and 6 is 11

Page 39: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 39

Exercise 11:2. Write a program in C to find the largest element using Dynamic Memory

Allocation.

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <alloc.h>

void main()

{

int i,n;

float *num,large;

clrscr();

printf("Pointer : Find the largest element \n");

printf("using Dynamic Memory Allocation : \n");

printf("-----------------------------------\n");

printf("Input n (number of element) ");

scanf("%d",&n);

num=(float*)calloc(n,sizeof(float));

// Memory is allocated for 'n' elements

if(num==NULL)

{

printf("No memory is allocated.");

exit(0);

}

printf("\n");

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

{

printf("Number %d: ",i+1);

scanf("%f",num+i);

}

large=*num;

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

if (*(num+i) > large)

large = *(num+i);

printf("The Largest element is : %.2f \n",large);

getch();

}

OUTPUT

Pointer : Find the largest element using Dynamic Memory Allocation :

Input total number of elements(1 to 100): 5

Number 1: 5

Number 2: 7

Number 3: 2

Number 4: 9

Number 5: 8

The Largest element is : 9.00

Page 40: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 40

Exercise 12:1. Write a program in C to swap elements using call by reference.

#include <stdio.h>

#include <conio.h>

void swap(int *x,int *y)

{

int tmp;

tmp=*x;

*x=*y;

*y=tmp;

}

void main()

{

int n1,n2;

clrscr();

printf("Pointer:Swap elements using call by reference :\n");

printf("-----------------------------------------------\n");

printf("Input number 1 : ");

scanf("%d",&n1);

printf("Input number 2 : ");

scanf("%d",&n2);

printf(" The values before swapping are :\n");

printf(" number 1 = %d\n number 2 = %d \n",n1,n2);

swap(&n1,&n2);

printf(" The values after swapping are :\n");

printf(" number 1 = %d\n number 2 = %d\n",n1,n2);

getch();

}

OUTPUT

Pointer:Swap elements using call by reference

---------------------------------------------------------

Input number 1 : 10

Input number 2 : 20

The values before swapping are:

Number 1= 10 number 2 = 20

The values after swapping are:

Number1= 20 number 2 = 10

Page 41: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 41

Exercise 12:2. Write a program in C to count the number of vowels and consonants in a string

using a pointer.

#include <stdio.h>

#include <conio.h>

void main()

{

char str[50];

char *pt,ch;

int i,nv,nc;

clrscr();

printf("Input a string: ");

i=0;

do

{

ch=getchar();

str[i]=ch;

i++;

}while(ch!='\n');

i--;

str[i]='\0';

//assign address of str to pt

pt=str;

nv=0;

nc=0;

while(*pt!='\0')

{

putchar(*pt);

if(*pt=='A' || *pt=='E' || *pt=='I' || *pt=='O' || *pt=='U' ||

*pt=='a' || *pt=='e' || *pt=='i' || *pt=='o' || *pt=='u')

nv++;

else

nc++;

printf("Number of vowels : %d\n",nv);

printf("Number of consonants : %d\n",nc);

pt++; //pointer is increasing for searching the next character

}

printf("Number of vowels : %d\n",nv);

printf("Number of consonants : %d\n",nc);

getch();

}

OUTPUT: Pointer : Count the number of vowels and consonants :

Input a string: string

Number of vowels : 1

Number of consonants : 5

Page 42: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 42

Exercise 13:1. Write a program in C to show how a function returning pointer.

#include <stdio.h>

#include <conio.h>

int* findlarge(int *n1,int *n2)

{

if (*n1 > *n2)

return n1;

else

return n2;

}

void main()

{

int num1,num2;

int *result;

clrscr();

printf("Pointer : Show a function returning pointer :\n");

printf("------------------------------------------------------\n");

printf("Input the first number : ");

scanf("%d",&num1);

printf("Input the second number : ");

scanf("%d",&num2);

result=findlarge(&num1,&num2);

printf("The number %d is large\n",*result);

getch();

}

Output Ex1: Pointer : Show a function returning pointer :

-------------------------------------------------------

Input the first number : 5

Input the second number : 6

The number 6 is larger.

Output Ex2: Pointer : Show a function returning pointer :

-------------------------------------------------------

Input the first number : 78

Input the second number : 45

The number 78 is larger.

Page 43: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 43

Exercise 13:2. Write a C program to find sum of n elements entered by user. To perform this

program, allocate memory dynamically using malloc( ) function.

#include<stdio.h>

#include<stdlib.h>

#include <conio.h>

void main()

{

int n, i, *ptr, sum=0;

clrscr();

printf("Enter number of elements: ");

scanf("%d",&n);

ptr =(int*) malloc(n *sizeof(int));

// if memory cannot be allocated

if (ptr == NULL)

{

printf("Error! memory not allocated.");

exit(0);

}

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

{

printf("Enter element: ");

scanf("%d", ptr+i);

}

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

sum += *(ptr+i);

printf("Sum = %d", sum);

// deallocating the memory

free(ptr);

getch();

}

OUTPUT

Enter number of items : 5

Enter element 50

Enter element 25

Enter element 45

Enter element 34

Enter element 79

Sum = 233

Page 44: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 44

Exercise 14:1. Write a C program to find sum of n elements entered by user. To perform this

program, allocate memory dynamically using calloc( ) function. Understand the difference

between the above two programs

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

void main()

{

// This pointer will hold the

// base address of the block created

int *ptr;

int n,i,sum=0;

clrscr();

printf("Enter n : ");

scanf("%d",&n);

ptr = (int*)calloc(n, sizeof(int));

if(ptr == NULL)

{

printf("Memory not allocated.\n");

exit(0);

}

else

{

printf("Memory successfully allocated using calloc.\n");

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

{

printf("Enter number ");

scanf("%d",ptr+i);

}

printf("The elements of the array are: \n");

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

{

printf("%d\n", ptr[i]);

sum=sum+*(ptr+i);

}

printf("sum of array elements = %d\n",sum);

}

getch();

}de<

OUTPUT

Enter number of items : 5

Enter number 50

Enter number 25

Enter number 45

Enter number 34

Enter number 79

Sum = 233

Page 45: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 45

Exercise 14:2. Write a program in C to convert decimal number to binary number using the

function.

#include <stdio.h>

#include <conio.h>

long convert(int dno)

{

long bno=0,remainder,f=1;

while(dno != 0)

{

remainder = dno % 2;

bno = bno + remainder * f;

f = f * 10;

dno = dno / 2;

}

return bno;

}

void main()

{

long bno;

int dno;

clrscr();

printf("Function : convert decimal to binary :\n");

printf("----------------------------------------------\n");

printf("Input any decimal number : ");

scanf("%d",&dno);

bno = convert(dno);

printf("The Binary value is : %ld\n",bno);

getch();

}

OUTPUT Ex 1)

Function : convert decimal to binary :

----------------------------------------------

Input any decimal number : 65

The Binary value is : 1000001

OUTPUT Ex 2)

Function : convert decimal to binary :

----------------------------------------------

Input any decimal number : 15

The Binary value is : 1111

Page 46: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 46

Exercise 15:1. Write a program in C to check whether a number is a prime number or not using

the function.

#include <stdio.h>

#include <conio.h>

int isprime(int n)

{

int i,rem,prime;

prime=1; // assume number is prime

i=2;

while (i<n)

{

rem=n%i;

if (rem==0)

{

prime=0;

break;

}

i++;

}

return prime;

}

void main()

{

int num,res;

clrscr();

printf(" Input a positive number : ");

scanf("%d",&num);

res = isprime(num);

if(res==1)

printf("The number %d is a prime number.\n",num);

else

printf("The number %d is not a prime number.\n",num);

getch();

}

OUTPUT Ex 1)

Function : check whether a number is prime number or not :

-------------------------------------------------------------------------

Input a positive number : 5

The number 5 is a prime number.

OUTPUT Ex 2)

Function : check whether a number is prime number or not :

-------------------------------------------------------------------------

Input a positive number : 67

The number 67 is a prime number.

Page 47: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 47

Exercise 15:2. Write a program in C to get the largest element of an array using the function.

#include <stdio.h>

#include <conio.h>

#define MAX 100

int findbig(int a[],int m)

{

int i,large;

large=a[0];

i=1;

while(i < m)

{

if(a[i]>large)

large=a[i];

i++;

}

return large;

}

void main()

{

int arr[MAX],big,n,i;

printf("Function : Get largest element of an array :\n");

printf("-----------------------------------------------------\n");

printf("Enter n : ");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

{

printf("enter element : ");

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

}

big=findbig(arr,n);

printf("The largest element in the array is : %d\n",big);

getch();

}

OUTPUT

Function : Get largest element of an array :

-----------------------------------------------------

Enter n : 5

Input 5 elements in the array

enter element 45

enter element 55

enter element 72

enter element 31

enter element 43

The largest element in the array is 72

Page 48: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 48

Exercise 16:1. Write a program in C to append multiple lines at the end of a text file.

Assume that the content of the file test.txt is :

test line 1

test line 2

test line 3

test line 4

#include <stdio.h>

#include <conio.h>

void main()

{

FILE * fptr;

int i,n;

char str[100];

char fname[20];

char ch;

clrscr();

printf("Append multiple lines at the end of a text file :\n");

printf("-------------------------------------------------\n");

printf(" Input the file name to be opened : ");

scanf("%s",fname);

fptr = fopen(fname, "a");

printf(" Input the number of lines to be written : ");

scanf("%d", &n);

printf(" The lines are : \n");

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

{

fgets(str, sizeof str, stdin);

fputs(str, fptr);

}

fclose (fptr);

//----- Read the file after appended -------

fptr = fopen (fname, "r");

printf("The content of the file %s is :\n",fname);

ch = fgetc(fptr);

while (ch != EOF)

{

printf ("%c", ch);

ch = fgetc(fptr);

}

printf("\n\n");

fclose (fptr);

//------- End of reading ------------------

getch();

}

Page 49: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 49

OUTPUT

Append multiple lines at the end of a text file :

Input the file name to be opened : test.txt

Input the number of lines to be written : 3

The lines are :

test line 5

test line 6

test line 7

The content of the file test.txt is :

test line 1

test line 2

test line 3

test line 4

test line 5

test line 6

test line 7

Page 50: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 50

Exercise 16:2. Write a program in C to copy a file in another name. Assume that the content of the

file test.txt is :

test line 1

test line 2

test line 3

test line 4

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

void main()

{

FILE *fptr1, *fptr2;

char ch, fname1[20], fname2[20];

clrscr();

printf(" Copy a file in another name :\n");

printf("-----------------------------------------\n");

printf(" Input the name of source file : ");

scanf("%s",fname1);

fptr1=fopen(fname1,"r");

if(fptr1==NULL)

{

printf(" File does not found or error in opening.!!");

exit(1);

}

printf(" Input the name of destination file : ");

scanf("%s",fname2);

fptr2=fopen(fname2,"w");

if(fptr2==NULL)

{

printf(" File does not found or error in opening.!!");

fclose(fptr1);

exit(2);

}

while(1)

{

ch=fgetc(fptr1);

if (ch==EOF)

{

break;

}

else

{

fputc(ch, fptr2);

}

}

printf("The file %s copied successfully in the file %s\n",fname1,fname2);

fclose(fptr1);

fclose(fptr2);

getch();

}

OUTPUT Copy a file in another name :

-------------------------------------------------------------------

Input the source file name : test.txt

Input the new file name : test1.txt

The file test.txt copied successfully in the file test1.txt.

Page 51: Lab Manual for C - mpesguntur.commpesguntur.com/PDF/NOTES/CSE/C_LabManual.pdfogramming for Problem Solving using C Lab Manual (Integrated Campus: Engineering, MBA & B.Pharmacy) Road,

MPES Programming for Problem Solving using C Lab Manual Page 51

Exercise 16:3. Write a program in C to remove a file from the disk.

#include <stdio.h>

#include <conio.h>

void main()

{

int status;

char fname[20];

clrscr();

printf("\n\n Remove a file from the disk :\n");

printf("----------------------------------\n");

printf(" Input the name of file to delete : ");

scanf("%s",fname);

status=remove(fname);

if(status==0)

{

printf(" The file %s is deleted successfully..!!\n\n",fname);

}

else

{

printf(" Unable to delete file %s\n\n",fname);

}

getch();

}

OUTPUT

Remove a file from the disk :

----------------------------------------------------

Input the name of file to delete : test.txt

The file test.txt is deleted successfully..!!