0% found this document useful (0 votes)
10 views

C Programming Lab Manual

The document outlines a C programming laboratory course aimed at familiarizing students with various programming constructs and developing applications using C. It includes a list of experiments covering topics such as I/O statements, decision-making constructs, loops, arrays, strings, pointers, functions, structures, and file processing. The course objectives and outcomes are clearly defined, along with recommended textbooks and references for further study.

Uploaded by

kingsakthi2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C Programming Lab Manual

The document outlines a C programming laboratory course aimed at familiarizing students with various programming constructs and developing applications using C. It includes a list of experiments covering topics such as I/O statements, decision-making constructs, loops, arrays, strings, pointers, functions, structures, and file processing. The course objectives and outcomes are clearly defined, along with recommended textbooks and references for further study.

Uploaded by

kingsakthi2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 64

CS3271 PROGRAMMING IN C LABORATORY LTP C

0 0 4 2
COURSE OBJECTIVES:

 To familiarise with C programming constructs.


 To develop programs in C using basic constructs.
 To develop programs in C using arrays.
 To develop applications in C using strings, pointers, functions.
 To develop applications in C using structures.
 To develop applications in C using file processing.

LIST OF EXPERIMENTS:

Note: The lab instructor is expected to design problems based on the topics
listed. The Examination shall not be restricted to the sample experiments
designed.

1. I/O statements, operators, expressions


2. decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing ar-
rays to function.
7. Recursion
8. Pointers: Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10.Files: reading and writing, File pointers, file operations, random access, pro-
cessor directives.

TOTAL: 60 PERIODS
COURSE OUTCOMES:

Upon completion of the course, the students will be able to

CO1: Demonstrate knowledge on C programming constructs.

CO2: Develop programs in C using basic constructs.

CO3: Develop programs in C using arrays.

CO4: Develop applications in C using strings, pointers, functions.


CO5: Develop applications in C using structures.

CO6: Develop applications in C using file processing.

TEXT BOOKS:
1. ReemaThareja, “Programming in C”, Oxford University Press, Second Edition, 2016.
2. Kernighan, B.W and Ritchie,D.M, “The C Programming language”,
Second Edition, Pearson Education, 2015.

REFERENCES:
1. Paul Deitel and Harvey Deitel, “C How to Program with an Introduction to
C++”, Eighth edition, Pearson Education, 2018.
2. Yashwant Kanetkar, Let us C, 17th Edition, BPB Publications, 2020.
3. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of Program-
ming with C", McGraw-Hill Education, 1996.
4. Pradip Dey, Manas Ghosh, “Computer Fundamentals and Programming in C”, Second
5. Edition, Oxford University Press, 2013.
6. Anita Goel and Ajay Mittal, “Computer Fundamentals and Programming in
C”, 1st Edition, Pearson Education, 2013.
List of Experiments

Page Staff
Sl.No Ex.No. Date Title of the Experiments
No. sign
1. 1a Programs using I/O statements
2. 1b Programs using Operators and Expressions
3. 2a Find Odd or Even using goto statement
4. 2b Finding Leap Year or Not
5. 2c Creating a Menu Driven Calculator
6. 2d Checking vowel or not using switch case
Print 1 to n numbers except multiples of 5
7. 2e
using continue statement
8. 3a Finding Armstrong Number
Sum of first N natural numbers using
9. 3b
Looping
10. 3c Sum the cubes of series of N numbers
Finding Sum Of Weights & Sorting The
11. 4a
Elements Based On Weights
Finding The Persons Having Above
12. 4b
Average Height
13. 4c Finding Body Mass Index Using Array
14. 4d Matrix Addition and Multiplication
Reverse String Without Changing The
15. 5
Position Of Special Character
16. 6a Swap the values using call by value
Sorting The Values Using Pass By
17. 6b
Reference
Sum of array elements by passing to a
18. 6c
function
19. 7a Towers of Hanoi
20. 7b Finding factorial of a number
21. 8a Pointer to pointer
22. 8b Array of Pointer
23. 9a Employee Salary Slip
24. 9b Students Internal Mark Sheet
25. 9c Accessing Union Members
26. 10a Sequential Access File
27. 10b Random Access File
28. 10c Processor Directives
EX.NO.:1a PROGRAMS USING I/O STATEMENTS

AIM:
To Write a C program to use Input and Output Statements

PROCEDURE:

1. Start the program


2. Use Unformulated input & output statements to get char or string
 getchar(), getche() , getch() & gets()
