Program: B.Tech.
Regulations: R23
Year & Sem: I-I
COMPUTER PROGRAMMING
LAB
Department of INFORMATION TECHNOLOGY
GIET ENGINEERING COLLEGE
Approved by AICTE New Delhi, Affiliated to JNTUK, Accredited by NAAC and ISO:2015 Certified.
NH-16, GIET Campus, Velugubanda (V), Rajanagaram (M), Rajamahendravaram-533296, AP, India.
Department of IT, GIET Engineering College
GIET ENGINEERING COLLEGE
Approved by AICTE New Delhi, Affiliated to JNTUK, Accredited by NAAC and ISO:2015 Certified.
NH-16, GIET Campus, Velugubanda (V), Rajanagaram (M), Rajamahendravaram-533296, AP, India.
VISION
To evolve as a Premier Engineering Institution in the Country with its continues strive for
excellence in Education, Research and Technological Services.
MISSION
To generate the community of highly learned students with greater acquirement of knowledge
and to apply it professionally with due consideration for ecological, economic and ethical
issues.
To provide knowledge-based technological services at the best satisfaction of society and for
the industrial needs.
To foster the research and disseminate research findings.
To build in capabilities for advancing education, technology, values, management and research
at international standards.
Department of IT, GIET Engineering College
DEPARTMENT OF INFORMATION TECHNOLOGY
VISION
To explore innovative approaches to enhance and expand learning opportunities through
the integration of various technologies.
To build a strong research and teaching environment that responds to the real-time
challenges of the industry
MISSION
To inculcate the blend of competence, aptitude of knowledge and investigate flair
through devising an ambient environment for sustainable learning.
To transform attitude, values, priorities by changing mindset and instill positive outlook
for socially conscious intellectual development.
Incubate, apply and spread innovative ideas to evolve the department as a centre of
excellence in thrust areas
Department of IT, GIET Engineering College
DEPARTMENT OF INFORMATION TECHNOLOGY
PROGRAM EDUCATIONAL OBJECTIVES (PEO):
PEO 1 Provide a strong foundation required to comprehend, analyse, design and develop
solutions to real world computing problems.
PEO 2 Expose the students to industry practices for providing computing solutions using current
models and techniques.
PEO 3 Enable the students to pursue higher studies and active research.
PEO 4 Foster sustained professional development through life-long learning to adapt new
computing technologies
Department of IT, GIET Engineering College
DEPARTMENT OF INFORMATION TECHNOLOGY
PROGRAM OUTCOMES (PO):
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 modelling 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 lifelong learning in the broadest context of technological
change
Department of IT, GIET Engineering College
PROGRAM SPECIFIC OUTCOMES (PSO)
PSO1 Ability to apply their skills in the field of algorithms, networking, web design, cloud
computing and databases.
PSO2 Ability to develop and deploy software solutions for real world problems.
PSO3 Gain knowledge in diverse areas of Computer Science and experience an environment
conducivein cultivating skills for successful career, entrepreneurship, research and higher
studies.
.
Department of IT, GIET Engineering College
DEPARTMENT OF INFORMATION TECHNOLOGY
Syllabus
UNIT I
WEEK 1
Objective: Getting familiar with the programming environment on the
computer and writing the first program.
Suggested Experiments/Activities:
Tutorial 1: Problem-solving using Computers.
Lab1: Familiarization with programming environment
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
ii) Exposure to Turbo C, gcc
iii) Writing simple programs using printf(), scanf()
WEEK 2
Objective: Getting familiar with how to formally describe a solution to a
problem in a series of finite steps both using textual notation and graphic
notation.
Suggested Experiments /Activities:
Tutorial 2: Problem-solving using Algorithms and Flow charts.
Lab 1: Converting algorithms/flow charts into C Source code.
Developing the algorithms/flowcharts for the following sample programs
i) Sum and average of 3 numbers
ii) Conversion of Fahrenheit to Celsius and vice versa
iii) Simple interest calculation
WEEK 3
Objective: Learn how to define variables with the desired data-type, initialize
them with appropriate values and how arithmetic operators can be used with
variables and constants.
Suggested Experiments/Activities:
Tutorial 3: Variable types and type conversions:
Lab 3: Simple computational problems using arithmetic expressions.
i) Finding the square root of a given number
ii) Finding compound interest
Department of IT, GIET Engineering College
iii) Area of a triangle using heron’s formulae
iv) Distance travelled by an object
UNIT II
WEEK 4
Objective: Explore the full scope of expressions, type-compatibility of variables
& constants and operators used in the expression and how operator
precedence works.
Suggested Experiments/Activities:
Tutorial4: Operators and the precedence and as associativity:
Lab4: Simple computational problems using the operator’ precedence and
associativity
i) Evaluate the following expressions.
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)
ii) Find the maximum of three numbers using conditional operator
iii) Take marks of 5 subjects in integers, and find the total, average in float
WEEK 5
Objective: Explore the full scope of different variants of “if construct” namely if-
else, nullelse, if-else if*-else, switch and nested-if including in what scenario
each one of them can be used and how to use them. Explore all relational and
logical operators while writing conditionals for “if construct”.
Suggested Experiments/Activities:
Tutorial 5: Branching and logical expressions:
Lab 5: Problems involving if-then-else structures.
i) Write a C program to find the max and min of four numbers using if-else.
ii) Write a C program to generate electricity bill.
iii) Find the roots of the quadratic equation.
iv) Write a C program to simulate a calculator using switch case.
v) Write a C program to find the given year is a leap year or not.
Department of IT, GIET Engineering College
WEEK 6
Objective: Explore the full scope of iterative constructs namely while loop, do-
while loop andfor loop in addition to structured jump constructs like break
and continue including when each of these statements is more appropriate to
use.
Suggested Experiments/Activities:
Tutorial 6: Loops, while and for loops
Lab 6: Iterative problems e.g., the sum of series
i) Find the factorial of given number using any loop.
ii) Find the given number is a prime or not.
iii) Compute sine and cos series
iv) Checking a number palindrome
v) Construct a pyramid of numbers.
UNIT III
WEEK 7:
Objective: Explore the full scope of Arrays construct namely defining and
initializing 1-D and 2-D and more generically n-D arrays and referencing
individual array elements from the defined array. Using integer 1-D arrays,
explore search solution linear search.
Suggested Experiments/Activities:
Tutorial 7: 1 D Arrays: searching.
Lab 7:1D Array manipulation, linear search
i) Find the min and max of a 1-D integer array.
ii) Perform linear search on1D array.
iii) The reverse of a 1D integer array
iv) Find 2’s complement of the given binary number.
v) Eliminate duplicate elements in an array.
WEEK 8:
Objective: Explore the difference between other arrays and character arrays
that can be used as Strings by using null character and get comfortable with
string by doing experiments that will reverse a string and concatenate two
strings. Explore sorting solution bubble sort using integer arrays.
Department of IT, GIET Engineering College
Suggested Experiments/Activities:
Tutorial 8: 2 D arrays, sorting and Strings.
Lab 8: Matrix problems, String operations, Bubble sort
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
iv) Concatenate two strings without built-in functions
v) Reverse a string using built-in and without built-in string functions
UNIT IV
WEEK 9:
Objective: Explore pointers to manage a dynamic array of integers, including
memory allocation & value initialization, resizing changing and reordering
the contents of an arrayand memory de-allocation using malloc (), calloc (),
realloc () and free () functions. Gainexperience processing command-line
arguments received by C
Suggested Experiments/Activities:
Tutorial 9: Pointers, structures and dynamic memory allocation
Lab 9: Pointers and structures, memory dereference.
i) Write a C program to find the sum of a 1D array using malloc()
ii) Write a C program to find the total, average of n students using structures
iii) Enter n students data using calloc() and display failed students list
iv) Read student name and marks from the command line and display the
student details along
with the total.
v) Write a C program to implement realloc()
WEEK 10:
Objective: Experiment with C Structures, Unions, bit fields and self-referential
structures (Singly linked lists) and nested structures
Suggested Experiments/Activities:
Tutorial 10: Bitfields, Self-Referential Structures, Linked lists
Lab10 : Bitfields, linked lists
Read and print a date using dd/mm/yyyy format using bit-fields and
differentiate the same without using bit- fields
Department of IT, GIET Engineering College
i) Create and display a singly linked list using self-referential structure.
ii) Demonstrate the differences between structures and unions using a C
program.
iii) Write a C program to shift/rotate using bitfields.
iv) Write a C program to copy one structure variable to another structure of the
same type.
UNIT V
WEEK 11:
Objective: Explore the Functions, sub-routines, scope and extent of variables,
doing some experiments by parameter passing using call by value. Basic
methods of numerical integration
Suggested Experiments/Activities:
Tutorial 11: Functions, call by value, scope and extent,
Lab 11: Simple functions using call by value, solving differential equations
using Eulerstheorem.
i) Write a C function to calculate NCR value.
ii) Write a C function to find the length of a string.
iii) Write a C function to transpose of a matrix.
iv) Write a C function to demonstrate numerical integration of differential
equations using Euler’s method
WEEK 12:
Objective: Explore how recursive solutions can be programmed by writing
recursive functions that can be invoked from the main by programming at-
least five distinct problems that have naturally recursive solutions.
Suggested Experiments/Activities:
Tutorial 12: Recursion, the structure of recursive calls
Lab 12: Recursive functions
i) Write a recursive function to generate Fibonacci series.
ii) Write a recursive function to find the lcm of two numbers.
iii) Write a recursive function to find the factorial of a number.
iv) Write a C Program to implement Ackermann function using recursion.
v) Write a recursive function to find the sum of series.
Department of IT, GIET Engineering College
WEEK 13:
Objective: Explore the basic difference between normal and pointer variables,
Arithmetic operations using pointers and passing variables to functions using
pointers
Suggested Experiments/Activities:
Tutorial 13: Call by reference, dangling pointers
Lab 13: Simple functions using Call by reference, Dangling pointers.
i) Write a C program to swap two numbers using call by reference.
ii) Demonstrate Dangling pointer problem using a C program.
iii) Write a C program to copy one string into another using pointer.
iv) Write a C program to find no of lowercase, uppercase, digits and other
characters using pointers.
WEEK14:
Objective: To understand data files and file handling with various file I/O
functions. Explore the differences between text and binary files.
Suggested Experiments/Activities:
Tutorial 14: File handling
Lab 14: File operations
i) Write a C program to write and read text into a file.
ii) Write a C program to write and read text into a binary file using fread() and
fwrite()
iii) Copy the contents of one file to another file.
iv) Write a C program to merge two files into the third file using command-line
arguments.
v) Find no. of lines, words and characters in a file
vi) Write a C program to print last n characters of a given file
Department of IT, GIET Engineering College
DEPARTMENT OF INFORMATION TECHNOLOGY
LESSON PLAN
COURSE: COMPUTER PROGRAMMING LAB
ACADEMIC YEAR: 2023-24
SEMESTER:I-I FACULTY NAME:Ms.B.SAI DEEPTHI
No. of
S.No.
Sub Topic Names Teaching Aid Classes
Unit/Topic No.
Require
i) Basic Linux environment and its editors like
Vi, Vim & Emacs etc.
1 Projector 3
ii) Exposure to Turbo C, gcc
iii) Writing simple programs using printf(), scanf()
i) Sum and average of 3 numbers
ii) Conversion of Fahrenheit to Celsius and vice
2 Projector 3
versa
iii) Simple interest calculation.
i) Finding the square root of a given number
ii) Finding compound interest
3 Projector 3
iii) Area of a triangle using heron’s formulae
iv) Distance travelled by an object
i) Evaluate the following expressions.
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
4 d. J= (i++) + (++i) Projector 3
ii) Find the maximum of three numbers using
conditional operator
iii) Take marks of 5 subjects in integers, and find
the total, average in float
Department of IT, GIET Engineering College
i) C program to find the max and min of four
numbers using if-else.
ii) C program to generate electricity bill.
iii) Find the roots of the quadratic equation.
5 Projector 3
iv) C program to simulate a calculator using
switch case.
v) C program to find the given year is a leap year
or not.
i) Find the factorial of given number using any
loop.
ii) Find the given number is a prime or not.
6 Projector 3
iii) Compute sine and cos series
iv) Checking a number palindrome
v) Construct a pyramid of numbers
i) Find the min and max of a 1-D integer array.
ii) Perform linear search on1D array.
iii) The reverse of a 1D integer array
7 Projector 3
iv) Find 2’s complement of the given binary
number.
v) Eliminate duplicate elements in an array.
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
8 iv) Concatenate two strings without built-in Projector 3
functions
v) Reverse a string using built-in and without
built-in string functions
Department of IT, GIET Engineering College
i) C program to find the sum of a 1D array using
malloc()
ii) C program to find the total, average of n
students using structures
iii) Enter n students data using calloc() and
9 Projector 3
display failed students list
iv) Read student name and marks from the
command line and display the student details
along with the total.
v) C program to implement realloc()
i) Create and display a singly linked list using
self-referential structure.
ii) Demonstrate the differences between
10 structures and unions using a C program. Projector 3
iii) C program to shift/rotate using bitfields.
iv) C program to copy one structure variable to
another structure of the same type.
i) C function to calculate NCR value.
ii) C function to find the length of a string.
iii) C function to transpose of a matrix.
11 Projector 3
iv) C function to demonstrate numerical
integration of differential equations using Euler’s
method
i) recursive function to generate Fibonacci
series.
ii) recursive function to find the lcm of two
numbers.
12 iii) recursive function to find the factorial of a Projector 3
number.
iv) C Program to implement Ackermann function
using recursion.
v) recursive function to find the sum of series.
Department of IT, GIET Engineering College
i) C program to swap two numbers using call by
reference.
ii) Demonstrate Dangling pointer problem using
a C program.
13 iii) C program to copy one string into another Projector 3
using pointer.
iv) C program to find no of lowercase,
uppercase, digits and other
characters using pointers.
i) C program to nd read text into a file.
ii) C program to nd read text into a binary file
using fread() and fwrite()
iii) Copy the contents of one file to another file.
iv) C program to merge two files into the third
14 file using command-line Projector 3
arguments.
v) Find no. of lines, words and characters in a
file
vi) C program to print last n characters of a
given file.
Total No. of hours: 42
Textbooks:
1. Ajay Mittal, Programming in C: A practical approach, Pearson.
2. Byron Gottfried, Schaum' s Outline of Programming with C, McGraw Hill
Reference Books:
1. Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, PrenticeHall of
India
2. C Programming, A Problem-Solving Approach, Forouzan, Gilberg, Prasad, CENGAGE
Department of IT, GIET Engineering College
INDEX
1. Basic Linux environment and its editors like Vi, Vim & Emacs etc.
2. Exposure to Turbo C, gcc
3. Writing simple programs using printf(), scanf()
4. Sum and average of 3 numbers
5. Conversion of Fahrenheit to Celsius and vice versa
6. Simple interest calculation
7. Finding the square root of a given number
8. Finding compound interest
9. Area of a triangle using heron’s formulae
10. Distance travelled by an object
11. Evaluate the following expressions.
i. A+B*C+(D*E) + F*G
ii. A/B*C-B+A*D/3
iii. A+++B---A
iv. J= (i++) + (++i)
12. Find the maximum of three numbers using conditional operator
13. Take marks of 5 subjects in integers, and find the total, average in float
14. C program to find the max and min of four numbers using if-else.
15. C program to generate electricity bill.
16. Find the roots of the quadratic equation.
17. C program to simulate a calculator using switch case.
18. C program to find the given year is a leap year or not.
19. Find the factorial of given number using any loop.
20. Find the given number is a prime or not.
21. Compute sine and cos series
22. Checking a number palindrome
23. Construct a pyramid of numbers.
24. Find the min and max of a 1-D integer array.
25. Perform linear search on1D array.
26. The reverse of a 1D integer array
27. Find 2’s complement of the given binary number.
Department of IT, GIET Engineering College
28. Addition of two matrices
29. Multiplication two matrices
30. Sort array elements using bubble sort
31. Concatenate two strings without built-in functions
32. Reverse a string using built-in and without built-in string functions
33. C program to find the sum of a 1D array using malloc()
34. C program to find the total, average of n students using
structures
35. Enter n students data using calloc() and display failed students list
36. Read student name and marks from the command line and display the
student
37. details along with the total.
38. C program to implement realloc()
39. Create and display a singly linked list using self-referential structure.
40. Demonstrate the differences between structures and unions using a C
41. program.
42. C program to shift/rotate using bitfields.
43. C program to copy one structure variable to another structure of
Thesame type.
44. C function to calculate NCR value.
45. C function to find the length of a string.
46. C function to transpose of a matrix.
47. C function to demonstrate numerical integration of differential
48. equations using Euler’s method
49. recursive function to generate Fibonacci series.
50. recursive function to find the lcm of two numbers.
51. recursive function to find the factorial of a number.
52. C Program to implement Ackermann function using recursion.
53. recursive function to find the sum of series.
54. C program to swap two numbers using call by reference.
55. Demonstrate Dangling pointer problem using a C program.
56. C program to copy one string into another using pointer.
57. C program to find no of lowercase, uppercase, digits and other
Department of IT, GIET Engineering College
58. characters using pointers.
59. C program to write and read text into a file.
60. C program to write and read text into a binary file using fread() and
61. fwrite()
62. Copy the contents of one file to another file.
63. C program to merge two files into the third file using command-line
64. arguments.
65. Find no. of lines, words and characters in a file
66. C program to print last n characters of a given file
Department of IT, GIET Engineering College
Linux Environment and Its Editors
Linux Environment and Its Editors
Exp.No:
Date:
1.Aim:
Basic Linux environment and its editors like Vi, Vim & Emacs etc.
2.Software Required:
Linux
3.Theory:
Creating a basic Linux environment and installing text editors like Vi, Vim, and Emacs can be
done on a Linux-based system. Here's a step-by-step guide to set up a basic Linux environment
and install these text editors:
1. **Choose a Linux Distribution**:
You can choose a Linux distribution that suits your preferences. Popular choices include
Ubuntu, Debian, Fedora, CentOS, and others. You can download the ISO image of the
distribution you prefer from the official website and install it on a virtual machine or a physical
computer.
2. **Install Linux**:
Follow the installation instructions for your chosen Linux distribution. During the installation,
you will be asked to create a user account and set a password. Ensure that you have internet
access.
3. **Update the System**:
After installation, open a terminal and update your system to ensure you have the latest
software packages. Use the package manager that comes with your distribution:
On Debian/Ubuntu:
```bash
Dept of IT, GIETEngineeringCollege, Rajahmundry
Linux Environment and Its Editors
sudo apt update
sudo apt upgrade
```
On Fedora:
```bash
sudodnf update
```
4. **Install Text Editors**:
- **Vi/Vim**:
Vi is usually pre-installed on most Linux distributions. You can also install Vim (an improved
version of Vi) if it's not already installed:
```bash
sudo apt install vim # On Debian/Ubuntu
sudodnf install vim # On Fedora
```
- **Emacs**:
You can install Emacs using your distribution's package manager:
```bash
sudo apt install emacs # On Debian/Ubuntu
Dept of IT, GIETEngineeringCollege, Rajahmundry
Linux Environment and Its Editors
sudodnf install emacs # On Fedora
```
5. **Usage**:
- To open a terminal, you can use the built-in terminal emulator in your Linux distribution.
You can typically find it in your applications or use a keyboard shortcut (e.g., Ctrl+Alt+T).
- To use Vi or Vim, open a terminal and type `vi` or `vim` followed by the name of the file you
want to edit. For example:
```bash
vi filename.txt
```
- To use Emacs, open a terminal and type `emacs` followed by the name of the file you want to
edit. For example:
```bash
emacs filename.txt
```
6. **Learn the Editors**:
Vi, Vim, and Emacs have steep learning curves, especially if you're new to them. You can find
online tutorials, documentation, and books to help you get started and become proficient with
these editors.
Dept of IT, GIETEngineeringCollege, Rajahmundry
Exposure to Turbo C,gcc
Exposure to Turbo C,gcc
Exp.No:
Date:
1.Aim:
Exposure to Turbo C,gcc
2. Software Required:
Turbo C
3. Theory:
Exposure to Turbo C and GCC (GNU Compiler Collection) can be valuable for individuals
interested in programming and software development, especially in the C and C++ programming
languages. These two development environments have been widely used for many years and
offer different advantages.
1. **Turbo C**:
- **Overview**: Turbo C was a popular Integrated Development Environment (IDE) and
compiler for C and C++ programming in the MS-DOS environment. It was developed by
Borland and was widely used during the 1980s and 1990s.
- **Advantages**:
- Simple and easy-to-use IDE, especially for beginners.
- Well-suited for learning C and C++ programming basics.
- Provided a compact and efficient development environment for its time.
- **Disadvantages**:
- Outdated and no longer actively maintained.
- Limited compatibility with modern operating systems.
Dept of IT, GIETEngineeringCollege, Rajahmundry
Exposure to Turbo C,gcc
- May not support all the features of the latest C/C++ standards.
2. **GCC (GNU Compiler Collection)**:
- **Overview**: GCC is a powerful and widely used collection of compilers and tools for
various programming languages, including C, C++, and others. It is open-source software and
can be used on various platforms.
- **Advantages**:
- Cross-platform compatibility; it can be used on various operating systems.
- Supports the latest C and C++ standards and features.
- Highly configurable and extensible.
- Continuously updated and maintained by the open-source community.
- **Disadvantages**:
- May have a steeper learning curve for beginners due to its configurability.
- Requires familiarity with the command-line interface.
Exposure to both Turbo C and GCC can provide a balanced understanding of the history and
evolution of C and C++ development tools:
- Turbo C can help beginners grasp the basics of C and C++ programming in a simple and user-
friendly environment.
- GCC can be essential for those who want to work on modern and cross-platform projects, as it
provides access to the latest language features and a wide range of development tools.
In the real world, many professional C and C++ developers primarily use GCC or other modern
IDEs like Visual Studio Code, CLion, or Xcode, depending on their platform and project
requirements. However, understanding the foundations and history of these tools can be
beneficial for a well-rounded knowledge of programming in C and C++.
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Program Using printf( ),scanf( )
Simple Program Using printf( ),scanf( )
Exp No:
Date:
1. Aim:
Implement a simple program using printf( ),scanf( )
2. Software Required:
Turbo C
3. Theory:
The input and output functions used in C language.printf( ) is a output statement used to display
the output on the console deviece . scanf( ) is the input statement which is used to read the
information from the user. These functions can be used by including the header file “stdio.h” in
the program.
4. Program:
#include<stdio.h>
#include<conio.h>
main()
printf("######");
printf("\n#");
printf("\n#");
printf("\n####");
printf("\n#");
printf("\n#");
getch();
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Program Using printf( ),scanf( )
5. Sample Input and Output:
######
#
#
####
#
#
4. Program:
#include<conio.h>
#include<stdio.h>
main()
int a,b;
printf("Enter a value:");
scanf("%d",&a);
printf("\nEnter b value:");
scanf("%d",&b);
printf("\n a=%d \n b=%d",a,b);
getch();
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Program Using printf( ),scanf( )
5. Sample Input and Output:
Enter a value: 6
Enter b value: 34
a=6
b=34
6. Result:
Successfully Executed.
7.Reference:
https://www.programiz.com/c-programming/examples
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculating Marks
Sum and Average of Three Numbers
Exp.No:
Date:
1.Aim:
Implement a C program to find sum and average of three numbers.
2.Software Required:
TURBO C
3.Theory:
In this program, we first declared four variables x, y, z, sum.
After that created another variable named avg with the help of float data type.
In this avg variable, we will store the average of the number three and in the sum variable, we
will store the sum of the number three.
After declaring the variables, we got input three numbers from the user and store those values
in the x, y, z variables.
After this, we Calculate the sum of three numbers using the formula (sum = x + y + z ) and
stored its result in the sum variable.
After that, we print the sum of three numbers.
After this, we Calculate the average of three numbers using the formula (average =
sum/count) and stored its result in the avg variable.
Finally, we printed the stored value in this avg variable on the screen with the help of printf()
function.
4.Program:
#include <stdio.h>
#include<conio.h>
main()
int a,b,c;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculating Marks
float sum,avg;
printf("Enter three values:");
scanf("%d%d%d",&a,&b,&c),
sum=a+b+c;
avg=sum/3;
printf("\nThe sum of three numbers=%f",sum);
printf("\nThe average of three numbers=%f",avg);
getch();
5.Algorithm:
1. Input: Receive three numbers, a, b, and c.
2. Sum: Calculate the sum of a, b, and c by adding them together: sum = a + b +c.
3. Average: Calculate the average by dividing the sum by 3: average = sum / 3.
4. Output: The average is the result you're looking for, representing the average of the
three numbers.
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculating Marks
6.Flow Chart:
7. Sample Input and Output:
Enter three values: 5 6 7
The sum of three numbers=18.000000
The average of three numbers=6.000000
8.Result:
Sum and average of three number executed Successfully.
9.Reference:
https://cstutorialpoint.com/c-program-to-find-the-sum-and-average-of-three-numbers/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Conversion of Fahrenheit to Celsius and Vice Versa
Conversion of Fahrenheit to Celsius and Vice Versa
Exp.No:
Date:
1.Aim:
Implement aC program for conversion of Fahrenheit to Celsius and vice versa
2.Software Required:
TURBO C
3.Theory:
Celsius is represented by oC and Fahrenheit is represented by oF. There is a formula to convert
Celsius to Fahrenheit, using which we will be able to convert Celsius to Fahrenheit.
Similarly, there is also a formula to convert Fahrenheit to Celsius, using which we will be able to
convert Fahrenheit to Celsius.
Formula to Convert Temperature From Celsius To Fahrenheit
Fahrenheit = (celsius * 1.8) + 32
Formula to Convert Temperature From Fahrenheit to Celsius
Celsius = ((Fahrenheit-32)*5)/9;
4.Program:
#include<stdio.h>
int main()
{
float t1, t2,c,f;
printf("Enter Temperature in Celsius : ");
scanf("%f",&t1);
printf("\nEnter Temperature in Fahrenheit : ");
Dept of IT, GIETEngineeringCollege, Rajahmundry
Conversion of Fahrenheit to Celsius and Vice Versa
scanf("%f",&t2);
//Temperature From Celsius To Fahrenheit
f=(t1*9/5)+32;
//Converting Fahrenheit into Celsius
c = ((t2-32)*5)/9;
printf("\nTemperature in Fahrenheit : %f",f);
printf("\nTemperature in Celsius : %f",c);
return 0;
}
5.Algorithm:
1. Program Start
2. Declaration of variable
3. Enter temperature in Celsius
4. Enter temperature in Fahrenheit
5. Converting conversion into Fahrenheit
6. Converting Fahrenheit into Celsius
7. Displaying result
8. Program End
Dept of IT, GIETEngineeringCollege, Rajahmundry
Conversion of Fahrenheit to Celsius and Vice Versa
6.FlowChart:
7.Sample Input and Output:
Enter Temperature in Celsius : 35
Enter Temperature in Fahrenheit : 95
Temperature in Fahrenheit : 95.000000
Temperature in Celsius : 35.000000
8.Result:
Conversion of Fahrenheit to Celsius and Vice Versa is Executed Successfully.
9.Reference:
https://cstutorialpoint.com/c-program-to-convert-temperature-from-celsius-to-fahrenheit-and-
fahrenheit-to-celsius/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Interest Calculation
Simple Interest Calculation
Exp No:
Date:
1. Aim: Implementing a C program to find simple interest calculation
2. Software Developed:
Turbo C
3.Theory:
Simple interest is a quick method of calculating the interest charge on a loan. In this article, we
will learn to calculate the simple interest in C programming language.
Simple interest is determined by multiplying the daily interest rate by the principal by the
number of days that elapse between payments. Divide the product of these three values to
convert the percentage value of the interest rate into decimals.
Simple Interest Formula
Si=p*t*r/100
Where P is the principal amount, T is the time, and, R is the interest rate
4. Program:
#include<stdio.h>
#include<conio.h>
int main()
float p,t,r;
float interest,amount;
printf("Enter principal amount=");
scanf("%f",&p);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Interest Calculation
printf("\nEnter time period in years=");
scanf("%f",&t);
printf("\nEnter rate of interest=");
scanf("%f",&r);
interest=(p*t*r)/100;
amount=p+interest;
printf("\n principal amount=%f ",p);
printf("\nTime period=%f",t);
printf("\nRate of interest=%f=",r);
printf("\n Simple Interest for %f years at %f rate is =%f",t,r,interest);
printf("\n The total amount=%f",amount);
getch();
4. Algorithm:
Step 1 : START
Step 2 : Declare variables p , n , r and si .
Step 3 : Read the values of variable p ( principal ) , n ( Number of years ) , r ( Rate of Interest ) .
Step 4 : calculate the values of “ si = ( p * n * r )/100 “ .
Step 5 : Display si ( simple interest ) .
Step 6 : STOP
Dept of IT, GIETEngineeringCollege, Rajahmundry
Simple Interest Calculation
5. Flow Chart:
6.Sample Input and Output:
Enter principal amount=500000
Enter time period in years=5
Enter rate of interest=2
principal amount=500000.000000
Time period=5.000000
Rate of interest=2.000000
Simple Interest for 5.000000 years at 2.000000 rate is =50000.000000
The total amount=550000.000000
7.Result:
Successfully Executed
8.References:
https://www.geeksforgeeks.org/c-program-for-simple-interest/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Square Root of a Given Number
Square Root of a Given Number
Exp no:
1.Aim: Implementing a C program to find the square root of a given number.
2. Software Required:
Turbo C
3.Theory:
A Square root of a number is a value that, when multiplied by itself, gives the number.
4.Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a;
float square ;
printf("Enter the value for which you want to find sqare root::");
scanf("%d",&a);
square=sqrt(a);
printf("\n The square root of %d is =%f",a,square);
getch();
}
5.Sample Input and Output:
Enter the value for which you want to find sqareroot::346
The square root of 346 is =18.601076
6.Result:
Successfully Executed.
7.Reference:
https://www.codingbroz.com/c-program-to-find-square-root-of-a-number/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Compound Interest
Compound Interest
Exp.No:
Date:
1.Aim: Implementing a Cprogram to find compound interest.
2.Software Required:
Turbo C
3.Theory:
Compound interest is the addition of interest to the principal sum of a loan or deposit,
or in other words, interest on interest. It is the result of reinvesting interest, rather than
paying it out, so that interest in the next period is then earned on the principal sum plus
previously-accumulated interest. Compound interest is standard in finance and
economics.
Compound Interest formula:
Formula to calculate compound interest annually is given by:
Amount= P(1 + R/100)t
4.Program:
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
/* Input principle, time and rate */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Compound Interest
printf("Enter rate: ");
scanf("%f", &rate);
/* Calculate compound interest */
CI = principle* (pow((1 + rate / 100), time));
/* Print the resultant CI */
printf("Compound Interest = %f", CI);
return 0;
}
5.Sample Input and Output:
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Compound Interest = 1333.099243
6.Result:
Successfully Executed
7.Reference:
https://www.geeksforgeeks.org/c-program-for-compound-interest/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Triangle Using Heron’s Formulae
Triangle Using Heron’s Formulae
Exp.No:
Date:
1.Aim:
Implementing a C program to find area of triangle using heron’s formulae.
2.Software Required:
Turbo C
3.Program:
#include <stdio.h>
#include <math.h>
int main()
{
float s1, s2, s3, s, area;
printf("\nEnter the length of three sides of triangle");
scanf("%f %f %f", &s1, &s2, &s3);
s = (s1 + s2 + s3)/2;
area = sqrt(s*(s-s1)*(s-s2)*(s-s3));
printf("Area of triangle : %0.4f\n", area);
return 0;
}
4.Sample Input and Output:
Enter the length of three sides of triangle54
87
90
Area of triangle : 2272.0667
5.Result:
Successfully completed.
6. References:
https://btechgeeks.com/c-program-to-calculate-area-of-any-triangle-using-herons-formula/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Distance Travelled by an Object
Distance Travelled by an Object
Exp.No:
Date:
1.Aim:
Implementing a C program to find the distance travelled by an object.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main() {
float u,a,s;
int t;
printf("enter the values");
scanf("%f%f",&a,&u);
printf("enter the value of time");
scanf("%d",&t);
s=(u*t)+(a*t*t)/2;
printf("%f",s);
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Distance Travelled by an Object
4.Sample Input and Output:
enter the values4
enter the value of time2
14.000000
5.Result:
Successfully Executed.
6.Reference:
https://btechgeeks.com/c-program-to-calculate-area-of-any-triangle-using-herons-formula/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Evaluate Expressions
Evaluate Expressions
Exp.No:
Date:
1.Aim:
Implementing a C program to Evaluate the following expressions.
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main()
int A = 5, B = 10, C = 2, D = 7, E = 3, F = 4, G = 6;
int i = 5, j ,result_a, result_b, result_c;
result_a = A + B * C + (D * E) + F * G;
printf("a. Result of expression A+B*C+(D*E) + F*G is: %d\n", result_a);
result_b = A / B * C - B + A * D / 3;
printf("b. Result of expression A/B*C-B+A*D/3 is: %d\n", result_b);
result_c = A++ + ++B - --A;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Evaluate Expressions
printf("c. Result of expression A+++B---A is: %d\n", result_c);
j = (i++) + (++i);
printf("d. Value of J after J = (i++) + (++i) is: %d\n", j);
return 0;
4.Algorithm:
Step 1: Declare A,B,C,D,E,F,G,i,j.
Step 2: Read A,B,C,D,E,F,G,i,j Values.
Step 3: Evaluate the below expressions result_a = A + B * C + (D * E) + F * G.result_b = A / B
* C - B + A * D / 3. result_c = A++ + ++B - --A.J = (i++) + (++i).
Step 4: Print result_a,result_b,result_c,j
Step 5: Exit or terminate the program.
5.Sample Input and Output:
a. Result of expression A+B*C+(D*E) + F*G is: 70
b. Result of expression A/B*C-B+A*D/3 is: 1
c. Result of expression A+++B---A is: 11
d. Value of J after J = (i++) + (++i) is: 12
6.Result:
Successfully Executed.
7.Reference:
https://www.educba.com/expression-evaluation-in-c
Dept of IT, GIETEngineeringCollege, Rajahmundry
Maximum of Three Numbers Using Conditional Operator
Maximum of Three Numbers Using Conditional Operator
Exp.No:
Date:
1.Aim:
Implementing a C program to find the maximum of three numbers using conditional operator.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main()
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 :
num3);printf("Maximum number is %d\n", max);
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Maximum of Three Numbers Using Conditional Operator
4.Algorithm:
Step-1: Declare num1, num2, num3, max.
Step-2: Read num1, num2, num3 using scanf() function.
Step-3: Calculate max value using the conditional operator (ternary operator) to find the
maximum.
Step-4: Print max value.
Step-5: Exit or terminate the program.
5.Sample Input and Output:
Enter three numbers: 4 5 6
Maximum number is 6
6.Result:
Successfully Executed.
7.Reference:
https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculating Marks
Calculating Marks
Exp.No:
Date:
1.Aim:
Implementing a C program for calculatingmarks of 5 subjects in integers, and find the total,
average in float
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
int marks[5] ,i;
float total = 0.0, average;
printf("Enter marks of 5 subjects: ");
for ( i = 0; i< 5; i++)
scanf("%d", &marks[i]);
total += marks[i];
average = total / 5.0;
printf("Total marks: %.2f\n", total);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculating Marks
printf("Average marks: %.2f\n", average);
return 0;
4.Sample Input and Output:
Enter marks of 5 subjects: 45
56
35
50
70
Total marks: 256.00
Average marks: 51.20
5.Result:
Successfully Executed.
6.Reference:
https://w3codeworld.com/article/72/c-program-to-enter-the-marks-of-five-subjects-and-
calculate-total--average--and-percentage
Dept of IT, GIETEngineeringCollege, Rajahmundry
Max and Min of Foir Numbers using If-Else
Max And Min of Four Numbers Using If-Else
Exp.No:
Date:
1.Aim:
Implement a C program to find the max and min of four numbers using if-else.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
int n1, n2, n3, n4;
int max, min;
printf("Enter four numbers: ");
scanf("%d %d %d %d", &n1, &n2, &n3, &n4);
if (n1 >= n2 && n1 >= n3 && n1 >= n4)
{
max = n1;
}
else if (n2 >= n1 && n2 >= n3 && n2 >= n4)
{
max = n2;
}
else if (n3 >= n1 && n3 >= n2 && n3 >= n4)
{
max = n3;
}
else
{
max = n4;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Max and Min of Foir Numbers using If-Else
}
if (n1 <= n2 && n1 <= n3 && n1 <= n4)
{
min = n1;
}
else if (n2 <= n1 && n2 <= n3 && n2 <= n4)
{
min = n2;
}
else if (n3 <= n1 && n3 <= n2 && n3 <= n4)
{
min = n3;
}
else
{
min = n4;
}
printf("Maximum is: %d\n", max);
printf("Minimum is: %d\n", min);
return 0;
}
4.Sample Input and Output:
Enter four numbers: 56 8 0 90
Maximum is: 90
Minimum is: 0
5.Result:
Successfully Executed.
6.Reference:
https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-
programming-basic-exercises-58.php
Dept of IT, GIETEngineeringCollege, Rajahmundry
Generate Electricity Bill
Generate Electricity Bill
Exp.No:
Date:
1.Aim:
Implement a C program to generate electricity bill.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
int unit;
float amt, total_amt, sur_charge;
printf("Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Generate Electricity Bill
}
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
return 0;
}
4.Sample Input and Output:
Enter total units consumed: 500
Electricity Bill = Rs. 714.00
5.Result:
Successfully Executed.
6.Reference:
https://codeforwin.org/c-programming/c-program-to-calculate-electricity-bill
Dept of IT, GIETEngineeringCollege, Rajahmundry
Roots of Quadratic Equation
Roots of Quadratic Equation
Exp.No:
Date:
1.Aim:
Implement a C program to find roots of quadratic equation.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <math.h>
main()
{
double a, b, c;
double discriminant, root1, root2;
printf("Enter the coefficients of the quadratic equation (a, b, c): ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two real and distinct roots:\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
}
else if (discriminant == 0)
{
root1 = -b / (2 * a);
printf("One real root (double root):\n");
printf("Root = %.2lf\n", root1);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Roots of Quadratic Equation
}
else
{
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Two complex roots:\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
return 0;
}
4.Sample Input and Output:
Enter the coefficients of the quadratic equation (a, b, c): 4
Two complex roots:
Root 1 = -0.62 + 1.05i
Root 2 = -0.62 - 1.05i
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/quadratic-roots
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculator Using Switch Case
Calculator Using Switch Case
Exp.No:
Date:
1.Aim:
Implement a C program to simulate a calculator using switch case.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Calculator Using Switch Case
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
4.Sample Input and Output:
Enter an operator (+, -, *, /): *
Enter two operands: 3 6
3.0 * 6.0 = 18.0
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/calculator-switch-case
Dept of IT, GIETEngineeringCollege, Rajahmundry
Leap Year or Not
Leap Year or Not
Exp.No:
Date:
1.Aim:
Implement a C program to find the given year is a leap year or not.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Leap Year or Not
4.Sample Input and Output:
Enter a year: 2023
2023 is not a leap year.
5.Result:
Successfully Executed.
6.Reference:
https://www.tutorialspoint.com/learn_c_by_examples/leap_year_program_in_c.htm
Dept of IT, GIETEngineeringCollege, Rajahmundry
Factorial Of Given Number Using Loop
Factorial Of Given Number Using Loop
Exp.No:
Date:
1.Aim:
Implement a C program to find the factorial of given number using any loop.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
int i=1, f = 1, n;
printf("Input the number : ");
scanf("%d", &n);
while (i<= n)
{
f = f * i;
i++;
}
printf("The Factorial of %d is: %d\n", n, f);
return 0;
}
4.Sample Input and Output:
Input the number : 6
The Factorial of 6 is: 720
5.Result:
Successfully Executed.
6.Reference:
https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-15.php
Dept of IT, GIETEngineeringCollege, Rajahmundry
Prime or Not
Prime Or Not
Exp.No:
Date:
1.Aim:
Implement a C program to find given number is a prime or not.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
main()
{
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Prime or Not
4.Sample Input and Output:
Enter the number to check prime:4
Number is not prime
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/prime-number
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sine and cons Series
Sine and Cos Series
Exp.No:
Date:
1.Aim:
Implement a C program to Compute sine and cos series
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <math.h>
// Function to calculate factorial of a number
double factorial(int n) {
double fact = 1.0;
for (int i = 1; i<= n; i++) {
fact *= i;
}
return fact;
}
int main() {
double x, sinX = 0.0, cosX = 0.0;
int n;
// Prompt the user to enter the angle in radians and the number of terms in the series
printf("Enter the angle in radians (x): ");
scanf("%lf", &x);
printf("Enter the number of terms in the series: ");
scanf("%d", &n);
// Calculate sine(x) and cosine(x) using the Taylor series expansions
for (int i = 0; i< n; i++) {
int exponent = 2 * i + 1;
double term = pow(-1, i) * pow(x, exponent) / factorial(exponent);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sine and cons Series
sinX += term;
if (i % 2 == 0) {
// For cosine, the terms with even exponents are added
cosX += term;
}
}
// Display the results
printf("sin(x) = %.6lf\n", sinX);
printf("cos(x) = %.6lf\n", cosX);
return 0;
}
4.Sample Input and Output:
Enter the angle in radians (x): 4
Enter the number of terms in the series: 4
sin(x) = -1.384127
cos(x) = 12.533333
5.Result:
Successfully Executed.
6.Reference:
https://www.codingconnect.net/c-program-sine-series/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Number Palindrome
Number Palindrome
Exp.No:
Date:
1.Aim:
Implement a C program to Checking a number palindrome
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
{
int num, r, sum = 0, t;
printf("Input a number: ");
scanf("%d", &num);
for(t = num; num != 0; num = num / 10)
{
r = num % 10;
sum = sum * 10 + r;
}
if(t == sum)
printf("%d is a palindrome number.\n", t);
else
printf("%d is not a palindrome number.\n", t);
return 0;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Number Palindrome
4.Sample Input and Output:
Input a number: 121
121 is a palindrome number.
Input a number: 123
123 is not a palindrome number.
5.Result:
Successfully Executed.
6.Reference:
https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-38.php
Dept of IT, GIETEngineeringCollege, Rajahmundry
Pyramid of Numbers
Pyramid Of Numbers
Exp.No:
Date:
1.Aim:
Implement a C program to Construct a pyramid of numbers.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main()
{
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; ++i)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
printf("%d ", i + k);
++count;
} else
{
++count1;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Pyramid of Numbers
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
4.Sample Input and Output:
Enter the number of rows: 5
232
34543
4567654
567898765
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/pyramid-pattern
Dept of IT, GIETEngineeringCollege, Rajahmundry
Min and Max A 1-D Integer Array
Min And Max of A 1-D Integer Array
Exp.No:
Date:
1.Aim:
Implement a C program to find the min and max of a 1-D integer array.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
printf("\n\nFind maximum and minimum element in 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]);
}
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
Dept of IT, GIETEngineeringCollege, Rajahmundry
Min and Max A 1-D Integer Array
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
}
4.Sample Input and Output:
Find maximum and minimum element in an array :
------------------------------------------------
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 12
element - 1 : 35
element - 2 : 3
element - 3 : 90
element - 4 : 1
Maximum element is : 90
Minimum element is : 1
5.Result:
Successfully Executed.
6.Reference:
https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-9.php
Dept of IT, GIETEngineeringCollege, Rajahmundry
Linear Search On 1D Array
Linear Search On 1D Array
Exp.No:
Date:
1.Aim:
Implement a C program to Perform linear search on 1D array.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main()
{
int a[10], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (i = 0; i< n; i++)
{
if (a[i] == search)
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Linear Search On 1D Array
}
4.Sample Input and Output:
Enter number of elements in array
Enter 4 integer(s)
12
90
-1
Enter a number to search
12
12 is present at location 1.
5.Result:
Successfully Executed.
6.Reference:
https://www.programmingsimplified.com/c/source-code/c-program-linear-search
Dept of IT, GIETEngineeringCollege, Rajahmundry
Reverse A 1D Integer Array
Reverse A 1D Integer Array
Exp.No:
Date:
1.Aim:
Implement a C program to reverse a 1D integer array.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int main()
{
int n, i, j, a[10], b[10];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (i = 0; i<n ;i++)
scanf("%d", &a[i]);
for (i = n - 1, j = 0; i>= 0; i--, j++)
b[j] = a[i];
for (i = 0; i< n; i++)
a[i] = b[i];
printf("Reverse array is\n");
for (i = 0; i< n; i++)
printf("%d\n", a[i]);
return 0;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Reverse A 1D Integer Array
4.Sample Input and Output:
Enter the number of elements in array
5
Enter the array elements
12
34
9
1
0
Reverse array is
0
1
9
34
12
5.Result:
Successfully Executed.
6.Reference:
https://www.w3schools.in/c-programming/examples/reverse-array
Dept of IT, GIETEngineeringCollege, Rajahmundry
2’s Complement of The Given Binary Number
2’s Complement of The Given Binary Number
Exp.No:
Date:
1.Aim:
Implement a C program to find 2’s complement of the given binary number.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;\
printf("Enter %d bit binary value: ", SIZE);
/* Input 8-bit binary string */
gets(binary);
/* Find ones complement of the binary number */
for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
2’s Complement of The Given Binary Number
onesComp[SIZE] = '\0';
/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
printf("Original binary = %s\n", binary);
printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);
return 0;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
2’s Complement of The Given Binary Number
4.Sample Input and Output:
Enter 8 bit binary value: 01110111
Original binary = 01110111
Ones complement = 10001000
Twos complement = 10001001
5.Result:
Successfully Executed.
6.Reference:
https://codeforwin.org/c-programming/c-program-to-find-twos-complement-of-binary-number
Dept of IT, GIETEngineeringCollege, Rajahmundry
Eliminate Duplicate Elements in An Array
Eliminate Duplicate Elements in An Array
Exp.No:
Date:
1.Aim:
Implement a C program to Eliminate duplicate elements in an array.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
int main()
{
int a[50],i,j,k, count = 0, dup[50], n;
printf("Enter size of the array");
scanf("%d",&n);
printf("Enter Elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<n;i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] == a[j]){
Dept of IT, GIETEngineeringCollege, Rajahmundry
Eliminate Duplicate Elements in An Array
for(k = j; k <n; k++)
{
a[k] = a[k+1];
}
j--;
n--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
4.Sample Input and Output:
Enter size of the array10
Enter Elements of the array:0 1 3 4 5 3 2 4 2 1
Entered element are: 0 1 3 4 5 3 2 4 2 1
After deleting the duplicate element the Array is:0 1 3 4 5 2
5.Result:
Successfully Executed.
6.Reference:
https://www.tutorialspoint.com/c-program-to-delete-the-duplicate-elements-in-an-array
Dept of IT, GIETEngineeringCollege, Rajahmundry
Addition of Two Matrices
Addition of Two Matrices
Exp.No:
Date:
1.Aim:
Implement a C program forAddition of two matrices.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
main()
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
Dept of IT, GIETEngineeringCollege, Rajahmundry
Addition of Two Matrices
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
printf("\nSum of two matrices: \n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
printf("%d ", sum[i][j]);
if (j == c - 1)
printf("\n\n");
return 0;
4.Sample Input and Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2
Enter elements of 1st matrix:
Dept of IT, GIETEngineeringCollege, Rajahmundry
Addition of Two Matrices
Enter element a11: 12
Enter element a12: 2
Enter element a21: 4
Enter element a22: 2
Enter elements of 2nd matrix:
Enter element b11: 1
Enter element b12: 3
Enter element b21: 2
Enter element b22: 4
Sum of two matrices:
13 5
6 6
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/add-matrix
Dept of IT, GIETEngineeringCollege, Rajahmundry
Multiplication of Two Matrices
Multiplicationof Two Matrices
Exp.No:
Date:
1.Aim:
Implement a C program forMultiplicationof two matrices.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
main()
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Please insert the number of rows and columns for first matrix \n ");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++)
Dept of IT, GIETEngineeringCollege, Rajahmundry
Multiplication of Two Matrices
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
tot = tot + fst[c][k] * sec[k][d];
}
mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the matrices is: \n ");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}
4.Sample Input and Output:
Please insert the number of rows and columns for first matrix
22
Insert your matrix elements:
11
11
Please insert the number of rows and columns for second matrix
22
Dept of IT, GIETEngineeringCollege, Rajahmundry
Multiplication of Two Matrices
Insert your elements for second matrix
22
22
The result of matrix multiplication or product of the matrices is:
4 4
4 4
5.Result:
Successfully Executed.
6.Reference:
https://www.w3schools.in/c-programming/examples/matrix-multiplication-in-c
Dept of IT, GIETEngineeringCollege, Rajahmundry
Bubble Sort
Bubble Sort
Exp.No:
Date:
1.Aim:
Implement a C program Sort array elements using bubble sort.
2.Software Required:
TURBO C
3.Theory:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
4.Program:
#include<stdio.h>
bubble(int *a,int n)
{
int tem,count, i,j;
for(i=0;i<n-1;i++)
{
count=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
count=1;
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Bubble Sort
}
if(count==0)
break;
}
}
main()
{
int n,a[100],i;
printf("\nenter number of elements:");
scanf("%d",&n);
printf("\nenter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements before sorting :");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
bubble(a,n);
printf("\nThe elements after sorting are:");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
return 0;
}
5.Algorithm:
Bubble(a,n)
Begin
For i=1 to n-1 by 1 do
For j=0 to n-1-I by 1 do
If a[j] >[j+1] then
Set tmp=a[j];
Dept of IT, GIETEngineeringCollege, Rajahmundry
Bubble Sort
Set a[j]=a[j+1];
Set a[j+1]=tmp;
Endif
Endfor
endfor
end
6.Sample Input and Output:
Enter number of elements:5
Enter array elements: 45 9 21 3 57
The elements before sorting: 45 9 21 3 57
The elements after sorting are : 3 9 21 45 57
7.Result:
Bubble Sort Program Successfully Executed.
6.Reference:
https://www.geeksforgeeks.org/bubble-sort/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Concatenating Two Strings Without Built-In Functions
Concatenating Two Strings Without Built-In Functions
Exp.No:
Date:
1.Aim:
Implement a C program forConcatenating two strings without built-in functions
2.Software Required:
TURBO C
3.Program:
void main(void)
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
str1[i]=str2[j];
j++;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Concatenating Two Strings Without Built-In Functions
i++;
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
4.Sample Input and Output:
Enter First String:sai
Enter Second String:deepthi
Concatenated String is saideepthi
5.Result:
Successfully Executed.
6.Reference:
https://www.w3schools.in/c-programming/examples/concatenate-two-strings-without-using-
strcat
Dept of IT, GIETEngineeringCollege, Rajahmundry
Reverse A String Using Built-In and Without Built-In String Functions
Reverse A String Using Built-In and Without Built-In String
Functions
Exp.No:
Date:
1.Aim:
Implement a C program toReverse a string using built-in and without built-in string functions
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100];
int l, i;
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");
}
Dept of IT, GIETEngineeringCollege, Rajahmundry
Reverse A String Using Built-In and Without Built-In String Functions
4.Sample Input and Output:
Print individual characters of string in reverse order :
Input the string :saideepthi
The characters of the string in reverse are :ih t p e e d i a s
5.Result:
Successfully Executed.
6.Reference:
https://www.tutorialspoint.com/cprogramming/index.htm
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sum of a 1D Array Using Malloc
Sum of a 1D Array Using Malloc()
Exp.No:
Date:
1.Aim:
Implement a C program to find the sum of a 1D array using malloc().
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
int i, n;
int *a, *b, *c;
printf("Enter the size of the arrays\n");
scanf("%d", &n);
a = (int *)malloc(n * sizeof(int));
b = (int *)malloc(n * sizeof(int));
c = (int *)malloc(n * sizeof(int));
printf("Enter Elements of First List\n");
for (i = 0; i< n; i++)
scanf("%d", a + i);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sum of a 1D Array Using Malloc
printf("Enter Elements of Second List\n");
for (i = 0; i< n; i++)
scanf("%d", b + i);
for (i = 0; i< n; i++)
*(c + i) = *(a + i) + *(b + i);
printf("Resultant List is\n");
for (i = 0; i< n; i++)
printf("%d\n", *(c + i));
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sum of a 1D Array Using Malloc
4.Sample Input and Output:
Enter the size of the arrays
Enter Elements of First List
12 23 34 1 2
Enter Elements of Second List
13456
Resultant List is
13263868
5.Result:
Successfully Executed.
6.Reference:
https://www.sanfoundry.com/c-program-compute-sum-two-one-dimensional-arrays-using-
malloc/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Total Average of N students using Structures
Total, Average of n students using Structures
Exp.No:
Date:
1.Aim:
Implement a C program to find the total,average of n students using structures.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
struct student
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter the detail of %d students\n",i+1);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Total Average of N students using Structures
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
for(i=0;i<n;i++)
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
4.Sample Input and Output:
Enter the number of student=2
Enter the detail of 1 students
Enter Name=deepu
Enter the three subject score
45 56 67
Enter the detail of 2 students
Enter Name=deepthi
Enter the three subject score
78 45 78
Dept of IT, GIETEngineeringCollege, Rajahmundry
Total Average of N students using Structures
deepu has scored above the average marks
deepthi has scored above the average marks1
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Display Failed Students Using Calloc()
Display Failed Students Using Calloc()
Exp.No:
Date:
1.Aim:
Implement a C program to Enter n students data using calloc() and display failed students list.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
struct Student
char name[50];
int rollNumber;
float marks;
};
int main()
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student *students = (struct Student *)calloc(n, sizeof(struct Student));
for (int i = 0; i< n; ++i)
printf("\nEnter details for student %d:\n", i + 1);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Display Failed Students Using Calloc()
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Marks: ");
scanf("%f", &students[i].marks);
printf("\nList of failed students:\n");
for (int i = 0; i< n; ++i)
if (students[i].marks<=40.0)
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
free(students);
return 0;
4.Sample Input and Output:
Enter the number of students: 2
Enter details for student 1:
Name: divitha
Dept of IT, GIETEngineeringCollege, Rajahmundry
Display Failed Students Using Calloc()
Roll Number: 1
Marks: 90
Enter details for student 2:
Name: deepu
Roll Number: 2
Marks: 40
List of failed students:
Name: deepu
Roll Number: 2
Marks: 40.00
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Read student name and marks from the command line and display the student details
Read student name and marks from the command line and display
the student details
Exp.No:
Date:
1.Aim:
Implement a C program to Read student name and marks from the command line and display the
student details along with the total
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int marks;
};
int main(int argc, char *argv[]) {
if (argc< 2) {
printf("Usage: %s <number_of_students>\n", argv[0]);
return 1;
int n = atoi(argv[1]);
struct Student *students = (struct Student *)calloc(n, sizeof(struct Student));
if (students == NULL) {
printf("Memory allocation failed. Exiting...\n");
Dept of IT, GIETEngineeringCollege, Rajahmundry
Read student name and marks from the command line and display the student details
return 1;
for (int i = 0; i< n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter marks of student %d: ", i + 1);
scanf("%d", &students[i].marks);
int total = 0;
int failedCount = 0;
for (int i = 0; i< n; i++) {
total += students[i].marks;
if (students[i].marks< 40) {
failedCount++;
float average = (float)total / n;
printf("\nStudent Details:\n");
for (int i = 0; i< n; i++) {
printf("Name: %s, Marks: %d\n", students[i].name, students[i].marks);
printf("\nTotal marks: %d\n", total);
printf("Average marks: %.2f\n", average);
if (failedCount> 0) {
printf("\nFailed students list:\n");
Dept of IT, GIETEngineeringCollege, Rajahmundry
Read student name and marks from the command line and display the student details
for (int i = 0; i< n; i++) {
if (students[i].marks< 40) {
printf("Name: %s, Marks: %d\n", students[i].name, students[i].marks);
} else {
printf("\nNo students failed.\n");
free(students);
return 0;}
4.Sample Input and Output:
Enter the number of students: 2
Enter name of student 1: sai
Enter marks of student 1: 12
Enter name of student 2: deepu
Enter marks of student 2: 23
Total marks: 35
Average marks: 17.5
Failed students list:
Name: sai, Marks: 12
Name: deepu, Marks: 23
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Realloc() Function
Realloc() Function
Exp.No:
Date:
1.Aim:
Implement a C program for realloc() function.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int main()
int n = 4, i, *p, s = 0;
p = (int*) calloc(n, sizeof(int));
if(p == NULL)
printf("Error! memory not allocated.");
exit(0);
printf("Enter elements of array : ");
for(i = 0; i< n; ++i)
scanf("%d", p + i);
s += *(p + i);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Realloc() Function
printf("Sum : %d", s);
p = (int*) realloc(p, 6);
printf("Enter elements of array : ");
for(i = 0; i< n; ++i)
scanf("%d", p + i);
s += *(p + i);
printf("Sum : %d", s);
return 0;
4.Sample Input and Output:
Enter elements of array : 2 3 5 4
Sum : 14
Enter elements of array : 2 3 4 5 0 1
Sum : 28
5.Result:
Successfully Executed.
6.Reference:
https://www.tutorialspoint.com/use-of-realloc-in-c
Dept of IT, GIETEngineeringCollege, Rajahmundry
Singly Linked List
Singly Linked List
Exp.No:
Date:
1.Aim:
Implement a C program to Create and display a singly linked list using self-referential structure.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
struct node *next;
};
int main()
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
head->data = 1;
second->data = 2;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Singly Linked List
third->data = 3;
head->next = second;
second->next = third;
third->next = NULL;
struct node *current = head;
while (current != NULL)
printf("%d ", current->data);
current = current->next;
return 0;
4.Sample Input and Output:
123
5.Result:
Successfully Executed.
6.Reference:
https://www.prepbytes.com/blog/c-programming/self-referential-structure-in-c/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Differences between structures and unions
Differences between structures and unions
Exp.No:
Date:
1.Aim:
Demonstrate the differences between structures and unions using a C program.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
struct Person
char name[50];
int age;
};
union Data {
int integerData;
float floatData;
};
int main()
struct Person person1;
strcpy(person1.name, "John");
person1.age = 25;
union Data data1;
data1.integerData = 42;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Differences between structures and unions
printf("Structure - Person:\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("\n");
printf("Union - Data:\n");
printf("Integer Data: %d\n", data1.integerData);
data1.floatData = 3.14;
printf("Float Data: %f\n", data1.floatData);
return 0;
4.Sample Input and Output:
Structure - Person:
Name: John
Age: 25
Union - Data:
Integer Data: 42
Float Data: 3.140000
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Shift/Rotate using bitfields
Shift/Rotate using bitfields
Exp.No:
Date:
1.Aim:
Implement a C program to shift/rotate using bitfields.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
struct BitfieldRotation
unsigned int data : 4;
};
void leftRotate(struct BitfieldRotation *bf)
bf->data = ((bf->data << 1) | (bf->data >> 3)) & 0x0F;
void rightRotate(struct BitfieldRotation *bf)
bf->data = ((bf->data >> 1) | (bf->data << 3)) & 0x0F;
void displayBitfield(struct BitfieldRotation bf)
printf("Bitfield Data: %X\n", bf.data);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Shift/Rotate using bitfields
int main()
struct BitfieldRotation bitfield;
bitfield.data = 0xC;
printf("Original Bitfield:\n");
displayBitfield(bitfield);
leftRotate(&bitfield);
printf("\nAfter Left Rotation:\n");
displayBitfield(bitfield);
rightRotate(&bitfield);
printf("\nAfter Right Rotation:\n");
displayBitfield(bitfield);
return 0;
4.Sample Input and Output:
Original Bitfield:
Bitfield Data: C
After Left Rotation:
Bitfield Data: 9
After Right Rotation:
Bitfield Data: C
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy one structure variable to another structure of the same type
Copy one structure variable to another structure of the same type
Exp.No:
Date:
1.Aim:
Implement a C program to copy one structure variable to another structure of the same type.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
struct Student
char name[50];
int age;
float marks;
};
void copyStruct(struct Student *dest, const struct Student *src)
strcpy(dest->name, src->name);
dest->age = src->age;
dest->marks = src->marks;
void displayStudent(const struct Student *student)
printf("Name: %s\n", student->name);
printf("Age: %d\n", student->age);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy one structure variable to another structure of the same type
printf("Marks: %.2f\n", student->marks);
int main()
struct Student student1 = {"John Doe", 20, 85.5};
struct Student student2;
copyStruct(&student2, &student1);
printf("Original Student:\n");
displayStudent(&student1);
printf("\nCopied Student:\n");
displayStudent(&student2);
return 0;}
4.Sample Input and Output:
Original Student:
Name: John Doe
Age: 20
Marks: 85.50
Copied Student:
Name: John Doe
Age: 20
Marks: 85.50
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
NCR value using Function
NCR value using Function
Exp.No:
Date:
1.Aim:
Implement a C program to calculate NCR value using Functions.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int fact(int z);
void main()
int n, r, ncr;
printf("\n Enter the value for N and R \n");
scanf("%d%d", &n, &r);
ncr = fact(n) / (fact(r) * fact(n - r));
printf("\n The value of ncr is: %d", ncr);
int fact(int z)
int f = 1, i;
if (z == 0)
return(f);
Dept of IT, GIETEngineeringCollege, Rajahmundry
NCR value using Function
else
for (i = 1; i<= z; i++)
f = f * i;
return(f);
4.Sample Input and Output:
Enter the value for N and R
52
The value of ncr is: 10
5.Result:
Successfully Executed.
6.Reference:
https://www.sanfoundry.com/c-program-ncr/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Find the length of a string
Find the length of a string
Exp.No:
Date:
1.Aim:
Implement a C program to find the length of a string using Functions.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
#include<string.h>
int main() {
char str[100]; int len;
printf("\nEnter the String : ");
scanf("%s",str);
len = strlen(str);
printf("\nLength of the given string is %d", len);
return(0);}
4.Sample Input and Output:
Enter the String :deepthi
Length of the given string is 7
5.Result:
Successfully Executed.
6.Reference:
https://www.codingninjas.com/studio/library/c-program-to-count-length-of-string-using-library-
function
Dept of IT, GIETEngineeringCollege, Rajahmundry
Transpose of a Matrix using Function
Transpose of a Matrix using Function
Exp.No:
Date:
1.Aim:
Implement a C program to find Transpose of a Matrix using Function.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#define N 4
void transpose(int A[][N], int B[][N])
int i, j;
for (i = 0; i< N; i++)
for (j = 0; j < N; j++)
B[i][j] = A[j][i];
int main()
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[N][N], i, j;
transpose(A, B);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Transpose of a Matrix using Function
printf("Result matrix is \n");
for (i = 0; i< N; i++) {
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
return 0;
4.Sample Input and Output:
Result matrix is
1234
1234
1234
1234
5.Result:
Successfully Executed.
6.Reference:
https://www.geeksforgeeks.org/c-transpose-matrix/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Euler’s method.
Euler’s method.
Exp.No:
Date:
1.Aim:
Implement a C program to demonstrate numerical integration of differential equations using
Euler’s method.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
double equation(double x, double y)
return x + y;
void eulerMethod(double x0, double y0, double h, double xn) {
double x = x0, y = y0;
printf("Euler's Method:\n");
printf("x\t\t y\n");
while (x <= xn) {
printf("%.4f\t%.4f\n", x, y);
y = y + h * equation(x, y);
x = x + h;
int main() {
Dept of IT, GIETEngineeringCollege, Rajahmundry
Euler’s method.
double x0 = 0.0;
double y0 = 1.0;
double h = 0.1;
double xn = 1.0;
eulerMethod(x0, y0, h, xn);
return 0;}
4.Sample Input and Output:
Euler's Method:
x y
0.0000 1.0000
0.1000 1.1000
0.2000 1.2200
0.3000 1.3620
0.4000 1.5282
0.5000 1.7210
0.6000 1.9431
0.7000 2.1974
0.8000 2.4872
0.9000 2.8159
1.0000 3.1875
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Fibonacci Series.
Fibonacci series
Exp.No:
Date:
1.Aim:
Write a recursive function to generate Fibonacci series.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int fibonacci(int num)
if (num == 0)
return 0;
else if (num == 1)
return 1;
else
return fibonacci(num - 1) + fibonacci(num - 2);
int main()
Dept of IT, GIETEngineeringCollege, Rajahmundry
Fibonacci Series.
int num;
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num);
int i;
for (i = 0; i< num; i++)
printf("%d, ", fibonacci(i));
return 0;
4.Sample Input and Output:
Enter the number of elements to be in the series : 10
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
5.Result:
Successfully Executed.
6.Reference:
https://www.scaler.com/topics/fibonacci-series-in-c-using-recursion/
Dept of IT, GIETEngineeringCollege, Rajahmundry
LCM of two numbers
LCM of two Numbers
Exp.No:
Date:
1.Aim:
Write a recursive function to find the lcm of two numbers.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
int lcm(int a, int b)
return (a * b);
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("LCM of %d and %d is: %d\n", num1, num2, lcm(num1, num2));
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
LCM of two numbers
4.Sample Input and Output:
Enter the first number: 5 3
Enter the second number: LCM of 5 and 3 is: 15
5.Result:
Successfully Executed.
6.Reference:
https://codeforwin.org/c-programming/c-program-to-find-lcm-of-two-numbers-using-recursion
Dept of IT, GIETEngineeringCollege, Rajahmundry
Factorial of a numbers
Factorial of a Number
Exp.No:
Date:
1.Aim:
Write a recursive function to find the factorial of a number.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
unsigned long longfactorial(int n)
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
int main()
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Factorial of a numbers
if (num < 0)
printf("Please enter a non-negative integer.\n");
else
printf("Factorial of %d is: %llu\n", num, factorial(num));
return 0;
4.Sample Input and Output:
Enter a non-negative integer: 5
Factorial of 5 is: 120
5.Result:
Successfully Executed.
6.Reference:
https://www.programiz.com/c-programming/examples/factorial-recursion
Dept of IT, GIETEngineeringCollege, Rajahmundry
Ackermann function using recursion
Ackermann function using recursion
Exp.No:
Date:
1.Aim:
Write a C Program to implement Ackermann function using recursion.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
int A(int m, int n);
main()
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
int A(int m, int n)
if(m==0)
return n+1;
else if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
Dept of IT, GIETEngineeringCollege, Rajahmundry
Ackermann function using recursion
4.Sample Input and Output:
Enter two numbers ::
13
OUTPUT :: 5
5.Result:
Successfully Executed.
6.Reference:
https://www.codezclub.com/c-implement-ackermann-function/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sum of Series using Recursive Function
Sum of Series using Recursive Function
Exp.No:
Date:
1.Aim:
Write a recursive function to find the sum of series.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
double seriesSum(int n)
if (n == 0)
return 0;
else
return 1.0 / n + seriesSum(n - 1);
int main()
int terms;
printf("Enter the number of terms in the series: ");
scanf("%d", &terms);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Sum of Series using Recursive Function
if (terms < 0)
printf("Please enter a non-negative integer for the number of terms.\n");
else
printf("Sum of the series: %lf\n", seriesSum(terms));
return 0;
4.Sample Input and Output:
Enter the number of terms in the series: 5
Sum of the series: 2.283333
5.Result:
Successfully Executed.
6.Reference:
https://www.geeksforgeeks.org/c-program-to-find-sum-of-natural-numbers-using-recursion/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Swapping of two numbers using call by reference
Swapping of two numbers using call by reference
Exp.No:
Date:
1.Aim:
Write a C program to swap two numbers using call by reference.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
swap (int *, int *);
int main()
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
swap (int *x, int *y)
int temp;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Swapping of two numbers using call by reference
temp = *x;
*x = *y;
*y = temp;
4.Sample Input and Output:
Enter value of a & b: 30 49
Before Swapping:
a = 30
b = 49
After Swapping:
a = 49
b = 30
5.Result:
Successfully Executed.
6.Reference:
https://www.programming9.com/programs/c-programs/194-swapping-of-two-numbers-using-
call-by-reference-in-c-functions
Dept of IT, GIETEngineeringCollege, Rajahmundry
Dangling pointer problem
Dangling pointer problem
Exp.No:
Date:
1.Aim:
Demonstrate Dangling pointer problem using a C program.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int* createIntArray(int size)
int* arr = (int*)malloc(size * sizeof(int));
return arr;
int main()
int* arr = createIntArray(5);
for (int i = 0; i< 5; i++)
arr[i] = i + 1;
printf("Values using valid pointer:\n");
for (int i = 0; i< 5; i++)
Dept of IT, GIETEngineeringCollege, Rajahmundry
Dangling pointer problem
printf("%d ", arr[i]);
printf("\n");
free(arr);
printf("Values using dangling pointer:\n");
for (int i = 0; i< 5; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
4.Sample Input and Output:
Values using valid pointer:
12345
Values using dangling pointer:
0 0 35393552 0 5
5.Result:
Successfully Executed.
6.Reference:
https://www.prepbytes.com/blog/c-programming/dangling-pointer-in-c/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy one string into another
Copy one string into another
Exp.No:
Date:
1.Aim:
Implement a C program to copy one string into another using pointer.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
void copy_string(char*, char*);
main()
char source[100], target[100];
printf("Enter source string\n");
gets(source);
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;
void copy_string(char *target, char *source)
while(*source)
*target = *source;
source++;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy one string into another
target++;
*target = '\0';
4.Sample Input and Output:
Enter source string
saideepthi
Target string is "saideepthi"
5.Result:
Successfully Executed.
6.Reference:
https://www.w3schools.in/c-programming/examples/copy-string-using-pointers
Dept of IT, GIETEngineeringCollege, Rajahmundry
No of Lowercase,Uppercase,Digits and other characters using pointers
No of Lowercase,Uppercase,Digits and other characters using
pointers
Exp.No:
Date:
1.Aim:
Implement a C program to find no of lowercase, uppercase, digits and other characters using
pointers.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int main()
char str[100];
int i;
int upper=0,lower=0,num=0,special=0;;
printf("Please enter the string \n");
gets(str);
for(i=0; str[i] != '\0'; i++)
if(str[i]>='A' && str[i]<='Z')
upper++;
Dept of IT, GIETEngineeringCollege, Rajahmundry
No of Lowercase,Uppercase,Digits and other characters using pointers
else if(str[i]>='a' && str[i]<='z')
lower++;
else if(str[i]>='1' && str[i]<='9')
num++;
else
special++;
printf("\nUpper case letters: %d",upper);
printf("\nLower case letters: %d",lower);
printf("\nNumbers: %d",num);
printf("\nSpecial characters: %d",special);
getch();
Dept of IT, GIETEngineeringCollege, Rajahmundry
No of Lowercase,Uppercase,Digits and other characters using pointers
4.Sample Input and Output:
Please enter the string
Sai Deepthi @1
Upper case letters: 2
Lower case letters: 8
Numbers: 1
Special characters: 3
5.Result:
Successfully Executed.
6.Reference:
https://code4coding.com/program-to-count-uppercase-lowercasespecial-character-and-numeric-
values-in-c/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Write and Read Text into a file
Write and Read Text into a file
Exp.No:
Date:
1.Aim:
Write a C program to write and read text into a file.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
file = fopen("example.txt", "w");
if (file == NULL)
printf("Could not open the file for writing.\n");
return 1;
fprintf(file, "Hello, this is a sample text.\n");
fprintf(file, "Writing to a file in C is easy!\n");
fclose(file);
file = fopen("example.txt", "r");
if (file == NULL)
Dept of IT, GIETEngineeringCollege, Rajahmundry
Write and Read Text into a file
printf("Could not open the file for reading.\n");
return 1;
char ch;
printf("Content of the file:\n");
while ((ch = fgetc(file)) != EOF)
putchar(ch);
fclose(file);
return 0;
4.Sample Input and Output:
Content of the file:
Hello, this is a sample text.
Writing to a file in C is easy!
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
write and read text into a binary file using fread() and fwrite()
write and read text into a binary file using fread() and fwrite()
Exp.No:
Date:
1.Aim:
Implement a C program to write and read text into a binary file using fread() and fwrite().
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
struct Person
char name[50];
int age;
};
int main()
FILE *file;
file = fopen("example.bin", "wb");
if (file == NULL)
printf("Could not open the file for writing.\n");
return 1;
struct Person person1 = {"deepu", 25};
Dept of IT, GIETEngineeringCollege, Rajahmundry
write and read text into a binary file using fread() and fwrite()
struct Person person2 = {"divitha", 05};
fwrite(&person1, sizeof(struct Person), 1, file);
fwrite(&person2, sizeof(struct Person), 1, file);
fclose(file);
file = fopen("example.bin", "rb");
if (file == NULL)
printf("Could not open the file for reading.\n");
return 1;
struct Person readPerson1, readPerson2;
fread(&readPerson1, sizeof(struct Person), 1, file);
fread(&readPerson2, sizeof(struct Person), 1, file);
printf("Data read from the file:\n");
printf("Person 1: Name=%s, Age=%d\n", readPerson1.name, readPerson1.age);
printf("Person 2: Name=%s, Age=%d\n", readPerson2.name, readPerson2.age);
fclose(file);
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
write and read text into a binary file using fread() and fwrite()
4.Sample Input and Output:
Data read from the file:
Person 1: Name=deepu, Age=25
Person 2: Name=divitha, Age=5
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy the contents of one file to another file.
Copy the contents of one file to another file.
Exp.No:
Date:
1.Aim:
Copy the contents of one file to another file.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *sourceFile, *destinationFile;
sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL)
printf("Could not open the source file for reading.\n");
return 1;
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL)
printf("Could not open the destination file for writing.\n");
fclose(sourceFile);
return 1;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Copy the contents of one file to another file.
char ch;
while ((ch = fgetc(sourceFile)) != EOF)
fputc(ch, destinationFile);
fclose(sourceFile);
fclose(destinationFile);
printf("File contents copied successfully.\n");
return 0;
4.Sample Input and Output:
Could not open the source file for reading.
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Merge two files into the third file using command-line arguments
Merge two files into the third file using command-line arguments
Exp.No:
Date:
1.Aim:
Write a C program to merge two files into the third file using command-line arguments.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <inputFile1><inputFile2><outputFile>\n", argv[0]);
return 1;
FILE *inputFile1, *inputFile2, *outputFile;
inputFile1 = fopen(argv[1], "r");
if (inputFile1 == NULL) {
printf("Could not open the first input file for reading.\n");
return 1; // Return an error code
inputFile2 = fopen(argv[2], "r");
if (inputFile2 == NULL) {
printf("Could not open the second input file for reading.\n");
fclose(inputFile1);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Merge two files into the third file using command-line arguments
return 1; // Return an error code
outputFile = fopen(argv[3], "w");
if (outputFile == NULL) {
printf("Could not open the output file for writing.\n");
fclose(inputFile1);
fclose(inputFile2);
return 1; // Return an error code
char ch;
while ((ch = fgetc(inputFile1)) != EOF) {
fputc(ch, outputFile);
while ((ch = fgetc(inputFile2)) != EOF) {
fputc(ch, outputFile);}
fclose(inputFile1);
fclose(inputFile2);
fclose(outputFile);
printf("Files merged successfully.\n");
return 0; }
4.Sample Input and Output:
Files merged successfully
5.Result:
Successfully Executed.
6.Reference:https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Find no. of lines, words and characters in a file
Find no. of lines, words and characters in a file
Exp.No:
Date:
1.Aim:
Find no. of lines, words and characters in a file
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
void countLinesWordsCharacters(FILE *file, int *lines, int *words, int *characters)
int ch;
int inWord = 0;
*lines = *words = *characters = 0;
while ((ch = fgetc(file)) != EOF)
(*characters)++;
if (ch == '\n')
(*lines)++;
if (ch == ' ' || ch == '\t' || ch == '\n') {
inWord = 0; // Not inside a word
} else if (inWord == 0) {
inWord = 1; // Inside a word
Dept of IT, GIETEngineeringCollege, Rajahmundry
Find no. of lines, words and characters in a file
(*words)++;
int main() {
FILE *file;
char fileName[50];
printf("Enter the name of the file: ");
scanf("%s", fileName);
file = fopen(fileName, "r");
if (file == NULL) {
printf("Could not open the file for reading.\n");
return 1; // Return an error code
int lines, words, characters;
countLinesWordsCharacters(file, &lines, &words, &characters);
fclose(file);
printf("Number of lines: %d\n", lines);
printf("Number of words: %d\n", words);
printf("Number of characters: %d\n", characters);
return 0;
4.Sample Input and Output:
Dept of IT, GIETEngineeringCollege, Rajahmundry
Find no. of lines, words and characters in a file
Enter the name of the file: sai
Could not open the file for reading.
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry
Print last n characters of a given file
Print last n characters of a given file
Exp.No:
Date:
1.Aim:
Write a C program to print last n characters of a given file.
2.Software Required:
TURBO C
3.Program:
#include <stdio.h>
#include <stdlib.h>
void printLastNCharacters(FILE *file, int n)
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
long startPosition = fileSize - n;
if (startPosition< 0)
startPosition = 0;
fseek(file, startPosition, SEEK_SET);
int ch;
while ((ch = fgetc(file)) != EOF)
putchar(ch);
Dept of IT, GIETEngineeringCollege, Rajahmundry
Print last n characters of a given file
int main()
FILE *file;
char fileName[50];
int n;
printf("Enter the name of the file: ");
scanf("%s", fileName);
file = fopen(fileName, "r");
if (file == NULL)
printf("Could not open the file for reading.\n");
return 1;
printf("Enter the value of 'n': ");
scanf("%d", &n);
printf("Last %d characters of the file:\n", n);
printLastNCharacters(file, n);
fclose(file);
return 0;
Dept of IT, GIETEngineeringCollege, Rajahmundry
Print last n characters of a given file
4.Sample Input and Output:
Enter the name of the file: deepthi
Could not open the file for reading.
5.Result:
Successfully Executed.
6.Reference:
https://www.vtuloop.com/program-13-c-programming-lab/
Dept of IT, GIETEngineeringCollege, Rajahmundry