KEMBAR78
SPC Manual Updated Record | PDF | String (Computer Science) | Pointer (Computer Programming)
0% found this document useful (0 votes)
8 views49 pages

SPC Manual Updated Record

The document is a lab manual for a structured programming course using C at Bharath Institute of Higher Education and Research. It includes various exercises covering fundamental programming concepts such as addition, multiplication, ASCII values, control statements, operators, arrays, strings, functions, pointers, dynamic memory allocation, and structures. Each exercise provides an aim, algorithm, flowchart, program code, and results of execution.

Uploaded by

B.PRAVEEN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views49 pages

SPC Manual Updated Record

The document is a lab manual for a structured programming course using C at Bharath Institute of Higher Education and Research. It includes various exercises covering fundamental programming concepts such as addition, multiplication, ASCII values, control statements, operators, arrays, strings, functions, pointers, dynamic memory allocation, and structures. Each exercise provides an aim, algorithm, flowchart, program code, and results of execution.

Uploaded by

B.PRAVEEN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

BHARATH INSTITUTE OF HIGHER EDUCATION AND RESEARCH

(Deemed to be University Estd u/s 3 of UGC Act, 1956)


CHENNAI 600 073, TAMIL NADU, INDIA

DEPARTMET OF COMPUTER SCIENCE AND ENGINEERING

SCHOOL OF COMPUTING

REGULATION 2024

STRUCTURED PROGRAMMING USING C

LAB MANUAL

Department of CSE, BIHER


TABLE OF CONTENTS

EX.NO TITLE PAGE .NO


1.a) ADDITION OF TWO NUMBERS 1
b) MULTIPLICATION OF TWO FLOATING NUMBERS 2
c) PRINTING THE ASCII VALUE OF A CHARACTER 3
d) FINDING AREA AND CIRUCUMFERENCE OF A CIRCLE 4
2. OPERATORS AND EXPRESSION IN C 5
3.a) CONTROL STATEMENTS DECISION MAKING AND BRANCHING 8
STATEMENTS
10 USAGE OF SIZEOF() OPERATOR 13
b) SIMPLE IF STATEMENTS 14
c) CHARACTER HANDLING 15
d) SWITCH CASE STATEMENT 16
4. DECISION MAKING CONTROL STATEMENTS 20

a) ARMSTRONG NUMBER 21
b) FIBONACCI SERIES 22
c) TO CHECK THE GIVEN NUMBER IS PRIME NUMBER OR NOT 24
5. a) TO PRINT THE SQUARE OF THE ARRAY ELEMENTS 27
b) INSERTING AN ELEMENT IN AN ONE DIMENSIONAL ARRAY 30
c) DELETING AN ELEMENT IN AN ARRAY 32
6. TWO DIMENSIONAL ARRAY 36
7. STRINGS AND STRING HANDLING FUNCTIONS 40
a) STRING COPY 41
b) STRING CONCATENATION 42
c) STRING COMPARISON 43
8. a) FUNCTIONS CALL BY VALUE AND CALL BY REFERENCE 44

Department of CSE, BIHER


b) IMPLEMENTATION OF PASS BY REFERENCE 48
9. a)_ POINTERS – OPERATIONS ON POINTERS, ARRAY OF POINTERS 50
b) IMPLEMENTATION OF PASS BY REFERENCE 52
10. DYNAMIC MEMORY ALLOCATION 53
11. STRUCTURES 55

Department of CSE, BIHER


EX. NO : 1a) ADDITION OF TWO NUMBERS
AIM : To write a program to calculate the sum of two numbers.
ALGORITHM :
1. Start the program.
2. Declaring the variables – a, b, sum.
3. Get the input from the user.
4. Calculate sum = a+b.
5. Print the output.
6. Stop the program.
FLOW CHART :

Start

Declaring the variables

Get the input from the user

Calculate sum = a+b

Print the output

Stop
PROGRAM :
#include<stdio.h>
void main()
{
int a, b,
sum;
clrscr();
printf(“\nEnter two numbers :”);
scanf(“%d%d”, &a, &b);
sum = a+b;
printf(“\nThe sum is %d”, sum);
getch();
}
RESULT :
Thus, the C program for calculating the sum of two numbers was written, executed and verified
successfully.

Department of CSE, BIHER 1


EX NO : b) MULTIPLICATION OF TWO FLOATING NUMBERS
AIM : To write a C program to multiply two floating point numbers.
ALGORITHM :
1. Start the program.
2. Declaring the variables – a, b, prod.
3. Get the input from the user.
4. Calculate prod = a*b.
5. Print the output.
6. Stop the program.
FLOW CHART :

