C Programming Lab Manual 2021
C Programming Lab Manual 2021
GENERAL INSTRUCTIONS
Rough record and Fair record are needed to record the experiments conducted in the laboratory. Rough
records are needed to be certified immediately on completion of the experiment. Fair records are due at
the beginning of the next lab period. Fair records must be submitted as neat, legible, and complete.
In the fair record, the index page should be filled properly by writing the corresponding experiment
number, experiment name , date on which it was done and the page number.
1. Title: The title of the experiment should be written on the page in capital letters.
2. In the left top margin, experiment number and date should be written.
6. Procedure: steps for doing the experiment and recording the readings should be briefly described
(flowchart/programs in the case of computer/processor related experiments)
7. Results: The results of the experiment must be summarized in writing and should be fulfilling the aim.
2. Design: The design of the circuit/experimental set up for selecting the components should be clearly
shown if necessary.
iii) Relevant calculations should be shown. If repetitive calculations are needed, only show a sample
calculation and summarize the others in a table.
4. Graphs: Graphs can be used to present data in a form that shows the results obtained, as one or more of
the parameters are varied. A graph has the advantage of presenting large amounts of data in a concise
visual form. Graph should be in a square format.
1. Always wear tight shirts/lab coats, pants and shoes inside workshops.
2. REMOVE ALL METAL JEWELRY since rings, wrist watches or bands, necklaces, etc. make
excellent electrodes in the event of accidental contact with electric power sources.
5. Avoid standing on metal surfaces or wet concrete. Keep your shoes dry.
7. Hot soldering irons should be rested in its holder. Never leave a hot iron unattended.
8. Avoid use of loose clothing and hair near machines and avoid running around inside the lab.
DO: 1. SET MULTIRANGE METERS to the highest range before connecting to an unknown source.
2. INFORM YOUR INSTRUCTOR about faulty equipment so that it can be sent for repair.
DO NOT: 1. Do not MOVE EQUIPMENT around the room except under the supervision of an instructor.
INDEX
SI. PAGE
NO EXPERIMENT NO.
1 ODD OR EVEN
3 SIMPLE CALCULATOR
5 FIBONACCI SERIES
6 AMSTRONG NUMBER
11 MATRIX ADDITION
12 LINEAR SEARCH
13 BIANRY SEARCH
14 SELECTION SORT
15 QUICK SORT
16 STRING PALINDROME
24 FILES
SYLLABUS
Program : Diploma in Computer Engineering / Computer Hardware Engineering
Course Objectives:
Acquire in depth experience in programming using C Language.
Analyze the problems given in general terms, outline solutions and transform into well-organized
programs using arrays, functions, structures and files.
Lay foundation for further courses like Data Structure, Object Oriented Programming, Embedded
systems, etc. and software development.
Course Prerequisites:
Topic Course Course name Semester
code
Course Outcomes:
Lab Exam 3
CO – PO Mapping:
Course PO 1 PO 2 PO 3 PO 4 PO 5 PO 6 PO 7
Outcomes
CO1 3 3
CO2 3 3
CO3 3 3
CO4 3 3 3 3 3
Course Outline
Name of the Experiment Duration Cognitive
(Hours) Level
CO2 Use one and two dimensional arrays in C to solve real world problems.
Lab Exam – I 1½
CO4 Illustrate the use of files and command line arguments as I/O to programs.
Lab Exam – II 1½
Text / Reference
T/R Book Title/Author
R2 YashavantKanetkar, Let Us C
Online Resources
1 https://nptel.ac.in/courses/106104128/
2 https://www.programiz.com/c-programming
3 https://www.tutorialspoint.com/cprogramming/index.htm
EXPERIMENT 1:
ODD OR EVEN
AIM:
Write a C program to find whether the number is odd
or even.
ALGORITHM:
Step 1: Start
Step 2: Initialize a variable num
Step 3: Read the variable num
Step 4: Check if num%2==0 then
Step 5: Print Even number
Step 6: Else, then
Step 7: Print Odd number
Step 8: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int num;
printf(“Enter any number”);
scanf(“%d”,&num);
if (num%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
}
13 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 2:
LARGEST AMONG THREE NUMBERS
AIM:
Write a C program to find largest among three numbers.
ALGORITHM:
Step1: Start
Step2: Read the three integer values in A, B, and C (integer
variables).
Step 3: Check if A is greater than B.
Step 4: If true, then check if A is greater than C.
Step 5: If true, then print ‘A’ as the greatest number.
Step 6: If false, then print ‘C’ as the greatest number.
Step 7: If false, then check if B is greater than C.
Step 8: If true, then print ‘B’ as the greatest number.
Step 9: If false, then print ‘C’ as the greatest number.
Step10: Stop
PROGRAM:
#include <stdio.h>
int main()
{
int A, B, C;
printf(" Enter the number1 = ");
scanf("%d", &A);
printf("\n Enter the number2 = ");
scanf("%d", &B);
if (A > B)
{
if (A > C)
{
printf("\n Largest number = %d\n",A);
}
else
{
printf("\n Largest number = %d \n",C);
}
}
else if (B > C)
{
printf("\n Largest number = %d \n",B);
}
else
{
printf("\n Largest number = %d \n",C);
}
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 3:
SIMPLE CALCULATOR
AIM:
Write a C program to generate a simple calculator
using switch statement.
ALGORITHM:
Step 1: Start
Step 2: Print enter your choice.
Step 3: Enter your choice.
Step 4: Enter two operands for operation.
Step 5: User will enter +,-,*,/ .
Step 6: Perform Switch (operator)
Step 7: Do the operation.
Step 8: Print the result.
Step 9: Stop
PROGRAM:
#include <stdio.h>
int main()
{
/* Variable declarations */
int i,a,b,choice,result;
void menu(int a,int b,int choice);
printf("ENTER TWO NUMBERS:n");
printf("a=:");
scanf("%d",&a);
printf("b=:");
scanf("%d",&b);
i=0;
while (choice != 6)
{
menu (a,b,choice);
i++;
}
return 0;
}
void menu(int a, int b, int choice)
{
int result,i;
for(i=0;i < 35;i++)
printf("*");
printf("\n\n");
printf("\tC CALCULATOR\n\n");
for(i=0;i < 35;i++)
printf("_");
printf("\n\n");
printf("1.ADDITION\n");
printf("2.SUBTRACTION\n");
printf("3.MULTIPLICATION\n");
printf("4.DIVISION\n");
printf("5.CLOSE\n");
for(i=0;i< 35;i++)
printf("_"); printf("\n\n");
printf("Enter your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
result = a + b;
18 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
for(i=0;i< 35;i++)
printf("_"); printf("\n\n");
printf("Result=%d\n",result);
/* Addition Operation */
printf("\n\n");
break;
case 2:
result = a - b;
for(i=0;i< 35;i++)
printf("_"); printf("\n\n");
printf("Result=%d\n",result);
/* Subtraction */
printf("\n\n");
break;
case 3:
result = a * b;
for(i=0;i< 35;i++)
printf("_");
printf("\n\n");
printf("Result=%d\n",result);
/* Multiplication */
break;
case 4:
result = a/b;
for(i=0;i < 35;i++)
printf("_"); printf("\n\n");
printf("Result=%d\n",result);
/* Division operation */
printf("\n\n");
break;
case 5:
exit(0);
}
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 4:
SUM OF DIGITS OF A NUMBER
AIM:
Write a C program to find the sum of individual digits
of a positive number.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0)
Step 5: r←n%10
Step 6: sum←sum+r
Step 7: n←n/10
Step 8: Print “sum”
Step 9: Stop
PROGRAM:
/*This is a program to find sum of digits*/
#include<stdio.h>
void main()
{
int n,r,sum=0;
printf("ENTER A POSITIVE INTEGER \n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUM OF INDIVIDUAL DIGITS OF A POSITIVE
INTEGER IS..%d\n",sum);
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 5:
FIBONACCI SERIES
AIM:
Write a C program to generate the first n terms of the Fibonacci
sequence.
ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4 :i=0
Step 5 : while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
Step 6: If not go to step 7
Step 7: stop
PROGRAM:
#include<stdio.h>
void main()
{
int f0,f1,f,n,i;
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d
TERMS:\n",n);
i=0;
while (i<n)
{
printf("%d\t",f0); f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 6:
ARMSTRONG NUMBER
AIM:
Write a C program to Check whether given number
is Armstrong Number or Not.
ALGORITHM:
Step 1: start
Step 2:read n
Step 3:assign sum=0, i, m, n, count=0
Step 4:if m>0, repeat the following
Step 4.1:m=m/10
Step 4.2:count++
Step 4.3:until the condition fail
Step5: if i>0, repeat step 4 until condition fails
Step 5.1:rem=I%10
Step 5.2:sum= sum+pow(rem,count)
Step 5.3:I=I/10
Step 6: if n=sum, then print Armstrong
Step 7: Otherwise, print not Armstrong
Step 8:stop
PROGRAM:
#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if (num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 7:
ADDITION OF TWO NUMBERS USING FUNCTION (WITH
ARGS AND RETURN TYPE
AIM:
Write a C program to add two numbers with arguments and
return type.
ALGORITHM:
Step 1: start
Step 2: initialize function ‘sum’ with two integer arguments.
Step 3: Read num1 and num2.
Step 4: call function sum
Step 5: calculate sum of two numbers in function.
Step 6: Return the result to main function.
Step 7: Print the result.
Step 8: Stop.
PROGRAM:
#include <stdio.h>
int sum(int , int);
int main()
{
int num1,num2,total;
printf("Enter the two number ");
scanf("%d %d",&num1,&num2);
total=sum(num1,num2);
printf("The sum of these numbers :%d",total);
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 8:
AREA OF SQUARE USING FUNCTION
AIM:
Write a C program to find area of square using function.
ALGORITHM:
Step 1: start
Step 2: initialize function ‘area’ with one integer argument.
Step 3: Read side.
Step 4: call function area
Step 5: calculate area with side in function.
Step 6: Return the result to main function.
Step 7: Print the result.
Step 8: Stop.
PROGRAM:
#include<stdio.h>
int area(int);
int main()
{
int side, a;
printf("\nEnter the Length of Side : ");
scanf("%d", &side);
a=area(side);
printf("\nArea of Square : %d", a);
}
int area(int side)
{
int area;
area = side * side;
return (area);
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 9:
FACTORIAL OF NUMBER USING RECURSION
AIM:
Write a C program to find factorial of number using function
recursion.
ALGORITHM:
Step 1: start
Step 2 : read number n.
Step 3 : call factorial (n)
Step 4 : in function factorial if n<=1 then return 1
Step 5 : else return i*factorial(i-1)
Step 6 : print factorial
Step 7 : stop.
PROGRAM:
#include<stdio.h>
int factorial(int i)
{
if(i<=1)
return 1;
return i*factorial(i-1);
}
int main()
{
int n;
printf(" Enter a number :");
scanf("%d",&n);
printf("Factorial of %d is %d \n",n,factorial(n));
return 0; }
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 10:
SUM OF ELEMENTS OF AN ARRAY
AIM:
Write a C program to find sum of elements in an array.
ALGORITHM:
Step 1: start
Step 2 : Initialize array
Step 3 : set sum = 0.
Step 4 : Read n.
Step 5 : Set i=0 repeat step 6,7 until i<n.
Step 6 : Read a[i]
Step 7 : i++
Step 8 : Set i=0 repeat step9,10 until i<n.
Step 9 : sum += a[i]
Step 10 : i++
Step 11 : Print sum.
Step 12 : Stop
PROGRAM:
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("\n\nFind sum of all elements of array:\n");
printf("--------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
33 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum += a[i];
}
printf("Sum of all elements stored in the array is : %d\n\n", sum);
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 11:
34 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
MATRIX ADDITION
AIM:
Write a C program to generate matrix addition using two
dimensional array.
ALGORITHM:
Step 1: start
Step 2: Initialize array a,b,c.
Step 3 : Read r,c.
Step 4 : set i=0 repeat step 5 to 8 until i<r.
Step 5 : set j=0 repeat step6,7 until j<c.
Step 6 : Read a[i][j].
Step 7 : j++
Step 8 : i++
Step 9 : set i=0 repeat step 10 to 13 until i<r.
Step 10 : set j=0 repeat step 11,12 until j<c.
Step 11: Read b[i][j].
Step 12 : j++
Step 13: i++
Step 14 : set i=0 repeat step 15 to 18 until i<r.
Step 15 : set j=0 repeat step16,17 until j<c.
Step 16: sum[i][j] = a[i][j] + b[i][j]
Step 17 : j++
Step 18: i++
Step 19 : set i=0 repeat step 20 to 23 until i<r.
Step 20 : set j=0 repeat step21,22 until j<c.
Step 21: Print sum[i][j]
Step 22 : j++
Step 23 : i++
Step 24 : Stop
PROGRAM:
35 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element a[%d] [%d]: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element b[%d] [%d]: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
// adding two matrices
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{ sum[i][j] = a[i][j] + b[i][j]; }}
// printing the result
printf("\nSum of two matrices: \n");
36 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 12:
LINEAR SEARCH
37 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
AIM:
Write a C program to Implement Linear Search.
ALGORITHM:
Step 1: start
Step 2 : Initialize array a.
Step 3 : Read n
Step 4 : Set i=0 repeat step 5,6 until i<n.
Step 5 : Read a[i]
Step 6 : i++
Step 7 : Read s
Step 8 : Set i=0 repeat step 9 to 11 until i<n.
Step 9 : if a[i] equal to s then
Step 10 : Print s,i+1
Step 11 : i++
Step 12 : if a[i] equal to n then
Step 13 : Print Not present.
Step 14 : Stop
PROGRAM:
#include <stdio.h>
int main()
{
int a[100], s, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search\n");
scanf("%d", &s);
38 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
EXPERIMENT 13:
BINARY SEARCH
AIM:
Write a C program to Implement Binary Search.
ALGORITHM:
Step 1: start
Step 2 : Initialize array a
Step 3 : Read n
Step 4 : Set i=0 repeat step 5,6 until i<n.
Step 5 : Read a[i]
Step 6 : i++
Step 7 : Read s
Step 8 : set first = 0, last = n – 1, middle = (first+last)/2
Step 9 : Repeat step 10 to 15 until first <= last
Step 10 : Check if (a[middle] < s)
Step 11 : Then, Set first = middle + 1
Step 12 : Else if (a[middle] == s) then
Step 13 : Print s and middle+1
Step 14 : Else then last = middle – 1
Step 15 : middle = (first + last)/2
Step 16 : if (first > last) then
Step 17 : Print Not found
Step 19 : Stop
PROGRAM:
#include <stdio.h>
int main()
{
int i, first, last, middle, n, s, a[100];
printf("Enter number of elements in ascending order\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d", &s);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (a[middle] < s)
first = middle + 1;
else if (a[middle] == s)
{
printf("%d found at location %d.\n", s, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", s);
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 14:
SELECTION SORT
AIM:
Write a C program to implement selection sort.
ALGORITHM:
Step 1 : Set min to the first location
Step 2 : Search the minimum element in the array
Step 3 : swap the first location with the minimum value in the array
Step 4 : assign the second element as min.
Step 5 : Repeat the process until we get a sorted array.
PROGRAM:
#include <stdio.h>
int main()
{
int a[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &a[c]);
for (c = 0; c < (n - 1); c++)
// finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (a[position] > a[d])
position = d;
}
if (position != c)
{
42 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
t = a[c];
a[c] = a[position];
a[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 15:
QUICK SORT
AIM:
Write a C program to implement quick sort.
PROGRAM:
#include<stdio.h>
void quicksort(int number[25],int first,int last) {
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot]) j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
{ printf(" %d",number[i]);}
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 16:
STRING PALINDROME
AIM:
Write a C program to find palindrome using string.
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,n,c=0;
printf("Enter the string : ");
scanf("%s",s);
n=strlen(s);
printf("Length of the string is %d\n",n);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 17:
PASSING ARRAY AS FUNCTION ARGUMENTS
AIM:
Write a C program to pass array as function arguments.
PROGRAM:
#include<stdio.h>
void func(int arr[]);
int main()
{
int arr[10], i;
printf("Enter 10 array elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nPassing array to the function...\n");
func(arr);
getch();
return 0;
}
void func(int arr[])
{
int i;
printf("\nThe array is:\n");
for(i=0; i<10; i++)
printf("%d ", arr[i]);
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 18:
SMALLEST ELMENT IN AN ARRAY USING POINTER
AIM:
Write a C program to find smallest element in array using pointer.
PROGRAM:
#include<stdio.h>
int main()
{
int a[5],*s,i,small;
s=&a[0];
printf("Enter 5-Elements :\n\n ");
for(i=0;i<5;i++,s++)
scanf("%d",s);
s=&a[0];
small=*s;
for(i=0;i<5;i++,s++)
if(*s<small)
small=*s;
printf("\nSmallest Element : %d",small);
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 19:
POINTER AS FUNCTION ARGUMENTS
AIM:
Write a C program to use pointer as function arguments.
PROGRAM:
#include<stdio.h>
void swap(int*n1,int*n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
}
int main()
{
int a,b;
printf("Enter Two Numbers: \n");
scanf("%d%d",&a,&b);
printf("\nNumbers Before Swapping: \n");
printf("\n a=%d b=%d\n",a,b);
swap(&a,&b);
printf("\nNumbers After Swapping: \n");
printf("\n a=%d b=%d",a,b);
return 0; }
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 20:
DYNAMIC MEMMORY ALLOCATION USING ONE
DIMENTIONAL ARRAY
AIM:
Write a C program to implements dynamic memory allocation using
one dimensional array.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Memory not allocated First Second Third\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
}
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 21:
DYNAMIC MEMMORY ALLOCATION USING TWO
DIMENTIONAL ARRAY
AIM:
Write a C program to implements dynamic memory allocation using
two dimensional array.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// `M × N` matrix
#define M 4
#define N 5
// dynamically allocate memory for 2D Array
int main()
{
// dynamically allocate memory of size `M × N`
int* A = (int*)malloc(M * N * sizeof(int));
if (A == NULL)
{
printf(stderr, "Out of memory");
exit(0);
}
// assign values to the allocated memory
for (int r = 0; r < M; r++)
{
for (int c = 0; c < N; c++)
{
*(A + r*N + c) = rand() % 100;
}
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 22:
STUDENTS LIST UNSING STRUCTURE
AIM:
Write a C program to implements students list using structure.
PROGRAM:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[50];
int class;
char section;
};
int main()
{
// created variable student1 for structure Student
struct Student student1;
// accessing student1 member and initializing them
strcpy(student1.name,"Student_name");
student1.class = 1;
student1.section = 'A';
// printing values
printf( "Student Name : %s\n",student1.name);
printf( "Student Class : %d\n",student1.class);
printf( "Student Section : %c\n",student1.section);
return 0; }
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 23:
STUDENTS LIST UNSING STRUCTURE AND ARRAY
AIM:
Write a C program to implements students list using structure and
array.
PROGRAM:
#include<stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 24:
FILES
AIM:
Write a C program to implements different file operations.
PROGRAM:
#include<stdio.h>
void main()
{
FILE *fp;
/* file pointer*/
char fName[20];
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
//exit(0);
/*exit from program*/
}
printf("File created successfully.");
/*writting into file*/
putc('B',fp);
58 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
// exit(0);
}
printf("Contents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
// return 0;
}
RESULT:
Program executed and output obtained successfully.
EXPERIMENT 25:
COMMAND LINE ARGUMENTS
AIM:
Write a C program to implements command line
arguments.
PROGRAM:
#include<stdio.h>
int main(int argc,char* argv[])
{
int counter;
printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument
Passed Other Than Program Name");
if(argc>=2)
{
printf("\nNumber Of Arguments
Passed: %d",argc);
printf("\n----Following Are The
Command Line Arguments Passed----");
for(counter=0;counter<argc;counter++)
printf("\nargv[%d]:
%s",counter,argv[counter]);
}
return 0;
}
RESULT:
Program executed and output obtained successfully.
AIM:
Write a menu driven program in C to create a structure
employee
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct details
{
char name[30]
int eid;
int salary;
}
emp[5];
void empsearch(int r)
{
int id,i;
printf("\nEnter Employee Id to be Searched: ");
scanf("%d", &id),
printf("------------------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].eid==id)
{
printf("Employee Id %d",emp[i].eid);
printf("\nName :%s",emp[i].name);
62 Ma’din Polytechnic College, Malappuram
REVISION - 2021 [C PROGRAMMING LAB - 3135 ]
printf("\nSalary :%d\n",emp[i].salary);
}
}
void display(int r)
{
int i;
printf("\nList of All Employees \n");
printf("------------------------------\n");
printf("Emp IditEmp-Name Salary\n");
printf("------------------------------\n");
for(i=0,i<r;i++)
{
printf("%d\ts\t%d\n".emplil eid.emplil.name.emplil
salary),
}
}
int main()
{
int n,i,ch;
printf(/n"How Many Employee Record You Want to Add*/\n\nEnter
Limit :");
scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("------------------------------\n");
printf("\n\tEnter Details of Employee-%d", i+1);
printf("------------------------------\n");
printf("\nName of Employee: ");
scanf("%s",emp[i].name),
printf("Employee-Id :");
scanf("%d",&emp[i].eid);
printf("Salary: ");
scanf("%d",&emplül salary);
while(1)
printf("\n"),
printf("\t\tMenu\n");
printf("printf("n 1 Search Employee by E-ID");
printf("\n 2 List of All Employee");
printf("\n 3 Display Employee Name whose Salary > 10000");
printf("n 4 Exit");
printf("n printf("Enter Your Choice: ");
scanf("n %d",&ch);
switch(ch)
case 1: empsearch(n);
break;
case 2: display(n);
break;
case 3: greater(n);
break;
case 4: exit(0);
return 0;
}
}
RESULT:
Program executed and output obtained successfully.