3. Print the char or string using unformulated output statements
 putch(), putchar() & puts()
4. Use formatted Input statements to read different types of data
 scanf()
5. Print different types of data using formatted output statements
 printf()
6. Stop the program

SYNTAX:
Unformatted I/O
◦ charvariable=getchar();
◦ charvariable=getche();
◦ charvariable=getch();
◦ gets(chararray_variable);
◦ putch(charvariable);
◦ puts(chararray_variable);
Formatted I/O
◦ scanf(“format specifiers”,address of variables);
◦ printf(“Any text”);
◦ printf(“Any text,format spcifiers”,variables);
◦ printf(“format spcifiers”,variables);

SAMPLE PROGRAMS:
P1: Get & Display a single character using getch()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getch();
putch(c1);
}
P2: Get & Display a single character using getche()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
}

P3: Get & Display a single character using getchar()


#include<stdio.h>
#include<conio.h>
main()
{
char c1;
}
P4: Get & Display a More characters using gets()
#include<stdio.h>
#include<conio.h>
main()
{
char name[20];
}

P5: Get & Display a student’s regno,Name and GPA using scanf & printf
#include<stdio.h>
#include<conio.h>
main()
{
int regno;
char name[25];
float GPA;
printf(“\nEnter a Student Regno,Name & GPA\n”);
scanf();
prinntf(“\n------------------------------------------------------\n”);
prinntf(“\n\t\t REG.NO \t\t NAME \t\t GPA \t\t\n”);
prinntf(“\n------------------------------------------------------\n”);
printf();
}
OUTPUT:

RESULT:
Thus the C program for I/O statements has been written & executed successfully.
EX.NO. :1b PROGRAMS USING OPERATORS AND EXPRESSIONS

AIM:
To Write C programs using Operators and Expressions

PROCEDURE:

1. Start the program


2. Use Arithmetic operators (+,-,*,/,%)
3. Use Increment & Decrement Operators (++,--)
4. Use comparison operators (>,<,>=,<=,==,!=)
5. Use Bit wise operators (~,^,|,&)
6. Use logical operators (||,&&,!)
7. Use Ternary Operator (?:)
8. Stop the program

SYNTAX:
Expression = Operand operator Operand
Operator : Symbol
Operand : variable or Constant
var1=Var2+Var3 (or) Var1=Value1+Value2; (or) Var1=Var2+value
Logical Operators : returns True(1) or false(0) values-Condition1&& Condition2
Ternary Operator: if condition is true True part will be executed. Otherwise, False part
will be executed - Var1= ( condition)? True part: False Part

SAMPLE PROGRAMS:
P1: Arithmetic Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Addition of %d + %d is = %d”,a+b);
printf(“Subtraction of);
printf(“Multiplication of);
printf(“Division of);
printf(“Modulo Division of);
getch();
}
P2: Bit wise Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Bit wise OR of %d + %d is = %d”,a|b);
printf(“Bit wise AND of %d + %d is = %d”, );
printf(“One’s complement of %d is = %d”, );
getch();
}
P3: Relational Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“a>b : %d”,a>b);
getch();
}
P4: Logical, Ternary Operators, Increment & decrement Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr(); //to clear the output screen
a=20;
b=5;
c=(a>b)?(a+b):(a-b);
printf(“The value of c is = ”, );
d=(a>b)&&(a>c);
printf(“The value of d is = ”, );
d=(b<c)&&(a<c);
printf(“The value of d is = ”, );
printf(“a++ & ++b is = ”,++a,b++);
getch();
}
OUTPUT:

RESULT:
Thus the C program for expressions has been written & executed successfully.

EX.NO.:2a FINDING ODD OR EVEN


AIM:
To Write a C program to find whether a given number is odd or even using goto
statement.

PROCEDURE:

1. Start the program


2. Read an input Value
3. Divide it by 2.
4. Take the remainder, if the remainder is equal to zero , then
5. Print the given Number is EVEN.
6. Otherwise, print the given Number is ODD.
7. Stop the Programs

SYNTAX:
if(condition)
statement;
else
statement;

SAMPLE PROGRAM:

#include <stdio.h>
#include <stdlib.h>

void main()
{
int num;

printf("Enter a number\n");
scanf("%d", &num);

if (num % 2 == 0)
goto even;
else
goto odd;

even:
printf("%d is even\n", num);
exit(0);
odd:
printf("%d is odd\n", num);
}

OUTPUT 1:

Enter a number
74
74 is even

OUTPUT 2:

Enter a number
35
35 is odd

