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

Ppsuc Manual r20

This document contains information about the Electrical and Electronics Engineering department at Jawaharlal Nehru Technological University, including their vision, mission, program educational objectives, program outcomes, course outcomes, and syllabus for the Programming for Problem Solving Using C Lab Manual course for the first semester of the B.Tech program. The vision is to be a leader in research and technology development. The mission includes producing high-quality graduates, undertaking research, and creating social awareness. The course aims to develop C programming skills for problem solving and includes exercises on basic programming concepts.

Uploaded by

202 SAI KRISHNA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Ppsuc Manual r20

This document contains information about the Electrical and Electronics Engineering department at Jawaharlal Nehru Technological University, including their vision, mission, program educational objectives, program outcomes, course outcomes, and syllabus for the Programming for Problem Solving Using C Lab Manual course for the first semester of the B.Tech program. The vision is to be a leader in research and technology development. The mission includes producing high-quality graduates, undertaking research, and creating social awareness. The course aims to develop C programming skills for problem solving and includes exercises on basic programming concepts.

Uploaded by

202 SAI KRISHNA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY:: KAKINADA

UNIVERSITY COLLEGE OF ENGINEERING KAKINADA (AUTONOMOUS )

DEPARTMENT OF ELECTRICAL AND


ELECTRONICS ENGINEERING

PROGRAMMING FOR PROBLEM SOLVING


USING C LAB MANUAL

I B. Tech I Semester (R20)

HOD EEE FACULTY INCHARGE


DEPARTMENT OF ELECTRICAL AND ELECTRONICS
ENGINEERING

Vision of the Department: -

To be in the forefront in advanced research in emerging areas of Electrical & Electronics


Engineering, be proactive with industry in technology development and moulding the
department into a center of academic excellence.

Mission of the Department: -

M1. To produce high quality Electrical and Electronics Engineering graduates with
the requisite theoretical and practical knowledge.

M2. To undertake research & development and extension activities in the field of
Electrical and Electronics Engineering in the area of relevance for immediate
application as well as for establishing and strengthening the fundamental
knowledge.

M3. To create social awareness and ethical values in the graduates so as to contribute
in the progress of the society.

Program Educational Objectives (PEO’s):

Graduates of the program shall be

PEO 1 Provide necessary domain knowledge in the field of electrical and electronics
engineering to deal with their challenges solve engineering problems, pursue higher
education and research.
PEO 2 Able to apply theoretical and practical engineering knowledge
i) Towards innovation and entrepreneurship that caters the needs of industry and
society.
ii) To inculcate an attitude of lifelong learning.
PEO 3 Act as team member and team leader and work effectively to develop and design
multidisciplinary projects.
PEO 4 Able to acquire social responsibility and ethical values towards the growth and
development of society.
Program Outcomes (PO’s): -
The graduates of the program will, upon the, completion of program demonstrate
the abilityto -
PO1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
PO2 Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
PO3 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.
PO4 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.
PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
PO7 Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO9 Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend
and write effective reports and design documentation, make effective presentations, and
give and receive clear instructions.
PO11 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.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
Program Specific Outcomes (PSO’s)

PSO 1 Able to apply the knowledge gained from Mathematics, stochastic, foundational
computing and sciences to formulate practical problems of industry relevance with
research focus.
PSO 2 Able to apply technical knowledge and usage of modern hardware & software
tools related to Electrical & Electronics engineering for solving problems of social
relevance.
PSO 3 Able to analyze, comprehend, design, develop innovative applications for
demonstrating professional expertise, entrepreneurship and develop intellectual
property for nation building.
Mapping:
Mapping of PEO’s with Mission of the department:

Mission of the department


PEO’s
M1 M2 M3
PEO1 3 3 1
PEO2 3 3 2
PEO3 3 2 1
PEO4 2 1 3

Mapping of PO’s and PSO’s with PEO’s:

PEO1 PEO2 PEO3 PEO4


PO1 3 2 3 1
PO2 3 2 3 1
PO3 2 2 3 2
PO4 3 -- 2 --
PO5 3 2 2 --
PO6 -- 3 -- 3
PO7 -- 2 -- 3
PO8 -- -- -- 3
PO9 -- -- 3 --
PO10 2 2 3 2
PO11 1 3 3 --
PO12 -- 3 2 --
PSO1 3 2 2 1
PSO2 2 1 3 3
PSO3 2 3 3 2
COURSE OUTCOMES: -

After successful completion of this course, the students will be able to


demonstrate the ability to –

CO1 Gain Knowledge on various concepts of a C language.


CO2 Draw flowcharts and write algorithms.
CO3 Design and develop C problem solving skills.
CO4 Design and develop modular programming skills.
CO5 Trace and debug a program

Mapping of CO’s with PO’s: -

PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO PSO


1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
CO1 3 3 3 1 3 2 1 1 1 2 1 1
CO2 2 3 3 1 3 2 1 1 1 2 1 1
CO3 2 2 3 1 3 1 1 1 1 2 1 1
CO4 2 2 2 1 2 1 1 1 1 2 1 1
CO5 2 2 2 1 2 1 1 1 1 2 1 1
1: Weekly Related
2: Moderately Related
3: Strongly Related
Syllabus: -
Week CONTENTS Contact
Hours
1 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 7 inches and width of 5 inches.
3. Write a C program to display multiple variables

2 Exercise 2:
4. Write a C program to calculate the distance between the two points.
5. 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".
3 Exercise 3:
6. Write a C program to convert a string to a long integer.
7. Write a program in C which is a Menu-Driven Program to
compute the area of the various geometrical shape.
8. Write a C program to calculate the factorial of a given number.

4 Exercise 4:
9. Write a program in C to display the n terms of even natural
numbers and their sum.
10. 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.
11. Write a C program to check whether a given number is an
Armstrong number or not.
5 Exercise 5:
12. Write a program in C to print all unique elements in an array.
13. Write a program in C to separate odd and even integers in
separate arrays.
14. Write a program in C to sort elements of array in ascending order.
6 Exercise 6:
15. Write a program in C for multiplication of two squareMatrices.
16. Write a program in C to find transpose of a given matrix.
17. Write a program in C to find inverse of a given matrix.
7 Exercise 7:
18. Write a program in C to search an element in a row wise and
column wise sorted matrix.
19. Write a program in C to print individual characters of string in
reverse order.
8 Exercise 8:
20. Write a program in C to compare two strings without using
string library functions.
21. Write a program in C to copy one string to another string.
9 Exercise 9:
22. Write a C Program to Store Information Using Structures with
Dynamically Memory Allocation
23. Write a program in C to demonstrate how to handle the pointers in
the program.