Start

Declaring the variables

Get the input from the user

Calculate prod = a*b

Print the Output

Stop
PROGRAM :
#include<stdio.h>
void main()
{
float a, b,
prod; clrscr();
printf(“\nEnter two numbers :”);
scanf(“%f%f”, &a, &b);
prod = a*b;
printf(“\nThe product is %f ”, prod);
getch();
}

RESULT :Thus, the C program for calculating the product of two floating point numbers was written,
executed and verified successfully.

Department of CSE, BIHER 2


EX. NO-c) PRINTING THE ASCII VALUE OF A CHARACTER
AIM : To write a C program to multiply two floating point numbers
ALGORITHM :
1. Start the program
2. Declaring a variable ch
3. Get the input from the user
4. Print the ASCII value of the user input
5. Stop the program
FLOW CHART :

Start

Declaring a variable ch

Print the output

Stop

PROGRAM :
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf(“\nEnter a Character
:”); scanf(“%c”, &ch);
printf(“\nThe ASCII value of %c is %d”, ch,ch);
getch();
}

RESULT :
Thus, the C program for printing the ASCII value of a character was written, executed and verified
successfully.

Department of CSE, BIHER 3


EX.NO-d) FINDING AREA AND CIRUCUMFERENCE OF A CIRCLE
Aim:
To write a c program to find the area and circumference of a circle and execute it to get the desired
output.

Algorithm:
STEP 1: Start
STEP 2: Define pi = 3.14.Get the value of radius of the circle.
STEP 3: Using formulae 3.14*r*r and 2*3.14*r the area and the circumference are found. STEP 4:
Print the value of area and circumference.
STEP 5: Stop.

Start

Circumference= 2*PI

Print area, circumference

Program:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
int main()
{
float r, area, circum;
clrscr();
printf("\n Enter the radius of circle:");
scanf("%f",&r);
area=PI*r*r;
circum=2*PI*r;
printf("\nArea of Circle: %f \n Circumference of a Circle: %f\
n",area,circum); getch();
return 0;
}
Output:
Enter the radius of circle: 2.34
Area of Circle: 17.193384
Circumference of a Circle: 14.695200

Result:
Thus, the C program for finding area and circumference of a circle was written, executed and verified
successfully.
Department of CSE, BIHER 4
Ex.No. 2 OPERATORS AND EXPRESSION IN C

Write a C program to show the results using printf() statement for the operations
given below from 1 to 6 in a single program.
a) logical operator
(a==b) && (c<b)
!(a!=b)|| (c<b)
b) Relational operaor
x>=y
c) Arithmetic Operator
d) Assignment operator
e) Increment and Decrement operator
f) Bitwise operator

Aim: To Write a C program to show the results using printf() statement for the logical, relational,
arithmetic, assignment, increment and decrement, bitwise operator.

Algorithm:
Step 1: Start
Step 2: Declare variables of different type as needed.
Step 3: Get the input values for the variables to demonstrate operations.
Step 4: Perform addition, subtraction, multiplication, division and modulus operations for arithmetic
operators.
Step 5: Comapare variables using >,<, >=,<=, ==, and != to demonstrate relational operators.
Step 6: Use Logical && , ||, ! to demonstrate logical operators.
Step 7: Use Assignment operator == for performing assignment operation.
Step 8: Use ++,-- operators for performing pre increment, post increment, pre decrement and post
decrement operation.
Step 9: Use Bitwise AND, OR, XOR, NOT, left shift, right shift for performing bitwise operations.
Step 10: Print the result of each operation.
Step 11: Stop

Program:
#include<stdio.h>
#include<conio.h>

int main()
{
int a = 5, b = 10, c =
8; int x = 15, y = 20;
int num = 6;
int p = 6, q =
3; int result =
0; clrscr();
// Logical operations
printf("Logical Operations:\n");
printf("(a == b) && (c < b) = %d\n", (a == b) && (c < b));
printf("!(a != b) || (c < b) = %d\n", !(a != b) || (c < b));

// Relational operations printf("\


nRelational Operations:\n");
printf("x >= y = %d\n", x >= y);
printf("x < y = %d\n", x < y);

Department of CSE, BIHER 5


// Arithmetic operations printf("\
nArithmetic Operations:\n");
printf("a + b = %d\n", a + b);
printf("b - c = %d\n", b - c);
printf("a * c = %d\n", a * c);
printf("b / a = %d\n", b / a);
printf("b %% a = %d\n", b % a);

// Assignment operator printf("\


nAssignment Operations:\n"); result
= a + b;
printf("result = a + b; result = %d\n", result);

// Increment and Decrement operators printf("\


nIncrement and Decrement Operations:\n"); printf("num
= %d\n", num);
printf("num++ = %d\n", num++);
printf("After num++: num = %d\n", num);
printf("++num = %d\n",
++num);
printf("num-- = %d\n", num--);
printf("After num--: num = %d\n", num);
printf("--num = %d\n", -- num);

// Bitwise operations printf("\


nBitwise Operations:\n");
printf("p & q = %d\n", p & q); //
AND printf("p | q = %d\n", p | q); //
OR printf("p ^ q = %d\n", p ^ q); //
XOR printf("~p = %d\n", ~p);
// NOT
printf("p << 1 = %d\n", p << 1); // Left shift
printf("p >> 1 = %d\n", p >> 1); // Right shift
getch();
return 0;
}