RESULT:
Thus the C program for finding even or odd is written & executed Successfully.

EX.NO.:2b FINDING LEAP YEAR OR NOT


AIM:
To Write a C program to find whether the given the given year is leap year or Not.

PROCEDURE:

1. Start the program


2. Read an input Value (Year)
3. If the Year is divisible by both 4 or 4 and 400 then,
4. Print the given year is LEAP YEAR
5. Otherwise, Print the given year is NOT A LEAP YEAR

PROGRAM:

#include <stdio.h>
int main(){
int y;
printf("Enter the year to check: ");
scanf("%d",&y);
if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))
printf("It is a leap year");
else
printf("It is not a leap year");
return 0;
}

Output:

RESULT:
Thus the C program for finding leap year or not is written & executed Successfully.

EX.NO.:2c CREATING A MENU DRIVEN CALCULATOR


AIM:
To Write a C program for Creating a Menu Driven Calculator using switch case.

PROCEDURE:

1. Start the program


2. Read an option to select the operation: 1.Add 2.Sub 3.Mul 4.Div
3. Enter the input values according to the operation.
4. Use switch case to perform the operation.
5. Print the Result.
6. Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int option,a,b,c;
clrscr();
printf(“Enter the option”);
printf(“\n 1.ADD \t 2.SUB \t 3.MUL \t 4.DIV \n”);
scanf(“%d”,&option);
switch()
{
case 1:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a+b;
break;
case 2:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a-b;
break;

case 3:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a*b;
break;
case 4:
printf(“Enter two values”);
scanf(“%d%d”,&a,&b);
c=a/b;
break;

default:
printf(“Choose the correct option”);
}
printf(“ Result is =%d”,c);
getch();
}

Output:

RESULT:
Thus the c program for creating a Menu Driven Calculator using switch case is
written & executed successfully.

EX.NO.:2d CHECKING VOWEL OR NOT USING SWITCH CASE


AIM:
To Write a C program for Checking vowel or not using switch case.

PROCEDURE:

PROGRAM:
Output:

RESULT:
Thus the c program for Checking vowel or not using switch case switch case is
written & executed successfully.
EX.NO.:2e PRINT 1 TO N NUMBERS EXCEPT MULTIPLES OF 5

AIM:
To Write a C program to Print 1 to n numbers except multiples of 5 using continue
statement.

ALGORITHM:

PROGRAM:
OUTPUT:

RESULT:

Thus the c program to print 1 to n numbers except multiples of 5 using continue


statement.
EX.NO.: 3a FINDING ARMSTRONG NUMBER

AIM:
To Write a C program to find whether a given number is Armstrong number or not.

ALGORITHM:

1. Start the program.


2. Read an input value.
3. Split the digits using modulo division.
4. Cube the individual digits
5. Sum the cubes.
6. If the given number == sum of cubes,then
Print “ ARMSTRONG NUMBER”
7. Otherwise,
Print “ NOT AN ARMSTRONG NUMBER”
8. Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int N,A,digit,cube,sum=0;
printf(“Enter a Number\n”);
scanf(“%d”,&N);
A=N;
while( )
{
digit=N%10;
cube=(digit*digit*digit);
sum=sum+cube;
N=N/10;
}
if ( sum==A)
printf(“%d is Armstrong Number”, );
else
printf(“%d is not an Armstrong Number”, );
getch();
}
OUTPUT:

Enter a Number 153

153 is Armstrong Number

RESULT:
Thus the C program to find whether a given number is Armstrong number or not
is written & executed successfully.
Ex:3b SUM OF FIRST N NATURAL NUMBERS USING LOOPING

AIM:
To Write a C program for sum of first n natural numbers using while, do…while and for
loop.

ALGORITHM: 1

1. Start the program.


2. Read the input from the user and stores it in the variable n.
3. Assign sum=0
4. Use while loop to calculate the sum up to n.
5. Stop the program.

ALGORITHM: 2

1. Start the program.


2. Read the input from the user and stores it in the variable n.
3. Assign sum=0
4. Use do…while loop to calculate the sum up to n.
5. Stop the program.

ALGORITHM: 3

1. Start the program.


2. Read the input from the user and stores it in the variable n.
3. Assign sum=0
4. Use for loop to calculate the sum up to n.
5. Stop the program.

PROGRAM:1

#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;

while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}

PROGRAM:2

#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;

do
{
sum += i;
++i;
} while (i <= n);

printf("Sum = %d", sum);


return 0;
}