10 Exercise 10:
24. Write a program in C to demonstrate the use of & (addressof) and
*(value at address) operator.
25. Write a program in C to add two numbers using pointers.

11 Exercise 11:
26. Write a program in C to add numbers using call by reference.
27. Write a program in C to find the largest element using Dynamic
Memory Allocation.
12 Exercise 12:
28. Write a program in C to swap elements using call byreference.
29. Write a program in C to count the number of vowels and consonants
in a string using a pointer.
13 Exercise 13:
30. Write a program in C to show how a function returning pointer.
31. Write a C program to find sum of n elements entered by user. To
perform this program, allocate memory dynamically using malloc( )
function.
14 Exercise 14:
32. 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
33. Write a program in C to convert decimal number to binary number
using the function.
15 Exercise 15:
34. Write a program in C to check whether a number is a prime
number or not using the function.
35. Write a program in C to get the largest element of an array using
the function.
16 Exercise 16:
36. Write a program in C to append multiple lines at the end of a text
file.
37. Write a program in C to copy a file in another name.
38. Write a program in C to remove a file from the disk.
17 Exercise 17:
39. Write a program to create a data file.
40. Write a program to calculate the resonant frequencyof an RLC series
circuit.
18 Exercise 18:
41. Write a program to solve sin x, cos x, tan x expressed in
exponential form where x is in radians – (i) 𝜋 (ii) 𝜋/6.
4
42. Write a program to draw a graph for y = mx + C where the
constants ’m’ and ‘C’ are defined and ‘x’ varies from 0 to 20at
intervals of 2 units.
43. Write a program to solve for ’x’, if 2 log x = 4 log 3.
Instructions to students: -
 Before entering the lab the student should carry the following things (MANDATORY)
 Identity card issued by the college.
 Class notes
 Lab observation book
 Lab Manual
 Lab Record
 Student must sign in and sign out in the register provided when attending the lab session
 without fail.
 Come to the laboratory in time. Students, who are late more than 15 min., will not be
 allowed to attend the lab.
 Students need to maintain 100% attendance in lab if not a strict action will be taken.
 All students must follow a Dress Code while in the laboratory
 Foods, drinks are NOT allowed.
 All bags must be left at the indicated place.
 Refer to the lab staff if you need any help in using the lab.
 Respect the laboratory and its other users.
 Workspace must be kept clean and tidy after experiment is completed.
 Read the Manual carefully before coming to the laboratory and be sure about what you
 are supposed to do.
 Do the experiments as per the instructions given in the manual.
 Copy all the programs to observation which are taught in class before attending the lab
 session.
 Students are not supposed to use floppy disks, pen drives without permission of lab- in
 charge.
 Lab records need to be submitted on or before the date of submission.
Introduction about lab
Steps involved in program development: -

To develop the program in high level language and translate it into machine level language
following steps have to be practiced.

1. Writing and editing the program.

2. Linking the program with the required library modules.

3. Compiling the program.

4. Executing the program.

Algorithm: -
It is a method of representing the step by step process for solving a problem. Each step is
called an instruction.

Characteristics of algorithm are:

 Finiteness: - It terminates with finite number of steps.


 Definiteness: - Each step of algorithm is exactly defined.
 Effectiveness: - All the operations used in the algorithm can be performed exactly in a
fixed duration of time.
 Input: - An algorithm must have an input before the execution of program begins.
 Output: - An algorithm has one or more outputs after the execution of the Program.

About C language
C is a programming language developed by Dennis Ritchie at AT&T‟s BELL Laboratory of
USA in 1972. Because of its reliability, C is very popular. C is highly portable & it is well
suited for structured programming. C program consists of collection of functions

History of C
The milestones in C's development as a language are listed below:

1. BCPL - a user friendly OS providing powerful development toolsmdeveloped from BCPL


c. 1967. Assembler was tedious, long and error prone.

2. A new language “B” was attempted c. 1970 by Ken Thompson at Bell Labs.

3. UNIX was developed c. 1970 using “B” - DEC PDP-7 Assembly Language.

4. A totally new language “C”, a successor to “B” was developed c. 1971.

5. By 1973, UNIX OS was almost totally written in “C”.


Hardware Requirement: Desktop Computer / laptop computer.

Software Requirement: Linux Operating System with GCC / TURBO C in WINDOWS OS /


TURBO C++ in WINDOWS OS.

GCC: -
GCC is a Linux-based C compiler released by the Free Software Foundation which is usually
operated via the command line. It often comes distributed freely with a Linux installation, so
if you are running UNIX or a Linux variant you will probably have it on your system. You
can invoke GCC on a source code file

simply by typing: -

gcc filename

The default executable output of GCC is "a.out", which can be run by typing “./a.out”. It is
also possible to specify a name for the executable file at the command line by using the syntax
“ -o outputfile” , as shown in the following

example : -

gcc filename -o outputfile

Again, you can run your program with "./outputfile". (The ./ is there to ensure you run the
program for the current working directory.)

Note: If you need to use functions from the math library (generally functions from “math.h”
such as sin or sqrt), then you need to explicitly ask it to link with that library with the “ –l ”
flag and the library “m”:

gcc filename -o outputfile -lm

Turbo C/C++
Open Turbo C/C++ from your Desktop or Programs menu. Select “File” from Menu bar and
select option “New” and Save C program with filename “.C” extension.

To do compiling – Select -> Compile from menu and click-> compile.

If the compilation is successful – you will see a “success” message. Else you will

see the number of errors.

To RUN the program – you may select ->Run from menu and click -> Run

Now you will see the output screen.


Structure of C program :
C program is a collection of several instructions where each instruction is written as a separate
statement. The C program starts with a main function followed by the opening braces which
indicates the start of the function. Then follows the variable and constant declarations which
are followed by the statements that include input and output statements.

C program may contain one or more sections as shown below:

Documentation section

Link section

Definition section

Global declaration section

Main() Function section

Declaration part

Executable part

SUBPROGRAM SECTION

User defined functions

Keywords: -
C has 32 keywords (reserved words with special meaning):

1. auto, 2. break, 3. case, 4. char, 5. const, 6. continue, 7. default, 8. do, 9. double, 10. else,

11. enum, 12. extern, 13. float, 14. for, 15. goto, 16. if, 17. int, 18. long, 19. register,

20. return, 21. short, 22. signed, 23. sizeof, 24. static, 25. struct, 26. switch, 27. typedef,

28. union, 29. unsigned, 30. void, 31. volatile, and 32. while.
Operators: -
C supports a rich set of operators, which are symbols used within an expression to specify the
manipulations to be performed while evaluating that expression. C has the following
operators:

arithmetic: +, -, *, /, %

assignment: =

augmented assignment: +=, - =, *=, /=, %=, &=, |=, ^=, <<=, >>=

bitwise logic: ~, &, |, ^

bitwise shifts: <<, >>

boolean logic: !, &&, ||

conditional evaluation: ? :

equality testing: = =, !=

calling functions: ( )

increment and decrement: ++ and - -

member selection: ., ->

object size: sizeof

order relations: <, <=, >, >=

reference and dereference: &, *, [ ]

sequencing: ,

subexpression grouping: ( )

type conversion: (typename)


Salient features of C: -
 C language has some characteristics that define the language and also have led to its
 popularity as a programming language.
 Small size.
 Extensive use of function calls.
 Structured language.
 Low level (Bitwise) programming readily available.
 Pointer implementation - extensive use of pointers for memory, array,
 structures and functions.
 It has high-level constructs.
 It can handle low-level activities.
 It produces efficient programs.
 It can be compiled on a variety of computers.
EXPERIMENT NO: 1

PRINT BLOCK ‘F’ USING HASH (#)

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

AIM: To print a block F using hash (#), where the F has a height of six characters and width of
five and four characters.

APPARATUS: 1. PERSONAL COMPUTER

2. TURBO C

ALGORITHM: FLOWCHART:

Step 1: START

Step 2: printf("######\n")

Step 3: printf("#\n")

Step 4: printf("#\n")

Step 5: printf("#####\n")

Step 6: printf("#\n")

Step 7: printf("#\n")

Step 8: printf("#\n")

Step 9: STOP

PROGRAM:

#include <stdio.h>
#include<conio.h>
void main ()
{
printf("######\n");
printf("#\n");
printf("#\n");
printf("#####\n");
printf("#\n");
printf("#\n");
printf("#\n");
getch();
}
EXPERIMENT NO: 1

FIND AREA AND PERIMETER OF RECTANGLE

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


height of 7 inches and width of 5 inches.

AIM: To Write a C program to compute the perimeter and area of a rectangle with a height of 7
inches and width of 5 inches

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize int width and height as 5 inches, 7 inches, area, and perimeter

Step 3: Compute area and perimeter as


Area of rectangle = height*width
Perimeter of rectangle =2*(height + width)

Step 4: Print perimeter and area of rectangle using printf function

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
/* height and width of a rectangle in inches */
int width;
int height;
int area;
int perimeter;
void main()
{
height = 7;
width = 5;
clrscr();
perimeter = 2*(height + width);
printf("Perimeter of the rectangle = %d inches\n", perimeter);
area = height * width;
printf("Area of the rectangle = %d square inches\n", area);
getch();
}
EXPERIMENT NO: 1

DISPLAY MULTIPLE VARIABLES

C. Write a C program to display multiple variables

AIM: To write a C program to display multiple variables

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART

Step 1: Start

Step 2: Initialize 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;

Step 3: Print the following using print function

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


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

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


printf("((int) dx) + ax = %ld\n", ((int) dx) + ax);
printf("a + x = %f\n", a + x);
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("ax + ux = %lu\n", ax + ux);
Step 4: Stop
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;
printf("a + c = %d\n", a + c);
printf("x + c = %f\n", x + c);
printf("dx + x = %f\n", dx + x);
printf("((int) dx) + ax = %ld\n", ((int) dx) + ax);
printf("a + x = %f\n", a + x);
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("ax + ux = %lu\n", ax + ux);
getch();
}
EXPERIMENT NO: 2

DISTANCE BETWEEN 2 POINTS

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

AIM: To write a C program to calculate the distance between the two points.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Initialize float x1, y1, x2, y2, gdistance;

Step 3: Enter the values using scanf function

Step 4: Compute distance =

Step 5: Print distance

Step 6: Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float x1, y1, x2, y2, gdistance;
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);
gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(gdistance));
printf("\n");
getch();
}
EXPERIMENT NO: 2

ACCEPTS 4 INTEGERS AND CHECK VARIOUS CONDITIONS

B. 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".

AIM: To write a c program such that it accepts 4 integers and prints correct values if they are
according to given conditions else prints wrong values

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Initialize int p, q, r, s variables

Step 3: Enter the values of the listed


variables using scanf function

Step 4: check the conditions 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.

Step 5: If conditions are satisfied then


print correct values. Else print wrong
values.

Step 6: Stop
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int p, q, r, s;
clrscr();
printf("\nInput the first integer: ");
scanf("%d", &p);
printf("\nInput the second integer: ");
scanf("%d", &q);
printf("\nInput the third integer: ");
scanf("%d", &r);
printf("\nInput the fourth integer: ");
scanf("%d", &s);
if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2 == 0))
{
printf("\nCorrect values\n");
}
else {
printf("\nWrong values\n");
}
getch();
}
EXPERIMENT NO: 3

CONVERT A STRING TO A LONG INTEGER

A. Write a C program to convert a string to a long integer

AIM: To write a C program to convert a string to a long integer

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Enter a string

Step 3: Convert a string to a long integer

Step 4: Display output

Step 5: Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
char inputString[20];
clrscr();
printf("Enter a String for long Integer conversion\n");
gets(inputString);
printf("Integer: %ld \n", atoi(inputString));
getch();
}
EXPERIMENT NO: 3

A MENU-DRIVEN PROGRAM TO COMPUTE THE AREA OF THE VARIOUS


GEOMETRICAL SHAPES.

