PPS Codetantra
PPS Codetantra
sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb549…
S.No: 1 Exp. Name: What is an OS Command, Familiarization of Editor - vi, OS Commands Date:
Aim:
Page No:
Write different commands in vi editor.
Explanation:
ID: 20331A05D1
Operating system (OS) command line interfaces are usually distinct programs supplied with the operating
system. A program that implements such a text interface is often called a command-line
interpreter, command processor or shell.
vi editor:
The default editor that comes with the UNIX operating system is called vi (visual editor).
The UNIX vi editor is a full screen editor and has two modes of operation:
Command mode commands which cause action to be taken on the file
Insert mode in which entered text is inserted into the file
vi always starts out in command mode. When you wish to move between the two modes, keep these
things in mind.
You can type i to enter the insert mode. If you wish to leave insert mode and return to the command
mode, hit the ESC key.
If you are not sure where you are, hit ESC a couple of times and that should put you back in command
mode.
A User can start using vi editor by using the commands like:
vi filename - Creates a new file if it does not exist, otherwise opens existing file
vi -R filename - Opens an existing file in read only mode
Commands of vi editor:
I - Insert text at the cursor
A - Append text at the end of the line
a - Append text after cursor
O - Open a new line of text above the cursor
o - Open a new line of text below the cursor
x - Delete a single character
dd - Delete an entire line
ndd - Delete n number of lines
d$ - Delete from cursor to end of line
yy - Copy a line in buffer
P - Paste text from buffer
nyy - Copy n number of lines to buffer
G - Go to last line of the file
1G - Go to first line in the file
w - Move forward to next word
b - Move backwards to next word
$ - Move to the end of line
MVGR College of Engineering (Autonomous)
OS Commands:
ls:
ls is a command to list files in Unix and Unix-like operating systems.
The ls command lists all files in the directory that match the name. If name is left blank, it will list all of the
files in the directory.
ls with no option list files and directories in bare format where we won’t be able to view details like file
types, size, modified date and time, permission and links etc.
Syntax: ls [option] [names]
The most common options to reveal this information or change the list of files are:
-l - long format, displaying Unix file types, permissions, number of hard links, owner, group, size,
last-modified date and filename.
-f - do not sort. Useful for directories containing large numbers of files.
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6de… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb549…
-F - appends a character revealing the nature of a file, for example, * for an executable, or / for a
directory. Regular files have no suffix.
-a - lists all files in the given directory, including those whose names start with "." (which are hidden
files in Unix). By default, these files are excluded from the list.
Page No:
-R - recursively lists sub directories. The command ls -R / would therefore list all files.
-d - shows information about a symbolic link or directory, rather than about the link's target or
listing the contents of a directory.
-t - sort the list of files by modification time.
ID: 20331A05D1
-h - print sizes in human readable format.
cd:
The cd command, which stands for "change directory", changes the shell's current working directory.
The cd command is one of the commands user will use the most at the command line in Linux. It allows
the user to change the working directory.
User use it to move around within the hierarchy of the file system.
Syntax: cd directory name
pwd:
The pwd command prints the name of the working directory. pwd prints the full path name of the current
working directory.
Syntax: pwd
mkdir:
The mkdir command is used to create directories on a file system. If the specified directory does not
already exist, mkdir creates it.
Syntax: mkdir [option] directory_name(s)
This command has following options:
-m (mode) - Set file mode
-p (parent) - Create parent directories as necessary. When this option is used, no error is reported
if a specified directory already exists.
-v (verbose) - Verbose output; print a message for each created directory.
--help - Display a help message, and exit.
--version - Display version information, and exit.
The directory_name is the name of any directory that the user is asking mkdir to create. Any number of
directories can be created simultaneously.
cat:
The cat command is one of the most frequently used command in Linux/Unix like operating systems.
The cat command allows us to create single or multiple files, view contain of file, concatenate files and
redirect output in terminal or files.
Syntax: cat [OPTIONS] [FILE]
To create a file syntax is : cat > filename
To view a file syntax is : cat filename
-v.
-u - Output is printed as unbuffered.
-v - Display control characters and non-printing characters.
cp:
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6de… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb549…
The cp command is used to make copies of files and directories.
Syntax: cp [OPTIONS] SourceDestination
The cp command has the following options:
cp -a - archive files
Page No:
cp -f - force copy by removing the destination file if needed
cp -i - interactive - ask before overwrite
cp -l - link files instead of copy
cp -L - follow symbolic links
ID: 20331A05D1
cp -n - no file overwrite
cp -R - recursive copy (including hidden files)
cp -u - update - copy when source is newer than destination
cp -v - verbose - print informative messages
mv:
The mv command is used to move or rename files. mv command renames file source to destination or
moves the source file(s) to directory.
Syntax:mv [OPTIONS] SourceDestination
The mv command has the following options:
mv-f - force move by overwriting destination file without prompt
mv-i - interactive prompt before overwrite
mv-u - update - move when source is newer than destination
mv-v - verbose - print source and destination files
manmv - help manual
man:
On Linux and other Unix like operating systems, man is the interface used to view the system's
reference manuals.
man is the system's manual viewer; it can be used to display manual pages, scroll up and down, search
for occurrences of specific text, and other useful functions.
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=1&qId=5c2343ab64bac15dbec1055a&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6de… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950…
S.No: 2 Exp. Name: Exposure to Turbo C, GCC, Code Blocks IDE Date:
Aim:
Page No:
Exposure to Turbo C, GCC, Code Blocks IDE:
Turbo C:
ID: 20331A05D1
Turbo C was a software development tool for writing programs in the C language. As an IDE, it included a
source code editor, a fast compiler, a linker and an offline help file for reference. Version 2 included a built-in
debugger. Turbo C was a follow-up product to Borland's Turbo Pascal, which had gained widespread use in
educational institutions because the Pascal language was suited for teaching programming to students.
Although Turbo C was initially developed by a different company, it shared a lot of features with Turbo Pascal,
namely, the look-and-feel of the interface and the various programming and debugging tools included.
However, it was not as successful as Turbo Pascal because of competition from other C products such as
Microsoft C, Watcom C, Lattice C, etc. Nevertheless, Turbo C still had the advantage in compile speed and
price.
The first version was released on May 13, 1987, and it offered the first-ever edit-compile-run environment for
software development on IBM PCs. Turbo C was not originally developed by Borland but was bought from Bob
Jervis and was initially called Wizard C. Turbo Pascal did not have pull-down menus before this time, and it was
only on its fourth version that it received a face lift to look like Turbo C.
Borland as a company no longer develops and sells these products, but Turbo C still lives on as a free
download from various online repositories, although it is really an old technology without real technical support
and is no longer viable for modern software development. Turbo C eventually evolved into Turbo C++, then into
Borland C++ and, finally, into C++ Builder.
Turbo C features:
Inline assembly with full access to the C language symbolic structures and names -- This allowed
MVGR College of Engineering (Autonomous)
programmers to write some assembly language codes right into their programs without the need for a
separate assembler.
Support for all memory models -- This had to do with the segmented memory architecture used by 16-bit
processors of that era, where each segment was limited to 64 kilobytes (Kb). The models were called
tiny, small, medium, large and huge, which determined the size of the data used by a program, as well
as the size of the program itself. For example, with the tiny model, both the data and the program must
fit within a single 64-Kb segment. In the small model, the data and the program each used a different 64-
Kb segment. So in order to create a program larger than 64 Kb or one that manipulates data larger than
64 Kb, the medium, large and huge memory models had to be used. In contrast, 32-bit processors used
a flat memory model and did not have this limitation.
Speed or size optimization -- The compiler could be configured to produce an executable program that
was either fast or small in size, but not both.
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6decf… 1/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950…
Constant folding -- This feature allowed the Turbo C compiler to evaluate constant expressions during
compile time rather than during run time.
Page No:
Short cut Keys in Turbo C:
1. F1 For Help
2. F2 Save
3. F3 Open
ID: 20331A05D1
4. F4 Go to cursor
5. F5 Zoom
6. F6 Next
7. F7 Trace into
8. F8 Step over
9. F9 Make
10. F10 Menu
11. Alt+X Quit
12. Alt+Bksp Undo
13. Shift+Alt+Bksp Redo
14. Shift+Del Cut
15. Ctrl+Ins Copy
16. Shift+Ins Paste
17. Ctrl+Del Clear
18. Ctrl+L Search again
19. Alt+F7 Previous error
20. Alt+F8 Next error
21. Ctrl+F9 Run
22. Ctrl+F2 Program reset
23. Alt+F9 Compile
24. Alt+F4 Inspect
25. Ctrl+F4 Evaluate/Modify
26. Ctrl+F3 Call stack
27. Ctrl+F8 Toggle breakpoint
28. Ctrl+F5 Size/Move
29. Alt+F3 Close
30. Alt+F5 User screen
31. Alt+0 List all
32. Shift+F1 Index
33. Ctrl+F1 Topic search
34. Alt+F1 Previous topic
35. Ctrl+F7 Add watch
GCC:
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6decf… 2/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950…
(https://en.wikipedia.org/wiki/Objective-C), Objective-C++ (https://en.wikipedia.org/wiki/Objective-
C%2B%2B), Fortran (https://en.wikipedia.org/wiki/Fortran), Java
(https://en.wikipedia.org/wiki/Java_(programming_language)), Ada
(https://en.wikipedia.org/wiki/Ada_(programming_language)), and Go
Page No:
(https://en.wikipedia.org/wiki/Go_(programming_language)), among others.
Version 4.5 of the OpenMP (https://en.wikipedia.org/wiki/OpenMP) specification is now supported in the C and
C++ compilers and a "much improved" implementation of the OpenACC
(https://en.wikipedia.org/wiki/OpenACC) 2.0a specification is also supported. By default, the current version
ID: 20331A05D1
supports gnu++14, a superset of C++14 (https://en.wikipedia.org/wiki/C%2B%2B14), and gnu11, a superset
of C11 (https://en.wikipedia.org/wiki/C11_(C_standard_revision)), with strict standard support also available. It
also provides experimental support for C++17 (https://en.wikipedia.org/wiki/C%2B%2B17) and later.
GCC has been ported (https://en.wikipedia.org/wiki/Porting) to a wide variety of instruction set architectures
(https://en.wikipedia.org/wiki/Instruction_set_architecture), and is widely deployed as a tool in the development
of both free and proprietary software (https://en.wikipedia.org/wiki/Proprietary_software). GCC is also available
for most embedded systems (https://en.wikipedia.org/wiki/Embedded_system), including ARM
(https://en.wikipedia.org/wiki/ARM_architecture)-based; AMCC
(https://en.wikipedia.org/wiki/Applied_Micro_Circuits_Corporation), and Freescale
(https://en.wikipedia.org/wiki/Freescale) Power ISA (https://en.wikipedia.org/wiki/Power_ISA)-based chips. The
compiler can target a wide variety of platforms.
As well as being the official compiler of the GNU operating system
(https://en.wikipedia.org/wiki/GNU_operating_system), GCC has been adopted as the standard compiler by
many other modern Unix-like (https://en.wikipedia.org/wiki/Unix-like) computer operating systems
(https://en.wikipedia.org/wiki/Operating_system), including Linux (https://en.wikipedia.org/wiki/Linux) and
the BSD (https://en.wikipedia.org/wiki/Berkeley_Software_Distribution) family, although FreeBSD
(https://en.wikipedia.org/wiki/FreeBSD) and macOS (https://en.wikipedia.org/wiki/MacOS) have moved to
the LLVM (https://en.wikipedia.org/wiki/LLVM) system. Versions are also available for Microsoft Windows
(https://en.wikipedia.org/wiki/Microsoft_Windows) and other operating systems; GCC can compile code
for Android (https://en.wikipedia.org/wiki/Android_(operating_system))and iOS
(https://en.wikipedia.org/wiki/IOS).
Application screenshot
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6decf… 3/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950…
Page No:
int main() {
cout << "Hello, world!" << endl;
return 0;
ID: 20331A05D1
}
1. Save the file as " Hello.cpp " in your project directory (e.g., " d:\project ").
2. Build (Compile and Link): Select "Build" menu ⇒ Build (Ctrl-F9).
3. Run: Select "Build" menu ⇒ Run (Ctrl-F10).
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=2&qId=5d522d34e3fffe0a91cf8019&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6decf… 4/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=4&qId=5a712069015de9062c85ac9c&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 4 Exp. Name: Write a C program to print the Digit at ones place of a given number Date:
Aim:
Page No:
Write a program to print the digit at ones place of a given number.
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter a number :
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DigitAtOnesPlace.c
#include<stdio.h>
void main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf("The digit at ones place of %d : %d\n",n,n%10);
}
Test Case - 1
User Output
Enter a number : 1993
The digit at ones place of 1993 : 3
Test Case - 2
MVGR College of Engineering (Autonomous)
User Output
Enter a number : 1895
The digit at ones place of 1895 : 5
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=4&qId=5a712069015de9062c85ac9c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/1
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Page No:
Aim:
Write a program to read two integer values and to print the results of various arithmetic operations on them.
ID: 20331A05D1
During execution, the program should print the following message on the console:
24 + 15 = 39
24 - 15 = 9
24 * 15 = 360
24 / 15 = 1
24 % 15 = 9
Note: Use the printf() function with a newline character ( \n ) at the end for every line being printed.
Source Code:
Program304.c
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two integers : ");
scanf("%d%d",&a,&b);
printf("%d + %d = %d\n",a,b,a+b);
printf("%d - %d = %d\n",a,b,a-b);
printf("%d * %d = %d\n",a,b,a*b);
printf("%d / %d = %d\n",a,b,a/b);
printf("%d %% %d = %d\n",a,b,a%b);
}
Test Case - 1
User Output
Enter two integers : 12 -6
12 + -6 = 6
12 - -6 = 18
12 * -6 = -72
12 / -6 = -2
12 % -6 = 0
Test Case - 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Test Case - 2
User Output
Page No:
Enter two integers : 15 3
15 + 3 = 18
15 - 3 = 12
15 * 3 = 45
ID: 20331A05D1
15 / 3 = 5
15 % 3 = 0
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 6 Exp. Name: Write a C program to Swap Two numbers with out using a Third variable Date:
Aim:
Page No:
Write a program to swap two integer values without using a third variable.
During execution, the program should print the following message on the console:
ID: 20331A05D1
Enter two integers :
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program311.c
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping a = %d, b = %d\n",a,b);
c=a;
a=b;
b=c;
printf("After swapping a = %d, b = %d\n",a,b);
}
Test Case - 1
User Output
Enter two integers: 23 67
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter two integers: 99 89
Before swapping a = 99, b = 89
After swapping a = 89, b = 99
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 3
User Output
Page No:
Enter two integers: 77 78
Before swapping a = 77, b = 78
After swapping a = 78, b = 77
ID: 20331A05D1
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 7 Exp. Name: Write a C program to find Sum and Average of three numbers Date:
Aim:
Page No:
Write a program to find the sum and average of the three given integers.
During execution, the program should print the message on the console as:
ID: 20331A05D1
Enter three integers :
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program314.c
#include<stdio.h>
void main()
{
int a,b,c,sum;
float avg;
printf("Enter three integers : ");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
avg=(float)sum/3.0;
printf("Sum of %d, %d and %d : %d\n",a,b,c,sum);
printf("Average of %d, %d and %d : %f\n",a,b,c,avg);
}
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter three integers : 5 8 3
Sum of 5, 8 and 3 : 16
Average of 5, 8 and 3 : 5.333333
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
User Output
Page No:
Enter three integers : 11 33 55
Sum of 11, 33 and 55 : 99
Average of 11, 33 and 55 : 33.000000
ID: 20331A05D1
Test Case - 4
User Output
Enter three integers : -1 5 -6
Sum of -1, 5 and -6 : -2
Average of -1, 5 and -6 : -0.666667
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e22010cf2f069ef156414&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 8 Exp. Name: Write a C program to convert the temperature from Fahrenheit to Celsius Date:
Aim:
Page No:
Write a program to convert the given temperature from fahrenheit(F) to celsius(C) .
During execution, the program should print the following message on the console:
ID: 20331A05D1
Enter F :
Enter F : 32.45
C = 0.250000
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program306.c
#include<stdio.h>
void main()
{
float Fahrenheit,Celsius;
printf("Enter F : ");
scanf("%f",&Fahrenheit);
Celsius=((Fahrenheit - 32)*5)/9;
printf("C = %f\n",Celsius);
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Enter F : 80.779
C = 27.099443
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e22010cf2f069ef156414&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/1
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=9&qId=585e21ed0cf2f069ef156403&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to convert the given temperature from Celsius to
S.No: 9 Date:
Fahrenheit
Page No:
Aim:
Write a program to convert the given temperature from celsius(C) to fahrenheit(F) .
ID: 20331A05D1
During execution, the program should print the following message on the console:
enter C :
enter C : 1.23
F = 34.214001
Note: Use the printf() function with a newline character ( \n ) at the end.
Program305.c
#include<stdio.h>
void main()
{
float celsius,fahrenheit;
printf("enter C : ");
scanf("%f",&celsius);
fahrenheit=((celsius*9.0)/5.0)+32.0;
printf("F = %f\n",fahrenheit);
}
Test Case - 1
User Output
enter C : 27.1
F = 80.779999
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
enter C : 1.23
F = 34.214001
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=9&qId=585e21ed0cf2f069ef156403&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/1
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 10 Exp. Name: Write a program to calculate Simple interest and Compound interest Date:
Aim:
Page No:
Write a program to calculate the simple interest and compound interest by reading principal
amount, rate of interest and time.
ID: 20331A05D1
During execution, the program should print the following message on the console:
Note: Use the printf() function and ensure that the character '\n' is printed at the end of the result.
The formula to find simple interest is simpleInterest = (principal * rate * time) / 100 .
Note: Use float data type for all the involved variables.
Source Code:
Program315.c
#include<stdio.h>
void main()
{
float p,r,t,si,ci;
printf("Enter principle amount, rate of interest, time : ");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
ci=p*pow((1 +r/100),t)-p;
printf("Simple interest = %f\n",si);
printf("Compound interest = %f\n",ci);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter principle amount, rate of interest, time : 5000 7 5
Simple interest = 1750.000000
Compound interest = 2012.760376
Test Case - 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 2
User Output
Page No:
Enter principle amount, rate of interest, time : 4500 12 2.5
Simple interest = 1350.000000
Compound interest = 1473.894897
ID: 20331A05D1
Test Case - 3
User Output
Enter principle amount, rate of interest, time : 10000 4.5 3.5
Simple interest = 1575.000000
Compound interest = 1665.596558
Test Case - 4
User Output
Enter principle amount, rate of interest, time : 1000 6 4
Simple interest = 240.000000
Compound interest = 262.476685
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4bebb54…
S.No: 11 Exp. Name: Write a program to find the Square root of a given number Date:
Aim:
Page No:
Write a program to find the square root of a given number.
During execution, the program should print the message on the console as:
ID: 20331A05D1
Enter a number :
Enter a number : 2
Note - 2: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
SquareRoot.c
#include<stdio.h>
#include<math.h>
void main()
{
double a,s;
printf("Enter a number : ");
scanf("%lf",&a);
s=pow((a),0.5);
printf("The square root is : %lf\n",s);
Test Case - 1
User Output
Enter a number : 25
The square root is : 5.000000
Test Case - 2
User Output
Enter a number : 2
The square root is : 1.414214
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6d… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4bebb54…
Test Case - 3
User Output
Page No:
Enter a number : 3.6
The square root is : 1.897367
Test Case - 4
ID: 20331A05D1
User Output
Enter a number : 16.8
The square root is : 4.098780
Test Case - 5
User Output
Enter a number : 35
The square root is : 5.916080
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6d… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 12 Exp. Name: Write a C program to find Area of a Triangle using Heron's formula Date:
Aim:
Page No:
Write a program to find the area of a triangle using Heron's formula.
During execution, the program should print the following message on the console:
ID: 20331A05D1
enter sides :
area : 2.485477
Note: Use the printf() function with a newline character ( \n ) at the end.
The area of a triangle is given by Area = √ p(p − a)(p − b)(p − c) , where p is half of the perimeter, or
(a + b + c) / 2 . Let a,b,c be the lengths of the sides of the given triangle.
Source Code:
Program313.c
#include<stdio.h>
void main()
{
float a,b,c,area,p;
printf("enter sides : ");
scanf("%f%f%f",&a,&b,&c);
p=(a+b+c)/2;
area=pow((p*(p-a)*(p-b)*(p-c)),0.5);
printf("area : %f\n",area);
}
Test Case - 1
User Output
enter sides : 2.3 2.4 2.5
MVGR College of Engineering (Autonomous)
area : 2.485477
Test Case - 2
User Output
enter sides : 5 4 6.7
area : 9.952600
Test Case - 3
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 3
enter sides : 24.67 35.46 36.78
area : 418.155121
Page No:
Test Case - 4
User Output
ID: 20331A05D1
enter sides : 1 2 3
area : 0.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 13 Exp. Name: Write a C program to find the Distance Travelled by an Object Date:
Aim:
Page No:
Write a program to find the distance travelled by an object.
ID: 20331A05D1
Enter the acceleration value : 2.5
Enter the initial velocity : 5.7
Enter the time taken : 20
Distance travelled : 614.000000
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DistanceTravelled.c
#include<stdio.h>
void main()
{
float a,u,t,distance;
printf("Enter the acceleration value : ");
scanf("%f",&a);
printf("Enter the initial velocity : ");
scanf("%f",&u);
printf("Enter the time taken : ");
scanf("%f",&t);
distance=(u*t) + 0.5*a*t*t;
printf("Distance travelled : %f\n",distance);
}
Test Case - 1
User Output
Enter the acceleration value : 4
Enter the initial velocity : 5
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the acceleration value : 5
Enter the initial velocity : 0
Enter the time taken : 10
Distance travelled : 250.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 3
User Output
Page No:
Enter the acceleration value : 2.5
Enter the initial velocity : 5.7
Enter the time taken : 20
Distance travelled : 614.000000
ID: 20331A05D1
Test Case - 4
User Output
Enter the acceleration value : 50
Enter the initial velocity : 34.67
Enter the time taken : 6
Distance travelled : 1108.020020
Test Case - 5
User Output
Enter the acceleration value : 125.6
Enter the initial velocity : 45.8
Enter the time taken : 4
Distance travelled : 1188.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 14 Exp. Name: Write a Program that prints the results of all the Operators available in C Date:
Aim:
Page No:
Write a simple program that prints the results of all the operators available in C (including pre / post increment,
bitwise and/or/not, etc.).
ID: 20331A05D1
Read required operand values from standard input.
At the time of execution, the program should print the message on the console as:
Addition of a and b : 20
Substraction of a and b : 8
Multiplication of a and b : 84
Remainder of a and b : 2
Division of a and b : 2
Logical AND result : 1
Logical OR and NOT result : 1
Bitwise AND : 6
Bitwise OR : 14
Bitwise NOT : 0
Bitwise complement : -15
Bitwise XOR : 8
Bitwise shift right : 3
Bitwise shift left : 48
Integer size : 4, Floating point size : 4
Conditional expression : 200
Preincrement : 15
Postincrement : 6
Predecrement : 14
Postdecrement : 7
AllOperators.c
MVGR College of Engineering (Autonomous)
#include <stdio.h>
void main() {
int a, b, result, result2;
printf("Enter a and b values : ");
scanf("%d %d", &a, &b);
printf("Addition of a and b : %d\n",a+b ); // Addition of a and b
printf("Substraction of a and b : %d\n",a-b ); // Subtract b from a
printf("Multiplication of a and b : %d\n",a*b ); // Multiply a and b
printf("Remainder of a and b : %d\n",a%b ); // Numerator is a and denominator is b
printf("Division of a and b : %d\n",a/b ); // Numerator is a and denominator is b
result =(a>0)&&(b<=10) ; // The condition is a is greater than 0 and b is less than
or equal to 10
printf("Logical AND result : %d\n", result);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d…
result2 = (a==b)||(b!=0); // The condition is a is equal to (Comparision) b or b is
not equal to 0
printf("Logical OR and NOT result : %d\n",result2);
printf("Bitwise AND : %d\n",a&b ); // Bitwise and of a and b
Page No:
printf("Bitwise OR : %d\n",a|b); // Bitwise or of a and b
printf("Bitwise NOT : %d\n",!a ); // Logical not of a
printf("Bitwise complement : %d\n",~a ); // Complement of a
printf("Bitwise XOR : %d\n",a^b); // Bitwise xor of a and b
ID: 20331A05D1
printf("Bitwise shift right : %d\n",a>>2 ); // Right shift a by 2 times
printf("Bitwise shift left : %d\n",b<<3 ); // Left shift b by 3 times
printf("Integer size : %d, Floating point size : %d\n",sizeof (int), sizeof(float));
// Sizes of int and float
printf("Conditional expression : %d\n",((a-b)>50?100:200)); // if (a - b) is greater
than 50 then it is 100, otherwise 200
printf("Preincrement : %d\n",++a); // Pre increment a
printf("Postincrement : %d\n",b++); // Post increment b
printf("Predecrement : %d\n",--a); // Pre decrement a
printf("Postdecrement : %d\n",b--); // Post decrement b
}
Test Case - 1
User Output
Enter a and b values : 14 6
Addition of a and b : 20
Substraction of a and b : 8
Multiplication of a and b : 84
Remainder of a and b : 2
Division of a and b : 2
Logical AND result : 1
Logical OR and NOT result : 1
Bitwise AND : 6
Bitwise OR : 14
Bitwise NOT : 0
Bitwise complement : -15
Bitwise XOR : 8
Bitwise shift right : 3
Bitwise shift left : 48
Integer size : 4, Floating point size : 4
Conditional expression : 200
MVGR College of Engineering (Autonomous)
Preincrement : 15
Postincrement : 6
Predecrement : 14
Postdecrement : 7
Test Case - 2
User Output
Enter a and b values : 8 5
Addition of a and b : 13
Substraction of a and b : 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 2
Multiplication of a and b : 40
Remainder of a and b : 3
Page No:
Division of a and b : 1
Logical AND result : 1
Logical OR and NOT result : 1
Bitwise AND : 0
ID: 20331A05D1
Bitwise OR : 13
Bitwise NOT : 0
Bitwise complement : -9
Bitwise XOR : 13
Bitwise shift right : 2
Bitwise shift left : 40
Integer size : 4, Floating point size : 4
Conditional expression : 200
Preincrement : 9
Postincrement : 5
Predecrement : 8
Postdecrement : 6
Test Case - 3
User Output
Enter a and b values : 25 30
Addition of a and b : 55
Substraction of a and b : -5
Multiplication of a and b : 750
Remainder of a and b : 25
Division of a and b : 0
Logical AND result : 0
Logical OR and NOT result : 1
Bitwise AND : 24
Bitwise OR : 31
Bitwise NOT : 0
Bitwise complement : -26
Bitwise XOR : 7
Bitwise shift right : 6
Bitwise shift left : 240
Integer size : 4, Floating point size : 4
Conditional expression : 200
Preincrement : 26
MVGR College of Engineering (Autonomous)
Postincrement : 30
Predecrement : 25
Postdecrement : 31
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to find the Largest and Smallest of Three numbers
S.No: 15 Date:
using Ternary operator
Page No:
Aim:
Write a program to find the largest and smallest of the three given integers.
ID: 20331A05D1
During execution, the program should print the following message on the console:
Enter 3 integers :
999 is largest
9 is smallest
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program310.c
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
printf("%d is largest\n",(a>b)&&(a>c)?a:(b>c)?b:c);
printf("%d is smallest\n",(a<b)&&(a<c)?a:(b<c)?b:c);
}
Test Case - 1
User Output
Enter 3 integers: 34 56 48
56 is largest
34 is smallest
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter 3 integers: 99 9 999
999 is largest
9 is smallest
Test Case - 3
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
Enter 3 integers: 56 34 12
56 is largest
Page No:
12 is smallest
Test Case - 4
ID: 20331A05D1
User Output
Enter 3 integers: 34 56 48
56 is largest
34 is smallest
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 16 Exp. Name: Write a C program to Total and Average of 5 subjects marks Date:
Aim:
Page No:
Write a program to take marks of 5 subjects in integers, and find the total , average in float.
ID: 20331A05D1
Enter 5 subjects marks : 55 56 57 54 55
Total marks : 277.000000
Average marks : 55.400002
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
TotalAndAvg.c
#include<stdio.h>
void main()
{
int a,b,c,d,e;
float total,avg;
printf("Enter 5 subjects marks : ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
total=(a+b+c+d+e);
avg=total/5.0;
printf("Total marks : %f\n",total);
printf("Average marks : %f\n",avg);
Test Case - 1
User Output
Enter 5 subjects marks : 45 67 89 57 49
Total marks : 307.000000
Average marks : 61.400002
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter 5 subjects marks : 55 56 57 54 55
Total marks : 277.000000
Average marks : 55.400002
Test Case - 3
User Output
Enter 5 subjects marks : 90 97 95 92 91
Total marks : 465.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 3
Average marks : 93.000000
Page No:
Test Case - 4
User Output
ID: 20331A05D1
Enter 5 subjects marks : 20 30 66 77 44
Total marks : 237.000000
Average marks : 47.400002
Test Case - 5
User Output
Enter 5 subjects marks : 56 78 88 79 64
Total marks : 365.000000
Average marks : 73.000000
Test Case - 6
User Output
Enter 5 subjects marks : 44 35 67 49 51
Total marks : 246.000000
Average marks : 49.200001
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 17 Exp. Name: Write a Program to find the Max and Min of Four numbers Date:
Aim:
Page No:
Write a program to find the max and min of four numbers.
ID: 20331A05D1
Enter 4 numbers : 9 8 5 2
Max value : 9
Min value : 2
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
MinandMaxOf4.c
#include<stdio.h>
void main()
{
int a,b,c,d,max,min;
printf("Enter 4 numbers : ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if (a>b&&a>c&&a>d)
{
max=a;
}
else if (b>c&&b>d)
{
max=b;
}
else if (c>d)
{
max=c;
}
else
{
max=d;
}
if (a<b&&a<c&&a<d)
{
min=a;
}
MVGR College of Engineering (Autonomous)
else if (b<c&&b<d)
{
min=b;
}
else if (c<d)
{
min=c;
}
else
{
min=d;
}
printf("Max value : %d\n",max);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D&lid=5d…
printf("Min value : %d\n",min);
}
Page No:
Execution Results - All test cases have succeeded!
ID: 20331A05D1
Test Case - 1
User Output
Enter 4 numbers : 9 8 5 2
Max value : 9
Min value : 2
Test Case - 2
User Output
Enter 4 numbers : 112 245 167 321
Max value : 321
Min value : 112
Test Case - 3
User Output
Enter 4 numbers : 110 103 113 109
Max value : 113
Min value : 103
Test Case - 4
User Output
Enter 4 numbers : -34 -35 -24 -67
Max value : -24
Min value : -67
Test Case - 5
User Output
Enter 4 numbers : 24 28 34 16
Max value : 34
Min value : 16
MVGR College of Engineering (Autonomous)
Test Case - 6
User Output
Enter 4 numbers : 564 547 574 563
Max value : 574
Min value : 547
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 18 Exp. Name: Write a C Program to find out the Electricity Charges of a Customer Date:
Aim:
Page No:
An electricity board charges the following rates for the use of electricity:
for the first 200 units 80 paise per unit
for the next 100 units 90 paise per unit
ID: 20331A05D1
beyond 300 units Rs. 1 per unit.
If the total amount is more than Rs. 400, then an additional surcharge of 15% of total amount is charged.
Write a C program to read the name of the user, number of units consumed and print out the charges.
At the time of execution, the program should print the following messages one by one on the console as:
Hint: Please convert charges per unit into float datatype [(ex: 80 paise as 0.8f or (float) 0.8)] before
multiplying with units for calculating the amount.
Source Code:
Program424.c
#include <stdio.h>
void main() {
char name[25];
int units;
// start writing your code below this.
// Use the above variables to read values using scanf from the user
float charge,surcharge=0,amount,totalamount;
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
else if (units>300)
{
charge=1.00;
}
Page No:
amount=units*charge;
if (amount>400)
{
surcharge=amount*15/100.0;
ID: 20331A05D1
}
totalamount=amount+surcharge;
if(amount<100)
{
totalamount=100;
}
printf("Customer name : %s\n",name);
printf("Units consumed : %d\n",units);
printf("Amount charged : %f\n",amount);
printf("Surcharges : %f\n",surcharge);
printf("Total amount to be paid by the customer : %f\n",totalamount);
}
Test Case - 1
User Output
Enter the customer name : Ganga
Enter the units consumed by customer : 199
Customer name : Ganga
Units consumed : 199
Amount charged : 159.199997
Surcharges : 0.000000
Total amount to be paid by the customer : 159.199997
Test Case - 2
User Output
Enter the customer name : Saraswathi
Enter the units consumed by customer : 89
Customer name : Saraswathi
Units consumed : 89
MVGR College of Engineering (Autonomous)
Test Case - 3
User Output
Enter the customer name : Krishna
Enter the units consumed by customer : 325
Customer name : Krishna
Units consumed : 325
Amount charged : 325.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
Surcharges : 0.000000
Total amount to be paid by the customer : 325.000000
Page No:
Test Case - 4
User Output
ID: 20331A05D1
Enter the customer name : Govinda
Enter the units consumed by customer : 535
Customer name : Govinda
Units consumed : 535
Amount charged : 535.000000
Surcharges : 80.250000
Total amount to be paid by the customer : 615.250000
Test Case - 5
User Output
Enter the customer name : Ram
Enter the units consumed by customer : 220
Customer name : Ram
Units consumed : 220
Amount charged : 198.000000
Surcharges : 0.000000
Total amount to be paid by the customer : 198.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d…
S.No: 19 Exp. Name: Write a C program to find all Roots of a Quadratic equation Date:
Aim:
Page No:
Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a quadratic
equation (ax2+bx+c=0) as input and computes all possible roots.
ID: 20331A05D1
An equation is quadratic only if a is non zero.
If a is zero and b is non zero in the above equation then it becomes a linear equation (bx + c = 0).
Implement a C program for the developed flowchart/algorithm and execute the same to output the possible
roots for a given set of coefficients with appropriate messages.
At the time of execution, the program should print the message on the console as:
Invalid coefficients
Enter valid inputs
Linear equation
Root = -4.000000
root1 = -2.000000+i1.732051
root2 = -2.000000-i1.732051
Note - 1: Do use the printf() function with a newline character ( \n ) at the end.
Note - 2: Use fabs() funtion (fabs(determinant)) when the roots are real and imaginary.
Program419.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d…
#include<stdio.h>
#include<math.h>
void main()
Page No:
{
float a,b,c,det,r1,r2,imag,real;
printf("Enter coefficients a, b and c : ");
scanf("%f%f%f",&a,&b,&c);
ID: 20331A05D1
if ((a==0)&&(b==0))
{
printf("Invalid coefficients\n");
printf("Enter valid inputs\n");
}
else if (a==0)
{
printf("Linear equation\n");
printf("Root = %f\n", -c/b);
}
else
{
det=(b*b)-(4*a*c);
if(det>0)
{
r1=(-b+sqrt(det))/(2*a);
r2=(-b-sqrt(det))/(2*a);
printf("The roots are real and distinct\n");
printf("root1 = %f and root2 = %f\n",r1,r2);
}
else if(det==0)
{
r1=r2=-b/(2*a);
printf("The roots are real and equal\n");
printf("root1 = root2 = %f\n",r1);
}
else
{
real= -b/(2*a);
imag= sqrt(fabs(det))/(2*a);
printf("The roots are real and imaginary\n");
printf("root1 = %f+i%f\n",real,imag);
printf("root2 = %f-i%f\n",real,imag);
}
}
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter coefficients a, b and c : 2 6 4
The roots are real and distinct
root1 = -1.000000 and root2 = -2.000000
Test Case - 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 2
User Output
Page No:
Enter coefficients a, b and c : 0 0 0
Invalid coefficients
Enter valid inputs
ID: 20331A05D1
Test Case - 3
User Output
Enter coefficients a, b and c : 0 2 8
Linear equation
Root = -4.000000
Test Case - 4
User Output
Enter coefficients a, b and c : 1 6 9
The roots are real and equal
root1 = root2 = -3.000000
Test Case - 5
User Output
Enter coefficients a, b and c : 1 -5 3
The roots are real and distinct
root1 = 4.302776 and root2 = 0.697224
Test Case - 6
User Output
Enter coefficients a, b and c : 1 4 7
The roots are real and imaginary
root1 = -2.000000+i1.732051
root2 = -2.000000-i1.732051
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to make a simple Calculator to Add, Subtract, Multiply
S.No: 20 Date:
or Divide using switch-case
Page No:
Aim:
Write a program to read two integer values and an arithmetic operator, depending on the operator perform
different arithmetic operations .
ID: 20331A05D1
If integer values 2 and 3 are given with operator +, then the output should be 2 + 3 = 5 .
If integer values 6 and 3 are given with operator /, then the output should be 6 / 3 = 2 .
If other than arithmetic operator is given, then display "Error! Operator is not correct".
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
12 + 10 = 22
Note-2: Space before %c removes any white space (blanks, tabs, or newlines). It means %c without
space will read white sapce like new line(\n), spaces(‘ ‘) or tabs(\t). By adding space before %c , we are
skipping this and reading only the char given.
Source Code:
Program406.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
#include<stdio.h>
void main()
{
Page No:
int a,b;
char op;
printf("Enter two integer values : ");
scanf("%d%d",&a,&b);
ID: 20331A05D1
printf("Enter an arithmetic operator : ");
scanf(" %c",&op);
switch (op)
{
case '+':printf("%d + %d = %d\n",a,b,a+b);
break;
case '-':printf("%d - %d = %d\n",a,b,a-b);
break;
case '*':printf("%d * %d = %d\n",a,b,a*b);
break;
case '/':
if(b!=0)
printf("%d / %d = %d\n",a,b,a/b);
else
printf("Division is not possible! Divide by zero error\n");
break;
case '%':
if(b!=0)
printf("%d %% %d = %d\n",a,b,a%b);
else
printf("Modulo division is not possible! Divide by zero error\n");
break;
Test Case - 1
User Output
Enter two integer values : 6 9
Enter an arithmetic operator : -
6 - 9 = -3
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter two integer values : 6 9
Enter an arithmetic operator : *
6 * 9 = 54
Test Case - 3
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
Enter two integer values : 8 9
Enter an arithmetic operator : @
Page No:
Error! Operator is not correct
Test Case - 4
ID: 20331A05D1
User Output
Enter two integer values : 45 9
Enter an arithmetic operator : %
45 % 9 = 0
Test Case - 5
User Output
Enter two integer values : 6 9
Enter an arithmetic operator : +
6 + 9 = 15
Test Case - 6
User Output
Enter two integer values : 12 0
Enter an arithmetic operator : /
Division is not possible! Divide by zero error
Test Case - 7
User Output
Enter two integer values : 5 0
Enter an arithmetic operator : %
Modulo division is not possible! Divide by zero error
Test Case - 8
User Output
Enter two integer values : 25 3
Enter an arithmetic operator : /
25 / 3 = 8
MVGR College of Engineering (Autonomous)
Test Case - 9
User Output
Enter two integer values : 99 65
Enter an arithmetic operator : %
99 % 65 = 34
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 21 Exp. Name: Write a C program to find whether the given year is a Leap year or not Date:
Aim:
Page No:
Write a program to check whether the given year is leap year or not.
Use if-else statement and print "__ is a leap year" if year is divisible by 4 or 400. Otherwise, print
ID: 20331A05D1
"__ is not a leap year" .
At the time of execution, the program should print the message on the console as:
Enter a year :
Program405.c
#include<stdio.h>
void main()
{
int year;
printf("Enter a year : ");
scanf("%d",&year);
if (year % 4 ==0)
{
if (year % 100 == 0)
{
if(year % 400 ==0)
{
printf("%d is a leap year\n",year);
}
else
{
printf("%d is not a leap year\n",year);
}
}
MVGR College of Engineering (Autonomous)
else
{
printf("%d is a leap year\n",year);
}
}
else
{
printf("%d is not a leap year\n",year);
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
Page No:
User Output
Enter a year : 2004
ID: 20331A05D1
2004 is a leap year
Test Case - 2
User Output
Enter a year : 1900
1900 is not a leap year
Test Case - 3
User Output
Enter a year : 3000
3000 is not a leap year
Test Case - 4
User Output
Enter a year : 2000
2000 is a leap year
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=22&qId=585e3ec70cf2f069ef156bf8&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 22 Exp. Name: Write a C program to find the Factorial of a given number Date:
Aim:
Page No:
Write a program to find the factorial of a given number.
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter an integer :
Enter an integer : 5
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program408.c
#include<stdio.h>
void main()
{
int num,fact=1;
printf("Enter an integer : ");
scanf("%d",&num);
printf("Factorial of given number %d = ",num);
while (num!=0)
{
fact = fact * num;
num = num -1 ;
}
printf("%d\n",fact);
}
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Enter an integer : 3
Factorial of given number 3 = 6
Test Case - 2
User Output
Enter an integer : 5
Factorial of given number 5 = 120
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=22&qId=585e3ec70cf2f069ef156bf8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/1
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 23 Exp. Name: Write a C program to find whether the given number is Prime or not Date:
Aim:
Page No:
Write a sample program to check whether the given number is a prime number or not.
[Hint: A prime number is a positive integer which is greater than 1 and divisible by 1 and itself only. A few
ID: 20331A05D1
prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, etc.]
At the time of execution, the program should print the message on the console as:
Similarly,If the input is given as 9 then the output should be "The given number 9 is not a prime number".
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program409.c
#include<stdio.h>
void main()
{
int num,count=0;
printf("Enter any number : ");
scanf("%d",&num);
for (int i=1;i<=num;i++)
{
if (num % i == 0)
{
count++;
}
}
if(count==2)
{
printf("The given number %d is a prime number\n",num);
MVGR College of Engineering (Autonomous)
}
else
{
printf("The given number %d is not a prime number\n",num);
}
}
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
User Output
Page No:
Enter any number : 11
The given number 11 is a prime number
Test Case - 2
ID: 20331A05D1
User Output
Enter any number : 37
The given number 37 is a prime number
Test Case - 3
User Output
Enter any number : 21
The given number 21 is not a prime number
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Aim:
Page No:
Develop a Program to compute Sine(x) using Taylor series approximation. Compare your result with the
built- in Library function.
ID: 20331A05D1
Print both the results with appropriate messages.
For example,
When n = 4 , Taylor series considered should be Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) [4 Terms].
When n = 5 , Taylor series considered should be Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + (x9/9!) [5
Terms]
Note: The input n refers to the number of terms in Taylor approximation series to be considered.
At the time of execution, the program should print the following messages one by one on the console as:
Source Code:
LoopExample9.c
#include <stdio.h>
MVGR College of Engineering (Autonomous)
#include <math.h>
void main() {
// decalre variables here
int n,i;
float degrees,radians,sum,term;
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
term=sum=radians;
for (i=1;i<n;i++)
{
term=((-term)*(radians*radians))/((2*i)*(2*i+1));
Page No:
sum=sum+term;
}
ID: 20331A05D1
printf("The sine value sin(%f) = %f\n",degrees,sum);
printf("The sine value using built-in sin(%f) = %f\n",degrees,sin(radians));
}
Test Case - 1
User Output
Enter the value of n : 5
Enter degrees : 90
The sine value sin(90.000000) = 1.000003
The sine value using built-in sin(90.000000) = 1.000000
Test Case - 2
User Output
Enter the value of n : 4
Enter degrees : 45
The sine value sin(45.000000) = 0.706825
The sine value using built-in sin(45.000000) = 0.706825
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Aim:
Page No:
Develop a program to compute Cosine(x) using Taylor series approximation. Compare your result with the
built- in library function.
ID: 20331A05D1
Print both the results with appropriate messages.
For example,
When n = 4 , Taylor series considered should be Cos(x) = 1 - (x2/2!) + (x4/4!) - (x6/6!) [4 Terms].
When n = 5 , Taylor series considered should be Cos(x) = 1 - (x2/2!) + (x4/4!) - (x6/6!) + (x8/8!) [5
Terms]
Note: The input n refers to the number of terms in Taylor approximation series to be considered.
At the time of execution, the program should print the following messages one by one on the console as:
Source Code:
CosineTaylorSeries.c
#include<stdio.h>
MVGR College of Engineering (Autonomous)
#include<math.h>
void main()
{
int n,i;
float degrees,radians,sum,term;
printf("Enter the value of n : ");
scanf("%d",&n);
printf("Enter degrees : ");
scanf("%f",°rees);
radians = (degrees*3.14)/180;
term = sum = 1;
for(i=1;i<n;i++)
{
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
term=((-term)*(radians*radians))/((2*i)*(2*i-1));
sum +=term;
}
printf("The cosine value cos(%f) = %f\n",degrees,sum);
Page No:
printf("The cosine value using built-in cos(%f) = %f\n",degrees,cos(radians));
}
ID: 20331A05D1
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter the value of n : 5
Enter degrees : 45
The cosine value cos(45.000000) = 0.707388
The cosine value using built-in cos(45.000000) = 0.707388
Test Case - 2
User Output
Enter the value of n : 3
Enter degrees : 120
The cosine value cos(120.000000) = -0.390926
The cosine value using built-in cos(120.000000) = -0.499080
Test Case - 3
User Output
Enter the value of n : 10
Enter degrees : 30
The cosine value cos(30.000000) = 0.866158
The cosine value using built-in cos(30.000000) = 0.866158
Test Case - 4
User Output
Enter the value of n : 8
Enter degrees : 120
The cosine value cos(120.000000) = -0.499080
The cosine value using built-in cos(120.000000) = -0.499080
MVGR College of Engineering (Autonomous)
Test Case - 5
User Output
Enter the value of n : 4
Enter degrees : 45
The cosine value cos(45.000000) = 0.707385
The cosine value using built-in cos(45.000000) = 0.707388
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Page No:
Aim:
Write a program to find the reverse of an integer number and check whether it is Palindrome or not.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter an integer :
Source Code:
Program421.c
#include<stdio.h>
void main()
{
int n,temp,digit,reverse=0;
printf("Enter an integer : ");
scanf("%d",&n);
temp = n;
while(n!=0)
{
digit = n % 10;
reverse = reverse*10 + digit;
n = n/10;
}
printf("The reverse of a given number : %d\n",reverse);
if (temp==reverse)
{
printf("%d is a palindrome\n",temp);
MVGR College of Engineering (Autonomous)
}
else
{
printf("%d is not a palindrome\n",temp);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Test Case - 1
User Output
Page No:
Enter an integer : 2017
The reverse of a given number : 7102
2017 is not a palindrome
ID: 20331A05D1
Test Case - 2
User Output
Enter an integer : 1221
The reverse of a given number : 1221
1221 is a palindrome
Test Case - 3
User Output
Enter an integer : 12321
The reverse of a given number : 12321
12321 is a palindrome
Test Case - 4
User Output
Enter an integer : 18771
The reverse of a given number : 17781
18771 is not a palindrome
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 27 Exp. Name: Write a C program to print the Pyramid with numbers Date:
Aim:
Page No:
Write a program to print a pyramid of numbers separated by spaces for the given number of rows.
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter number of rows :
1
1 2
1 2 3
Source Code:
PyramidDemo15.c
#include <stdio.h>
void main() {
int n, i, j, s;
printf("Enter number of rows : ");
scanf("%d", &n);
// Fill the missing code
for (i=1;i<=n;i++)
{
for (s=1;s<=n-i;s++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ", j);
}
printf("\n");
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter number of rows : 3
1
1 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
1 2 3
Page No:
Test Case - 2
User Output
ID: 20331A05D1
Enter number of rows : 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Test Case - 3
User Output
Enter number of rows : 8
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 28 Exp. Name: Write a C program to find the Largest and Smallest elements of the array Date:
Aim:
Page No:
Write a program to read an array of integers (with max size 10) and print the largest and the smallest of
the given numbers.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Next, the program should print the messages one by one on the console as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
ArrayAccessDemo10.c
#include<stdio.h>
void main()
{
int array[10],i,n,largest,smallest;
printf("Enter how many values you want to read : ");
scanf("%d",&n);
for (i=0;i<n;i++)
MVGR College of Engineering (Autonomous)
{
printf("Enter the value of a[%d] : ",i);
scanf("%d",&array[i]);
}
largest=array[0];
smallest=array[0];
for (i=0;i<n;i++)
{
if (array[i]>largest)
{
largest=array[i];
}
if (array[i]<smallest)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
{
smallest=array[i];
}
}
Page No:
printf("The largest element of the array = %d\n",largest);
printf("The smallest element of the array = %d\n",smallest);
}
ID: 20331A05D1
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter how many values you want to read : 6
Enter the value of a[0] : 1
Enter the value of a[1] : 5
Enter the value of a[2] : 9
Enter the value of a[3] : 6
Enter the value of a[4] : 4
Enter the value of a[5] : 2
The largest element of the array = 9
The smallest element of the array = 1
Test Case - 2
User Output
Enter how many values you want to read : 4
Enter the value of a[0] : -23
Enter the value of a[1] : -45
Enter the value of a[2] : -12
Enter the value of a[3] : -29
The largest element of the array = -12
The smallest element of the array = -45
Test Case - 3
User Output
Enter how many values you want to read : 7
Enter the value of a[0] : 24
Enter the value of a[1] : 567
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 29 Exp. Name: Write a C program to Search an element using Linear Search process Date:
Aim:
Page No:
Write a program to search a key element with in the given array of elements using linear search
process.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 3
Next, the program should print the messages one by one on the console as:
Next, the program should print the message on the console as:
Similarly if the key element is given as 25 for the above one dimensional array elements then the program
should print the output as "The Key element 25 is not found in the array".
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program509.c
MVGR College of Engineering (Autonomous)
#include<stdio.h>
void main()
{
int i,j,n,a[10],key,flag,index;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element for a[%d] : ",i);
scanf("%d",&a[i]);
}
printf("Enter key element : ");
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
scanf("%d",&key);
for (i=0;i<n;i++)
{
if (a[i]==key)
Page No:
{
flag = 1;
index = i;
break;
ID: 20331A05D1
}
else
{
flag = 0;
}
}
if (flag == 1)
{
printf("The key element %d is found at the position %d\n",key,index);
}
else
{
printf("The key element %d is not found in the array\n",key);
}
}
Test Case - 1
User Output
Enter value of n : 5
Enter element for a[0] : 45
Enter element for a[1] : 67
Enter element for a[2] : 35
Enter element for a[3] : 28
Enter element for a[4] : 16
Enter key element : 28
The key element 28 is found at the position 3
Test Case - 2
User Output
Enter value of n : 5
MVGR College of Engineering (Autonomous)
Test Case - 3
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
Enter value of n : 4
Enter element for a[0] : 452
Page No:
Enter element for a[1] : 356
Enter element for a[2] : 754
Enter element for a[3] : 127
Enter key element : 127
ID: 20331A05D1
The key element 127 is found at the position 3
Test Case - 4
User Output
Enter value of n : 3
Enter element for a[0] : 5
Enter element for a[1] : 7
Enter element for a[2] : 3
Enter key element : 4
The key element 4 is not found in the array
Test Case - 5
User Output
Enter value of n : 3
Enter element for a[0] : 11
Enter element for a[1] : 45
Enter element for a[2] : 37
Enter key element : 25
The key element 25 is not found in the array
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 30 Exp. Name: Write a C program to display the elements of an Array in reverse order Date:
Aim:
Page No:
Write a program to print the given integer elements of an array (with max size 10) in reverse order .
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter size of the array :
Next, the program should print the message on the console as:
[Hint: First read an integers from standard input into the array and then use a loop to iterate on that array in the
reverse order (meaning starting from the last element till the first) to print the elements.]
ArrayAccessDemo1.c
#include <stdio.h>
void main() {
int arr[10], i, n;
printf("Enter size of the array : ");
scanf("%d", &n );
printf("Enter array elements : ");
// Fill the missing code
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
User Output
Page No:
Enter size of the array : 3
Enter array elements : 10 20 30
Array elements in reverse order : 30 20 10
ID: 20331A05D1
Test Case - 2
User Output
Enter size of the array : 6
Enter array elements : 11 88 66 22 33 44
Array elements in reverse order : 44 33 22 66 88 11
Test Case - 3
User Output
Enter size of the array : 4
Enter array elements : 123 345 675 845
Array elements in reverse order : 845 675 345 123
Test Case - 4
User Output
Enter size of the array : 3
Enter array elements : -23 -45 -99
Array elements in reverse order : -99 -45 -23
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 31 Exp. Name: Write a Program to find 2’s complement of a given Binary number Date:
Aim:
Page No:
Write a C program to find 2’s complement of a given binary number.
ID: 20331A05D1
Enter the size of binary number : 5
Enter 5 bit binary number : 1 0 0 1 0
2's complement is : 0 1 1 1 0
TwosComplement.c
#include<stdio.h>
void main()
{
int n,a[20],i,flag=0;
printf("Enter the size of binary number : ");
scanf("%d",&n);
printf("Enter %d bit binary number : ",n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=n-1;i>=0;i--)
{
if (a[i]==1 && flag == 0)
{
flag = 1;
continue;
}
if (a[i] == 0 && flag ==0)
{
continue;
}
if (a[i] == 1)
a[i] = 0;
else
MVGR College of Engineering (Autonomous)
a[i] = 1;
}
printf("2's complement is : ");
for (i = 0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 1
User Output
Page No:
Enter the size of binary number : 6
Enter 6 bit binary number : 1 0 0 0 1 1
2's complement is : 0 1 1 1 0 1
ID: 20331A05D1
Test Case - 2
User Output
Enter the size of binary number : 10
Enter 10 bit binary number : 1 1 1 1 1 1 1 1 1 0
2's complement is : 0 0 0 0 0 0 0 0 1 0
Test Case - 3
User Output
Enter the size of binary number : 4
Enter 4 bit binary number : 1 1 1 1
2's complement is : 0 0 0 1
Test Case - 4
User Output
Enter the size of binary number : 8
Enter 8 bit binary number : 1 0 1 0 1 0 1 0
2's complement is : 0 1 0 1 0 1 1 0
Test Case - 5
User Output
Enter the size of binary number : 15
Enter 15 bit binary number : 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
2's complement is : 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 32 Exp. Name: Write a Program to Eliminate Duplicate elements in an array Date:
Aim:
Page No:
Write a C program to eliminate duplicate elements in an array.
ID: 20331A05D1
Enter array size : 9
Enter 9 array elements : 1 2 2 3 4 4 4 5 5
After eliminating duplicates, the array is : 1 2 3 4 5
EliminateDuplicates.c
#include<stdio.h>
void main()
{
int a[20],i,j,k,n;
printf("Enter array size : ");
scanf("%d",&n);
printf("Enter %d array elements : ",n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<n;i++)
{
for (j=i+1;j<n; )
{
if (a[j] == a[i]){
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
}
else
{
j++;
MVGR College of Engineering (Autonomous)
}
}
}
printf("After eliminating duplicates, the array is : ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
Test Case - 1
User Output
Page No:
Enter array size : 5
Enter 5 array elements : 1 2 1 2 3
After eliminating duplicates, the array is : 1 2 3
ID: 20331A05D1
Test Case - 2
User Output
Enter array size : 5
Enter 5 array elements : 11 13 11 12 13
After eliminating duplicates, the array is : 11 13 12
Test Case - 3
User Output
Enter array size : 10
Enter 10 array elements : 1 5 1 2 3 9 2 5 8 6
After eliminating duplicates, the array is : 1 5 2 3 9 8 6
Test Case - 4
User Output
Enter array size : 5
Enter 5 array elements : 10 20 30 20 10
After eliminating duplicates, the array is : 10 20 30
Test Case - 5
User Output
Enter array size : 4
Enter 4 array elements : 2 2 2 2
After eliminating duplicates, the array is : 2
Test Case - 6
User Output
Enter array size : 3
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to find the Addition of Two matrices by checking
S.No: 33 Date:
compatibility
Page No:
Aim:
Write a program to find the addition of two matrices .
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
2 4
6 8
Source Code:
Program511.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
#include<stdio.h>
void main()
{
Page No:
int i,j,m,n,p,q,a[5][5],b[5][5],c[5][5];
printf("Enter the row & column sizes of matrix-1 : ");
scanf("%d%d",&m,&n);
printf("Enter matrix-1 %d elements : ",m*n);
ID: 20331A05D1
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the row & column sizes of matrix-2 : ");
scanf("%d%d",&p,&q);
printf("Enter matrix-2 %d elements : ",p*q);
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The given matrix-1 is\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The given matrix-2 is\n");
for (i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
if(m==p&&n==q)
{
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
printf("Addition is not possible\n");
}
}
Page No:
Execution Results - All test cases have succeeded!
ID: 20331A05D1
Test Case - 1
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 11 22 33 44
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 22 33 44 55
The given matrix-1 is
11 22
33 44
The given matrix-2 is
22 33
44 55
Addition of two matrices is
33 55
77 99
Test Case - 2
User Output
Enter the row & column sizes of matrix-1 : 3 3
Enter matrix-1 9 elements : 1 2 3 4 5 6 7 8 9
Enter the row & column sizes of matrix-2 : 3 3
Enter matrix-2 9 elements : 1 3 2 4 6 5 8 7 9
The given matrix-1 is
1 2 3
4 5 6
7 8 9
The given matrix-2 is
1 3 2
4 6 5
8 7 9
Addition of two matrices is
2 5 5
MVGR College of Engineering (Autonomous)
8 11 11
15 15 18
Test Case - 3
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 1 2 3 4
Enter the row & column sizes of matrix-2 : 3 2
Enter matrix-2 6 elements : 1 2 3 4 5 6
The given matrix-1 is
1 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 3
3 4
The given matrix-2 is
Page No:
1 2
3 4
5 6
Addition is not possible
ID: 20331A05D1
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 4/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to find the Multiplication of Two matrices by checking
S.No: 34 Date:
compatibility
Page No:
Aim:
Write a program to find the multiplication of two matrices .
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Source Code:
Program513.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
#include<stdio.h>
void main()
{
Page No:
int i,j,k,m,n,p,q,a[5][5],b[5][5],c[5][5];
printf("Enter the row & column sizes of matrix-1 : ");
scanf("%d%d",&m,&n);
printf("Enter matrix-1 %d elements : ",m*n);
ID: 20331A05D1
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the row & column sizes of matrix-2 : ");
scanf("%d%d",&p,&q);
printf("Enter matrix-2 %d elements : ",p*q);
for(i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The given matrix-1 is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The given matrix-2 is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
if (n==p)
{
MVGR College of Engineering (Autonomous)
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Multiplication of two matrices is\n");
for(i=0;i<m;i++)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
{
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
Page No:
}
printf("\n");
}
}
ID: 20331A05D1
else
{
printf("Multiplication is not possible\n");
}
}
Test Case - 1
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 11 33 22 44
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 11 33 44 22
The given matrix-1 is
11 33
22 44
The given matrix-2 is
11 33
44 22
Multiplication of two matrices is
1573 1089
2178 1694
Test Case - 2
User Output
Enter the row & column sizes of matrix-1 : 2 3
Enter matrix-1 6 elements : 1 2 3 4 5 6
Enter the row & column sizes of matrix-2 : 3 2
Enter matrix-2 6 elements : 1 2 3 4 5 6
The given matrix-1 is
MVGR College of Engineering (Autonomous)
1 2 3
4 5 6
The given matrix-2 is
1 2
3 4
5 6
Multiplication of two matrices is
22 28
49 64
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 3
User Output
Page No:
Enter the row & column sizes of matrix-1 : 2 3
Enter matrix-1 6 elements : 1 2 3 4 5 6
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 1 2 3 4
ID: 20331A05D1
The given matrix-1 is
1 2 3
4 5 6
The given matrix-2 is
1 2
3 4
Multiplication is not possible
Test Case - 4
User Output
Enter the row & column sizes of matrix-1 : 3 3
Enter matrix-1 9 elements : 11 22 33 44 55 66 77 88 99
Enter the row & column sizes of matrix-2 : 3 3
Enter matrix-2 9 elements : 99 88 77 66 55 44 33 22 11
The given matrix-1 is
11 22 33
44 55 66
77 88 99
The given matrix-2 is
99 88 77
66 55 44
33 22 11
Multiplication of two matrices is
3630 2904 2178
10164 8349 6534
16698 13794 10890
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 4/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to Sort the given elements in Ascending order using
S.No: 35 Date:
Bubble Sort
Page No:
Aim:
Write a program to sort ( Ascending order ) the given elements using bubble sort technique .
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 3
Next, the program should print the messages one by one on the console as:
Program504.c
#include<stdio.h>
void main()
{
MVGR College of Engineering (Autonomous)
int i,j,a[10],n,temp;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element for a[%d] : ",i);
scanf("%d",&a[i]);
}
printf("Before sorting the elements in the array are\n");
for (i=0;i<n;i++)
{
printf("Value of a[%d] = %d\n",i,a[i]);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
for (i=0;i<n-1;i++)
{
for (j=0;j<n-i-1;j++)
{
Page No:
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
ID: 20331A05D1
a[j+1] = temp;
}
}
}
printf("After sorting the elements in the array are\n");
for(i=0;i<n;i++)
{
printf("Value of a[%d] = %d\n",i,a[i]);
}
}
Test Case - 1
User Output
Enter value of n : 5
Enter element for a[0] : 2
Enter element for a[1] : 7
Enter element for a[2] : 6
Enter element for a[3] : 4
Enter element for a[4] : 1
Before sorting the elements in the array are
Value of a[0] = 2
Value of a[1] = 7
Value of a[2] = 6
Value of a[3] = 4
Value of a[4] = 1
After sorting the elements in the array are
Value of a[0] = 1
Value of a[1] = 2
Value of a[2] = 4
Value of a[3] = 6
Value of a[4] = 7
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter value of n : 4
Enter element for a[0] : 28
Enter element for a[1] : 34
Enter element for a[2] : 26
Enter element for a[3] : 29
Before sorting the elements in the array are
Value of a[0] = 28
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
Value of a[1] = 34
Value of a[2] = 26
Page No:
Value of a[3] = 29
After sorting the elements in the array are
Value of a[0] = 26
Value of a[1] = 28
ID: 20331A05D1
Value of a[2] = 29
Value of a[3] = 34
Test Case - 3
User Output
Enter value of n : 8
Enter element for a[0] : 7
Enter element for a[1] : 3
Enter element for a[2] : 9
Enter element for a[3] : 2
Enter element for a[4] : 5
Enter element for a[5] : 4
Enter element for a[6] : 6
Enter element for a[7] : 1
Before sorting the elements in the array are
Value of a[0] = 7
Value of a[1] = 3
Value of a[2] = 9
Value of a[3] = 2
Value of a[4] = 5
Value of a[5] = 4
Value of a[6] = 6
Value of a[7] = 1
After sorting the elements in the array are
Value of a[0] = 1
Value of a[1] = 2
Value of a[2] = 3
Value of a[3] = 4
Value of a[4] = 5
Value of a[5] = 6
Value of a[6] = 7
Value of a[7] = 9
MVGR College of Engineering (Autonomous)
Test Case - 4
User Output
Enter value of n : 4
Enter element for a[0] : -23
Enter element for a[1] : -14
Enter element for a[2] : -56
Enter element for a[3] : -35
Before sorting the elements in the array are
Value of a[0] = -23
Value of a[1] = -14
Value of a[2] = -56
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 4
Value of a[3] = -35
After sorting the elements in the array are
Page No:
Value of a[0] = -56
Value of a[1] = -35
Value of a[2] = -23
Value of a[3] = -14
ID: 20331A05D1
Test Case - 5
User Output
Enter value of n : 5
Enter element for a[0] : 28
Enter element for a[1] : 45
Enter element for a[2] : -1
Enter element for a[3] : -5
Enter element for a[4] : 2
Before sorting the elements in the array are
Value of a[0] = 28
Value of a[1] = 45
Value of a[2] = -1
Value of a[3] = -5
Value of a[4] = 2
After sorting the elements in the array are
Value of a[0] = -5
Value of a[1] = -1
Value of a[2] = 2
Value of a[3] = 28
Value of a[4] = 45
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 4/4
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to Concatenate two given strings without using string
S.No: 36 Date:
library functions
Page No:
Aim:
Write a program to concatenate two given strings without using string library functions.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
string1 :
string1 : ILove
Next, the program should print the message on the console as:
string2 :
string2 : Coding
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program605.c
#include<stdio.h>
void main()
{
int i,j;
char str1[20],str2[20],str3[20];
printf("string1 : ");
scanf("%s", str1);
printf("string2 : ");
scanf("%s", str2);
for (i=0; str1[i] != '\0';i++)
{
str3[i]=str1[i];
}
for (j=0; str2[j] != '\0';j++)
MVGR College of Engineering (Autonomous)
{
str3[i]=str2[j];
i++;
}
str3[i] = '\0';
printf("concatenated string = %s\n",str3);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
User Output
Page No:
string1 : ILove
string2 : Coding
concatenated string = ILoveCoding
ID: 20331A05D1
Test Case - 2
User Output
string1 : 1234
string2 : 567
concatenated string = 1234567
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=37&qId=5866094d0cf2bd9bdd2c0b07&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Exp. Name: Write a C program to Reverse the given string without using the Library
S.No: 37 Date:
Functions
Page No:
Aim:
Write a program to reverse the given string without using the library functions.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter a string :
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program609.c
#include<stdio.h>
void main()
{
int i,j,temp;
char ch[20];
printf("Enter a string : ");
scanf("%s",ch);
for (i=0;ch[i] != '\0';i++)
{
}
for (j=0;j<i;j++)
{
temp = ch[i-1];
ch[i-1] = ch[j];
ch[j] = temp;
i--;
}
printf("Reverse string : %s\n",ch);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter a string : Dallas
Reverse string : sallaD
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=37&qId=5866094d0cf2bd9bdd2c0b07&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/1
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Aim:
Page No:
Write a C program to compute the binomial coefficient ncr .
ID: 20331A05D1
n! = n * (n - 1)!; if n > 0 (Recursive criteria)
n! = 1; if n = 0 (Base criteria)
At the time of execution, the program should print the message on the console as:
Source Code:
FindingNcr.c
#include <stdio.h>
#include "RecursiveFactorial.c"
void main() {
long int n, r;
printf("Enter the value of n and r : ");
scanf("%ld%ld", &n, &r);
printf("The value of %ldc%ld = %ld\n", n, r, FindNcr(n, r));
}
RecursiveFactorial.c
else
{
return n*factorial(n-1);
}
}
long int FindNcr(long int n, long int r)
{
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d…
Page No:
Test Case - 1
User Output
ID: 20331A05D1
Enter the value of n and r : 6 2
The value of 6c2 = 15
Test Case - 2
User Output
Enter the value of n and r : 5 3
The value of 5c3 = 10
Test Case - 3
User Output
Enter the value of n and r : 15 6
The value of 15c6 = 5005
Test Case - 4
User Output
Enter the value of n and r : 18 10
The value of 18c10 = 43758
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
S.No: 39 Exp. Name: Write a Program to find the Length of a String Date:
Aim:
Page No:
Write a C program to find the length of a given string.
ID: 20331A05D1
Enter the string : CodeTantra
Length of CodeTantra : 10
Source Code:
StrLength.c
#include <stdio.h>
#include "StrLength1.c"
void main() {
char str[30];
printf("Enter the string : ");
scanf("%s", str);
printf("Length of %s : %d\n", str, myStrLen(str));
}
StrLength1.c
Test Case - 1
User Output
Enter the string : CodeTantra
Length of CodeTantra : 10
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the string : IndoUsUk
Length of IndoUsUk : 8
Test Case - 3
User Output
Enter the string : MalayalaM
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Test Case - 3
Length of MalayalaM : 9
Page No:
Test Case - 4
User Output
ID: 20331A05D1
Enter the string : Oh!MyGod
Length of Oh!MyGod : 8
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to find the Transpose of a given Matrix using
S.No: 40 Date:
Functions
Page No:
Aim:
Write a program to find the transpose of a given matrix using functions.
ID: 20331A05D1
Sample Input and Output:
Source Code:
TransposeFunction.c
#include <stdio.h>
#include "TransposeFunction1.c"
void main() {
int a[5][5], b[5][5], m, n, p, q;
printf("Enter the size of matrix : ");
scanf("%d%d", &m, &n);
read(a, m, n);
printf("The given matrix is\n");
display(a, m, n);
transposeMatrix(a, b, m, n);
printf("The transpose matrix is\n");
display(b, n, m);
}
TransposeFunction1.c
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&x[i][j]);
}
}
}
void display(int y[5][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
for(j=0;j<n;j++)
{
printf("%d ",y[i][j]);
}
Page No:
printf("\n");
}
}
void transposeMatrix(int a[5][5], int b[5][5], int m, int n)
ID: 20331A05D1
{
int i,j;
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
}
Test Case - 1
User Output
Enter the size of matrix : 2 2
Enter 4 elements : 1 2 3 4
The given matrix is
1 2
3 4
The transpose matrix is
1 3
2 4
Test Case - 2
User Output
Enter the size of matrix : 2 3
Enter 6 elements : 22 33 44 55 66 77
The given matrix is
22 33 44
55 66 77
MVGR College of Engineering (Autonomous)
Test Case - 3
User Output
Enter the size of matrix : 3 3
Enter 9 elements : 11 22 55 66 44 77 88 99 33
The given matrix is
11 22 55
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 3
66 44 77
88 99 33
Page No:
The transpose matrix is
11 66 88
22 44 99
55 77 33
ID: 20331A05D1
Test Case - 4
User Output
Enter the size of matrix : 3 2
Enter 6 elements : 111 222 333 444 555 666
The given matrix is
111 222
333 444
555 666
The transpose matrix is
111 333 555
222 444 666
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to display the Fibonacci series up to the given number
S.No: 41 Date:
of terms using Recursion
Page No:
Aim:
Write a program to display the fibonacci series up to the given number of terms using recursion process.
ID: 20331A05D1
The fibonacci series is 0 1 1 2 3 5 8 13 21 34...... .
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 6
Program908.c
#include <stdio.h>
#include "Program908a.c"
void main() {
int n, i;
printf("Enter value of n : ");
scanf("%d", &n);
printf("The fibonacci series of %d terms are : ", n);
for (i = 0; i < n; i++) {
printf(" %d ", fib(i));
}
}
Program908a.c
int fib(int n)
{
if (n==0)
{
MVGR College of Engineering (Autonomous)
return 0;
}
else if (n==1)
{
return 1;
}
else
{
return fib(n-1)+ fib(n-2);
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
Page No:
User Output
Enter value of n : 4
ID: 20331A05D1
The fibonacci series of 4 terms are : 0 1 1 2
Test Case - 2
User Output
Enter value of n : 8
The fibonacci series of 8 terms are : 0 1 1 2 3 5 8 13
Test Case - 3
User Output
Enter value of n : 14
The fibonacci series of 14 terms are : 0 1 1 2 3 5 8 13 21 34 55 89 144 233
Test Case - 4
User Output
Enter value of n : 3
The fibonacci series of 3 terms are : 0 1 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 42 Exp. Name: Write a C program to find the LCM of two numbers using Recursion Date:
Aim:
Page No:
Write a program to find the lcm (Least Common Multiple) of a given two numbers using recursion process.
The least common multiple ( lcm ) of two or more integers, is the smallest positive integer that is divisible by
ID: 20331A05D1
both a and b.
At the time of execution, the program should print the message on the console as:
Note: Write the function lcm() and recursive function gcd() in Program907a.c .
Source Code:
Program907.c
#include <stdio.h>
#include "Program907a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("The lcm of two numbers %d and %d = %d\n", a, b, lcm(a, b));
}
Program907a.c
else
{
return gcd(b, a % b);
}
}
int lcm(int a,int b)
{
return (a * b)/gcd(a, b) ;
}
Test Case - 1
User Output
Page No:
Enter two integer values : 34 24
The lcm of two numbers 34 and 24 = 408
ID: 20331A05D1
Test Case - 2
User Output
Enter two integer values : 6 9
The lcm of two numbers 6 and 9 = 18
Test Case - 3
User Output
Enter two integer values : 345 467
The lcm of two numbers 345 and 467 = 161115
Test Case - 4
User Output
Enter two integer values : 100 88
The lcm of two numbers 100 and 88 = 2200
Test Case - 5
User Output
Enter two integer values : 123 420
The lcm of two numbers 123 and 420 = 17220
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to find the Factorial of a given number using
S.No: 43 Date:
Recursion
Page No:
Aim:
Write a program to find the factorial of a given number using recursion process.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter an integer :
Enter an integer : 6
Factorial of 6 is : 720
Program901.c
#include <stdio.h>
#include "Program901a.c"
void main() {
long int n;
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}
Program901a.c
}
}
Test Case - 1
User Output
Enter an integer : 5
Factorial of 5 is : 120
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 2
User Output
Page No:
Enter an integer : 7
Factorial of 7 is : 5040
Test Case - 3
ID: 20331A05D1
User Output
Enter an integer : 4
Factorial of 4 is : 24
Test Case - 4
User Output
Enter an integer : 8
Factorial of 8 is : 40320
Test Case - 5
User Output
Enter an integer : 0
Factorial of 0 is : 1
Test Case - 6
User Output
Enter an integer : 9
Factorial of 9 is : 362880
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 44 Exp. Name: Write a program to find the Sum of Series using Recursion Date:
Aim:
Page No:
Write a program to find the sum of series 11 + 22 + 33 + ....... using recursion process.
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter value of n :
Enter value of n : 3
Source Code:
SumOfSeries.c
#include <stdio.h>
#include "SumOfSeries1.c"
void main() {
long int n;
printf("Enter value of n : ");
scanf("%li", &n);
printf("Sum of the series = %li\n", sumOfSeries(n));
}
SumOfSeries1.c
#include<stdio.h>
long int sumOfSeries(int n)
{
if (n==1)
{
return 1;
}
else
{
return (pow(n, n) + sumOfSeries(n-1));
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter value of n : 4
Sum of the series = 288
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
User Output
Page No:
Enter value of n : 3
Sum of the series = 32
Test Case - 3
ID: 20331A05D1
User Output
Enter value of n : 2
Sum of the series = 5
Test Case - 4
User Output
Enter value of n : 7
Sum of the series = 873612
Test Case - 5
User Output
Enter value of n : 1
Sum of the series = 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 45 Exp. Name: Write a program to implement Ackermann function using Recursion Date:
Aim:
Page No:
Write a program to implement Ackermann function (https://en.wikipedia.org/wiki/Ackermann_function) using
recursion process.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
A(2, 1) = 5
Source Code:
AckermannFunction.c
#include <stdio.h>
#include "AckermannFunction1.c"
void main() {
long long int m, n;
printf("Enter two numbers : ");
scanf("%lli %lli", &m, &n);
printf("A(%lli, %lli) = %lli\n", m, n, ackermannFun(m, n));
}
AckermannFunction1.c
#include<math.h>
long long int ackermannFun(long long int m, long long int n)
{
if (m == 0)
return n + 1;
else if (n==0)
return ackermannFun(m-1,1);
else
return ackermannFun(m-1,ackermannFun(m,n-1));
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter two numbers : 0 1
A(0, 1) = 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
User Output
Page No:
Enter two numbers : 2 2
A(2, 2) = 7
Test Case - 3
ID: 20331A05D1
User Output
Enter two numbers : 2 1
A(2, 1) = 5
Test Case - 4
User Output
Enter two numbers : 1 1
A(1, 1) = 3
Test Case - 5
User Output
Enter two numbers : 1 0
A(1, 0) = 2
Test Case - 6
User Output
Enter two numbers : 2 3
A(2, 3) = 9
Test Case - 7
User Output
Enter two numbers : 2 5
A(2, 5) = 13
Test Case - 8
User Output
Enter two numbers : 1 3
MVGR College of Engineering (Autonomous)
A(1, 3) = 5
Test Case - 9
User Output
Enter two numbers : 0 5
A(0, 5) = 6
Test Case - 10
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 10
Enter two numbers : 1 6
A(1, 6) = 8
Page No:
Test Case - 11
User Output
ID: 20331A05D1
Enter two numbers : 2 7
A(2, 7) = 17
Test Case - 12
User Output
Enter two numbers : 2 8
A(2, 8) = 19
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 46 Exp. Name: Write a C program to Swap two values by using Call-by-Address method Date:
Aim:
Page No:
Write a program to swap two values by using call by address method.
At the time of execution, the program should print the message on the console as:
ID: 20331A05D1
Enter two integer values :
Note: Write the function swap() in Program1002a.c and do use the printf() function with a newline character
( \n ).
Source Code:
Program1002.c
#include <stdio.h>
#include "Program1002a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("Before swapping in main : a = %d b = %d\n", a, b);
swap(&a, &b);
printf("After swapping in main : a = %d b = %d\n", a, b);
}
Program1002a.c
*q=temp;
printf("After swapping in swap : *p = %d *q = %d\n", *p, *q);
}
Test Case - 1
User Output
Enter two integer values : 121 131
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
Before swapping in main : a = 121 b = 131
After swapping in swap : *p = 131 *q = 121
Page No:
After swapping in main : a = 131 b = 121
Test Case - 2
ID: 20331A05D1
User Output
Enter two integer values : 555 999
Before swapping in main : a = 555 b = 999
After swapping in swap : *p = 999 *q = 555
After swapping in main : a = 999 b = 555
Test Case - 3
User Output
Enter two integer values : 1001 101
Before swapping in main : a = 1001 b = 101
After swapping in swap : *p = 101 *q = 1001
After swapping in main : a = 101 b = 1001
Test Case - 4
User Output
Enter two integer values : 9999 2999
Before swapping in main : a = 9999 b = 2999
After swapping in swap : *p = 2999 *q = 9999
After swapping in main : a = 2999 b = 9999
Test Case - 5
User Output
Enter two integer values : 10101 11010
Before swapping in main : a = 10101 b = 11010
After swapping in swap : *p = 11010 *q = 10101
After swapping in main : a = 11010 b = 10101
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Aim:
Page No:
Dangling pointer is a pointer which points to a memory that has been freed or deallocated. Such a
pointer still points to the memory location of the deallocated memory, accessing such memory might cause
segmentation fault error.
ID: 20331A05D1
In simple terms, a pointer pointing to a non-existing memory location is called a dangling pointer.
void main() {
int *p = (int *) malloc(sizeof(int)); // allocating heap memory
....
free(p); // after deallocating the heap memory, *p now becomes a dangling
pointer
*p = 12; // trying to access non-existing memory location can result in error
}
void main() {
int *q;
int *p = (int *) malloc(sizeof(int)); // allocating heap memory
q = p; // store the address containing in p to q i.e., p and q are referring the
same heap memory
free(q); // after deallocating the heap memory through q
*p = 12; // trying to access non-existing memory location through p which is also
a dangling pointer, can result in an error
}
The below code gives an error message as it is referencing dangling pointer, Correct the code and print the
given value on the console.
Source Code:
DanglingPointer.c
#include <stdio.h>
#include <stdlib.h>
void main() {
int *p, *q;
p = (int *) malloc(sizeof(int));
*p = 99;
MVGR College of Engineering (Autonomous)
q = p;
printf("The given value = %d\n", *p);
free(q);
}
Test Case - 1
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
The given value = 99
Page No:
ID: 20331A05D1
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 48 Exp. Name: Write a C program to Copy one String into another using Pointers Date:
Aim:
Page No:
Write a C program to copy one string into another using pointers.
ID: 20331A05D1
Enter source string : Robotic Tool
Target string : Robotic Tool
Source Code:
CopyStringPointers.c
#include <stdio.h>
#include "CopyStringPointers1.c"
void main() {
char source[100], target[100];
printf("Enter source string : ");
gets(source);
copyString(target, source);
printf("Target string : %s\n", target);
}
CopyStringPointers1.c
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter source string : CodeTantra
Target string : CodeTantra
Test Case - 2
User Output
Enter source string : Robotic Tool
Target string : Robotic Tool
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 3
User Output
Page No:
Enter source string : Virat Pointer Sachin Ganguly Dravid Warne
Target string : Virat Pointer Sachin Ganguly Dravid Warne
Test Case - 4
ID: 20331A05D1
User Output
Enter source string : Hyderabad London Losangels Weelington Colombo
Target string : Hyderabad London Losangels Weelington Colombo
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to Count number of Lowercase, Uppercase, digits and
S.No: 49 Date:
Other Characters using Pointers
Page No:
Aim:
Write a C program to find number of lowercase , uppercase , digits and other characters using
pointers.
ID: 20331A05D1
Sample Input and Output:
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"
void main() {
char str[80];
int upperCount = 0, lowerCount = 0, digitCount = 0, otherCount = 0;
printf("Enter a string : ");
gets(str);
countCharDigitOthers(str, &upperCount, &lowerCount, &digitCount, &otherCount);
printf("Number of uppercase letters = %d\n", upperCount);
printf("Number of lowercase letters = %d\n", lowerCount);
printf("Number of digits = %d\n", digitCount);
printf("Number of other characters = %d\n", otherCount);
}
CountCharDigitOthers1.c
{
*upperCount = *upperCount + 1;
}
else if (*str >= 'a' && *str <='z')
{
*lowerCount = *lowerCount +1;
}
else if (*str >= '0' && *str <= '9')
{
*digitCount = *digitCount +1;
}
else
{
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
4/16/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
*otherCount = *otherCount +1;
}
str++;
}
Page No:
}
ID: 20331A05D1
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter a string : CodeTantra123&*@987.
Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6
Number of other characters = 4
Test Case - 2
User Output
Enter a string : Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7
Test Case - 3
User Output
Enter a string : 12345
Number of uppercase letters = 0
Number of lowercase letters = 0
Number of digits = 5
Number of other characters = 0
Test Case - 4
User Output
Enter a string : USA@
Number of uppercase letters = 3
MVGR College of Engineering (Autonomous)
Test Case - 5
User Output
Enter a string : Wellington@NZ I will Stay Here
Number of uppercase letters = 6
Number of lowercase letters = 19
Number of digits = 0
Number of other characters = 5
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to find Sum of array elements by allocating memory
S.No: 50 Date:
using malloc() function
Page No:
Aim:
Write a program to find the sum of n elements by allocating memory by using malloc() function.
ID: 20331A05D1
At the time of execution, the program should print the message on the console as:
Enter n value :
Enter n value : 4
Next, the program should print the message on the console as:
Enter 4 values :
Enter 4 values : 1 5 4 2
SumOfArray1.c
#include <stdio.h>
#include <stdlib.h>
#include "UsingMalloc.c"
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);
read(p, n);
printf("The sum of given array elements : %d\n", sum(p, n));
}
MVGR College of Engineering (Autonomous)
UsingMalloc.c
int* allocateMemory(int n)
{
return (int* )malloc(n * sizeof(int));
}
void read(int *p,int n)
{
int i;
for (i=0;i<n;i++)
{
scanf("%d",p+i);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
}
}
int sum(int *p, int n)
{
Page No:
int i,total = 0;
for (i=0; i<n;i++)
{
total=total+ *(p+i);
ID: 20331A05D1
}
return total;
}
Test Case - 1
User Output
Enter n value : 4
Enter 4 values : 1 4 5 2
The sum of given array elements : 12
Test Case - 2
User Output
Enter n value : 3
Enter 3 values : 10 20 30
The sum of given array elements : 60
Test Case - 3
User Output
Enter n value : 4
Enter 4 values : -5 -6 -4 -2
The sum of given array elements : -17
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 51 Exp. Name: Write a Program to find Average Marks of the Class using Structures Date:
Aim:
Page No:
Implement structures to read, write and compute average marks for a class of N students. Also identify
students scoring above and below the average marks
ID: 20331A05D1
Sample Input and Output for the program are given below:
Source Code:
Program1110.c
#include <stdio.h>
struct student {
MVGR College of Engineering (Autonomous)
int roll;
char name[50];
int marks;
};
void main() {
struct student s[10];
int i, n, sum = 0;
float average = 0;
printf("Enter the number of students : ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the details of student - %d\n", i+1);
printf("Enter the roll number : ");
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
scanf("%d",&s[i].roll); // Correct the code
printf("Enter the name : ");
scanf("%s",s[i].name); // Correct the code
printf("Enter total marks : ");
Page No:
scanf("%d",&s[i].marks); // Correct the code
}
for (i=0;i<n;i++){ // Complete the code
sum = sum + s[i].marks; // Complete the code
ID: 20331A05D1
}
average = (float)sum/n; // Complete the code
printf("Class average : %f\n", average);
printf("RollNo\tStudentName\tTotalMarks\tAboveAverage(Y/N)\n");
for (i=0;i<n;i++) { // Complete the code
printf("%4d",s[i].roll); // Complete the code
printf("%15s",s[i].name); // Complete the code
printf("%15d",s[i].marks); // Complete the code
if (s[i].marks>=average) // Complete the code
printf("\tYes");
else
printf("\tNo");
printf("\n");
}
}
Test Case - 1
User Output
Enter the number of students : 3
Enter the details of student - 1 101
Enter the roll number : 101
Enter the name : Ganga
Enter total marks : 78
Enter the details of student - 2 102
Enter the roll number : 102
Enter the name : Saraswathi
Enter total marks : 83
Enter the details of student - 3 103
Enter the roll number : 103
Enter the name : Lakshmi
Enter total marks : 79
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the number of students : 4
Enter the details of student - 1 201
Enter the roll number : 201
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
Enter the name : Nadal
Enter total marks : 67
Page No:
Enter the details of student - 2 201
Enter the roll number : 201
Enter the name : Federer
Enter total marks : 75
ID: 20331A05D1
Enter the details of student - 3 203
Enter the roll number : 203
Enter the name : Murray
Enter total marks : 69
Enter the details of student - 4 204
Enter the roll number : 204
Enter the name : Andry
Enter total marks : 71
Class average : 70.500000
RollNo StudentName TotalMarks AboveAverage(Y/N)
201 Nadal 67 No
201 Federer 75 Yes
203 Murray 69 No
204 Andry 71 Yes
Test Case - 3
User Output
Enter the number of students : 2
Enter the details of student - 1 501
Enter the roll number : 501
Enter the name : Sindhu
Enter total marks : 68
Enter the details of student - 2 502
Enter the roll number : 502
Enter the name : Saina
Enter total marks : 68
Class average : 68.000000
RollNo StudentName TotalMarks AboveAverage(Y/N)
501 Sindhu 68 Yes
502 Saina 68 Yes
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 3/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=52&qId=5d5285b3e3fffe0a91cfea4d&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Exp. Name: Write a Program to enter n students data using calloc() and display
S.No: 52 Date:
Failed Students List
Page No:
Aim:
Write a C program to enter n students data using calloc() and display the failed students list.
ID: 20331A05D1
Sample Input and Output for the program are given below:
Source Code:
FailedList.c
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;
int marks[6], sum;
float avg;
};
#include "FailedList1.c"
void main() {
struct student *s;
int i, n;
printf("Enter the number of students : ");
scanf("%d", &n);
s = allocateMemory(s, n);
read(s, n);
calculateMarks(s, n);
displayFailedList(s, n);
MVGR College of Engineering (Autonomous)
FailedList1.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=52&qId=5d5285b3e3fffe0a91cfea4d&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=52&qId=5d5285b3e3fffe0a91cfea4d&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
{
printf("Enter the details of student - %d\n",i+1);
printf("Enter the roll number : ");
scanf("%d",&s[i].roll);
Page No:
printf("Enter 6 sujects marks : ");
scanf("%d%d%d%d%d%d", &s[i].marks[0],&s[i].marks[1],&s[i].marks[2],&s[i].marks
[3],&s[i].marks[4],&s[i].marks[5]);
}// write the code
ID: 20331A05D1
}
void calculateMarks(struct student *s, int n) {
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<6;j++)
s[i].sum = s[i].sum + s[i].marks[j];
}
for (i=0;i<n;i++)
{
s[i].avg =(float)s[i].sum/6;
}
}
void displayFailedList(struct student *s, int n) {
int i;
printf("RollNo\tTotalMarks\tAverageMarks\tStatus\n");
for (i = 0; i < n; i++) {
printf("%d\t",s[i].roll); // Fill the missing code
printf("%d\t",s[i].sum); // Fill the missing code
printf("%f\t",s[i].avg); // Fill the missing code
if (s[i].marks[0]<35||s[i].marks[1]<35||s[i].marks[2]<35||s[i].marks[3]<35||s[i].
marks[4]<35||s[i].marks[5]<35) // Fill the missing code
printf("Fail");
else
printf("Pass");
printf("\n");
}
}
Test Case - 1
User Output
Enter the number of students : 3
MVGR College of Engineering (Autonomous)
Test Case - 1
103 421 70.166664 Pass
Page No:
Test Case - 2
User Output
ID: 20331A05D1
Enter the number of students : 2
Enter the details of student - 1 1001
Enter the roll number : 1001
Enter 6 sujects marks : 26 57 68 67 67 65
Enter the details of student - 2 1002
Enter the roll number : 1002
Enter 6 sujects marks : 58 67 58 89 87 76
RollNo TotalMarks AverageMarks Status
1001 350 58.333332 Fail
1002 435 72.500000 Pass
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=52&qId=5d5285b3e3fffe0a91cfea4d&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to find Total Marks of a Student using Command-line
S.No: 53 Date:
arguments
Page No:
Aim:
Write a C program to read student name and 3 subjects marks from the command line and display the student
details along with total.
ID: 20331A05D1
Sample Input and Output - 1:
Hint : atoi() is a library function that converts string to integer. When program gets the input from command
line, string values transfer in the program, we have to convert them to integers. atoi() is used to return the
integer of the string arguments.
Source Code:
TotalMarksArgs.c
#include<stdio.h>
int main(int argc,char *argv[])
{
if (argc !=5)
{
printf("Arguments passed through command line are not equal to 4\n");
return 1;
}
printf("Student name : %s\n",argv[1]);
printf("Subject-1 marks : %s\n",argv[2]);
printf("Subject-1 marks : %s\n",argv[3]);
printf("Subject-1 marks : %s\n",argv[4]);
MVGR College of Engineering (Autonomous)
int sum;
sum = atoi(argv[2]) + atoi(argv[3]) + atoi(argv[4]);
printf("Total marks : %d\n",sum);
return 0;
}
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 1
User Output
Page No:
Student name : Sachin
Subject-1 marks : 67
Subject-1 marks : 89
Subject-1 marks : 58
ID: 20331A05D1
Total marks : 214
Test Case - 2
User Output
Arguments passed through command line are not equal to 4
Test Case - 3
User Output
Student name : Kohli
Subject-1 marks : 78
Subject-1 marks : 98
Subject-1 marks : 83
Total marks : 259
Test Case - 4
User Output
Student name : Taylor
Subject-1 marks : 45
Subject-1 marks : 45
Subject-1 marks : 45
Total marks : 135
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=54&qId=5d529c1de3fffe0a91d00a2c&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Aim:
Page No:
Write a C program to implement realloc() .
The process is
ID: 20331A05D1
1. Allocate memory of an array with size 2 by using malloc()
2. Assign the values 10 and 20 to the array
3. Reallocate the size of the array to 3 by using realloc()
4. Assign the value 30 to the newly allocated block
5. Display all the 3 values
Source Code:
ProgramOnRealloc.c
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for (i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
}
Test Case - 1
User Output
10 20 30
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=54&qId=5d529c1de3fffe0a91d00a2c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/1
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Aim:
Page No:
Write a C program to read and print a date using dd/mm/yyyy format using bit-fields and differentiate the
same without using bit-fields.
ID: 20331A05D1
The process is
1. Create a structure date with bit fields
2. Assign the day, month and year to the members
3. Display the structure member values along with size of the structure
Source Code:
ProgramOnBitFields.c
#include <stdio.h>
struct date {
unsigned int day : 5;
unsigned int month : 4;
unsigned int year;
};
struct date1 {
unsigned int day;
unsigned int month;
unsigned int year;
};
void main() {
struct date d;
struct date1 d1;
printf("Enter a date : ");
scanf("%d%d%d", &d1.day, &d1.month, &d1.year);
d.day =d1.day;
d.month = d1.month;
d.year = d1.year;
printf("The size of the structure without bit-fields : %zu\n",sizeof(struct date1));
// Fill in the missing code
printf("Date stored without bit-fields : %d/%d/%d\n",d1.day,d1.month,d1.year); // Fi
ll in the missing code
printf("The size of the structure uses bit-fields : %zu\n",sizeof(struct date)); //
Fill in the missing code
printf("Date stored in bit-fields : %d/%d/%d\n",d.day,d.month,d.year); // Fill in th
e missing code
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter a date : 23 8 1998
The size of the structure without bit-fields : 12
Date stored without bit-fields : 23/8/1998
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 23/8/1998
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
User Output
Page No:
Enter a date : 29 02 2016
The size of the structure without bit-fields : 12
Date stored without bit-fields : 29/2/2016
The size of the structure uses bit-fields : 8
ID: 20331A05D1
Date stored in bit-fields : 29/2/2016
Test Case - 3
User Output
Enter a date : 1 1 2020
The size of the structure without bit-fields : 12
Date stored without bit-fields : 1/1/2020
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 1/1/2020
Test Case - 4
User Output
Enter a date : 31 12 2018
The size of the structure without bit-fields : 12
Date stored without bit-fields : 31/12/2018
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 31/12/2018
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 56 Exp. Name: Write a C program to Create a Singly Linked List Date:
Aim:
Page No:
The addNodes() function creates a new list and adds elements to the list until delimiter -1 is occurred.
ID: 20331A05D1
Step-1: Allocate memory to the node temp .
Step-2: Store an integer value into data field of node temp .
Step-3: If the first is referenced to NULL then assign temp to first node,
otherwise traverse the list up to the last node (meaning the next field of a
node contains Null )
and then assign the temp node to next field of the lastNode .
Step-4: Finally return the first node.
The traverseList() function traverses and prints all the elements of the list.
Fill in the missing code in the below functions addNodes(NODE first, int x) and
traverseList(NODE first) in the file CreateAndAddNodes.c .
Source Code:
SingleLL1.c
#include<stdio.h>
#include<stdlib.h>
#include "CreateAndAddNodes.c"
void main() {
NODE first = NULL;
int x;
printf("Enter elements up to -1 : ");
scanf("%d", &x);
while (x != -1) {
first = addNodes(first, x);
MVGR College of Engineering (Autonomous)
scanf("%d", &x);
}
if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
}
CreateAndAddNodes.c
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
struct node {
int data;
struct node *next;
Page No:
};
typedef struct node *NODE;
NODE createNode() {
ID: 20331A05D1
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
Test Case - 1
User Output
Enter elements up to -1 : 9 18 27 36 45 -1
The elements in SLL are : 9 --> 18 --> 27 --> 36 --> 45 --> NULL
Test Case - 2
User Output
Enter elements up to -1 : 12 14 19 23 -1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
The elements in SLL are : 12 --> 14 --> 19 --> 23 --> NULL
Page No:
ID: 20331A05D1
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Page No:
Aim:
Write a C program to demonstrate the differences between structures and unions .
ID: 20331A05D1
The process is
1. Create a structure student-1 with members rollno, m1, m2, m3, total of int type and avg of float type
2. Read rollno, m1, m2 and m3 of student-1
3. Find and display total and average marks of student-1
4. Display the size of struct student-1
5. Create a union student-2 with members rollno, m1, m2, m3, total of int type and avg of float type
6. Read rollno, m1, m2 and m3 of student-2
7. Find and display total and average marks of student-2
8. Display the size of union student-2
Source Code:
StructureAndUnion.c
#include<stdio.h>
struct student1
{
int rollno;
int m1,m2,m3,total;
float avg;
};
union student2
{
int rollno;
int m1,m2,m3,total;
MVGR College of Engineering (Autonomous)
float avg;
};
void main()
{
union student2 u;
struct student1 s;
int sum=0;
printf("Enter rollno and 3 subjects marks of student - 1 : ");
scanf("%d%d%d%d",&s.rollno,&s.m1,&s.m2,&s.m3);
s.total = s.m1 + s.m2 + s.m3;
s.avg = s.total/3.0;
printf("Total and average marks of student - 1 : %d %f\n",s.total,s.avg);
printf("Size of struct student - 1 : %zu\n",sizeof(struct student1));
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
printf("Enter rollno of student - 2 : ");
scanf("%d",&u.rollno);
printf("Enter first subject marks of student - 2 : ");
scanf("%d",&u.m1);
Page No:
sum = u.m1;
printf("Enter second subject marks of student - 2 : ");
scanf("%d",&u.m2);
sum = sum + u.m2;
ID: 20331A05D1
printf("Enter third subject marks of student - 2 : ");
scanf("%d",&u.m3);
sum = sum + u.m3;
u.total = sum;
printf("Total marks of student - 2 : %d\n",u.total);
u.avg = u.total/3.0;
printf("Average marks of student - 2 : %f\n",u.avg);
printf("Size of union student - 2 : %zu\n",sizeof(union student2));
}
Test Case - 1
User Output
Enter rollno and 3 subjects marks of student - 1 : 101 76 58 67
Total and average marks of student - 1 : 201 67.000000 102
Size of struct student - 1 : 24 102
Enter rollno of student - 2 : 102
Enter first subject marks of student - 2 : 76
Enter second subject marks of student - 2 : 87
Enter third subject marks of student - 2 : 69
Total marks of student - 2 : 232
Average marks of student - 2 : 77.333336
Size of union student - 2 : 4
Test Case - 2
User Output
Enter rollno and 3 subjects marks of student - 1 : 105 66 65 68
Total and average marks of student - 1 : 199 66.333336 106
Size of struct student - 1 : 24 106
Enter rollno of student - 2 : 106
MVGR College of Engineering (Autonomous)
Test Case - 3
User Output
Enter rollno and 3 subjects marks of student - 1 : 501 76 85 84
Total and average marks of student - 1 : 245 81.666664 502
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Test Case - 3
Size of struct student - 1 : 24 502
Enter rollno of student - 2 : 502
Page No:
Enter first subject marks of student - 2 : 99
Enter second subject marks of student - 2 : 57
Enter third subject marks of student - 2 : 69
Total marks of student - 2 : 225
ID: 20331A05D1
Average marks of student - 2 : 75.000000
Size of union student - 2 : 4
Test Case - 4
User Output
Enter rollno and 3 subjects marks of student - 1 : 201 75 46 59
Total and average marks of student - 1 : 180 60.000000 201
Size of struct student - 1 : 24 201
Enter rollno of student - 2 : 201
Enter first subject marks of student - 2 : 66
Enter second subject marks of student - 2 : 57
Enter third subject marks of student - 2 : 61
Total marks of student - 2 : 184
Average marks of student - 2 : 61.333332
Size of union student - 2 : 4
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/3
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
S.No: 58 Exp. Name: Write a C program to Open a File and to Print its contents on the screen Date:
Aim:
Page No:
Follow the instructions given below to write a program to open a file and to print its contents on the
screen.
Open a new file " SampleText1.txt " in write mode
ID: 20331A05D1
Write the content in the file
Close the file
Open the same file in read mode
Read the content from file and print them on the screen
Close the file
Source Code:
Program1501.c
#include <stdio.h>
void main() {
FILE *fp;
char ch;
fp = fopen("SampleText1.txt", "w"); // Open a new file in write mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') { // Repeat loop till read @ at the end
putc(ch, fp); // Put read character onto the file
}
putc(ch, fp); // Put delimiter @ at the end on the file
fclose(fp); // Close the file
fp = fopen("SampleText1.txt", "r"); // Open the existed file in read mode
printf("Given message is : ");
while ((ch = getc(fp)) != '@') { // Repeat loop till get @ at the end of existed fil
e
putchar(ch); // Put the character on the screen
}
printf("\n");
fclose(fp); // Close the file
}
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the text with @ at end : CodeTantra is
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 2
increasing development of Languages Year increasing development of Languages
Year
Page No:
by Year@ by Year@
Given message is : CodeTantra is
increasing development of Languages Year
ID: 20331A05D1
by Year
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Exp. Name: Write a C program to write and read text into a binary file using fread()
S.No: 59 Date:
and fwrite()
Page No:
Aim:
Write a C program to write and read text into a binary file using fread() and fwrite().
ID: 20331A05D1
The program is to write a structure containing student roll number, name, marks into a file and read them to
print on the standard output device.
Source Code:
FilesStructureDemo1.c
#include<stdio.h>
struct student {
int roll;
char name[25];
float marks;
};
void main() {
FILE *fp;
char ch;
struct student s;
fp = fopen("student-information.txt","w"); // Complete the statement
do {
printf("Enter Roll number : ");
scanf("%d",&s.roll); // Complete the statement
printf("Enter Name : ");
scanf("%s",s.name); // Complete the statement
printf("Enter Marks : ");
scanf("%f",&s.marks); // Complete the statement
fwrite(&s, sizeof(s), 1, fp); // Complete the statement
printf("Do you want to add another data (y/n) : ");
scanf(" %c", &ch);
}while (ch == 'y' || ch == 'y'); // Complete the condition
printf("Data written successfully...\n");
fclose(fp);
fp = fopen("student-information.txt","r"); // Complete the statement
printf("\t Roll \t Name \t Marks\n");
while (fread(&s, sizeof(s), 1, fp) > 0) { // Complete the condition
printf("\t %d \t %s \t %f\n",s.roll,s.name,s.marks); // complete the statement
}
fclose(fp);
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter Roll number : 501
Enter Name : Ganga
Enter Marks : 92
Do you want to add another data (y/n) : y
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4b…
Test Case - 1
Enter Roll number : 502
Enter Name : Smith
Page No:
Enter Marks : 62
Do you want to add another data (y/n) : n
Data written successfully...
Roll Name Marks
ID: 20331A05D1
501 Ganga 92.000000
502 Smith 62.000000
Test Case - 2
User Output
Enter Roll number : 2001
Enter Name : Federer
Enter Marks : 89
Do you want to add another data (y/n) : y
Enter Roll number : 2001
Enter Name : Kohli
Enter Marks : 95
Do you want to add another data (y/n) : y
Enter Roll number : 2003
Enter Name : Srikant
Enter Marks : 73
Do you want to add another data (y/n) : n
Data written successfully...
Roll Name Marks
2001 Federer 89.000000
2001 Kohli 95.000000
2003 Srikant 73.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 60 Exp. Name: Write a C program to Copy contents of one File into another File Date:
Aim:
Page No:
Write a program to copy contents of one file into another file. Follow the instructions given below to write a
program to copy the contents of one file to another file:
Open a new file " SampleTextFile1.txt " in write mode
ID: 20331A05D1
Write the content onto the file
Close the file
Open an existing file " SampleTextFile1.txt " in read mode
Open a new file " SampleTextFile2.txt " in write mode
Copy the content from existing file to new file
Close the files
Open the copied file in read mode
Read the text from file and print on the screen
Close the file
Source Code:
Program1502.c
#include <stdio.h>
void main() {
FILE *fp, *fp1, *fp2;
char ch;
fp = fopen("SampleTextFile1.txt", "w"); // Open a new file in write mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') { // Read loop till get @ at the end
putc(ch, fp);
}
putc(ch, fp);
fclose(fp);
fp1 = fopen("SampleTextFile1.txt", "r"); // Open an existed file in read mode
fp2 = fopen("SampleTextFile2.txt", "w"); // Open a new file in write mode
while ((ch = getc(fp1)) != '@') { // Repeat loop till get @ at the end of existed fi
le
putc(ch, fp2);
}
putc(ch, fp2);
fclose(fp1); // Close the existed file
fclose(fp2); // Close the copied file
fp2 = fopen("SampleTextFile2.txt", "r"); // Open the copied file in read mode
printf("Copied text is : ");
while ((ch = getc(fp2)) != '@') { // Repeat loop till get @ at the end of copied fil
e
MVGR College of Engineering (Autonomous)
putchar(ch);
}
printf("\n");
fclose(fp2); // Close the copied file
}
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
User Output
Page No:
Enter the text with @ at end : CodeTantra started in the year 2014@
Copied text is : CodeTantra started in the year 2014
Test Case - 2
ID: 20331A05D1
User Output
Enter the text with @ at end : CodeTantra received
best Startup award from Hysea in 2016@ best Startup award from Hysea in
2016@
Copied text is : CodeTantra received
best Startup award from Hysea in 2016
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
Exp. Name: Write a C program to Merge two Files and stores their contents in
S.No: 61 Date:
another File using Command-Line arguments
Page No:
Aim:
Write a program to merge two files and stores their contents in another file using command-line arguments.
Open a new file specified in argv[1] in write mode
ID: 20331A05D1
Write the content onto the file
Close the file
Open another new file specified in argv[2] in write mode
Write the content onto the file
Close the file
Open first existing file specified in argv[1] in read mode
Open a new file specified in argv[3] in write mode
Copy the content from first existing file to new file
Close the first existing file
Open another existing file specified in argv[2] in read mode
Copy its content from existing file to new file
Close that existing file
Close the merged file
Source Code:
MergeFilesArgs.c
#include <stdio.h>
void main(int argc, char* argv[]) {
FILE *fp1, *fp2, *fp3;
char ch;
fp1 = fopen(argv[1],"w"); // Open file in corresponding mode
printf("Enter the text with @ at end for file-1 :\n");
while ((ch = getchar()) != '@') { // Write the condition
putc(ch, fp1);
}
putc(ch, fp1);
fclose(fp1);
fp2 = fopen(argv[2], "w"); // Open file in corresponding mode
printf("Enter the text with @ at end for file-2 :\n");
while ((ch = getchar()) != '@'){ // Write the condition
putc(ch, fp2);
}
putc(ch, fp2);
fclose(fp2);
fp1 = fopen(argv[1],"r"); // Open a first existed file in read mode
fp3 = fopen(argv[3] ,"w"); // Open a new file in write mode
MVGR College of Engineering (Autonomous)
while ((ch = getc(fp1)) != '@') { // Repeat loop till get @ at the end of existed fi
le
putc(ch, fp3);
}
fclose(fp1); // Close the first existed file
fp2 = fopen(argv[2], "r"); // Open a secong existed file in read mode
while ((ch = getc(fp2)) !='@') { // Repeat loop till get @ at the end of existed fil
e
putc(ch, fp3);
}
putc(ch, fp3);
fclose(fp2);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4be…
fclose(fp3);
fp3 = fopen(argv[3], "r"); // Open the merged file in read mode
printf("Merged text is : ");
while ((ch = getc(fp3)) != '@') { // Repeat loop till get @ at the end of merged fil
Page No:
e
putchar(ch);
}
printf("\n");
ID: 20331A05D1
fclose(fp3); // Close the merged file
}
Test Case - 1
User Output
Enter the text with @ at end for file-1 : This is CodeTantra
They implemented automatic robotic tool@ They implemented
automatic robotic
tool@
Enter the text with @ at end for file-2 : Started the company in
2014@ 2014@
Merged text is : This is CodeTantra
They implemented automatic robotic tool
Started the company in
2014
Test Case - 2
User Output
Enter the text with @ at end for file-1 : Best
Fair Fair
Awesome@ Awesome@
Enter the text with @ at end for file-2 : False@
Merged text is : Best
Fair
Awesome
False
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Exp. Name: Write a C program to Count number of Characters, Words and Lines of a
S.No: 62 Date:
given File
Page No:
Aim:
Write a program to count number of characters, words and lines of given text file.
open a new file " DemoTextFile2.txt " in write mode
ID: 20331A05D1
write the content onto the file
close the file
open the same file in read mode
read the text from file and find the characters, words and lines count
print the counts of characters, words and lines
close the file
Source Code:
Program1508.c
#include <stdio.h>
void main() {
FILE *fp;
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
fp = fopen("DemoTextFile2.txt", "w"); // Open a new file in write mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') { // Repeat loop till read @ at the end
putc(ch, fp); // Put read character onto the file
}
putc(ch, fp); // Put delimiter @ at the end on the file
fclose(fp); // Close the file
fp = fopen("DemoTextFile2.txt", "r"); // Open the existing file in read mode
do {
if ((ch == ' ') || (ch == '\n') || (ch == '@')) // Write the condition to count w
ords
wordCount++;
else
charCount++;
if (ch == '\n' || ch == '@') // Write the condition to count lines
lineCount++;
} while ((ch = getc(fp)) != '@'); // Repeat loop till read @ at the end
fclose(fp);
printf("Total characters : %d\n", charCount);
printf("Total words : %d\n", wordCount);
printf("Total lines : %d\n", lineCount);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter the text with @ at end : Arise! Awake!
and stop not until and stop not until
the goal is reached@ the goal is reached@
Total characters : 43
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
Total words : 10
Total lines : 3
Page No:
Test Case - 2
User Output
ID: 20331A05D1
Enter the text with @ at end : Believe in your self
and the world will be and the world will be
at your feet@ at your feet@
Total characters : 44
Total words : 12
Total lines : 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
S.No: 63 Exp. Name: Write a C program to Copy last n characters from one File to another File Date:
Aim:
Page No:
Write a program to copy last n characters from file-1 to file-2.
open a new file " TestDataFile1.txt " in write mode
write the content onto the file
ID: 20331A05D1
close the file
open an existing file " TestDataFile1.txt " in read mode
open a new file " TestDataFile2.txt " in write mode
read the number of characters to copy
set the cursor position by using fseek()
copy the content from existing file to new file
close the files
open the copied file " TestDataFile2.txt " in read mode
read the text from file and print on the screen
close the file
Source Code:
Program1505.c
#include <stdio.h>
void main() {
FILE *fp, *fp1, *fp2;
int num, length;
char ch;
fp = fopen("TestDataFile1.txt", "w"); // Write the mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') {
putc(ch, fp);
}
putc(ch, fp);
fclose(fp);
fp1 = fopen("TestDataFile1.txt", "r"); // Write the mode
fp2 = fopen("TestDataFile2.txt", "w"); // Write the mode
printf("Enter number of characters to copy : ");
scanf("%d",&num); // Read number
fseek(fp1, 0L, SEEK_END); // Write the correct operation
length = ftell(fp1);
fseek(fp1, (length - num - 1), SEEK_SET); // Write the correct operation
while ((ch = getc(fp1)) != '@') { // Write the condition
putc(ch, fp2);
}
putc(ch, fp2);
MVGR College of Engineering (Autonomous)
fclose(fp1);
fclose(fp2);
fp2 = fopen("TestDataFile2.txt", "r");// Write the mode
printf("Copied text is : ");
while ((ch = getc(fp2)) != '@') {
putchar(ch); // Write the condition
}
printf("\n");
fclose(fp2);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
5/5/2021 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4…
Test Case - 1
Page No:
User Output
Enter the text with @ at end : We should not give up
ID: 20331A05D1
and we should not allow the problem to defeat and we should not allow the
us@ problem to defeat us@
Enter number of characters to copy : 15
Copied text is : em to defeat us
Test Case - 2
User Output
Enter the text with @ at end : You have to dream
before before
Your dreams can come true@ Your dreams can come true@
Enter number of characters to copy : 20
Copied text is : dreams can come true
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2