PROGRAM:3

#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
}
OUTPUT:

Enter a positive integer:10

Sum =55

RESULT:
Thus the C program to for sum of first n natural numbers using while, do…while and for
loop has been executed successfully and the output was verified.
EX.NO.: 3c SUM THE CUBES OF SERIES OF N NUMBERS

AIM:
To Write a C program to Sum the cubes of series of N numbers.

ALGORITHM:

1. Start the Program


2. Read the input from the user and stores it in the variable n.
3. Assign sum=0
4. Iterate the loop for n times, cubing it and taking sum of them.
5. Print the sum
6. Stop the Program

PROGRAM:

#include <stdio.h>
int main()
{
int n;
int sum = 0;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for (int i = 1; i <= n; i++)
{
sum += i * i * i;
}
printf(“sum= %d”, sum);
return 0;
}
OUTPUT:

Enter the number 3


Sum=36

RESULT:
Thus the C program for Sum the cubes of series of N numbers has been executed
successfully and the output was verified.
Ex:4a FINDING SUM OF WEIGHTS & SORTING THE ELEMENTS BASED ON
WEIGHTS
AIM:
Write a C program to find sum of weights for a given set of numbers like <10, 36, 54, 89, 12,
27>, Find the sum of weights based on the following conditions.
1. 5 if it is a perfect cube.
2. 4 if it is a multiple of 4 and divisible by 6.
3. 3 if it is a prime number.
ALGORITHM:

1. Declare & initialize an integer array : a[]


2. Declare an array called w[] to store the weight values of the respective elements.
3. Assign the weight values to each element of that array by checking the conditions
a. If a[i] is a perfect cube , assign weight 5 to w[i]
b. If a[i] is multiples of 4 & divisible by 6 then assign 4 to w[i]
c. If a[i] is a prime number then assign 3 to w[i]
4. Add all weight values
5. Print the sum of weights
6. Sort the array elements in increasing order based on their weight values.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double cubroot,cube;
int a[10]={ },w[10],i,j,N=6,count=0,A,sum=0;
clrscr();
for(i=0;i<N;i++)
{
A=a[i];
w[i]=0;
cubroot=ceil(pow(A,(1/3)));
cube=cubroot*cubroot*cubroot;
if(A==cube)
w[i]=w[i]+5;
if(A%4==0 && A%6==0)
w[i]=w[i]+4;

for(j=2;j<=A;j++)
{
if(A%j == 0)
count=count+j;
}
if(A==1 || A==count)
w[i]=w[i]+3;

}
printf("\n\nELEMENT\t\t\t\tWEIGHT \n\n");
for(i=0;i<N;i++)
{
printf("%d \t\t\t\t%d\n\n",a[i],w[i]);
sum=sum+w[i];
}
printf("The sum of Weights is = sum);
printf("Sorting of the Elements based on weight values\n");
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(w[j]>w[j+1])
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;

k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}

for(i=0;i<N;i++)
{
printf("<%d,%d> \t",a[i],w[i]);
}

getch();
}
OUTPUT:

RESULT:
Thus the program is written & executed Successfully.
Ex.No:4b FINDING THE PERSONS HAVING ABOVE AVERAGE HEIGHT

AIM:
Write a C program to populate an array with height of persons and find how many persons are
above the average height.

ALGORITHM:

1. Declare an array to store the height values of persons


2. Initialize the count value to 0.
3. Read the number of persons
4. Read the individual person’s height
5. Find the sum of heights
6. Find the average of heights
7. Check whether a person’s height is greater than the average height or not.
1. If Yes, then increase the count value
8. Print the count value.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],avg;
int N,i,sum=0,count=0;
clrscr();
printf("Enter the no. of persons");
scanf("%d",&N);
printf("Enter the persons height on eby one\n");
for(i=0;i<N;i++)
{
scanf("%f",&height[i]);
sum=sum+height[i];
}
avg=sum/N;
for(i=0;i<N;i++)
{
if(height[i]>avg)
{ count=count+1; }
}
printf("Totally %d Persons are having above average height",count);
getch();
}
OUTPUT:

RESULT:
Thus the program is written & executed Successfully.

EX.NO.: 4c FINDING BODY MASS INDEX USING ARRAY


AIM:
To Write a C program to perform the following,
▪ Populate a two dimensional array with height and weight of persons
▪ Compute the Body Mass Index of the individuals.