B. Write a program in C which is a Menu-Driven Program to compute the


area of the various geometrical shapes.

AIM: To write a program in C which is a Menu-Driven Program to compute the area of the
various geometrical shape

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Initialize int r, l, w, b, h and area


type float

Step 3: Take the inputs through scanf


function such that which area is to be
computed for ex:
1. area of circle
2. area of rectangle
3. area of triangle
Step 4:
Case 1:
Print input radius of circle
Take input using scanf function
Compute area =3.14*r*r

Case 2:
Print input length and width of rectangle
Take input using scanf function
Compute area =l*w

Case 3:
Print input base and height of triangle
Take input using scanf function
Compute area =0.5*b*h

Step 5: Print the area

Step 6: Stop
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <string.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 radious 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 hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %f\n",area);
getch();
}
EXPERIMENT NO: 3

CALCULATE THE FACTORIAL OF A GIVEN NUMBER.

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

AIM: To write a C program to calculate the factorial of a given number.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Initialize int i, f=1, num

Step 3: Enter your number using scanf


function

Step 4: Using a for loop


for(i=1;i<=num;i++)
until i=num; f=f*i;

Step 5: Print factorial

Step 6: Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
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();
}
EXPERIMENT NO: 4

DISPLAY N TERMS OF EVEN NATURAL NUMBERS AND THEIR SUM

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


their sum

AIM: To write a program in C to display the n terms of even natural number and their sum

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Initialize integer variables i, n, sum=0

Step 3: Read number of terms

Step 4: Print the the even numbers are 2*i


using for loop and calculate the sum.

Step 5: Print sum of even natural


numbers upto required number of terms

Step 6: Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
void main() {
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 Number upto %d terms : %d \n",n,sum);
getch();
}
EXPERIMENT NO: 4

DISPLAY THE N TERMS OF HARMONIC SERIES AND THEIR SUM.

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

AIM: To 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.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: START

Step 2: Read the value of n and initialize sum.

Step 3: Using for loop display the harmonic series and calculate sum.

Step 4: Print harmonic series and their sum.

Step 5: STOP

FLOWCHART:
PROGRAM:

#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\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
getch();
}
EXPERIMENT NO: 4

CHECK WHETHER A GIVEN NUMBER IS AN ARMSTRONG NUMBER OR NOT.

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


number or not.

AIM: To write a C program to check whether a given number is an Armstrong number or not.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Read r, n, sum, temp=n.

Step 3: Initialize sum=0,temp=n.

Step 4: Using while loop, find r, sum, n.

Step 5: Repeat for loop upto num!=0

Step 6: Check if temp== sum then print


the number is Armstrong
number. Else print the number
is not an Armstrong number
Step 7: Stop.
PROGRAM:

#include <stdio.h>
#include <conio.h>
int main()
{
int r, n, temp, sum=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
temp=n;
while(n1!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(temp==sum)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}
EXPERIMENT NO: 5

PRINT ALL UNIQUE ELEMENTS IN AN ARRAY

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

AIM: To write a program in C to print all unique elements in an array

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Read the size and elements of array

Step 3: Using for loop find the unique elements


and update the counter
Step 4: If counter is zero then print elements of array.

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main() {
int arr1[100], n,ctr=0;
int i, j, k;
clrscr();
printf("\n\nPrint all unique elements of an array:\n");
printf("------------------------------------------\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\nThe unique elements found in the array are: \n");
for(i=0; i<n; i++)
{
ctr=0;
for(j=0,k=n; j<k+1; j++)
{
/*Increment the counter when the search value is duplicate.*/
if (i!=j)
{
if(arr1[i]==arr1[j])
{
ctr++;
}
}
}
if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
getch();
}
EXPERIMENT NO: 5

SEPARATE ODD AND EVEN INTEGERS IN SEPARATE ARRAYS.

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


arrays.

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

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int arr1[10], arr2[10], arr3[10]; int i, j=0, k=0, n;

Step 3: Initialize a for loop to enter elements in arr1

Step 4: If element in arr1 is divisible by 2 then store the element in arr2.


Else store the element in arr3
Step 5: If even numbers are to be displayed then print arr2.
If odd numbers are to be displayed then print arr3 using for loop.
Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
clrscr();
printf("\n\nSeparate odd and even integers in separate arrays:\n");
printf("------------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
getch();

}
EXPERIMENT NO: 5

SORT ELEMENTS OF ARRAY IN ASCENDING ORDER

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

AIM: To write a program in C to sort elements of array in ascending order

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Read size and elements of an array.

Step 3: Using for loop i from 0 to n-1,by using one more for loop j in j from i+1 to n to sort.

Step 4: Check if (a[i]>a[j]) then swap a[i],a[j].

Step 5: Print the final array

Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int arr1[100];
int n, i, j, temp;
clrscr();
printf("\n\nsort elements of array in ascending order :\n ");
printf("----------------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
getch();
}
EXPERIMENT NO: 6

MULTIPLICATION OF TWO SQUARE MATRICES

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

AIM: To write a program in C for multiplication of two square Matrices

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize three matrices and required variables.


Step 3: Read order and enter elements of two matrices using for loop.

Step 4: Multiply the first row of first matrix and first column of second matrix

Step 5: Store the value of multiplication in a variable called sum

Step 6: Print the sum matrix

Step 7: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main() {
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
clrscr() ;
printf("\n\nMultiplication of two Matrices :\n");
printf("----------------------------------\n");
printf("\nInput the rows and columns of first matrix : ");
scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
{
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}

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


for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
getch();
}
EXPERIMENT NO: 6(B)

TRANSPOSE OF A GIVEN MATRIX

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

AIM: To write a program in C to find transpose of a given matrix

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize int arr1[50][50], brr1[50][50], i, j, r, c;


Step 3: Enter the values of r and c and enter the elements in arr1 using scanf function

Step 4: Store the elements of arr1 such that brr1[j][i]=arr1[i][j] that is in reverse manner using a
for loop

Step 5: Print the brr1 which is the required transpose of matrix

Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
main()
{
int arr1[50][50],brr1[50][50],i,j,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 first matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("\nThe matrix is :\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d\t",arr1[i][j]);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
}
}
printf("\n\nThe transpose of a matrix is : ");
for(i=0;i<c;i++){
printf("\n");
for(j=0;j<r;j++){
printf("%d\t",brr1[i][j]);
}
}
printf("\n\n");
getch();
}
EXPERIMENT NO: 6