Output :
Logical Operations:
(a == b) && (c < b) = 0
!(a != b) || (c < b) = 1
Relational Operations:
x >= y = 0 x < y = 1
Arithmetic Operations:
a + b = 15
b-c=2
a * c = 40
b/a=2b%a=0
Assignment Operations:
Department of CSE, BIHER 6
result = a + b; result =
15

Department of CSE, BIHER 7


Increment and Decrement Operations: num = 6 num+
+ = 6 After
num++: num = 7
++num = 8
num-- = 8
After num-- :num = 7
--num = 6
Bitwise Operations:
p&q=
2p|q=
7p^q=
5
~p = -7
p << 1 = 12
p >> 1 = 3

RESULT:
Thus, the above C program for various operator functions was written, executed and verified
successfully.

Department of CSE, BIHER 8


Ex. No. 3 a) CONTROL STATEMENTS DECISION MAKING AND
BRANCHING STATEMENTS

Write a C program to find the biggest of three numbers using ladder if else
statement.

AIM: To Write a C program to find the biggest of three numbers using ladder if else statement.

ALGORITHM:
Step 1:Start
Step 2:Declare three variables with name a, b, c, largest Step
3:Display “Enter three numbers:”

Step 5:If a is greater than both b and c, set largest = a


Step 6:Else if b is greater than both a and c, set largest = b Step
7:Else, set largest = c
Step 8:Display “Largest number is: largest”
Step 9:End

PROGRAM:

#include<stdio.h>
#include<conio.h> int
main()
{
int a,b,c;
clrscr();
printf("\nEnter a,b, and c\t");
scanf(“%d%d%d”, &a,&b,&c);
if((a>b)&&(a>c))
printf(“ The number a=%d is big”,a);
else if((b>a)&&(b>c))
printf(“ The number b=%d is big”,b);
else
printf(“ The number c=%d is big”,c);
getch();
return 0;
}

Department of CSE, BIHER 9


OUTPUT:

Enter a,b, and c 10


73

The number a=10 is big

RESULT:
Thus, the C program for finding biggest of three numbers using ladder if else statement was written,
executed and verified successfully.

Department of CSE, BIHER 10


EX. NO-3b) SIMPLE IF STATEMENTS

Write a C program to check whether a number is odd or even using 2 simple if statements.

AIM:
To write a C program to check whether the given number is odd or even.

ALGORITHM:

1. Start the program.


2. Declare the variable num as integer.
3. Get the input from the user.
4. Check the number as odd or even using mod(%) operator.
5. Print the output
6. Stop the program.

PROGRAM:

#include <stdio.h>
#include
<conio.h> int
main()
{
int num;
clrscr();
printf("\nEnter a number:\t");
scanf(“%d”, &num); if(num
%2==0)
{
printf(“ \nThe number is even”);
}
If(num%2!=0)
{
printf(“ \nThe number is odd”);
}
getch();
return 0;
}

OUTPUT:
Enter a number: 4
The number is even

RESULT:
Thus the C Program to check whether the given number is odd or even, was written, executed and verified
successfully.

Department of CSE, BIHER 11


Ex.No-3 c) CHARACTER HANDLING
Write a C program to find whether entered value is character or not.
AIM:
To write a C program to check the entered input is character or not.
ALGORITHM:
1. Start the program
2. Declare a variable as char.
3. Get the input from the user.
4. Check the entered character lies between lowercase a to z or in uppercase A to Z using IF condition.
5. Print the result.
6. Stop the program.
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\nEnter a character\t");
scanf(“ %c”, &ch);
if(ch>=’a’ && ch<=’z’)
printf(“ \nThe entered value is a lower case letter”);
if(ch>=’A’ && ch<=’Z’)
printf(“ \nThe entered value is a upper case letter”);
if(ch>=’0’ && ch<=’9’)
printf(“ \nThe entered value is a number”);
getch();
return 0;
}
OUTPUT:
Enter a character d
The entered value is a lower case
letter Enter a character L
The entered value is a upper case
letter Enter a character 7
The entered value is a number

