Sri Venkateswara College of Engineering
Autonomous Institution, Chennai
B.E Electrical & Electronics Engineering
IT22111 –Programming for Problem Solving
Laboratory (Credit: 1.5)
List of Experiments
CO: Be exposed to the syntax of C; Be familiar with programming in C; Learn to use arrays, strings, functions,
pointers, structures and unions in C.
1 Usage of Basic Linux commands.
2 C Programming using Simple statements and expressions.
3 Scientific problem-solving using decision making and looping.
4 Simple programming for one dimensional and two-dimensional arrays.
5 Solving problems using Strings.
6 C Programming using Pointers.
7 C Programming using user defined functions (Pass by value and Pass by reference).
8 C Programming using Recursion.
9 C Programming using structures and union.
10 C Programming using enumerated data types.
11 C Programming using macros and storage classes.
12 C Programming using Files.
13 Develop modularized application
Steps to follow to write and execute the “C” program
vi hello.c # write your program # hello is the name of your file
Press i and then start to type a program
Then press “Esc” and type “: wq”
cc hello.c # compile
./[Link] # run it
.
Steps to follow to write and execute the “C” program
vi /mnt/d/PP_LAB/Practice/practice.c # To access the program again
Simple “C” program
#include <stdio.h>
int main() {
// Your program code here
printf("Hello!\n");
return 0;
}
.
#include <stdio.h>
Car Mechanic Description
Mechanic The person who fixes the car
Toolbox manual Instruction manual explaining how to use tools
Tools in the toolbox The physical tools used for fixing
Reading the manual Understanding how to use tools safely
Fixing the car Actually using the tools to fix the problem
Not reading the manual Using tools incorrectly or not knowing how to use them
C Program Description
C Programming The code you write that performs tasks
Header file (stdio.h) File that declares functions like printf and scanf
Functions (printf, scanf) Pre-written code you use to perform input/output
#include <stdio.h> Directive to include the header file at the top
Calling functions Using functions like printf to print text
Missing #include Compiler errors because it doesn't know about functions
int main() {
➢ Main function where your program starts running. (Like the mechanic’s
starting point)
➢ ( ) # Call a function with no info
➢ ( ) with info inside # Call a function with information
➢ { } - Groups multiple lines of code into a block ( Like the workshop space or
the entire repair job area where all the steps done inside one place)
printf("Hello!\n");
➢ # “Hello!” is a message to print on the screen. \n means print it, then move
to next line.
➢ “semicolon ;” marks the end of a statement. Tells to compiler the line of the
code is complete and move to the next line.
➢ Without semicolon the compiler doesn't know where one instruction ends
and the next begins
return 0
➢ keyword, return 0 tells the system the program finished without error.
Exercise 2.1
➢ vinesh@THIRUBABU:~$ cd /mnt/d
➢ vinesh@THIRUBABU:/mnt/d$ cd /mnt/d/PP_LAB
➢ vinesh@THIRUBABU:/mnt/d/PP_LAB$ cd /mnt/d/PP_LAB/Program_1.1
➢ vinesh@THIRUBABU:/mnt/d/PP_LAB/Program_1.1$ vi Exercise_2.1.c
➢ vinesh@THIRUBABU:/mnt/d/PP_LAB/Program_1.1$ cc Exercise_2.1.c
➢ vinesh@THIRUBABU:/mnt/d/PP_LAB/Program_1.1$ ./[Link]
Exercise 2.1 - Print your name
Exercise 2.2 - Print your details with one detail per line
and tab between label and value
Exercise 2.3 - Read your details through keyboard and display them
Exercise 2.3 (steps)
#include <string.h> # standard C library header file named string.h
Declarations of functions that help you work with strings
“string.h” provides many useful functions like:
strlen() — to find the length of a string
strcpy() — to copy one string into another
strcat() — to concatenate (join) two strings
strcmp() — to compare two strings
memset() — to fill a block of memory with a specific value
printf("Name: "); # Prints exactly what’s inside the quotes. Asking the user to input a name
Exercise 2.3 (steps)
fgets(...) : file get string : Reads a full line (with spaces) from the keyboard
Name : Stores the text you typed
sizeof(name) : Makes sure it doesn't read too much
Stdin : standard input : A source of input (like the keyboard)
fgets() uses stdin to read input.
strcspn = string complementary span
Example: fgets() stores “Alice\n\0”
\n appear when typing Enter : Alice⏎
Then it adds \0 at the end automatically to make it a valid C string. Not a visible character,
meaning end of the string.
address[strcspn(address, "\n")] = 0
Exercise 2.3 (Output)
Exercise 2.4 - Swap two values in the variables using temp
Exercise 2.5 (Steps)
int a,b,temp;
int is a data type that stands for integer
Declares three integer variables: a → will store the first number. b → will store the second number.
temp → a temporary variable used to help swap a and b
printf("enter values for a and b: ");
printf = “print formatted” output.
Displays the message "enter values for a and b: " on the screen.
scanf("%d%d",&a,&b);
scanf stands for “scan formatted input”.
Reads two integers from the user (keyboard input).
%d%d → tells scanf to expect two integers.
&a → tells scanf the memory address where the first number will be stored.
&b → tells scanf the memory address where the second number will be stored.
printf("After swapping\n a=%d\n b=%d",a,b);
\n = goes to new line
a=%d = prints "a=" followed by the value of a
Exercise 2.4 - Output
Exercise 2.5 - Calculate the daily wage of a labour
Exercise 2.5 (Output)
Exercise 2.6 - Swap the values of the variables
without temporary variable
Exercise 2.6 (Output)
Initial values: m = 4 n = 7
Step 1: m = m + n; m = 4 + 7 = 11; n = 7 (unchanged)
Step 2: n = m - n; n = 11 - 7 = 4 (n now holds the original m); m = 11 (unchanged)
Step 3: m = m - n; m = 11 - 4 = 7 (m now holds the original n) n = 4 (unchanged)
Final values after swapping: m = 7; n=4
Exercise: 3.1 Scientific problems solving using decision making
Exercise: 3.1 Scientific problems solving using decision making
(Output)
Exercise: 3.2 To check if a year is a leap year or not.
Exercise: 3.2 To check if a year is a leap year or not.
(Output)
Exercise: 3.3 To find the oldest and youngest among three members
Exercise: 3.3 To find the oldest and youngest among three members
(Output)
Exercise: 3.4 To read a number and print the respective planet.
Exercise: 3.4 To read a number and print the respective planet.
(Output)
Exercise: 3.5 To determine the grade from a student marks.
Exercise: 3.5 To determine the grade from a student marks.
(Output)