ALGORITHM:
1. Start the program.
2. Read the Number of Persons.
3. Enter the height,weight values.
4. Read height in centi meters & weight in Kilograms of a person.
5. Height in Meter= height in cm/100;
6. Compute the body mass index value using the following formula
BMI=(weight in Kg)/(Height in Meters)2
7. Store the BMI values in a resultant array.
8. Print the results.
Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],weight[20],BMI[20],HIM[20];
int i,j,N;
clrscr();
printf("\nEnter the No.of the elements\n");
scanf("%d",&N);
printf("\n Enter the Height & Weight values\n");
for(i=0;i<N;i++)
{
scanf("%f%f",&height[i],&weight[i]);
HIM[i]=height[i]/100;
}

printf("\nPerson\tHeight\tWeight\tBMS\n");
for(i=0;i<N;i++)
{
BMI[i]=weight[i]/(HIM[i]*HIM[i]);
printf("\n%d\t%.2f\t%.2f\t%.2f\n",(i+1),HIM[i],weight[i],BMI[i]);
}
getch();
}

OUTPUT:
Enter No. of the elements
3

Enter the Height & Weight values


152 58
122 28
135 40

Person Height Weight BMS

1 1.52 58.00 25.10

2 1.22 28.00 18.81

3 1.35 40.00 21.95

RESULT:
Thus the program is written & executed Successfully.

EX.NO.: 4d MATRIX ADDITION AND MULTIPLICATION


AIM:
To Write a C program to calculate Matrix Addition and Multiplication.

ALGORITHM:1

1. Start the program.


2. Enter the number of rows r and columns c.
3. Enter the elements of the two matrices (of order rxc).
4. Add the corresponding elements of two matrices and saved it in another matrix (two-
dimensional array).
5. Print the resultant matrix.
6. Stop the Program.

ALGORITHM:2

1. Start the program.


2. Read the size (rows and columns) of two matrices.
3. To multiply two matrices, the number of columns of the first matrix should be equal to
the number of rows of the second matrix.
4. Multiply two matrices by using the following three functions:
a) getMatrixElements() - to take matrix elements input from the user.
b) multiplyMatrices() - to multiply two matrices.
c) display() - to display the resultant matrix after multiplication.
5. Stop the Program

PROGRAM:1

#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");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

PROGRAM:2
#include <stdio.h>

// function to get matrix elements entered by the user


void getMatrixElements(int matrix[][10], int row, int column) {

printf("\nEnter elements: \n");

for (int i = 0; i < row; ++i) {


for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}

// function to multiply two matrices


void multiplyMatrices(int first[][10],
int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}

// Multiplying first and second matrices and storing it in result


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

// function to display the matrix


void display(int result[][10], int row, int column) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}

int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);

// Taking input until


// 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}

// get elements of the first matrix


getMatrixElements(first, r1, c1);

// get elements of the second matrix


getMatrixElements(second, r2, c2);

// multiply two matrices.


multiplyMatrices(first, second, result, r1, c1, r2, c2);

// display the result


display(result, r1, c2);

return 0;
}

OUTPUT :1

Enter the number of rows (between 1 and 100): 2


Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
-2 8 7

10 8 6

OUTPUT :2

Enter rows and column for the first matrix: 2


3
Enter rows and column for the second matrix: 3
2

Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5

Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4

Output Matrix:
-21 22
159 179

RESULT:
Thus the C program to calculate Matrix Addition and Multiplication has been
executed successfully and the output was verified.

EX.NO.: 5 REVERSE STRING WITHOUT CHANGING THE POSITION OF


SPECIAL CHARACTER

AIM:
To Write a C program for reversing a string without changing the position of special
characters. (Example input:a@gh%;j and output:j@hg%;a)

ALGORITHM:

1. Start the program.


2. Read a String.
3. Call a function to reverse a string.
4. Reverse function
 Find strlen
 Set forward & reverse pointers ( f=0,r=strlen-1)
 Check the following
o If both str[f] and str[l] are alphanumeric then swap these two chars in that string
and do f++,r--
o If str[f] is a special char then f++
o Id str[r] is a special char then r--;
o If both are special chars then f++ &r--
5. Print the reversed string.
6. Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
char str[50];
clrscr();
printf("Enter a string\n");
scanf("%s",str);
printf( "Input string: %s\n",str);
reverse(str);
printf("Output string:%s\n",str);
getch();
}
void reverse(char *str)
{
int r = strlen(str) - 1, f= 0;
char t;
while (f < r)
{
if(isalnum(str[f])!=0 && isalnum(str[r])!=0)
{
t=str[r];
str[r]=str[f];
str[f]=t;
f++;
r--;
}
else if(isalnum(str[f])!=0 && isalnum(str[r])==0)
{ r--; }

else if(isalnum(str[f])==0 && isalnum(str[r])!=0)


{ f++; }
else
{
f++;
r--;
}

}
}