RESULT:
Thus the C program to check the entered value as character or not was written, executed and verified
successfully.

Department of CSE, BIHER 12


Ex.No-3 d) SWITCH CASE STATEMENT

Write a C program to implement simple calculator using switch case statement

AIM:
To write a C program to implement simple calculator using switch case statement.

ALGORITHM:

1. Start the program.


2. Declare the variables as integer.
3. Get the option and values from the user.
4. Depending upon the option, do the calculation using switch case statement.
5. Print the output.
6. Stop the program.

PROGRAM:

#include <stdio.h>
#include <conio.h>
int main()
{
int opt,a,b,c;
clrscr();
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulus \n");
printf("EXIT \n"); printf("\
tEnter an option\t");
scanf(“%d”, &opt);
printf("\nEnter a and b values:\t");
scanf(“%d%d”, &a,&b);
switch(opt)
{
case 1: c=a+b;
printf(“\nAddition a and b is %d”,c);
break;
case 2: c=a-b;
printf(“\nSubtraction of a and b is %d”,c);
break;
case 3: c=a*b;
printf(“\nMultiplication of a and b is %d”,c);
break;
Department of CSE, BIHER 13
case 4: c=a/b;
printf(“\nDivision of a and b is %d”,c);
break;
case 5: c=a%b;
printf(“\nRemainder of a and b is %d”,c);
break;
default: printf(“\nWrong option No operation You exit”);
}
getch();
return 0;
}

OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
EXIT
enter an option 2
enter a and b 11 9
Subtraction of a and b is 2

RESULT:

Thus the C program using switch-case statements for mathematical calculations was written, executed and
verified successfully.

Department of CSE, BIHER 14


Ex.No-4 a) ARMSTRONG NUMBER

Write a C program to find whether the number is Armstrong number or


not using while looping statement.

AIM:
To write a C program to find whether the number is Armstrong number or not using while looping
statement.

ALGORITHM:

STEP 1 : Start.
STEP 2: Declare n,n1,digit,sum=0.
STEP 3: Get the required positive number and store in n.
STEP 4: Use while loop until original number is not zero.
digit=n%10;
sum=sum+digit*digit*digit;
n=n /10
STEP 5: Check if digit and n are equal
STEP 6: Print the appropriate output
STEP 7: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int n,n1,digit,sum=0;
clrscr();
printf("Enter any positive number");
scanf("%d",&n);
n1=n;
while(n>0)
{
digit=n%10;
sum=sum+digit*digit*digit;
n=n/10;
}
if (n1==sum)
printf("\nThe given number is an Armstrong No.%d",n1);
else
printf("\nThe given number is NOT an Armstrong No.%d",n1);
getch();
}

Output:
Enter any positive number 153
The given number is an Armstrong No.153

Result:
Thus, the above C program was written, executed and verified successfully.

Department of CSE, BIHER 15


EX.NO -4 b) FIBONACCI SERIES

Write a C program to print the Fibonacci series of n number of terms using do … while statement.

AIM:
To write a C program to print the Fibonacci series of n number of terms using do …while statement.

ALGORITHM:

Step 1:Start
Step 2:Declare variables i, a,b , show
Step 3:Initialize the variables, a=0, b=1, and show =0
Step 4:Enter the number of terms of Fibonacci series to be printed
Step 5:Print First two terms of series
Step 6:Use loop for the following steps
show=a+b
a=b
b=show
increase value of i each time by 1
print the value of show
Step 7:End

PROGRAM:

#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, count = 1;
printf("\nEnter the number of terms: ");
scanf("%d", &n);
if (n <= 0)
{
printf("\nPlease enter a positive number.\n");
return 1;
}
printf("\nFibonacci Series: ");
do
{
if (count == 1)
{
printf("%d", first);
}
else if (count == 2)
{
printf(", %d", second);
}
else
{
next = first +
second; printf(",
%d", next); first =
second; second =

Department of CSE, BIHER 16


n
e
x
t
;

Department of CSE, BIHER 17


}
count++;
} while (count <= n); printf("\
n");
return 0;
}

OUTPUT:
Enter the number of terms: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8

RESULT:
Thus, the above C program was written, executed and verified successfully.

Department of CSE, BIHER 18


EX NO-4 c) TO CHECK THE GIVEN NUMBER IS PRIME NUMBER OR NOT

Write a C program to find the number is prime or not using for loop.

AIM:
To write a C program to find the number is prime or not using for loop.