INVERSE OF A GIVEN MATRIX

C. Write a program in C to find inverse of a given matrix

AIM: To write a program in C to find inverse of a given matrix

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize float a[25][25] ,k, d and int i ,j

Step 3: Enter k and elements of matrix using scanf function

Step 4: Calculate determinant and cofactor matrix and print inverse of matrix

Step 5: STOP

FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] -
a[1][(i+2)%3]*a[2][(i+1)%3]));
printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) -
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}
return 0;
}

SAMPLE OUTPUT:
EXPERIMENT NO: 7

SEARCH AN ELEMENT IN A ROW WISE AND COLUMN WISE SORTED MATRIX

A. Write a program in C to search an element in a row wise and column wise


sorted matrix

AIM: To write a program in C to search an element in a row wise and column wise sorted matrix

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Enter no of rows and columns and read them.

Step 3: Using for loop, read elements of matrix.

Step 4: Print elements in matrix using for loop.

Step 5: Read element to be find.

Step 6: If (mat[i][j]==ele) print location of element

Step 7: Else print element not found

Step 8: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
int searchElement(int arr2D[4][4], int n, int x)
{
int i = 0, j = n-1;
clrscr();
while ( i < n && j >= 0 )
{
if ( arr2D[i][j] == x )
{
printf("\nThe element Found at the position in the matrix is: %d, %d", i, j);
return 1;
}
if ( arr2D[i][j] < x )
j--;
else
i++;
}
printf("\nThe given element not found in the 2D array.");
return 0;
}
int main()
{
int arr2D[4][4] = { {15, 23, 31, 39},
{18, 26, 36, 43},
{25, 28, 37, 48},
{30, 34, 39, 50},
};
int i,j,v;
v=37;
//------------- print original array ------------------
printf("The given array in matrix form is : \n");
for(i = 0; i < 4; i++)
{
for (j=0;j<4;j++)
{
printf("%d ", arr2D[i][j]);
}
printf("\n");
}
//------------------------------------------------------
printf("The given value for searching
is:%d",v);
searchElement(arr2D, 4, v);
getch();

}
EXPERIMENT NO: 7

PRINT INDIVIDUAL CHARACTERS OF STRING IN REVERSE ORDER.

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


order.

AIM: To write a program in C to print individual characters of string in reverse order.

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize a string variable str[100] and integer variables l and i

Step 3: Get the length of string using stlen function

Step 4: Using for loop get individual characters of string in reverse order.

Step 5: Prints the string in the reverse order

Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include<string.h>
void main() {
char str[100]; /* Declares a string of size 100 */
int l,i;
clrscr();
printf("\n\nPrint individual characters of string in reverse order :\n");
printf("------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are : \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
getch();

OUTPUT:
EXPERIMENT NO: 8

COMPARE TWO STRINGS WITHOUT USING STRING LIBRARY FUNCTIONS

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


functions
C.
AIM: To write a program in C to compare two strings without using string library functions

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Read the two strings.

Step 3: Using while loop compare both strings.

Step 4: If string1 is equal to string 2 print both are equal

Step 5: Else if string1 greater than string2 print string1 is greater.

Step 6: Else string2 is greater

Step7: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int p, q, r;
int i = 0, j = 0;
char str1[1000];
char str2[1000];
clrscr();
printf("Enter the 1st string which is to be compared with 2nd string\n");
scanf("%s", &str1);
printf("Enter the 2nd string which is to be compared with 1st string\n");
scanf("%s", &str2);
printf("The first string is %s\n", str1);
printf("The second string is %s\n", str2);
while (str1[i] != '\0')
{
p++;
i++;
}
printf("The length of str1 is %d\n", p);
while (str2[j] != '\0')
{
q++;
j++;
}
printf("The length of str2 is %d\n", q);
if (p == q)
{
for (i = 0, j = 0; i < p, j < q; i++, j++)
{
if (str1[i] == str2[j])
{
r++;
}
}
printf("The common length of two strings is %d\n",r);
if (p != r || p > r || p < r || q > r || q < r || q != r)
{
printf("The given two strings %s and %s are not equal\n", str1, str2);
}
}
if (p == r && q == r)
{
printf("The given two strings %s and %s are equal\n", str1, str2);
}
getch();
}
EXPERIMENT NO: 8

COPY ONE STRING TO ANOTHER STRING

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

AIM: To write a program in C to copy one string to another string

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize 2 strings str1 and str2

Step 3: Read the two strings using scanf function

Step 4: Using strcpy(“target”, “source”) and print the string using printf function

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[1000];
char str2[1000];
clrscr();
printf("Enter the first string\n");
scanf("%s",&str1);
printf("Enter the second string\n");
scanf("%s",&str2);
printf("The string 1 is %s\n",str1);
printf("The string 2 is %s\n",str2);
printf("The first string after copying second string to first string is %s\n", strcpy(str1,str2));
getch();
}

SAMPLE OUTPUT
EXPERIMENT NO: 9

STORE INFORMATION USING STRUCTURES WITH DYNAMICALLY MEMORY


ALLOCATION

A. Write a C Program to Store Information Using Structures with


Dynamically Memory Allocation

AIM: To write a C Program to Store Information Using Structures with Dynamically Memory
Allocation

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize structure data type


struct course {
int marks;
char subject[30];
};
Step 3: Read the information through pointer and store in an array.

Step 4: Create the dynamic memory allocation using ptr = (struct course *)malloc(no of
Records * sizeof(struct course));

Step 5: Print the stored information.

Step 6: Stop
FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
struct course {
int marks;
char subject[30];
};
void main() {
struct course *ptr;
int noOfRecords;
clrscr();
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);

// Memory allocation for noOfRecords structures


ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (int i = 0; i < noOfRecords; ++i) {
printf("Enter subject and marks:\n");
scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
}

printf("Displaying Information:\n");
for (int i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
}
free(ptr);
getch();
}

SAMPLE OUTPUT:

Enter the number of records: 2


Enter subject and marks:
Science 82
Enter subject and marks:
DSA 73