OUTPUT:
Enter a string:
a@gh%;j
Input string: a@gh%;j
Output string:j@hg%;a

RESULT:
Thus the C program to Reverse String Without Changing The Position of Special
Character has been executed successfully and the output was verified.

Ex.No.: 6a SWAP THE VALUES USING CALL BY VALUE


AIM:
To Write a C program for Swapping of two numbers using call by value.

ALGORITHM:

1. Start the program


2. Read values from the user
3. Swap the values of two variables using third variable
4. Print two values after swapping
5. Stop the program.

PROGRAM:

#include <stdio.h>
void swap(int, int);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}

OUTPUT:

Enter the value of x and y


Before Swapping
x = 10
y=5
Values of a and b is 5 10
After Swapping
x = 10
y=5

RESULT:
Thus the C program for Swapping of two numbers using call by value has been
executed successfully and the output was verified.

Ex.No.: 6b SORT THE LIST OF NUMBERS USING PASS BY REFERENCE

AIM:
Write a C program to sort numbers using pass by Reference.

ALGORITHM:

1. Start the Program


2. Read the size of array
3. Read the elements one by one
4. Call the function sort()
5. Pass the stating address of the array to the sort function as input.
6. Store the address in the formal argument of that function (Pointer variable)
 Access the array values by (increasing the addresses) the pointer variable
 Sort the values using the pointer variable
7. Print the Result
8. Stop the program

PROGRAM:

#include<stdio.h>
void sort(int *,int);
void main()
{
int a[20],N,i;
printf("Enter the no. of elements\n");
scanf("%d",&N);
printf("Enter the Elements one by one\n");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
sort(a,N);

printf("\n Sorted Order\n");


for(i=0;i<N;i++)
printf("%d\t",a[i]);
getch();
}

void sort(int *x,int n)


{
int t,i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(*(x+j) > *(x+(j+1)))
{
t=*(x+j);
*(x+j) =*(x+(j+1));
*(x+(j+1))=t;
}
}
}
}

OUTPUT:
Enter the no. of elements
3
Enter the Elements one by one
100
3
45
Sorted Order
3 45 100
RESULT:
Thus the C program for Sorting the Values Using Pass By Reference has been executed
successfully and the output was verified

Ex.No.: 7a TOWERS OF HANOI

AIM:
To Write a C program to solve towers of Hanoi using recursion.

ALGORITHM:

1. Start the program


2. Create a function called towers()
3. Read Number of disks
4. Call the function towers()
5. Recursively call this function to move the disks from source to destination.
6. Stop the program

PROGRAM:

#include <stdio.h>

void towers(int,char,char,char);

void main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d",&num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num,'A','C','B');
}

void towers(int num,char frompeg,char topeg,char auxpeg)


{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

OUTPUT:

Enter the number of disks : 3


The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
RESULT:

Thus the program is written & executed successfully.

Ex.No.: 7b FINDING FACTORIAL OF A NUMBER


AIM:
To Write a C program for finding factorial of a number using recursion.

ALGORITHM:

1. Start the program


2. Ask the user to enter an integer to find the factorial
3. Read the integer and assign it to a variable
4. From the value of the integer up to 1, multiply each digit and update the final value
using recursion.
5. The final value at the end of all the multiplication till 1 is the factorial
6. Stop the program

PROGRAM:

#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}

OUTPUT:

Enter a number: 6
Factorial of 5 is: 720

RESULT:
Thus the C program for finding factorial of a number using recursion has been executed
successfully and the output was verified

Ex.No.: 8a POINTER TO POINTER

AIM:
To Write a C program to print the value and address of the variable using pointer to
pointer.

ALGORITHM:

PROGRAM:

#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10
will be printed
printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the
pointer stoyred at pp
}

OUTPUT:
RESULT:

Thus the C program to print the value and address of the variable using pointer to pointer.
has been executed successfully and the output was verified

Ex.No.: 8b ARRAY OF POINTER


AIM:
To Write a C program to read and display 5 students names using array of pointer.

ALGORITHM:
PROGRAM:
#include <stdio.h>

const int MAX = 5;

int main () {

char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",”Sameer Ali”};

int i = 0;

for ( i = 0; i < MAX; i++) {


printf("Value of names[%d] = %s\n", i, names[i] );
}

return 0;
}