ALGORITHM:
STEP 1: Start
STEP 2: Take n as input.
STEP 3: Initialize a variable flag to 0.
STEP 4: Iterate a “for” loop from 2 to num/2.
STEP 5: If n is divisible by loop iterator, then increment flag.
STEP 6: If the flag is equal to 0,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.
STEP 7: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,flag=0;
clrscr();
printf("Enter any number");
scanf("%d",&n);
for (i=2;i<=n/2;i++)
{
if (n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number",n);
else
printf("%d is NOT a prime number",n);
getch();
}

OUTPUT:
Enter any number 37
37 is a prime number

Enter any number 99


99 is NOT a prime number

RESULT:
Thus, the above C program was written, executed and verified successfully.

Department of CSE, BIHER 19


Department of CSE, BIHER 20
EX.NO-5 a) TO PRINT THE SQUARE OF THE ARRAY ELEMENTS

Write a C program to read a one dimensional array and square its elements and print the array with squared
values.

AIM:
To write a C program to print the squared values of a one dimensional array.

ALGORITHM:

1. Start the program.


2. Declare the array of size 5 and get the input from the user to store in it.
3. Square the values of array and print it.
4. Stop the program.

PROGRAM:

#include<stdio.h>
void main()
{
clrscr();
int a[5],i;
printf(“\nEnter the values to store in an array:”);
for(i=1;i<=5;i++)
scanf(“%d”, &a[i]);
printf(“\nThe squared values of the given array:”);
for(i=1;i<=5;i++)
printf(“%d”,a[i]*a[i]);
getch();
}

OUTPUT:

Enter the values to store in an array: 1 2 3 4 5


The squared values of the given array: 1 4 9 16 25

RESULT:

Thus the C program to print squared values of the given array was written, executed and verified successfully.

Department of CSE, BIHER 21


Department of CSE, BIHER 22
EX.No-5 b) INSERTING AN ELEMENT IN AN ONE DIMENSIONAL ARRAY

Write a C program to insert an element in an 1 dimensional array.

AIM:
To write a C program to insert an element in an 1 dimensional array.

ALGORITHM:

Step 1: Start
Step2. Get the element value which needs to be inserted.
Step3. Get the position value.
Step4. Check whether the position value is valid or not.
Step5. If it is valid,Shift all the elements from the last index to position index by 1 position to
the right. insert the new element in arr[position]
Step 6: Otherwise, Print array Is out of boundary.
Step 7:Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{ int i,a[30],element,n,pos;
clrscr();
printf(“\nEnter number of elements in an array\n”);
scanf(“%d”,&n);
if(n<30)
{
printf("---------- INPUT------------\n\n");
printf("\nEnter the Array Elements....\n");
for (i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
printf(“\nEnter the position and element to be inserted in an array\n”);
scanf(“%d%d”,&pos,&element);
for(i=n;i>=pos;i--)
{ a[i]=a[i-1];
} a[pos]=element;
printf("----------- OUTPUT----------\n\n");
printf("\nThe Array Elements after inserting an element are...\n");
for(i=0;i<=n;i++)
{
printf("%d\n",a[i]);
}
} else
{
18
printf(“\nArray is out of boundary”);
}
getch();
}

Department of CSE, BIHER 23


OUTPUT:
Enter number of elements in an array
5 Enter the Array Elements ...
10
20
30
40
50
Enter the position and element to be inserted in an array
2 100
OUTPUT
The Array Elements after inserting an element are…
10
20
100
30
40
50

RESULT:

Thus, the above C program to insert an element in an 1 dimensional array was written, executed and verified
successfully.

Department of CSE, BIHER 24


Ex .No -5 c) DELETING AN ELEMENT IN AN ARRAY

Write a program to delete an element from one dimensional array.

AIM:
To Write a program to delete an element from one dimensional array.

ALGORITHM:
Step 1: Start
Step 2:Find the given element in the given array and note the index.
Step 3:If the element found,Shift all the elements from index + 1 by 1 position to the left.Reduce
the array size by 1.
Step 4: Otherwise, print "Array is out of boundary".
Step 5:Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>

