0613-122 - Programming I
0613-122 - Programming I
Programming I
[1]
Lab Guidelines
1. Read the lab manual carefully and understand the objective and requirements of the
experiment before starting.
2. Follow the instructions provided in the manual step by step, making sure to
complete each task before moving on to the next one.
3. Document your code properly by adding comments to explain the purpose and logic
of each segment. This will help you and your lab instructor understand your code
more easily.
4. Use proper programming syntax, formatting, and indentation to make your code
more readable and easy to follow.
5. Use meaningful variable names that reflect the purpose of the variable.
6. Test your code frequently as you work on the experiment to make sure it is
functioning correctly and producing the desired results.
7. Before submitting your lab report, double-check your code for any syntax errors or
logical errors that may cause unexpected results.
8. Install and configure any required software or programming environments before
starting the experiment.
9. Make sure to save and backup your code regularly to avoid losing any progress in
case of computer or software malfunctions.
10. Seek help from your lab instructor or classmates if you encounter any issues or
have any questions about the experiment or the programming language being used.
[2]
Content
Programming I ......................................................................................................................... 1
Lab Guidelines ......................................................................................................................... 2
Content ...................................................................................................................................... 3
Experiment no: 01 .................................................................................................................... 4
Experiment no: 02 .................................................................................................................... 7
Experiment no: 03 .................................................................................................................. 12
Experiment no: 04 .................................................................................................................. 14
Experiment no: 05 .................................................................................................................. 17
Experiment no: 06 .................................................................................................................. 21
Experiment no: 07 .................................................................................................................. 25
Experiment no: 08 .................................................................................................................. 27
Experiment no: 09 .................................................................................................................. 29
Experiment no: 10 .................................................................................................................. 31
Experiment no: 11 .................................................................................................................. 35
Experiment no: 12 .................................................................................................................. 39
[3]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 01
Objectives:
1. To introduce students to the basic components of a computer system and their roles in computer
organization and architecture.
2. To explain the difference between a compiler and an interpreter and their respective roles in
translating high-level programming languages into machine code.
3. To familiarize students with the different types of Integrated Development Environments
(IDEs) available for C programming and their features.
Theory:
Computer Organization:
Computer organization refers to the way a computer's hardware is designed and
arranged to perform specific tasks. It includes the design of the central processing unit
(CPU), memory, input/output devices, and other hardware components that make up a
computer system. The goal of computer organization is to ensure that the computer can
execute programs and carry out tasks as efficiently as possible.
[4]
Computer Architecture:
Computer architecture refers to the design of the computer system and its components, such
as the central processing unit (CPU), memory, input/output devices, and the interconnects
between these components. It includes the instruction set architecture (ISA), which is the set
of instructions that a CPU can execute, as well as the organization of the memory hierarchy,
bus structure, and other system-level features.
Compiler:
A compiler is a program that translates the source code written in a high-level programming
language, such as C, into machine code that can be executed by a computer. The process of
converting the source code into machine code is known as compilation. A compiler checks
[5]
the syntax and structure of the program and generates an executable file, which can be run on
the computer.
Interpreter:
An interpreter is a program that reads the source code of a program line by line and executes
each line immediately. The interpreter does not generate an executable file, but it runs the
program directly. The interpreter checks the syntax and structure of each line of the program
before executing it.
IDE:
An IDE, or Integrated Development Environment, is a software application that provides a
comprehensive development environment for programmers. It typically includes a text editor,
a compiler or interpreter, debugging tools, and other features to help programmers write and
test their code. An IDE can make the process of developing software faster and easier by
providing tools for code editing, debugging, and testing in one place.
To get started with C programming, you can use the following tools:
Compiler: GCC is a popular compiler for C programming. It is open-source and available for
multiple platforms.
Interpreter: CINT is an interpreter for C programming that allows you to execute C code
interactively.
IDE: Code::Blocks is a popular open-source IDE for C programming. It includes a code
editor, compiler, and debugger, as well as other tools to help you develop C programs.
[6]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 02
Objectives:
1. Understanding the structure and organization of C programming.
2. Mastery of C programming keywords and operators for writing efficient code.
3. Developing the ability to create algorithms and flowcharts to design and implement solutions
to complex problems.
Theory:
History of C:
C is a general-purpose programming language developed by Dennis Ritchie at Bell
Labs in 1972. It was designed to be used for systems programming and quickly became
popular among programmers because of its efficiency, simplicity, and flexibility. C was
an evolution of the earlier programming language called B, which was also developed
at Bell Labs. C became widely adopted in the 1980s and 1990s because of the increasing
popularity of personal computers and the need for efficient, portable software.
C has since become a foundational language in computer science and is used in a wide
range of applications, including operating systems, device drivers, compilers, and
applications software. Its influence can be seen in many modern programming
languages, such as C++, Java, and Python. C programming language continues to be
widely used today, over 50 years after its creation.
Structure of C program:
The structure of a C program is typically divided into several parts that include:
1. Preprocessor Directives: These are statements that begin with a hash (#) symbol and
are used to include header files, define constants, and perform other pre-compilation
tasks.
2. Global Declarations: These are variables and functions that are defined outside of any
function and can be accessed from any part of the program.
3. main() Function: This is the starting point of the program and is required in every C
program. All other functions are called from the main() function.
4. Function Definitions: These are the individual functions that are defined in the program
to perform specific tasks. Each function has a unique name and a set of input and output
parameters.
[7]
5. Statements and Expressions: These are the actual instructions that perform the desired
tasks. C programs use a variety of statements and expressions, including assignment
statements, conditional statements, loop statements, and function calls.
6. Comments: These are non-executable statements that are used to provide information
about the program and explain how it works. Comments begin with /* and end with */.
/* Preprocessor Directives */
#include <stdio.h>
/* Global Declarations */
int global_var = 10;
/* Function Declarations */
void my_function();
/* Main Function */
int main() {
/* Statements and Expressions */
printf("Hello World!\n");
my_function();
return 0;
}
/* Function Definitions */
void my_function() {
int local_var = 20;
[8]
In this example, the program begins with preprocessor directives that include the
standard input/output header file. The global declarations include a global variable,
and function declarations declare a function called my_function(). The main
function contains statements and expressions that output "Hello World!" and call
the my_function() function. Finally, the my_function() function is defined with
local variable declarations and additional statements and expressions that output the
values of the global and local variables.
Keywords:
C programming language has a set of keywords and operators that are used to create
statements and expressions.
C has 32 keywords (reserved words with special meaning):
1. auto,
2. break,
3. case,
4. char,
5. const,
6. continue,
7. default,
8. do,
9. double,
10. else,
11. enum,
12. extern,
13. float,
14. for,
15. goto,
16. if,
17. int,
18. long,
19. register,
20. return,
21. short,
22. signed,
23. sizeof,
24. static,
25. struct,
26. switch,
27. typedef,
28. union,
29. unsigned,
30. void,
31. volatile, and
32. while.
[9]
Operators:
C supports a rich set of operators, which are symbols used within an expression to
specify the manipulations to be performed while evaluating that expression. C has the
following operators:
arithmetic: +, -, *, /, % assignment: =
augmented assignment: +=, - =, *=, /=, %=, &=, |=, ^=, <<=, >>=
bitwise logic: ~, &, |, ^
bitwise shifts: <<, >>
boolean logic: !, &&, ||
conditional evaluation: ? :
equality testing: = =, !=
calling functions: ( )
increment and decrement: ++ and - -
object size: sizeof
order relations: <, <=, >, >=
reference and dereference: &, *, [ ]
sequencing: ,
subexpression grouping: ( )
type conversion: (typename)
Salient Features of C:
C language has some characteristics that define the language and also have led to its
popularity as a programming language.
• Small size.
• Extensive use of function calls.
• Structured language.
• Low level (Bitwise) programming readily available.
• Pointer implementation - extensive use of pointers for memory, array,
structures and functions.
• It has high-level constructs.
• It can handle low-level activities.
• It produces efficient programs.
• It can be compiled on a variety of computers.
[10]
Algorithm:
It is a method of representing the step by step process for solving a problem. Each step
is called an instruction.
Characteristics of algorithm are:
• Step1: BEGIN
• Step2: READ a, b
• Step3: ADD a and b and store in variable c
• Step4: DISPLAY c
• Step5: STOP
[11]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 03
Name of the Experiment: Write a C Program using C syntax, structure, comments, and
descriptive variables.
Objectives:
4. Familiarize with C programming syntax, keywords, and operators.
5. Understand how to use variables and user input in C programming.
6. Learn how to write basic arithmetic operations in C programming.
Program:
Write a C program that prompts the user to enter two integers and displays their sum,
difference, and product.
Instructions:
• Declare two integer variables and prompt the user to enter their values.
• Read in the user's input and assign the values to the variables.
• Calculate the sum, difference, and product of the two integers.
• Display the results to the user.
Algorithm:
1. Start
2. Declare two integer variables (a and b)
3. Prompt the user to enter two integers
4. Read in the user's input and assign values to variables a and b
5. Calculate sum, difference, and product of a and b
6. Display the results to the user
7. Stop
Code:
[12]
C Input:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
return 0;
}
C Output:
Enter two integers: 10 5
Sum: 15
Difference: 5
Product: 50
Output Explanation:
• The program prompts the user to enter two integers and then uses the scanf() function to
read in the values. It then performs the arithmetic operations on the two integers and
displays the results to the user using the printf() function.
Practice Problems:
1. Write a C program to convert temperature from Celsius to Fahrenheit and vice versa.
(Difficulty: Same)
2. Write a C program to calculate the area and perimeter of a rectangle given its length and width.
(Difficulty: Easier)
3. Write a C program to check if a given number is even or odd. (Difficulty: Same)
4. Write a C program to calculate the factorial of a given number. (Difficulty: Difficult)
5. Write a C program to find the greatest common divisor (GCD) of two integers. (Difficulty:
Difficult)
[13]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 04
Name of the Experiment: Write a C program to take input related to different data types,
Operators and Expressions in C.
Objectives:
1. Familiarize the students with different data types, operators, and expressions in C
programming.
2. Develop the skills of taking input and outputting results in C programming.
3. Improve problem-solving skills by performing operations using variables.
Program:
Write a C program that takes input from the user related to different data types,
operators, and expressions in C. The program should take the input for a few variables,
perform operations using these variables, and output the results.
Instructions:
• Declare variables of different data types (int, float, char, etc.)
• Take input for each variable from the user
• Perform various operations (arithmetic, relational, and logical) on the variables
• Output the results of the operations
Algorithm:
1. Start
2. Declare variables of different data types (int, float, char, etc.)
3. Take input for each variable from the user using scanf() function
4. Perform various operations (arithmetic, relational, and logical) on the variables
5. Output the results of the operations using printf() function
6. Stop
Code:
[14]
C Input:
#include <stdio.h>
int main() {
int num1, num2;
float float_num;
char ch;
return 0;
}
[15]
C Output:
Enter an integer: 10
Enter another integer: 5
Enter a float: 3.14
Enter a character: a
Output Explanation:
• In this program, we declared variables of different data types like int, float, and
char. Then we took input from the user for these variables using the scanf()
function. We performed arithmetic operations like addition, subtraction,
multiplication, and division using the int variables and outputted the results using
the printf() function. We also outputted the value of the float variable and the
character entered by the user.
Practice Problems:
1. Write a C program to take input for two integers and output their remainder after
division. (Difficulty: Easier)
2. Write a C program to take input for the radius of a circle and output its area. (Difficulty:
Same)
3. Write a C program to take input for three integers and output their average. (Difficulty:
Same)
4. Write a C program to take input for a temperature in Celsius and output its equivalent
in Fahrenheit. (Difficulty: Same)
5. Write a C program to take input for two integers and output the larger of the two.
(Difficulty: Difficult)
[16]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 05
Name of the Experiment: Write a program using decision statements (if, if-else, if-else-
if ladder, switch and GOTO).
Objectives:
1. To introduce decision statements in C programming.
2. To teach how to use if, if-else, if-else-if ladder, switch and GOTO statements in C
programming.
3. To learn how to take input from the user in C programming.
Program:
Write a C program that takes input of a number and checks if it is positive, negative or
zero using decision statements.
Instructions:
• Take input of a number from the user.
• Check if the number is positive, negative or zero using if-else statements.
• Print the result.
Algorithm:
1. Start
2. Take input of a number from the user.
3. Check if the number is greater than zero using if statement.
4. If the number is greater than zero, print "The number is positive".
5. If the number is less than zero, check if the number is equal to zero using else-if statement.
6. If the number is equal to zero, print "The number is zero".
7. else, print "The number is negative".
8. Stop
Code:
[17]
C Input:
#include <stdio.h>
int main()
{
int num;
if (num > 0)
{
printf("The number is positive\n");
}
else if (num == 0)
{
printf("The number is zero\n");
}
else
{
printf("The number is negative\n");
}
return 0;
}
C Output:
Enter a number: 5
The number is positive
Enter a number: 0
The number is zero
Enter a number: -5
The number is negative
Output Explanation:
The program takes input of a number from the user using the scanf() function and stores it
in the variable "num". Then, it checks if the number is positive, negative or zero using if-
else statements. If the number is greater than zero, it prints "The number is positive". If the
number is less than zero, it checks if the number is equal to zero using else-if statement. If
the number is equal to zero, it prints "The number is zero". If the number is less than zero,
it prints "The number is negative".
[18]
Practice Problems:
1. Write a C program to check if a given year is a leap year or not using if-else statements.
(Difficulty: Same)
2. Write a C program to find the largest of three numbers using if-else-if ladder. (Difficulty: Same)
3. Write a C program to convert a given temperature from Celsius to Fahrenheit using switch
statement. (Difficulty: Same)
4. Write a C program to find the factorial of a given number using GOTO statement. (Difficulty:
Easier)
5. Write a C program to find the sum of all even numbers between 1 to n using if-else statements.
(Difficulty: Difficult)
[19]
[20]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 06
Name of the Experiment: Write a program implementing loop & nested loop statements
(for, while, do-while).
Objectives:
1. To familiarize students with for loops in C programming
2. To teach students how to use loops to perform repetitive tasks
3. To improve students' understanding of arithmetic operators in C programming
Program:
Write a C program that takes an integer input from the user and prints the multiplication
table of that number using a for loop.
Instructions:
• Ask the user to input an integer number.
• Use a for loop to print the multiplication table of that number.
• The multiplication table should start from 1 and end at 10.
• Print each line of the multiplication table on a separate line.
• Use proper variable naming and comments to make the code easy to read.
Algorithm:
1. Start
2. Ask the user to input an integer number.
3. Use a for loop to loop through the numbers 1 to 10.
4. Multiply the input number by the current loop index and store it in a variable.
5. Print the variable to the console.
6. Stop
Code:
[21]
C Input:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
return 0;
}
C Output:
Enter a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Output Explanation:
• In this program, the user is prompted to input a number. The program then uses a for loop
to loop through the numbers 1 to 10. For each number, the program multiplies it by the
input number and stores the result in a variable. Finally, the program prints the
multiplication table to the console in the format "input_number x loop_index = result".
Practice Problems:
1. Write a program that calculates the sum of the first 50 even numbers using a for loop.
(Difficulty: Same)
2. Write a program that takes an integer input from the user and prints all of its factors using a for
loop. (Difficulty: Easier)
3. Write a program that takes an integer input from the user and checks if it is a prime number
using a for loop. (Difficulty: Same)
[22]
4. Write a program that takes two integer inputs from the user and prints all the prime numbers
between them using a for loop. (Difficulty: Difficult)
5. Write a program that takes an integer input from the user and prints the sum of all the digits of
the number using a for loop. (Difficulty: Easier)
[23]
[24]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 07
Name of the Experiment: Write a program to insert multiple elements into an array and
print the elements of the array, using different dimensions of array.
Objectives:
1. To learn how to take input from the user and store it in an array.
2. To learn how to print the elements of an array.
3. To understand the concept of arrays in C.
Program:
Write a C program that takes input of multiple elements from the user and stores them
in an array of size 10. The program should then print out the elements of the array.
Instructions:
• Declare an array of size 10.
• Use a loop to take input of multiple elements from the user.
• Store each element in the array.
• Use a loop to print out the elements of the array
Algorithm:
1. Start
2. Declare an integer array of size 10.
3. Use a for loop to take input of 10 integers from the user and store them in the array.
4. Use another for loop to print out the elements of the array.
5. Stop
Code:
[25]
C Input:
#include <stdio.h>
int main()
{
int arr[10], i;
return 0;
}
C Output:
Enter 10 integers: 1 2 3 4 5 6 7 8 9 10
Elements of array are: 1 2 3 4 5 6 7 8 9 10
Output Explanation:
• The program first takes input of 10 integers from the user and stores them in an integer
array of size 10. It then prints out the elements of the array. In the output, we can see that
the elements of the array are printed as entered by the user.
Practice Problems:
1. Write a program to take input of 5 decimal numbers from the user and store them in an array.
Then, calculate and print the sum of all the numbers in the array. (Difficulty: Same)
2. Write a program to take input of 3 names from the user and store them in a 2D array. Then,
print out the names in the format "First name Last name". (Difficulty: Same)
3. Write a program to take input of 6 integers from the user and store them in a 2D array of size
2x3. Then, calculate and print the sum of all the numbers in the array. (Difficulty: Easier)
4. Write a program to take input of 10 integers from the user and store them in an array. Then,
find and print out the largest number in the array. (Difficulty: Easier)
5. Write a program to take input of 4 strings from the user and store them in a 2D array. Then,
print out the strings in reverse order. (Difficulty: Difficult)
[26]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 08
Name of the Experiment: Write a program implementing function programming, its types
and function-call.
Objectives:
1. To understand the concept of functions in C programming.
2. To be able to write a user-defined function.
3. To practice function-call in C programming.
Program:
Write a program in C that uses function programming to find the sum of two numbers
and print the result.
Instructions:
• Define a function sum that takes two integer parameters a and b and returns their sum.
• Call the function sum in the main function and pass two integers as arguments.
• Store the result returned by the sum function in a variable.
• Print the result on the screen.
Algorithm:
1. Start
2. Define the function sum(a,b) that takes two integer parameters a and b.
3. Inside the sum function, declare an integer variable result.
4. Compute the sum of a and b and store it in result.
5. Return result from the sum function.
6. In the main function, declare two integer variables x and y and initialize them with some
values.
7. Call the sum function and pass x and y as arguments.
8. Store the result returned by the sum function in an integer variable z.
9. Print z on the screen.
10. Stop
Code:
[27]
C Input:
#include <stdio.h>
int main()
{
int x = 5, y = 7, z;
z = sum(x, y);
printf("The sum of %d and %d is %d\n", x, y, z);
return 0;
}
C Output:
Output Explanation:
In this program, we have defined a user-defined function sum that takes two integer
parameters a and b and returns their sum. In the main function, we have called the sum
function and passed two integers as arguments. The result returned by the sum function is
stored in a variable z, which is then printed on the screen. The output shows the sum of x
and y i.e., 5 and 7 respectively, which is 12.
Practice Problems:
1. Write a program to find the factorial of a given number using a user-defined function.
(Difficulty: Easier)
2. Write a program to convert Fahrenheit to Celsius using a user-defined function. (Difficulty:
Same)
3. Write a program to check whether a given number is prime or not using a user-defined function.
(Difficulty: Same)
4. Write a program to reverse a given string using a user-defined function. (Difficulty: Difficult)
5. Write a program to sort an array of integers in ascending order using a user-defined function.
(Difficulty: Difficult)
[28]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 09
Name of the Experiment: Write a program using pointer, string and function call by
reference.
Objectives:
1. Implement function programming in C.
2. Use pointers to modify string data.
3. Familiarize students with function call by reference in C.
Program:
Write a C program that takes a string input from the user, converts all the characters to
uppercase using a function, and prints the modified string using a pointer.
Instructions:
• Take a string input from the user.
• Pass the string to a function that converts all the characters to uppercase.
• Modify the original string using pointers and function call by reference.
• Print the modified string using a pointer.
Algorithm:
1. Start
2. Take a string input from the user.
3. Declare a function that takes a string parameter and converts all the characters to uppercase.
4. Pass the string to the function and modify the original string using pointers and function call
by reference.
5. Print the modified string using a pointer.
6. Stop
Code:
[29]
C Input:
#include <stdio.h>
#include <ctype.h>
int main(){
char str[100];
printf("Enter a string: ");
scanf("%[^\n]", str);
convertToUpper(str);
printf("Modified string: %s", str);
return 0;
}
C Output:
Output Explanation:
1. The program prompts the user to enter a string input using printf() and takes the input
using scanf().
2. The function convertToUpper() is defined, which takes a string parameter and converts
all the characters to uppercase using toupper() function. The string is modified using
pointers and function call by reference.
3. The convertToUpper() function is called with the string parameter str in the main
function.
4. The modified string is printed using a pointer in the printf() statement in the main
function.
Practice Problems:
1. Write a C program that takes two integer inputs from the user and swaps their values using a
function call by reference. (Difficulty: Same)
2. Write a C program that takes a string input from the user and prints the length of the string
using a function and a pointer. (Difficulty: Same)
3. Write a C program that takes an array of integers as input from the user, finds the maximum
element in the array using a function and a pointer, and prints the maximum element.
(Difficulty: Easier)
4. Write a C program that takes a matrix as input from the user and calculates the determinant of
the matrix using a function and a pointer. (Difficulty: Difficult)
5. Write a C program that takes a string input from the user, reverses the string using a function
and a pointer, and prints the reversed string. (Difficulty: Easier)
[30]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 10
Name of the Experiment: Write a C program to create, declare and initialize structure,
implementing programming with structure.
Objectives:
1. To understand the concept of structures in C programming.
2. To learn how to create, declare, and initialize a structure in C programming.
3. To practice using structures to store and manipulate data in C programming.
Program:
Write Create a program that creates, declares, and initializes a structure named
"Student" which contains the following fields:
• name (string)
• id (integer)
• grade (float)
Then, create an array of three Student structures, initialize their fields, and print out the
information of each student.
Instructions:
• Declare the Student structure with the fields mentioned above.
• Declare an array of three Student structures.
• Initialize the fields of each structure.
• Print out the information of each student.
Algorithm:
1. Start
2. Declare the Student structure with the fields name (string), id (integer), and grade (float).
3. Declare an array of three Student structures.
4. For each structure in the array, initialize the fields with the desired values.
5. For each structure in the array, print out the information of the student using printf.
6. Stop
Code:
[31]
C Input:
#include <stdio.h>
struct Student {
char name[50];
int id;
float grade;
};
int main() {
struct Student students[3];
students[0].id = 1;
strcpy(students[0].name, "John");
students[0].grade = 85.5;
students[1].id = 2;
strcpy(students[1].name, "Sarah");
students[1].grade = 92.0;
students[2].id = 3;
strcpy(students[2].name, "Peter");
students[2].grade = 77.3;
return 0;
}
[32]
C Output:
Name: John
ID: 1
Grade: 85.5
Name: Sarah
ID: 2
Grade: 92.0
Name: Peter
ID: 3
Grade: 77.3
Output Explanation:
1. We first declare a structure named "Student" with three fields: name, id, and grade.
2. We then declare an array of three Student structures named "students".
3. We initialize the fields of each student in the array using dot notation.
4. Finally, we loop through the array and print out the information of each student using
printf.he program prompts the user to enter a string input using printf() and takes the
input using scanf().
5. The function convertToUpper() is defined, which takes a string parameter and converts
all the characters to uppercase using toupper() function. The string is modified using
pointers and function call by reference.
6. The convertToUpper() function is called with the string parameter str in the main
function.
7. The modified string is printed using a pointer in the printf() statement in the main
function.
Practice Problems:
1. Create a program that uses a structure named "Rectangle" to store the length and width of a
rectangle. Then, calculate and print out the area and perimeter of the rectangle. (Difficulty:
Easier)
2. Create a program that uses a structure named "Employee" to store the name, age, and salary of
an employee. Then, calculate and print out the average salary of three employees. (Difficulty:
Same)
3. Create a program that uses a structure named "Book" to store the title, author, and price of a
book. Then, print out the title of the book with the highest price. (Difficulty: Same)
4. Create a program that uses a structure named "Circle" to store the radius of a circle. Then,
calculate and print out the diameter and circumference of the circle. (Difficulty: Easier)
[33]
[34]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 11
Name of the Experiment: Write a program to create a file called emp.rec and store
information about a person using data files and file handling in C.
Objectives:
1. To learn file handling in C programming.
2. To understand the concept of data files and file I/O operations.
3. To create a file and store information about a person in the file.
Program:
Write a C program to create a file called emp.rec and store information about a person
using data files and file handling in C
Instructions:
• Create a structure for employee information.
• Open a file in write mode.
• Read the employee information from the user and store it in the structure.
• Write the employee information to the file.
• Close the file.
Algorithm:
1. Start
2. Define a structure named 'employee' with fields for name, age, and salary.
3. Create a file 'emp.rec' using fopen() function in write mode.
4. Check if the file is created successfully or not.
5. Take input from the user for employee name, age and salary.
6. Store the employee information in the structure.
7. Write the employee information to the file using fprintf() function.
8. Close the file using fclose() function.
9. Stop
Code:
[35]
C Input:
#include <stdio.h>
struct employee {
char name[50];
int age;
float salary;
};
int main() {
struct employee emp;
FILE *fptr;
if (fptr == NULL) {
printf("Error in creating file\n");
return 0;
}
return 0;
}
C Output:
Output Explanation:
In this program, we have created a structure named 'employee' with fields for name,
age, and salary. We have also created a file 'emp.rec' using the fopen() function in write
mode.
[36]
Then, we have taken input from the user for employee name, age, and salary, stored
the employee information in the structure, and written the employee information to the
file using the fprintf() function. Finally, we have closed the file using the fclose()
function.
Practice Problems:
1. Write a C program to read data from a file and display it on the screen. (Difficulty: Same)
2. Write a C program to append new data to an existing file. (Difficulty: Same)
3. Write a C program to delete a record from a file. (Difficulty: Difficult)
4. Write a C program to search for a record in a file based on a given key. (Difficulty: Easier)
5. Write a C program to sort the records in a file based on a given field. (Difficulty: Difficult)
[37]
[38]
Department of Electrical and Electronic Engineering (EEE)
Course Code: 0613-122
Title: Programming I
Experiment no: 12
[39]