Displaying Information:
Science 82
DSA 73
EXPERIMENT NO: 9

DEMONSTRATE HOW TO HANDLE THE POINTERS IN THE PROGRAM

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


program

AIM: To write a program in C to demonstrate how to handle the pointers in the program

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int m and *ab

Step 3: Case 1 store ab=&m print address and value stored in the ab

Case 2 store *ab=7 print address and value of ab

Case 3 store m=7 and print address and value of m

Step 4: Print all the values using print function

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int* ab;
int m;
m=29;
clrscr();
printf("\n\n Pointer : How to handle the pointers in the program :\n");
printf("------------------------------------------------------------\n");
printf(" Here in the declaration ab = int pointer, int m= 29\n\n");
printf(" Address of m : %p\n",&m);
printf(" Value of m : %d\n\n",m);
ab=&m;
printf(" Now ab is assigned with the address of m.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
m=34;
printf(" The value of m assigned to 34 now.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
*ab=7;
printf(" The pointer variable ab is assigned the value 7 now.\n");
printf(" Address of m : %p\n",&m);//as ab contain the address of m
//so *ab changed the value of m and now m become 7
printf(" Value of m : %d\n\n",m);
getch();
}
EXPERIMENT NO: 10

DEMONSTRATE THE USE OF & (ADDRESS OF) AND *(VALUE AT ADDRESS)


OPERATOR

A. Write a program in C to demonstrate the use of & (address of) and


*(value at address) operator

AIM: To write a program in C to demonstrate the use of & (address of) and *(value at address)
operator

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int m=30 0 and float fx=300.60 char ch =z

Step 3: Initialize 3 pointers int*pt1, float*pt2, char*pt3 and pt1=&m, pt2=&fx and pt3=&cht

Step 4: Print address, value of the operators with different combinations of & and *

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int m=300;
float fx = 300.60;
char cht = 'z';
printf("\n\n Pointer : Demonstrate the use of & and * operator :\n");
printf("--------------------------------------------------------\n");
int *pt1;
float *pt2;
char *pt3;
pt1= &m;
pt2=&fx;
pt3=&cht;
printf ( " m = %d\n",m);
printf ( " fx = %f\n",fx);
printf ( " cht = %c\n",cht);
printf("\n Using & operator :\n");
printf("-----------------------\n");
printf ( " address of m = %p\n",&m);
printf ( " address of fx = %p\n",&fx);
printf ( " address of cht = %p\n",&cht);
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 cht = %c\n",*(&cht));
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 cht = %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 cht= %c\n\n",*pt3);
getch();
}
EXPERIMENT NO: 10

ADD TWO NUMBERS USING POINTERS

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

AIM: To write a program in C to add two numbers using pointers.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Declare a function with name add with 2 pointer variables int*i and int *j

Step 3: Initialize variables int a, b, *i, *j;


int addition; i=&a; j=&b;
Step 4: Enter values of a and b using scanf function and pass them to add function created and
store the value in addition variable.

Step 5: Print the addition of two pointers.

Step 6: Stop

FLOWCHART:
PROGRAM:

#include<stdio.h>
int add(int *i,int *j);
int main(){
int a,b,*i,*j;
int addition;
clrscr();
i=&a;
j=&b;
printf("Enter the two numbers which are to be added using pointers\n");
scanf("%d%d",&a,&b);
add(&a,&b);
addition = add(&a,&b);
printf("The addition of given two numbers %d and %d is %d which is caluclated using
pointers\n",a,b,addition);
getch();
}
int add(int *i,int *j){
return *i+*j; }

OUTPUT:
EXPERIMENT NO: 11

ADD NUMBERS USING CALL BY REFERENCE

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

AIM: To write a program in C to add numbers using call by reference

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

Step 2: Initialize long fno, sno, sum

Step 3: Enter numbers using scanf function

Step 4: Pass the arguments to addTwoNumbers as address and receive them as pointer variables.

Step 5: Return the addition of those two pointer variables and print that sum

Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
long fno, sno, sum;
clrscr();
printf("\n\n Pointer : Add two numbers using call by reference:\n");
printf("-------------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%ld", &fno);
printf(" Input the second number : ");
scanf("%ld", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %ld and %ld is %ld\n\n", fno, sno, sum);
getch();
}
long addTwoNumbers(long *n1, long *n2)
{
long sum;
sum = *n1 + *n2;
return sum;
}
EXPERIMENT NO: 11

FIND THE LARGEST ELEMENT USING DYNAMIC MEMORY ALLOCATION

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


Allocation

AIM: To program in C to find the largest element using Dynamic Memory Allocation

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int i, n float *element

Step 3: Input total number of elements using scanf function to allocate size for those elements

Step 4: Element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements

Step 5: If element == NULL then print a statement that no memory is allocated

Step 6: enter the elements using a for loop and a scanf function

Step 7: Initialze a for loop to check the largest element entered


for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
Step 7: print the largest element

STEP 8: Stop
FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main() {
int i,n;
float *element;
clrscr();
printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input total number of elements(1 to 100): ");
scanf("%d",&n);
element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements
if(element==NULL)
{
printf(" No memory is allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)
{
printf(" Number %d: ",i+1);
scanf("%f",element+i);
}
for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
printf(" The Largest element is : %.2f \n\n",*element);
getch();
}
EXPERIMENT NO: 12

SWAP ELEMENTS USING CALL BY REFERENCE

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

AIM: To write a program in C to swap elements using call by reference

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Declare a function with name swap, initialize a, b

Step 3: Read elements using scanf function print the value before swapping

Step 4: Pass the value to swap function where


temp=*x;
*x=*y;
*y=temp; print value after swapping
Step 5: Stop

FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<conio.h>
int swap(int *x,int *y);
int main()
{
int a,b;
clrscr();
printf("Enter the values of 'a' and 'b' :\n");
scanf("%d%d",&a,&b);
printf("The values of 'a' and 'b' before swapping is %d and %d\n",a,b);
swap(&a,&b);
}
int swap(int *x , int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("The value of 'a' and 'b' after swapping is %d and %d",*x,*y);
getch();
}

OUTPUT:
EXPERIMENT NO: 12

COUNT THE NUMBER OF VOWELS AND CONSONANTS IN A STRING USING A


POINTER

B. Write a program in C to count the number of vowels and consonants in a string


using a pointer

AIM: To write a program in C to count the number of vowels and consonants in a string using a
pointer

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Create 2 functions such that they count no of vowels and no of consonants

Step 3: Enter the string using scanf function

Step 4: Pass that string to the strvowel and strconsonant functions created

Step 6: Enter the ascii values of the vowels and if the ascii values are matched then increase the
count of vowels and print the vowel and it’s position

Step 7: Stop
FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <string.h>
void strvowel(char *ptr);
void strconsonant(char *ptr);
int main()
{
char name[100];
clrscr();
printf("Enter the string in which vowels and consonats are to be counted\n");
scanf("%s", &name);
printf("The entered string is %s\n", name);
strvowel(name);
strconsonant(name);
return 0;
}
void strvowel(char *ptr)
{
int x;
int i;
int countvowel = 0;
for (i = 0; i < strlen(ptr); i++)
{
x = (int)(ptr[i]);/*conversion of string to integers which are equal to ascii value*/
if (64 < x < 123 && (x == 65 || x == 69 || x == 73 || x == 79 || x == 85 || x == 97 || x == 101
|| x == 105 || x == 111 || x == 117))
{
countvowel++;
printf("The vowel--%d is %c\n",i+1,ptr[i]);
}
}
if(countvowel==strlen(ptr)){
printf("There are no consonants in the given string\n");
}
printf("The number of vowels is %d\n", countvowel);
}
void strconsonant(char *ptr)
{
int x;
int i;
int countconsonant = 0;
for (i = 0; i < strlen(ptr); i++)
{
x = (int)(ptr[i]);/*conversion of string to integers which are equal to ascii value*/
if (64 < x < 123 && x != 65 && x != 69 && x != 73 && x != 79&& x != 85 && x != 97
&& x != 101 && x != 105 && x != 111 && x != 117)
{
countconsonant++;
printf("The consonant--%d is %c\n",i+1,ptr[i]);
}

}
if(countconsonant==strlen(ptr)){
printf("There are no vowels in the given string\n");
}
!
printf("The number of consonants is %d\n", countconsonant);
}

OUTPUT:
EXPERIMENT NO: 13

SHOW HOW A FUNCTION RETURNING POINTER

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

AIM: To write a program in C to show how a function returning pointer

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

STEP 2: Initialize int numa=0; int numb=0; int *result

STEP 3: Enter the numbers using scanf function

STEP 4: Pass those numbers to the function int * find Larger() and check the condition
if(*n1 > *n2)
return n1;
else
return n2;
Step 5: Print the result

STEP 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include<conio.h>
int* findLarger(int*, int*);
void main()
{
int numa=0;
int numb=0;
int *result;
clrscr();
printf("\n\n Pointer : Show a function returning pointer :\n");
printf("--------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%d", &numa);
printf(" Input the second number : ");
scanf("%d", &numb);
result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
getch();
}
int* findLarger(int *n1, int *n2)
{
if(*n1 > *n2)
return n1;
else
return n2;
}
EXPERIMENT NO: 13

FIND SUM OF N ELEMENTS ENTERED BY USER USING MALLOC() FUNCTION

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


program, allocate memory dynamically using malloc() function

AIM: To write C program to find sum of n elements entered by user. To perform this program,
allocate memory dynamically using malloc( ) function.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int num, i, *ptr, sum=0

Step 3: Allocate memory using malloc() function ptr= (int*) malloc (num*sizeof(int));

Step 4: Enter elements and while entering the numbers add them and store them into sum.

STEP 5: Print the sum

STEP 6: Stop

FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int num, i, *ptr, sum=0;
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &num);
// memory allocated using malloc()
ptr= (int*) malloc (num*sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.");
return EXIT_FAILURE;
}
printf("Enter elements :\n");
for(i=0;i<num;i++)
{
scanf("%d",ptr+i);
sum += *(ptr+i);
}
printf("Sum= %d",sum);
free(ptr);
getch();
}

OUTPUT:
EXPERIMENT NO: 14

FIND SUM OF N ELEMENTS ENTERED BY USER USING CALLOC() FUNCTION

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

AIM: To 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

APPARATUS: 1. Personal Computer

2. Turbo C
ALGORITHM:

Step 1: Start

STEP 2: Initialize int n, i, *ptr, sum=0;

Step 3: Enter the number of elements using scanf function

Step 4: Ptr=(int*)calloc(n,sizeof(int));
Store the numbers using a for loop and enter the values using scanf function

Step 5: For every run add the numbers to sum and store the sum value in the sum variable.

Step 6: print sum

Step 7: Stop

FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
getch();
}

Calloc() : The calloc() function allocates multiple block of requested memory. It initially
initialize all bytes to zero.

Malloc() : The malloc() function allocates single block of requested memory. It doesn't initialize
memory at execution time, so it has garbage value initially.

OUTPUT:
EXPERIMENT NO: 14

TO CONVERT DECIMAL NUMBER TO BINARY NUMBER USING THE FUNCTION

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


function

AIM: to write a program in C to convert decimal number to binary number using the function.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Divide the number by through % operator and store the remainder in array

Step 3: Divide the number by 2 through division operator

Step 4: Repeat the step 2 until number is greater than 0

Step 5: Print the binary no.

Step 6: Stop

FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main(){
int a[10],n,i;
clrscr();
system ("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
getch();
}

OUTPUT:
EXPERIMENT NO: 15

CHECK WHETHER A NUMBER IS A PRIME NUMBER OR NOT USING THE


FUNCTION

A. Write a program in C to check whether a number is a prime number or not using


the function

AIM: To write a program in C to check whether a number is a prime number or not using the
function

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Declare a function and initialize int num, res=0, read a number.
Step 3: Call a function for prime no.

Step 4: Prime function checks the condition, and return a value.

Step 5: If result is zero print prime no, Else print not a prime no.

Step 6: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int num,res=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
res=prime(num);
if(res==0)
printf("\n%d IS A PRIME NUMBER",num);
else
printf("\n%d IS NOT A PRIME NUMBER",num);
getch();
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else return 1;
}
return 0;
}

OUTPUT:
EXPERIMENT NO: 15

GET THE LARGEST ELEMENT OF AN ARRAY USING THE FUNCTION

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