void main()
{
int i,a[30],n,pos;
clrscr();
printf(“\nEnter number of elements in an array\n”);
scanf(“%d”,&n);
if(n<30)
{
printf("---------- INPUT------------\n\n");
printf("\nEnter the Array Elements....\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf(“\nEnter the position to be deleted in an array\n”);
scanf(“%d”,&pos);
for(i=pos;i<=n;i++)
{
a[i]=a[i+1];
}
printf("----------- OUTPUT----------\n\n");
printf("\nThe Array Elements after deleting an element are...\n");
for(i=0;i<n-1;i++)
{
printf("%d\n",a[i]);
}
}
else
{
printf(“\nArray is out of boundary”);
}
getch();
}

Department of CSE, BIHER 25


OUTPUT:
Enter number of elements in an array
5 INPUT
Enter the Array Elements …
12
34 5
20
Enter the position to be deleted in an array
2
OUTPUT
The Array Elements after deleting an element are..
1
24
5

RESULT:

Thus, the above C program to delete an element from one dimensional array was written, executed
and verified successfully.

Department of CSE, BIHER 26


.

Department of CSE, BIHER 27


Ex.No. 6 TWO DIMENSIONAL ARRAY

Write a C program to do the addition of two matrices.

AIM:
To write a C program to do the addition of two matrices.

ALGORITHM:

Step 1: Start.
Step 2: Declare matrix matrx A[r1][c1] and matrix matrx B[r2][c2] and matrix c[row][col], row= no. of
rows, col= no. of columns.
Step 3: Read row, col, mat1[][] and mat2[][].
Step 4: Declare variable i=0, j=0.
Step 5: Repeat until i < row
Repeat until j < col
c[i][j]=a[i][j] + b[i][j]
Set j=j+1
Set i=i+1
Step 6: c[i][j] is the required matrix after addition.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],I,j,r1,c1,r2,c2;
printf(“\nEnter the row and column of the matrx A\t”); scanf(“%d
%d”,&r1,&c1);
printf(“\nEnter the row and column of the matrx B\t”); scanf(“%d
%d”,&r2,&c2);
if (r1==r2 &&c1==c2)
{
printf(“\nEnter the elements of A”)
for (i=0;i<r1;i++)
for (j=0;j<c1;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\nEnter the elements of B”)
for (i=0;i<r2;i++)
for (j=0;j<c2;j++)
scanf(“%d”,&b[i][j]);
for (i=0;i<r2;i++)
{
for (j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(“d\t”,c[i][j]);
} printf(“\n”);
}
else
printf(“\nAddition is not possible”);
}

Department of CSE, BIHER 28


OUTPUT:
Enter the row and column of the matrx A
2
3
Enter the row and column of the matrx B 2 3
Enter the elements of A
123456
Enter the elements of B
666666
The addition of two matrices is
789
10 11 12

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 29


Department of CSE, BIHER 30
EX.NO 7 a) STRINGS AND STRING HANDLING FUNCTIONS

STRING COPY

Write a C program to copy one string to another using Library functions.

AIM:
To write a C program to copy one string to the other using built-in library function strcpy().

ALGORITHM:

Step 1:Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Copy the string from str1 to str2 using strcpy(str2, str1).
Step 5: Print "String1 = <str1> and String2 = <str2>".
Step 6: End

PROGRAM:

#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter a string :\n”);
gets(str1);
strcpy(str2,str1);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
getch();
}

OUTPUT:
enter a string : Good Habit
String1 = Good Habit and string 2= Good Habit

RESULT:

Thus, the above C program to copy one string to the other string was written,executed and verified
successfully.

Department of CSE, BIHER 31


EX.NO- 7 b) STRING CONCATENATION

Write a C program to concatenate one string with the other string using Library functions.

AIM:
To write a C program to concatenate one string with the other string using Library functions.

ALGORITHM:

Step 1 :Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Read the input string into str2 using gets().
Step 5:Print "Before concatenation: String1 = <str1>, String2 = <str2>".
Step 6:Concatenate str2 to str1 using strcat(str1, str2).
Step 7: Print "After concatenation: String1 = <str1>, String2 = <str2>".
Step 8: Stop

PROGRAM:
#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter first string :\n”);
gets(str1);
printf(“\nEnter second string :\n”);
gets(str2);
printf(“\nBefore concatenation \n”);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
strcat(str1,str2);
printf(“\nAfter concatenation \n”);
printf(“\nString1 = %s and string 2=%s\n”,str1, str2);
getch();
}

OUTPUT:

Enter first string : Good


Enter second string : Morning
Before concatenation
String1 = Good and string 2= Morning
String1 = GoodMorning and string 2=Morning

RESULT:

Thus, the above C program to concatenate one string with the other string was written, executed and
verified successfully.

Department of CSE, BIHER 32


EX.NO-7 c) STRING COMPARISON

Write a C program to compare two strings using Library functions.

AIM:
To write a C program to compare two strings using Library functions.

ALGORITHM:

Step 1:Start
Step 2: Declare two character arrays: str1[30], str2[30].
Step 3: Read the input string into str1 using gets().
Step 4: Read the input string into str2 using gets().
Step 5: Print "Before concatenation: String1 = <str1>, String2 = <str2>".
Step 6:Concatenate str2 to str1 using strcat(str1, str2).
Step 7: Print "After concatenation: String1 = <str1>, String2 = <str2>".
Step 8: End

PROGRAM:
#include<stdio.h>
#incluce<conio.h>
#incluce<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf(“\nEnter first string :\n”);
gets(str1);
printf(“\nEnter second string :\n”);
gets(str2);
if(strcmp(str1,str2)==0)
{
printf(“\nStrings are same”);
} else
{
printf(“\nStrings are not same”);
}
getch();
}

OUTPUT:
Enter first string : Chain
Enter second string : Chain
Strings are same

RESULT:
Thus, the above C program to compare two strings was written,executed and verified successfully.

Department of CSE, BIHER 33


EX.NO-8 a) FUNCTIONS CALL BY VALUE AND CALL BY REFERENCE

Write a C program to swap two numbers by pass by value in functions.

AIM:
To Write a C program to swap two numbers by pass by value in functions.

ALGORITHM:
Step 1 : Start
Step 2 : Declare variables a and b
Step 3 : Input values for a and b.
Step 4 : Print "Before swapping: a = <value>, b = <value>".
Step 5 : Call the function swap(a, b). Inside swap function:
i. Declare a temporary variable temp.
ii. temp = a.
iii. a = b.
iv. b = temp.
v. Print "Inside swap function: a = <value>, b = <value>".
Step 6: Print "After swapping in main: a = <value>, b =
<value>". Step 7: End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void swap (int a, int
b); int main()
{
int a,b;
clrscr();
printf(“\nEnter the values of a and b”);
scanf(“%d%d”,&a,&b); printf(“\
nBefore swapping \n”); printf(“a = %d
\t b= %d\n”,a,b); swap(a,b);
printf(“\nAfter swapping \n”);
printf(“a = %d \t b= %d\n”,a,b);
getch();
return 0;
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

Department of CSE, BIHER 34


OUTPUT:
Enter the values of a and b 10 20
Before swapping
a=10 b=20
After swapping
a=10 b=20

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 35


.

Department of CSE, BIHER 36


EX.NO- 8 b) IMPLEMENTATION OF PASS BY REFERENCE

Write a C program to pass array to a function

AIM:
To pass one dimensional array to a function in order to implement the pass by reference.

ALGORITHM:

Start
1. Declare an array a[30] and integers n, i.
2. Read n.
3. Print "Enter the array values".
4. For i = 0 to n-1:
Read a[i].
5. Call update(a, n). Function update(a[], n)
6. For i = 0 to n-1:
a[i] = a[i] *
a[i].
7. Return to main.
8. Print "After updating the array a in subfunction…".
9. For i = 0 to n-
1: Print a[i].
End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void update (int [],int
); int main()
{
int a[30],i,n;
clrscr();
printf(“\nEnter the values of n \n”);
scanf(“%d”,&n);
printf(“\nEnter the array values \n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\nPassing 1 D array to the function update\n”);
update(a, n);// Passing the address reference to the function
printf(“\nAfter updating the array a in subfunction… \n”);
for(i=0;i<n;i++)
{
printf(“a[%d] = %d\n”,i,a[i]);
}
getch();
return 0;
}

Department of CSE, BIHER 37


void update(int a[], int n)

Department of CSE, BIHER 38


{
int i; for(i=0;i<n;i+
+)
{
a[i]=a[i]*a[i];
}
}

OUTPUT:
Enter the values of n 5
Enter the array values 10 20 11 12 13
Passing 1 D array to the function update
After updating the array a in subfunction…
a[0]=100

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 39


EX.NO-9 a) POINTERS – OPERATIONS ON POINTERS, ARRAY OF POINTERS

Write a C program to do the sum of 5 integers using pointer variable.

AIM:
To do the sum of integers using pointer variable implemented in C program.

ALGORITHM:
Start
1. Declare variables: n, i, sum = 0, *ptr
2. Print "Enter number of elements:"
3. Read n
4. Allocate memory for n integers: ptr = (int*) calloc(n, sizeof(int))
5. If ptr == NULL:
a. Print "Error! memory not allocated."
b. Exit the program
6. Print "Enter elements:"
7. For i = 0 to n - 1:
a. Read element into *(ptr + i)
b. Add *(ptr + i) to sum
8. Print "Sum = ", sum
9. Free allocated memory: free(ptr)
10. End

