Cp Lab Record
Cp Lab Record
Submitted by
Name :
Reg. No. : 24A21A
Course/Branch/Section : B.Tech. / /’ ’
Semester : I SEM
Year : 2024-2025
Submitted to
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COMPUTER PROGRAMMING LABORATORY
(Subject Code: 23CS1L01)
*** LAB RECORD ***
SWARNANDHRA
COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
2024-2025
SWARNANDHRA
COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
Narsapur – 534 280
BONAFIDE CERTIFICATE
with Reg. No. 24A21A in the Computer Programming laboratory during the
XV Add on programs
SYSTEM REQUIREMENTS
SOFTWARE :
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
Vi:
The default editor that comes with the UNIX operating system is
called vi (visual editor). Using vi editor, we can edit an existing file or create a
new file from scratch. we can also use this editor to just read a text file. To open
vi editors, we just need to type the command mentioned below.
vi [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-
existing file.
Creating a new file with `file_name` = scet
vi scet
Modes of Operation in the vi editor
There are three modes of operation in vi:
Command Mode:
When vi starts up, it is in Command Mode. This mode is where vi interprets
any characters we type as commands and thus does not display them in the
window. This mode allows us to move through a file, and delete, copy, or paste
a piece of text. Enter into Command Mode from any other mode, requires
pressing the [Esc] key. If we press [Esc] when we are already in Command
Mode, then vi will beep or flash the screen.
Insert mode:
This mode enables you to insert text into the file. Everything that’s typed in this
mode is interpreted as input and finally, it is put in the file. The vi always starts
in command mode. To enter text, you must be in insert mode. To come in insert
mode, you simply type i. To get out of insert mode, press the Esc key, which
will put you back into command mode.
Last Line Mode (Escape Mode):
Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The
cursor will jump to the last line of the screen and vi will wait for a command.
This mode enables you to perform tasks such as saving files and executing
commands.
Vim:
Vim is an advanced and highly configurable text editor built to enable efficient
text editing. Vim text editor is developed by Bram Moolenaar. It supports most
file types and vim editor is also known as a programmer’s editor. We can use its
plugin based on our needs.
To open vim editors, we just need to type the command mentioned below.
vim [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-
existing file.
Emacs:
The Emacs is referred to a family of editors, which means it has many versions
or flavors or iterations. The most commonly used version of Emacs editor is
GNU Emacs and was created by Richard Stallman. The main difference
between text editors like vi, vim, nano, and the Emacs is that is faster, powerful,
and simple in terms of usage because of its simple user interface. Unlike the vi
editor, the Emacs editor does not use an insert mode, and it is by default in
editing mode, i.e., whatever you type will directly be written to the buffer,
unless you manually enter command mode by using keyboard shortcuts.
To open vim editors, we just need to type the command mentioned below.
emacs [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-
existing file.
gcc:
GNU Compiler Collection, commonly known as GCC, is one of the most popular
compilers for programming languages like C, C++, and Fortran. It's the default
compiler for many Linux distributions and is also available for macOS and
Windows.
Is open-source and freely available under the GNU General Public License
(GPL). This can be executed by following syntax:
gcc -o output_file source_file.c
Let us consider we have a file hello.c which consists of the following code:
#include <stdio.h>
main ()
{
printf ("Hello, world!");
return 0;
}
example:
gcc -o hello hello.c
The resulting executable ‘hello’ now uses the new main function to produce the
following output:
$ ./hello
Hello, everyone!
Program:
#include<stdio.h>
main()
{
int x,y,z,avg,sum;
printf(“Enter input for X, Y, Z : ”);
scanf(“%d%d%d”,&x,&y,&z);
sum= x+y+z;
avg= sum/3;
printf(“Sum of three given numbers is: %d”,sum);
printf(“Average of three given numbers is: %d”,avg);
}
Output:
Flowchart:
Algorithm :
Step-1 Start
Step-2 Input temperature in Celsius say C
Step-3 F = (9.0/5.0 x C) + 32
Step-4 Display Temperature in Fahrenheit F
Step-5 Input temperature in Fahrenheit Say F
Step-6 C = (F-32)/9*5;
Step-7 Display Temperature in Celsius C
Step-8 Stop
Program:
#include <stdio.h>
main()
{
float fh,cs;
int ch;
printf("\n1. Convert Fahrenheit to celsius");
printf("\n2. Convert celsius to fahrenheit");
printf("\nEnter choice 1 or 2: ");
scanf("%d",&ch);
if(ch==1)
{
printf("\nEnter temperature in fahrenheit");
scanf("%f",&fh);
cs=(fh-32)/1.8;
printf("Temperature in celsius is %.2f",cs);
}
else if(ch==2)
{
printf("\nEnter temperature in celsius");
scanf("%f",&cs);
fh=1.8*cs+32;
printf("Temperature in fahrenheit is %.2f",fh);
}
else
{
printf("Invalid choice");
}
}
Output:
Step 1:start.
Step 2: Input value of P,N,R
Step 3: SI = (P*N*R)/100
Step 4: Display SI
Step 5: Stop
Program:
#include<stdio.h>
main()
{
int p,n;
float r,si;
printf("Enter Principal Amount\n");
scanf("%d",&p);
printf("Enter No of Years\n");
scanf("%d",&n);
printf("Enter Rate Of Interest\n");
scanf("%f",&r);
si=p*n*r/100;
printf("\n Simple Interest is %f",si);
}
Output:
WEEK 3 Objective: Learn how to define variables with the desired data-
type, initialize them with appropriate values and how arithmetic operators
can be used with variables and constants.
Program:
#include<stdio.h>
main()
{
int a,b,c,d,e,f,g,y,z,j,i,ans;
printf("Enter the value of a,b,c,d,e,f,g:");
scanf("\n%d\n%d\n%d\n%d\n%d\n%d\n%d",&a,&b,&c,&d,&e,&f,&g);
ans=a+b+c+(d*e)+f*g;
printf("Answer is:%d",ans);
y=a/b*(-b*d/3);
printf("\ny=%d",y);
z=a+++b---a;
printf("\nz=%d",z);
j=(i++)+(++i);
printf("\nj=%d",z);
}
Output:
Output:
iii) Take marks of 5 subjects in integers, and find the total, average in float
Program:
#include <stdio.h>
main()
{
int eng, phy, chem, math, comp;
float total, average;
printf("Enter marks of five subjects: :- ");
scanf("%d%d%d%d%d", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
}
Output:
Output:
#include <stdio.h>
main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
}
Output:
Output:
Output:
Output:
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j;
printf("enter size of the matrix of a \n");
scanf("%d%d",&r1,&c1);
printf("enter size of the matrix of b \n");
scanf("%d%d",&r2,&c2);
printf("enter matrix a element \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter matrix b element \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("matrix a \n");
for(i=0;i<r1;i++)
{
#include <stdio.h>
int main()
{
int arr[50], num, x, y, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(x = 0; x < num; x++)
scanf("%d", &arr[x]);
for(x = 0; x < num - 1; x++){
for(y = 0; y < num - x - 1; y++){
if(arr[y] > arr[y + 1]){
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(x = 0; x < num; x++){
printf("%d ", arr[x]);
}
return 0;
}
Output:
iii) Enter n students data using calloc() and display failed students list
Program:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[50];
float marks;
};
iv) Read student name and marks from the command line and display the
student details along with the total.
Program:
#include <stdio.h>
int main() {
char name[50];
int rollNumber;
int marks[5];
int total = 0;
printf("Enter student name: ");
scanf("%s", name);
printf("Enter roll number: ");
scanf("%d", &rollNumber);
printf("Enter marks for 5 subjects:\n");
for (int i = 0; i < 5; ++i) {
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
total += marks[i];
}
float average = (float)total / 5;
printf("\nStudent Details:\n");
printf("Name: %s\n", name);
printf("Roll Number: %d\n", rollNumber);
printf("Total Marks: %d\n", total);
printf("Average Marks: %.2f\n", average);
}
Output:
WEEK 10: Objective: Experiment with C Structures, Unions, bit fields and
self-referential structures (Singly linked lists) and nested structures
Output:
Output:
WEEK 11: Objective: Explore the Functions, sub-routines, scope and extent
of variables, doing some experiments by parameter passing using call by
value. Basic methods of numerical integration
}
void transpose(int a[2][2])
{
int b[2][2],i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
}
}
printf("*** transpose matrix ***\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
Output:
Output:
WEEK 13: Objective: Explore the basic difference between normal and
pointer variables, Arithmetic operations using pointers and passing
variables to functions using pointers
Output:
iii) Write a C program to copy one string into another using pointer.
Program:
#include <stdio.h>
void copyString(char *destination, const char *source);
main()
{
char source[100], destination[100];
printf("Enter the source string: ");
scanf("%s", source);
copyString(destination, source);
printf("Source String: %s\n", source);
printf("Copied String: %s\n", destination);
}
void copyString(char *destination, const char *source)
{
while (*source != '\0') {
*destination = *source;
Output:
Program:
#include <stdio.h>
void countCharacters(const char *str, int *lowercase, int *uppercase, int *digits,
int *others);
int main() {
char inputString[100];
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
int lowercaseCount = 0, uppercaseCount = 0, digitCount = 0, otherCount = 0;
countCharacters(inputString, &lowercaseCount, &uppercaseCount, &digitCount,
&otherCount);
printf("Lowercase characters: %d\n", lowercaseCount);
printf("Uppercase characters: %d\n", uppercaseCount);
printf("Digits: %d\n", digitCount);
printf("Other characters: %d\n", otherCount);
}
void countCharacters(const char *str, int *lowercase, int *uppercase, int *digits,
int *others)
{
while (*str != '\0') {
if (*str >= 'a' && *str <= 'z') {
(*lowercase)++;
}
else if (*str >= 'A' && *str <= 'Z')
Output:
ii) Write a C program to write and read text into a binary file using fread()
and fwrite()
Program:
#include<stdio.h>
struct student
{
int sno;
char sname [30];
float marks;
char temp;
};
main ( )
{
struct student s[60];
int i;
FILE *fp;
fp = fopen ("student1.txt", "w");
for (i=0; i<2; i++)
{
printf ("enter details of student %d", i+1);
printf("student number:");
scanf("%d",&s[i].sno);
scanf("%c",&s[i].temp);
printf("student name:");
gets(s[i].sname);
printf("student marks:");
scanf("%f",&s[i].marks);
fwrite(&s[i], sizeof(s[i]),1,fp);
}
fclose (fp);
fp = fopen ("student1.txt", "r");
for (i=0; i<2; i++)
{
printf ("details of student %d are", i+1);
fread (&s[i], sizeof (s[i]) ,1,fp);
printf("student number = %d ", s[i]. sno);
printf("student name = %s ", s[i]. sname);
printf("marks = %f ", s[i]. marks);
} fclose(fp);
getch( );
}
Output:
iv) Write a C program to merge two files into the third file using command-
line arguments.
Program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
FILE *fs1, *fs2, *ft;
char ch, file1[20], file2[20], file3[20];
if ( argc != 4 )
{
printf("There is no file names..\n");
exit(0);
}
fs1 = fopen(argv[1],"r");
fs2 = fopen(argv[2],"r");
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(0);
exit(EXIT_FAILURE);
}
ft = fopen(argv[3],"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
Output
ADD ON PROGRAMS
Program:
#include <stdio.h>
int main()
{
int arr[10], FreqArr[10], i, j, Count, Size;
printf("\n Please Enter Number of elements in an array : ");
scanf("%d", &Size);
printf("\n Please Enter %d elements of an Array : ", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
FreqArr[i] = -1;
}
for (i = 0; i < Size; i++)
{
Count = 1;
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
Count++;
FreqArr[j] = 0;
}
}
if(FreqArr[i] != 0)
{
FreqArr[i] = Count;
}
}
printf("\n Frequency of All the Elements in this Array are : \n");
for (i = 0; i < Size; i++)
{
if(FreqArr[i] != 0)
{
printf("%d Occurs %d Times \n", arr[i], FreqArr[i]);
}
}
}
Output
#include <stdio.h>
int main()
{
int n;
printf("Enter the upper limit = ");
scanf("%d", & n);
printf("First %d natural numbers are : ", n);
printUptoN(n);
return 0;
}
// recursive function
void printUptoN(int n)
{
//condition for calling
if (n > 1)
printUptoN(n - 1);
printf("%d\t ", n);
}
Output
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0; i < len ; i++)
{
if(str[i] != str[len-i-1])
{ temp = 1;
break;
}
}
if (temp==0)
{
printf("String is a palindrome");
}
else
{
printf("String is not a palindrome");
}
}
Output
Program:
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee DOJ(dd/mm/yyyy) : %d/%d/%d\n",
e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
return 0;
}
Output