OUTPUT:

Value of names[0] = Zara Ali


Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
Value of names[4] = Sameer Ali

RESULT:
Thus the C program to read and display 5 student names using array of pointer has been
executed successfully and the output was verified

EX.NO.: 9a EMPLOYEE SALARY SLIP

AIM:
To Write a C program to generate employee salary slip using structure & pointer.

ALGORITHM:

1. Start the program.


2. Create a structure called Employee with empid,empname,dept,designation & salary
fields.
3. Create a pointer to a structure.
4. Call a function to generate the Salary slip of particular employee
5. Stop the program

PROGRAM:

#include<stdio.h>
struct employee
{
char ename[25];
int eid;
char edes[20];
char edept[20];
int esal;
};

void salaryslip(struct employee *e,int n)


{
int id,i;
printf("\nEnter the employee id to generate the salary slip\n");
scanf("%d",&id);
for(i=0;i<=n;i++)
{
if((e+i)->eid==id)
{
printf("\n-----------------------------------------------------------------------------");
printf("\nNAME\t\tDEPARTMENT\t\tDESIGNATION\t\tSALARY");
printf("\n----------------------------------------------------------------------");
printf("\n%s\t\t%s\t\t%s\t\t%d",(e+i)->ename,(e+i)->edept,(e+i)->edes,(e+i)->esal);
printf("\n--------------------------------------------------------------------------------");
}
}
}

void main()
{
struct employee emp[20],*emp1;
int m,i;
printf("Enter the no. of employee details");
scanf("%d",&m);
printf("\nEnter employee id, name, department, designation & salary\n");
for(i=0;i<m;i++)
{
scanf("%d%s%s%s
%d",&emp[i].eid,&emp[i].ename,&emp[i].edes,&emp[i].edept,&emp[i].esal);
}
emp1=emp;
salaryslip(emp1,m);
getch();
}

OUTPUT:

Enter the no. of employee details2

Enter employee id, name, department, designation & salary


101 A CSE AP 20000
202 B ECE AP 25000

Enter the employee id to generate the salary slip101


-----------------------------------------------------------------------------
NAME DEPARTMENT DESIGNATION SALARY
-----------------------------------------------------------------------------
A CSE AP 20000
-----------------------------------------------------------------------------

RESULT:

Thus the C program to generate employee salary slip using structure & pointer has been
executed successfully and the output was verified.

EX.NO.: 9b STUDENTS INTERNAL MARK SHEET


AIM:
To Write a C program to compute internal marks of students for five different subjects
using nested structure.

ALGORITHM:

1. Start the program.


2. Create a structure called student with the following fields,
 Name
 Test marks for subjects
 Internal mark
3. Create a function called internal
4. Call a function to compute and print the marks
5. Stop the program.

PROGRAM:

#include<stdio.h>

struct student
{
char name[20];
int t[15][15];
int mark[5];
};
void internal( struct student);
void main()
{
struct student s;
int j,k;
clrscr();
for(j=0;j<3;j++)
{
printf("Enter the internal test %d marks for five subjects:\t",j+1);
for(k=0;k<5;k++)
scanf("%d",&s.t[j][k]);
}

internal(s);
getch();
}
void internal(struct student s1)
{
int i,j,k,sum[20],c=0;
for(j=0;j<5;j++)
{
c=0;
for(k=0;k<3;k++)
{
c=c+s1.t[k][j];
}
s1.mark[j]=((c/3)/5);
}
for(i=0;i<5;i++)
printf("\nSubject %d Internal Mark (max. marks 20)= %d",i+1,s1.mark[i]);
}

OUTPUT:

Enter the internal test 1 marks for five subjects: 10 10 10 10 10


Enter the internal test 2 marks for five subjects: 70 80 90 10 80
Enter the internal test 3 marks for five subjects: 90 90 90 90 90

Subject 1 Internal Mark (max. marks 20)= 11


Subject 2 Internal Mark (max. marks 20)= 12
Subject 3 Internal Mark (max. marks 20)= 12
Subject 4 Internal Mark (max. marks 20)= 7
Subject 5 Internal Mark (max. marks 20)= 12

RESULT:

Thus the C program to compute internal marks of students for five different subjects
using nested structure has been executed successfully and the output was verified.
EX.NO.: 9c ACCESSING STUDENT DETAILS USING UNION

AIM:
To Write a C program for accessing student details using union.

ALGORITHM:

PROGRAM:
OUTPUT:

RESULT:

Thus the C program for accessing student details using union has been executed
successfully and the output was verified.
Ex.No: 10a SEQUENTIAL ACCESS FILE

AIM:
Write a C program to count the number of account holders whose balance is less than the
minimum balance using sequential access file.

ALGORITHM:

1. Start the program


2. Read choice to insert records & count minimum balance account
3. If choice is 1, then
 Open a dat file in write mode
 Read the No. of records
 Write the records into the file using fprintf() function
 Close the file
4. If Choice is 2, then
 Open the file in Read mode
 Read the records one by one using fscanf(0 function until reach the end of file.
 Check the account balance with min bal.
 If account balance is less than min balance, then display the account details
 Close the file
5. Stop the program

PROGRAM:

#include <stdio.h>

void insert();
void count();

int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: count(); break;
}
}
}