AIM: To write a program in C to get the largest element of an array using the function

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Enter the size of array and using a for loop enter the elements into the array using scanf
function

Step 3: Check the larger number using findLarge function created

Step 4: Using for loop find the largest value and receive largest no from function.

Step 4: Print the largest element

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
#include<conio.h>
int findLarge(int[],int);
int main()
{
int a[100],a_size,L_Num,i;
clrscr();
printf(“Enter size of array: “);
scanf(“%d”,&a_size);
printf(“Enter the elements into array:\n”);
for(i=0;i<a_size;i++)
scanf(“%d”,&a[i]);
L_Num=findLarge(a,a_size);
printf(“Largest number is: %d\n”,L_Num);
getch();
}
int findLarge(int a[],int a_size)
{
int i,Large_num;
Large_num=a[0];
for(i=1;i<a_size;i++)
{
if(Large_num<a[i])
Large_num=a[i];
}
return Large_num;
}

OUTPUT:
EXPERIMENT NO: 16

APPEND MULTIPLE LINES AT THE END OF A TEXT FILE

A. Write a program in c to append multiple lines at the end of a text file

AIM: To write a program in c to append multiple lines at the end of a text file

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize FILE * fptr;


int i,n;
char str[100];
char fname[20];
char str1;
STEP 3: Open the required file and start appending the lines by entering the no of lines to be
appended and enter the lines using a scanf function

Step 4: Close the file after operations are done

Step 5: Stop
FLOWCHART:
PROGRAM:

#include <stdio.h>
#include<conio.h>
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20];
char str1;
clrscr();
printf("\n\n Append multiple lines at the end of a text file :\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("\n The content of the file %s is :\n",fname);
str1 = fgetc(fptr);
while (str1 != EOF)
{
printf ("%c", str1); OUTPUT:
str1 = fgetc(fptr);
}
printf("\n\n");
fclose (fptr);
getch();
}
EXPERIMENT NO: 16

COPY A FILE IN ANOTHER NAME

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

AIM: To write a program in C to copy a file in another name

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

STEP 2: Open the file using fopen function

STEP 3: Using getc function get the contents of the file opened

STEP 4: Open the second file to which the contents are to be copied and copy them

STEP 5: Stop
FLOWCHART:
PROGRAM:

#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
printf("\n\n Copy a file in another name :\n");
printf("----------------------------------\n");
printf(" Input the source file name : ");
scanf("%s",fname1);
fptr1=fopen(fname1, "r");
if(fptr1==NULL)
{
printf(" File does not found or error in opening.!!");
exit(1);
}
printf(" Input the new file name : ");
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) OUTPUT:
{
break;
}
else
{
fputc(ch, fptr2);
}
}
printf(" The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
getchar();
}
EXPERIMENT NO: 16

REMOVE A FILE FROM THE DISK.

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

AIM: to write a program in C to remove a file from the disk.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM:

Step 1: Start

Step 2: Initialize int status;


char fname[20];

Step 3: Enter the file name which is to be removed using scanf function

Step 4: Using remove function remove the file from directory

Step 5: Stop

FLOWCHART:
PROGRAM:

#include <stdio.h>
void main()
{
int status;
char fname[20];
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);
}
}
EXPERIMENT NO: 17

CREATE AND WRITE A DATA FILE

A. Write a C program to create a file and write data into file.

AIM: To create a file and writing data into file using C.

APPARATUS: 1. Personal Computer

2. Turbo C

ALGORITHM: FLOWCHART:

Step 1: Start

Step 2: Declare variables and a FILE type


pointer variable.

Step 3: Create or open file using to fopen()


function fptr=fopen(“data/file.txt”,”W”);

Step 4: If fptr==NULL print unable to create


file and exit

Step 5: else print enter contents to store in


file and read those

Step 6: using fputs() function write data to


file.

Step 7: After completing all operations,


close file.

Step 8: Print file Created and Saved


successfully

Step 9: Stop
PROGRAM:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
define data. Size 1000
main()
{
Char data (data-size);
FILE *fptr;
clrscr();
fptr = fopen("data \file | txt", "W");
if (fptr= = NULL)
{
printf (“unable to Create file ");
exit (EXIT_ FAILURE);
}
printf("Enter Contents to store in file: \n ");
fgets(data, data. Site, stdin);
fputs (data, fptr);
fclose(fptr);
printf("File Created and saved successfully ");
}

SAMPLE OUTPUT:

Enter contents to store in file

This is C lab

File created and saved successfully.


EXPERIMENT NO: 17

CALCULATE RESONANT FREQUENCY OF RLC

B. Write a program to calculate the resonant frequency of an RLC Circuit.

AIM: To write a c program to calculate the resonant frequency of an RLC series Circuit.

APPARATUS: 1. Personal Computer

2. Turbo C++

ALGORITHM:

Step 1: Start

Step 2: Define Pi value and declare function Current and frequency.

Step 3: Declare variables r, l, c, i, f in main function.

Step 4: Read values of r, l, c

Step 5: Call function frequency current at i.

In Current function

V=240, 14 and return i

Step 6: Print value of current

Step 7: Call function frequency at f

In frequency function

Declare s,result

s=l*c, result=sqrt(s), f=1/2*pi*result, result

return f

Step 8: Print value of frequency

Step 9: Stop
FLOWCHART:
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.142
double current (double);
double frequency (double, double);
main()
}
double r, l, c, I, f;
printf("Enter Value of R (ohm):");
scanf ("%\f", &r);
printf ("Enter value of L (henry):”);
scanf (“%\f", &i);
printf ("Enter value of C ((farad): ");
scanf ("%\f", &c);
i=current (r);
printf(" The value of current : %3\f”,i);
f=frequency(l, c);
printf(“ The value of frequency :%3\f”,f);
getch();
}
double Current (double, r)
{
double V=240, i;
i=v/r;
return i;
}
double frequency (double l, double C)
{
double S=0, result, f;
s=l*c
result = sqrt(s);
f=(1/2*pi*result);
return f;

SAMPLE OUTPUT:
Enter value of R (ohm): 5
Enter Value of L(henry):2
Enter value of C(farad):4
The Value of Current: 48.000
The Value of frequency: 0.065

You might also like