PROGRAM:
#include<stdio.h>
void main()
{
int *p, sum, I;
Int x[5] = {10, 20, 30, 40, 50);
i=0;
p=x; //initialization of integer pointer p to the base address of an array x
printf(“\nElement Value Address\n\n”);
while(i<5)
{
printtf(“ x[%d]\t%d\t%u\n”, i, *p, p);
sum = sum + *p;
i++;
p++;
}
printf(“ \nSum =%d\n”, sum); printf(“\
n &x[0] = %u\n”, x[0]); printf(“\n p =
%u \n”,p);
}

Department of CSE, BIHER 40


OUTPUT:
Element Value Address
x[0] 10 655520
x[1] 20 655522
x[2] 30 655524
x[3] 40 655526
x[4] 50 655528
Sum = 150
&x[0]= 655522
p=655530

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 41


EX.NO-9 b) IMPLEMENTATION OF ARRAY OF POINTERS

Write a C program to explain the array of pointers.

AIM: To implement the array of pointer concept using character array in C program.

ALGORITHM:
Start
1. Declare an array of pointers: name[3]
a. Assign "New Zealand" to name[0]
b. Assign "Australia" to name[1]
c. Assign "India" to name[2]
2. Print "Using array of pointers"
3. For i = 0 to 2:
a. Print the string stored at *(name + i)
4. End

PROGRAM:

#include <stdio.h>
void main()
{
char *name[3] = { “New Zealand”, “Australia” ,“India” };
printf(“\nUsing array of poitners\n”);
for(i=0; i<3;i++)
{
printf(“%s\n”, *(name+i));
}
}

OUTPUT:
New Zealand
Australia
India

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 42


EX.NO-10 DYNAMIC MEMORY ALLOCATION
Write a C program to calculate the sum of n numbers entered by the user.

AIM:
To Write a C program to calculate the sum of n numbers entered by the user.

ALGORITHM:
1. Print a prompt: "Enter number of elements".
2. Read the integer value n from the user.
3. Use calloc to allocate memory for n
integers. ptr = (int*) calloc(n,
sizeof(int));
4. Check if memory allocation was successful:
If ptr == NULL, print an error message: "Error! memory not allocated."
Exit the program if memory allocation fails.
5. Print a prompt: "Enter elements".
6. Loop through i from 0 to n - 1:
Read each integer value from the user and store it in ptr[i] using pointer arithmetic:
scanf("%d", ptr + i);.
7. Initialize a variable sum to 0.
8. Loop through i from 0 to n - 1:
Add the value stored at ptr[i] to sum: sum += *(ptr + i);.
Print the sum using: printf("Sum = %d", sum);.
8. Use the free function to release the dynamically allocated memory: free(ptr);.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("\nEnter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("\nError! memory not allocated.");
exit(0);
}
printf("\nEnter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("\nSum =%d", sum);
free(ptr);
return 0;
}

Department of CSE, BIHER 43


OUTPUT:
Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156
38

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 44


EX.NO-11 STRUCTURES

Write a C program to get the book details using simple structure.

AIM: To write algorithm and C program for getting the book details using C structure.

ALGORITHM:
Start
1. Define a structure `book` with fields:
- `name` (character array)
- `price` (float)
- `pages` (integer)
2. Declare variables `b1`, `b2`, `b3` of type `struct book`
3. Print "Enter names, prices & number of pages of 3 books"
4. For each book:
a. Prompt user to input name, price, and pages
b. Read the inputs using `scanf`
5. Print "And this is what you entered:"
6. For each book:
a. Print name, price, and pages using `printf`
End

PROGRAM:
# include <stdio.h>
struct book
{
char name[30];
float price ;
int pages ;
};
int main( )
{
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" )
; scanf ( "\n%s%f %d", b1.name, &b1.price, &b1.pages ) ;
scanf ( "\n%s %f %d", b2.name, &b2.price, &b2.pages ) ;
scanf ( "\n%s %f %d", b3.name, &b3.price, &b3.pages ) ;
printf ( "\nAnd this is what you entered\n" ) ;
printf ( "\n%s %f %d\n", b1.name, b1.price, b1.pages )
; printf ( "\n%s %f %d\n", b2.name, b2.price, b2.pages
) ; printf ( "\n%s %f %d\n", b3.name, b3.price,
b3.pages ) ; return 0 ;
}

Department of CSE, BIHER 45


OUTPUT:
Enter names, prices and no. of pages of 3 books
Adam 100.00 354
Catherine 256.50 682
Filomine 233.70 512
And this is what you entered
Adam 100.000000 354
Catherine 256.500000 682
Filomine 233.700000 512

RESULT:
Thus, the above C program was executed successfully.

Department of CSE, BIHER 46

You might also like