void insert()
{
unsigned int account,i;
char name[30];
double balance;
FILE* cfPtr;

if ((cfPtr = fopen("clients.dat", "w")) == NULL) {


puts("File could not be opened");
}
else {
int records,i=0;
printf("Enter the No. of records ");
scanf("%d", &records);
while (i<records)
{
printf("Enter the account, name, and balance.");
scanf("%d%29s%lf", &account, name, &balance);
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
i++;
}
fclose(cfPtr);
}
}

void count()
{
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL)
printf("File could not be opened");
else
{
printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);

while (!feof(cfPtr))
{
if (balance < minBal)
{
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
}

fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance: %d",
count);
}
}

OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A 10000
Enter the account, name, and balance.1002 B 300
1 insert records
2 Count min balance holders
3 Exit
Enter choice:2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance holders
3 Exit
Enter choice:

RESULT:
Thus the c program to count the number of account holders whose balance is less than the
minimum balance using sequential access file has been executed successfully and the output was
verified.

Ex.No: 10b RANDOM ACCESS FILE

AIM:
To Write a C program to update telephone details of an individual or a company into a
telephone directory using random access file.

ALGORITHM:

1. Start the program


2. Store the telephone details into a file
3. Read the data & Display it
4. Enter the telephone number to be modified & the new number
5. Use fseek() function to randomly access the record
6. Copy the contents from source file to destination file
7. Store the updated record into the new file
8. Stop the program

PROGRAM:

#include<stdio.h>
struct teledir
{
int no;
char name[3];
};
void main()
{
struct teledir t1,t2,t3;
int i,n,p,newp;
FILE *fp,*fp1;
clrscr();
fp=fopen("td.txt","w");
printf("Enter the no of records\n");
scanf("%d",&n);
printf("Enter the record\n");
for (i=0;i<n;i++)
{
scanf("%d%s",&t1.no,t1.name);
fwrite(&t1,sizeof(struct teledir),1,fp);
}
fclose(fp);
fp=fopen("td.txt","r");

while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)
{
printf("\t%d%s\n",t2.no,t2.name);
}

printf("Enter number to be modified & a new number\n");


scanf("%d%d",&p,&newp);
fclose(fp);
fp=fopen("td.txt","r+");
fp1=fopen("td1.txt","w");
while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)
{
if(t2.no==p)
{
fseek(fp,-sizeof(struct teledir),SEEK_CUR);
t3.no=newp;
strcpy(t3.name,t2.name);
fwrite(&t3,sizeof(struct teledir),1,fp1);
}
else
{
fwrite(&t2,sizeof(struct teledir),1,fp1);
}
}
fclose(fp);
fclose(fp1);
fp=fopen("td1.txt","r");
while(fread(&t3,sizeof(struct teledir),1,fp)!=NULL)
{
printf("\t%d\t%s\n",t3.no,t3.name);
}

fclose(fp);
getch();
}
OUTPUT:
Enter the no of records
3
Enter the record
111 abc
222 xyz
333 nmo
111abc
222xyz
333nmo
Enter number to be modified & a new number
222 9898
111 abc
9898 xyz
333 nmo

RESULT:
Thus the c program to update telephone details of an individual or a company into a
telephone directory using random access file has been executed successfully and the output was
verified.

Ex.No: 10c PROCESSOR DIRECTIVES

AIM:
To Write a C program to find greatest of three numbers using ternary operator in
preprocessor directives.

ALGORITHM:

PROGRAM:
OUTPUT:
RESULT:
Thus the c program to find greatest of three numbers using ternary operator in
preprocessor directives has been executed successfully and the output was verified.

You might also like