0% found this document useful (0 votes)
1 views52 pages

C LAB

The document contains multiple C programming exercises demonstrating various concepts such as I/O statements, operators, expressions, decision-making constructs, and looping statements. Each exercise includes an aim, algorithm, program code, output examples, and a result confirming successful execution. Topics covered include calculating the area and circumference of a circle, swapping numbers, converting Fahrenheit to Celsius, finding the largest of three numbers, and performing arithmetic operations using switch statements.

Uploaded by

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

C LAB

The document contains multiple C programming exercises demonstrating various concepts such as I/O statements, operators, expressions, decision-making constructs, and looping statements. Each exercise includes an aim, algorithm, program code, output examples, and a result confirming successful execution. Topics covered include calculating the area and circumference of a circle, swapping numbers, converting Fahrenheit to Celsius, finding the largest of three numbers, and performing arithmetic operations using switch statements.

Uploaded by

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

lOMoARcPSD| 356 459 56

Ex.No:1(a) C Programs to demonstrate I/O Statements, Operators & Expressions


Date:

Aim:
To write a C program to find area and circumference of a circle.

Algorithm:

Step 1: Start the program.


Step 2: Input the radius of the Circle.
Step 3: Find the area and circumference of the circle using the formula Area =3.14*r*r
Circum=2*3.14*r
Step 4: Print the area and Circumference Step 5: Stop the Program

Program:

#include<stdio.h>
void main()
{
float r,area,circum;
printf("\n Enter the radius of the Circle");
scanf("%f",&r);
area=3.14*r*r;
circum=2*3.14*r;
printf("\n Area=%f",area);
printf("\n Circumference=%f",circum);
getch();
}

Output:
Enter the radius of the Circle 5
Area = 78.500000
Circumference = 31.400000

Result:
Thus the C program to find the area and circumference of the circle has been created successfully
and verified.
lOMoARcPSD| 356 459 56

Ex. No:1(b) To swap two numbers without using temporary variable


Date:

Aim:

To write a C program to swap two numbers using temporary variable.

Algorithm:

Step 1: Start the program.


Step 2: Input the first number and second number.
Step 3: Swap the numbers without using temporary variable num1=num1+num2
num2=num1-num2 num1=num1-num2
Step 4: Print the first number and second number. Step 5: Stop the Program

Program:

#include<stdio.h>
int main()
{
int num1,num2;
printf("\n Enter the first number:");
scanf("%d",&num1);
printf("\n Enter the second number:");
scanf("%d",&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n The first number is %d",num1);
printf("\n The second number is %d",num2);
}

Output:
Enter the first number: 50
Enter the Second number: 75

The first number is 75


The second number is 50

Result:
Thus the C program to swap two numbers without using temporary variable has been created
successfully and verified.
lOMoARcPSD| 356 459 56

Ex.No.1(c) To Convert Fahrenheit into degree Celsius


Date:

Aim:

To write a C program to convert Fahrenheit into degree Celsius.

Algorithm:

Step 1: Start the program.


Step 2: Input the temperature in fahrenheit.
Step 3: Calculate the temperature in Celsius using below formula celsius=(0.56)*(Fahrenheit-32);
Step 4: Print the temperature in degree celsius. Step 5: Stop the Program

Program:

#include<stdio.h>
void main()
{
float fahrenheit,celsius;
printf("\n Enter the temperature in Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius=(0.56)*(fahrenheit-32);
printf("\n Temperature in Degree Celsius is %f ",celsius);
getch();
}

Output:
Enter the temperature in Fahrenheit: 100
Temperature in Degree Celsius is 37.78

Result:
Thus the C program to convert Fahrenheit into Degree Celsius has been created successfully and
verified.
lOMoARcPSD| 356 459 56

Ex.No.2(a) C Programs to demonstrate Decision Making Constructs


Date:
Demonstration on if.else contruct
Aim:
To write a C program to find largest of three numbers.

Algorithm:
Step 1: Start the program
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b & If a>c
Display a is the largest number. Else
Display c is the largest number.
Else If b>c
Display b is the largest number. Else
Display c is the greatest number.
Step 5: Stop the program.

Program:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the values of A,B and C: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("A is Greater than B and C");
}
else if (b > a && b > c) {
printf("B is Greater than A and C");
}
else if (c > a && c > b) {
printf("C is Greater than A and B");
}
else {
printf("all are equal or any two values are equal");
}
return 0;
}

Output:

Enter the values of A,B and C: 3 5 8

C is Greater than A and B

Result:

Thus the C program to find largest of three numbers has been created successfully and verified.
lOMoARcPSD| 356 459 56

Ex.No.2(b) Demonstration on “goto” statement


Date:

Aim:
To write a C Program to check for age eligibility on voting.

Algorithm:
Step 1: Start the program
Step 2: Read age.
Step 3: Check the condition, if age>18
Step 4: if true, then go to the label, “yes” and print the statement.
Step 5: Else, go to the label, “no” and print the statement.
Step 6: Stop the program.

Program:

#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age < 18)
goto not_eligible;
printf("You are eligible to vote!\n");
return 0;
not_eligible:
printf("Sorry, you are not eligible to vote. \n You must be at least 18 years old.\n");
return 1;
}

Output:-1
Enter your age: 32
You are Eligible

Output:-2
Enter your age: 22
You are not Eligible

Result:
Thus the C program to demonstrate on goto label has been created successfully and verified.
lOMoARcPSD| 356 459 56

Ex.No.2(c) Demonstration on “switch” statement


Date:

Aim:
To write a C program for demonstrating arithmetic operations using switch case statement.

Algorithm:

Step-1 Start the program.


Step-2 Display menu showing addition, subtraction, multiplication and division operation.
Step-3 Get the values for two variables
Step-4 Obtain the choice from the user and accordingly switch over to particular block.
Step-5 Display the result.
Step-6 If the user wishes to continue repeat steps 2 and 3
Step-7 Stop the program.

Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b, c, n;
while (1) {
printf("\n1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &n);
if (n == 0) {
printf("Exiting program...\n");
exit(0);
}
printf("Enter the two numbers: ");
scanf("%d %d", &a, &b); // Corrected input format
switch (n) {
case 1:
c = a + b;
printf("Addition: %d\n", c);
break;
case 2:
c = a - b;
printf("Subtraction: %d\n", c);
break;
case 3:
c = a * b;
printf("Multiplication: %d\n", c);
break;
case 4:
if (b == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
c = a / b;
lOMoARcPSD| 356 459 56

printf("Division: %d\n", c);


}
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}

Output:

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
Enter your choice: 1
Enter the two numbers: 10 5
Addition: 15

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
Enter your choice: 0
Exiting program...

Result:
Thus to write a C program for demonstrating arithmetic operations using switch case statement was
compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No.2(d) Demonstration on “break - continue” statement


Date:

Aim:
To write a C Program to take the inputs until entering zero using break statement.

Algorithm:
Step 1: Start the program
Step 2: Using while loop till false, Read the value, a
Step 4: Check the condition, if a==0 then
Step 5: if true, exit from the executions
Step 6: Else, ask for reading the value
Step 7: Stop the program.

Program: (Taking input from the user until entering zero)

#include <stdio.h>
int main() {
int a;
while (1) { // Infinite loop
printf("Enter the number: ");
scanf("%d", &a);
if (a == 0) {
printf("Bye\n");
break; // Exit the loop when input is 0
}
}
return 0;
}

Output:

Enter the number:2


Enter the number:3
Enter the number:4
Enter the number:5
Enter the number:0
Bye

Result:
Thus the C program to take the inputs until entering zero using break statement has been created
successfully and verified.
lOMoARcPSD| 356 459 56

Ex.No.2(e) Demonstration on “break - continue” statement


Date:

Aim:
To write a C Program to print sum of odd numbers upto 10 using continue statement.

Algorithm:
Step 1: Start the program
Step 2: Initialize a=0, sum=0
Step 3: Using for loop with condition a<10
Step 4: check the condition a%2==0 then
Step 5: If true, move to the beginning of the for loop
Step 6: Else, calculate sum=sum + a
Step 7: Repeat the steps 4 to 6, until for loop is satisfied
Step 8: Print the sum
Step 9: Stop the program.

Program:

#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{

if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("Sum = %d",sum);
return 0;
}

Output:

Sum = 25

Result:
Thus the C program to print sum of odd numbers upto 10 using continue statement has been created
successfully and verified.
lOMoARcPSD| 356 459 56

Ex.No:3 Demonstration on Looping Statements


DATE:

(a) To print the number series up to the given limit (using for loop)
Aim:
To write a C program to print the number series up to the given limit, n.

Algorithm:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using for loop, initialize i =0 to i<n
Step 4: Print the value n
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

Program (To Print the number series up to the given limit n)


#include <stdio.h>

int main() {
int i, n;
printf("Enter the limit: ");
scanf("%d", &n);

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


printf("%d\n", i);
}

return 0;
}

Output:

Enter the limit 10 1


2
3
4
5
6
7
8
9
10

Result:
Thus a C program to print the number series up to the given limit n was compiled and executed
successfully.
Ex.No:3 Demonstration on Looping Statements
DATE:

(b) To print the sum of series up to given limit (using while loop)

Aim:
To write a C program to find sum of series up to given limit, n.

Algorithm:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using while loop, initialize i =0 check i<n
Step 4: if true, sum = sum + i, increment i = i + 1
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Print the value of Sum
Step 7: Stop the program

Program (To Print the sum of number series up to the given limit n)
#include <stdio.h>
int main() {
int i = 0, n, sum = 0;
printf("Enter the limit: ");
scanf("%d", &n);

// Calculate sum of the series from 0 to n


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

// Print the result


printf("Sum of series up to %d is %d\n", n, sum);
return 0;
}

Output: - 1

Enter the limit 10

Sum of series upto 5 is 15

Output: -2

Enter the limit 10

Sum of series upto 5 is 55

Result:
Thus a C program to print the sum of number series up to the given limit n was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No:3 Demonstration on Looping Statements


DATE:

(c)To print the even numbers up to given limit (using do..while loop)
Aim:

To write a C program to print the even numbers up to limit, n.

Algorithm:

Step 1: Start the program


Step 2: Read the limit, n, sum=0
Step 3: Using do while loop, initialize i =0 to i<n
Step 4: Print the i value and increment i = i + 2
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

Program (To Print the even numbers up to the given limit n)

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

// Prompt user for input


printf("Enter the limit: ");
scanf("%d", &n);

do {
i = i + 2;
sum = sum + i;
printf("%d\n", i);
} while (i <= n);

printf("Sum of series up to %d is %d\n", n, sum);

return 0;
}

Output: - 1

Enter the limit 15

2
4
6
8
10
12
14

Result:
Thus a C program to print the even numbers up to the given limit n was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 4 Demonstration on Arrays

(a) To print sum of array elements using one dimensional array


Aim:
To write a C program to print sum of array elements using one dimensional array

Algorithm:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using for loop, initialize i =0 to i<n
Step 4: Print the value n
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

Program (To Print sum of given array elements up to the given limit n)
#include <stdio.h>

int main() {
int a[5], i, n, sum = 0;

printf("Enter the limit (up to 5): ");


scanf("%d", &n);

if (n > 5 || n < 1) {
printf("Please enter a valid limit between 1 and 5.\n");
return 1; // Exit the program if the input is invalid
}

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


printf("Enter the number: ");
scanf("%d", &a[i]);
sum = sum + a[i]; // Add the entered number to sum
}

printf("Sum of the given array numbers is %d\n", sum);


return 0;
}

Output:-1

Enter the limit 5


Enter the number: 10
Enter the number: 20
Enter the number: 30
Enter the number: 40
Enter the number: 50
Sum of the given array numbers is 150

Result:

Thus a C program to print sum of given array elements up to the given limit n was compiled and
executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 4 Demonstration on Arrays


Date:

b) To demonstrate on Matrix Addittion using two dimensional array

Aim:
To write a C program for matrix Addition.
Algorithm:

Step 1: Start the program


Step 2: Enter the row and column of the matrix
Step 3: Enter the element of A matrix
Step 4: Enter the element of B matrix
Step 5: Read matrix values in a[i][j] & b[i]
Step 6: Print the A matrix in matrix form
Step 7: Print the B matrix in matrix form
Step 8: Set a loop up to the row and inner loop upto the column
Step 9: Add the elements of the matrix c[i][j]=a[i][j]+b[i][j]
Step 10: Set a loop to print matrix values in c[i][j]
Step 11: Stop the program

Program (for matrix addition)


#include <stdio.h>

void main() {
int a[25][25], b[25][25], c[25][25], i, j, m, n;

printf("Enter the number of rows and columns of two matrices:\n");


scanf("%d%d", &m, &n);
printf("\nEnter the elements of A matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}

printf("\nEnter the elements of B matrix:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}

printf("\nThe elements of A matrix:\n");


for (i = 0; i < m; i++) {
printf("\n");
for (j = 0; j < n; j++) {
printf("\t%d", a[i][j]);
}
}

printf("\nThe elements of B matrix:\n");


for (i = 0; i < m; i++) {
printf("\n");
for (j = 0; j < n; j++) {
lOMoARcPSD| 356 459 56

printf("\t%d", b[i][j]);
}
}

printf("\nThe addition of two matrices:\n");


for (i = 0; i < m; i++) {
printf("\n");
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
printf("\t%d", c[i][j]);
}
}

Output:

The elements of A matrix:


1 2 3
4 5 6

The elements of B matrix:


7 8 9
10 11 12

The addition of two matrices:


8 10 12
14 16 18

Result:
Thus a C program to implement matrix addition was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 4 Demonstration on Arrays


Date:

c) To print the position of elements stored in Multi-dimensional array

Aim:
To write a C program to print the position of elements stored in Multi-dimensional array.

Algorithm:

Step 1: Start the program


Step 2: Read the table, rows and columns
Step 3: Assign the values for the Employees [2][2][3]
Step 4: Using for loop, with tables, rows and columns
Step 5: print the each element with the position.
Step 6: Stop the program

Program ( For Multi Dimensional Array)

#include<stdio.h>
int main()
{
int tables, rows, columns;
int Employees[2][2][3] = { { {9, 99, 999}, {8, 88, 888} },
{ {225, 445, 665}, {333, 555, 777} }
};
for (tables = 0; tables < 2; tables++)
{
for (rows = 0; rows < 2; rows++)
{
for (columns =0; columns < 3; columns++)
{
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns,
Employees[tables][rows][columns]);
}
}
}
return 0;
}
lOMoARcPSD| 356 459 56

Output:

Employees [0][0][0] = 9
Employees [0][0][1] = 99
Employees [0][0][2] = 999
Employees [0][1][0] = 8
Employees [0][1][1] = 88
Employees [0][1][2] = 888
Employees [0][2][0] = 225
Employees [0][2][1] = 445
Employees [0][2][2] = 665
Employees [0][0][0] = 333
Employees [0][0][0] = 555
Employees [0][0][0] = 777

Result:

Thus a C program to print the position of elements stored in Multi dimensional array was compiled and
executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 4 Demonstration on Arrays

d) To traverse the one dimensional array (Reaching all the elements)

Aim:
To write a C program to demonstrate on the traversal of one dimensional array.

Algorithm:

Step 1: Start the program


Step 2: Read the number of elements
Step 3: Read one dimensional arrays with values
Step 4: Using for loop with variable i, check the condition i<n
Step 5: Print the element
Step 6: Repeat steps 4 & 5 till the loop ends
Step 7: Stop the program

Program (Traversing One Dimensional Array)


#include <stdio.h>

int main() {
int a[6] = {10, 20, 30, 40, 50, 60};
int i;

// Loop through each element of the array and print it


for (i = 0; i < 6; i++) {
printf("Element in position[%d] is %d\n", i + 1, a[i]);
}

return 0;
}

Output:

Element in position[1] is 10
Element in position[2] is 20
Element in position[3] is 30
Element in position[4] is 40
Element in position[5] is 50
Element in position[6] is 60

Result:
Thus a C program to traverse the one dimensional array was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No:5 Demonstration on Operation of Strings using Built in Functions


Date:

a) To find the length of the given string

Aim:
To write a C program to find the length of the given string.
Algorithm:
Step 1: Start the program
Step 2: Read the string
Step 3: Using the builtin function strlen(), find the length.
Step 4: Print the length of the given string
Step 5: Stop the program

Program
#include <stdio.h>
#include <string.h> // For strlen function

int main() {
char str1[20];
int len;

printf("Enter the string: ");


scanf("%s", str1); // No need for '&' with a string variable
len = strlen(str1); // Use strlen to find the length of the string

printf("Length of the given string \"%s\" is %d\n", str1, len);


return 0;
}

Output:

Enter the string: vimalraj

Length of the given string vimalraj is 8

Result:
Thus a C program to find the length of the given string was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 5 Demonstration on Operation of Strings using Built in Functions


Date:

b) To copy from one string to another string

Aim:
To write a C program to copy the one string to another string.

Algorithm:
Step 1: Start the program
Step 2: Read the string
Step 3: Using the builtin function, strcpy(), copy to the new string
Step 4: print the new string
Step 5: Stop the program

Program
#include<stdio.h>
#include<string.h> // For strcpy function

int main() {
char str1[20], str2[20];

// Input a string
printf("Enter the string: ");
scanf("%s", str1); // No need for '&' with a string variable

// Copy string str1 to str2


strcpy(str2, str1);

// Output the copied string


printf("Copied New String is: %s\n", str2);

return 0;
}

Output:

Enter the string: vimal Copied

New String is vimal

Result:

Thus a C program to copy from one string to another string was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 5 Demonstration on Operation of Strings using Built in Functions


Date:
c) concatenate two string (join)

Aim:
To write a C program to concatenate two strings.

Algorithm:

Step 1: Start the program


Step 2: Read two strings separately.
Step 3: Using the builtin function, strcat(), join the strings
Step 4: print the concatenated string
Step 5: Stop the program

Program
#include <stdio.h>
#include <string.h>
int main() {
char str1[40], str2[20];
printf("Enter the string1: ");
scanf("%s", str1);

printf("Enter the string2: ");


scanf("%s", str2);
strcat(str1, str2);
printf("Concatenated New String is: %s\n", str1);
return 0;
}

Output:

Enter the string1: Vimalraj

Enter the string2: Raja

Copied New String is VimalrajRaja

Result:

Thus a C program to concatenate two strings was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 5 Demonstration on Operation of Strings using Built in Functions


Date:

d) To compare two strings are same

Aim:
To write a C program to compare two strings are same or not.

Algorithm:

Step 1: Start the program


Step 2: Read two strings separately.
Step 3: Using the builtin function, strcmp(), compare the strings
Step 4: if ascii value is 0, print it is same
Step 5: Else, print not same
Step 5: Stop the program

Program
#include <stdio.h>
#include <string.h>

int main() {
char str1[20], str2[20];
int comp;
printf("Enter the string1: ");
scanf("%s", str1);
printf("Enter the string2: ");
scanf("%s", str2);
comp = strcmp(str1, str2);
if (comp == 0) {
printf("Two strings are the same\n");
} else {
printf("Two strings are not the same\n");
}

return 0;
}
Output-1
Enter the string1: Vimal
Enter the string2: Vimal
Two strings are same
Output-2
Enter the string1: Vimal
Enter the string2: Kamal
Two strings are not same

Result:

Thus a C program to compare two strings to check same or not was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 5 Demonstration on Operation of Strings using Built in Functions


Date:
e) To reverse the given string

Aim:
To write a C program to reverse the given string.

Algorithm:

Step 1: Start the program


Step 2: Read the string
Step 3: Using the builtin function strrev()
Step 4: Print the reverse of the given string
Step 5: Stop the program

Program
#include<stdio.h>
#include<string.h>

int main() {
char str1[20], rec[20];
int len, i, j;

printf("Enter the string: ");


scanf("%s", str1); // No need for '&' with a string variable

len = strlen(str1);
for(i = 0, j = len - 1; j >= 0; i++, j--) {
rec[i] = str1[j];
}
rec[i] = '\0';

printf("Reverse of the given string is %s\n", rec);

return 0;
}

Output

Enter the string: vimalraj

Reverse of the given string is jarlamiv

Result:
Thus a C program to find the reverse of the given string was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 6 FUNCTIONS – Function call


Date:

(a) To search an element using linear search

Aim:
To write a C program to search an element in a given array using linear search.

Algorithm:

Step 1: Start the program


Step 2: Read no. of elements and enter the values.
Step 3: Read the searching element.
Step 4: Call the function, linear()
Step 5: Stop the program
Function:
Step 4.1: Read i, and flag=0
Step 4.2: Using for loop, check the condition if x==a[i]
Step 4.3: if true, flag=1, break and print element is found
Step 4.4: Else, print element is not found.

Program
#include<stdio.h>
void linear(int a[], int n, int x);
int main() {
int i, n, x, a[20];
printf("Enter how many elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the element to search: ");
scanf("%d", &x);

linear(a, n, x); // Function call


}

void linear(int a[], int n, int x) {


int i, flag = 0;
for (i = 0; i < n; i++) {
if (x == a[i]) {
flag = 1;
break;
}
}

if (flag == 1) {
printf("\nElement %d is found at position %d\n", x, i + 1);
} else {
printf("\nThe element is not found in the list\n");
}
}
lOMoARcPSD| 356 459 56

OUTPUT-1:

Enter how many elements: 8


Enter the elements: 50
20
60
30
10
80
90
70

Enter the element to search: 30 Element

30 is found in the position 4

OUTPUT-2:

Enter how many elements: 8


Enter the elements: 50
20
60
30
10
80
90
70

Enter the element to search: 100 The

Element Is Not Found

Result:
Thus a C program to search an element using linear search was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 6 FUNCTIONS – Return Value


Date:
(b) To find square of a number

Aim:
To write a C program to find square of a given number.

Algorithm:

Step 1: Start the program


Step 2: Read the number.
Step 3: Pass the value to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Return the value

Program
#include<stdio.h>
int square(int); // Function prototype
int main() {
int sq, n;
printf("Enter the number: "); // Corrected quote characters and extra quotes
scanf("%d", &n);
sq = square(n); // Function Call
printf("Square of %d is %d\n", n, sq);

return 0; // Added return statement for main


}

int square(int n) {
return n * n;
}
Output
Enter the number: 9

Square of 9 is 81

Result:
Thus a C program to find square of a number was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 6 FUNCTIONS – Pass by Value


Date:
(c) To find cube of a given number

Aim:
To write a C program to find cube for a given number using pass by value.

Algorithm:

Step 1: Start the program


Step 2: Read the number.
Step 3: Pass the value to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Return the value

Program
#include<stdio.h>
int cube(int);
int main() {
int c, n;
printf("Enter the number: ");
scanf("%d", &n);
c = cube(n);
printf("Cube of %d is %d\n", n, c);
return 0;
}
int cube(int n) {
return n * n * n; // Function to calculate cube
}
Output

Enter the number: 5 Cube

of 5 is 125

Result:
Thus a C program to find cube of a number using pass by value was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 6 FUNCTIONS – Pass by reference


Date:
(d) To add two numbers

Aim:
To write a C program to add two numbers using pass by reference

Algorithm:
Step 1: Start the program
Step 2: Read the two values.
Step 3: Call and Pass the values to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Add the two values
Step 3.3: Return the values

Program
#include<stdio.h>
int add(int *x, int *y);
int main() {
int a, b, c;
printf("Enter the two numbers: ");
scanf("%d%d", &a, &b);
c = add(&a, &b);
printf("Addition is %d\n", c);

return 0;
}
int add(int *x, int *y) {
int z;
z = *x + *y;
return z;
}

Output
Enter the two numbers: 5 10

Addition is 15

Result:
Thus a C program to add two numbers using pass by reference was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 6 FUNCTIONS – Passing Arrays to function

(e) To sort the numbers using bubble sort

Aim:
To write a C program to sort the numbers in ascending order using bubble sort.

Algorithm:

Step 1: Start the program


Step 2: Read no. of elements and enter the values.
Step 3: Call the function, bubble() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the array elements
Step 4.2: Using for loop in i, j, check the condition if a[i] > a[j]
Step 4.3: if true, swap the elements
Step 4.4: return the elements after the loop.

Program
#include<stdio.h>

void bubble(int a[], int n);

int a[25], i, j, n, temp;

int main() {
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter the elements: ");


for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

bubble(a, n); // Function call

printf("The sorted elements are: ");


for(i = 0; i < n; i++) {
printf("%d ", a[i]); // Print sorted array elements in a single line
}
printf("\n");

return 0;
}

void bubble(int a[], int n) {


for(i = 0; i < n - 1; i++) { // Outer loop iterates n-1 times
for(j = 0; j < n - i - 1; j++) { // Inner loop reduces the comparison range
if(a[j] > a[j + 1]) { // Swap adjacent elements if they are in the wrong order
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
lOMoARcPSD| 356 459 56

}
}

Output

Enter no. of elements: 10

Enter the elements:


50
20
30
10
100
80
90
70
40
60

The sorted elements are:


10
20
30
40
50
60
70
80
90
100

Result:
Thus a C program to sort the numbers in ascending order using bubble sort was compiled and
executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 7 FUNCTIONS – Recursion


Date:

To find factorial of a given number using recursion

Aim:
To write a C program to find factorial of a given number using recursion.

Algorithm:

Step 1: Start the program


Step 2: Read the number
Step 3: Call the function, fact() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the value
Step 4.2: Using recursive function, calculate the value
Step 4.3: return the value after the recursive condition.

Program
#include<stdio.h>
int fact(int n); // Function prototype

int main() {
int n, f;
printf("\nEnter a number: ");
scanf("%d", &n);
f = fact(n);
printf("\nThe factorial value of %d is %d\n", n, f);

return 0;
}

int fact(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * fact(n - 1); // Recursive case: n * factorial of (n-1)
}
}

OUTPUT

Enter a number:6

The factorial value of 6 is 720

Result:
Thus a C program to find factorial of a given number using recursion was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 8 POINTERS – Passing pointers to functions


Date:
(a) To swap two numbers with passing pointers to functions

Aim:

To write a C program to swap two numbers with passing pointers to functions

Algorithm:

Step 1: Start the program


Step 2: Read the two values.
Step 3: Call and Pass the values to the function using address
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Swap the values
Step 3.3: Return the values

Program
#include<stdio.h>
int swap(int *x, int *y); // Function prototype
int main() {
int a, b;
printf("Enter the two numbers: "); // Corrected quotes
scanf("%d%d", &a, &b)
swap(&a, &b); // Function call to swap values
printf("Now A value is %d\n", a); // Added newline for clearer output
printf("Now B value is %d\n", b); // Added newline for clearer output
return 0; // Return statement for main
}
int swap(int *x, int *y) {
int z;
z = *x; // Save the value of *x in z
*x = *y; // Assign the value of *y to *x
*y = z; // Assign the value of z (old *x) to *y
}
Output
Enter the two numbers: 5 10
Now A value is 10
Now B value is 20

Result:
Thus a C program to swap two numbers with passing pointers to functions was compiled and
executed successfully.
lOMoARcPSD| 356 459 56

Ex.No: 8 POINTERS – Passing pointers to arrays


Date:

b) Printing the array values using pointers

Aim:

To write a C program to print the array values using pointers (Address)

Algorithm:

Step 1: Start the program


Step 2: Initialize array values and declare a pointer variable.
Step 3: Assign array variable to the pointer variable.
Step 4: Using for loop, access the elements with help of pointers
Step 5: Print the value
Step 6: Stop the program

Program
#include <stdio.h>

int main() {
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p; // Pointer to double
int i;

p = balance; // Correct assignment to point to the first element of balance

/* output each array element's value */


printf("Array values using pointer\n");
for (i = 0; i < 5; i++) {
printf("*(p + %d) : %f\n", i, *(p + i)); // Accessing values using pointer
}

printf("Array values using balance as address\n");


for (i = 0; i < 5; i++) {
printf("*(balance + %d) : %f\n", i, *(balance + i)); // Accessing values using array name
}

return 0;
}
lOMoARcPSD| 356 459 56

Output

Array values using pointer


*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000

Array values using balance as address


*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000

Result:
Thus a C program to print the array values using pointers was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 8 POINTERS – Passing pointers to strings


Date:
c) To print the string using pointers

Aim:

To write a C program to print the string using pointers.

Algorithm:

Step 1: Start the program


Step 2: Declare and initialize the string.
Step 3: Assign string variable to the pointer variable.
Step 4: Print the normal string variable
Step 5: Print the string with string pointer
Step 6: Stop the program

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

int main() {
char Name[] = "Dinesh";
char* ptrname = Name;

system("cls");

printf("Name = %s\n", Name);

// Printing Name via pointer


printf("Name via ptrname = %s\n", ptrname);

return 0;
}

Output

Name = Dinesh
Name via ptrname = Dinesh

Result:
Thus a C program to print the string using pointers was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 8 POINTERS – Passing pointers to pointers


Date:
d) To print the values using pointers to pointers

Aim:

To write a C program to print the values using pointers to pointers

Algorithm:

Step 1: Start the program


Step 2: Declare pointers with * and **
Step 3: Intialize a value with a variable
Step 4: Assign pointer address for the variable.
Step 5: Print the values with pointer with pointers
Step 6: Stop the program

Program
#include <stdio.h>
int main() {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
printf("Value of var = %d\n", var);
printf("Value available at *ptr = %d\n", *ptr);
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}

Output
Value of var = 3000
Value available at *ptr = 3000 Value
available at **pptr = 3000

Result:
Thus a C program to print the values using pointers to pointers was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No: 8 POINTERS – Array of Pointers


Date:
e) To print the values using array of pointers

Aim:

To write a C program to print the values using array of pointers

Algorithm:

Step 1: Start the program


Step 2: Declare pointer variable and variable with one dimensional values
Step 3: Initialize a value for one dimensional array
Step 4: Using loop assign the address for the array values
Step 5: Using loop print the values of the array.
Step 6: Stop the program

Program
#include <stdio.h>
const int MAX = 3;

int main() {
int var[] = {10, 100, 200};
int i, *ptr[MAX];

// Assign addresses of var elements to ptr array


for (i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // Point ptr[i] to the address of var[i]
}
for (i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i]);
}

return 0; // Successful program termination


}
Output

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

Result:
Thus a C program to print the values using array of pointers was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 9 STRUCTURE – Nested Structure


Date:

(a) To print student details using nested structure

Aim:
To write a C program to print the student details with help of nested structure.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare another structure with members joined with old structure
Step 5. Read the values for new structure
Step 6: Print the values using respective structure variables.
Step 5: Stop the program

Program
#include <stdio.h>

// Define the structure for student details


struct student {
int rno;
char name[25];
char dept[10];
};

// Define the structure for address details, which contains a student structure
struct address {
int door;
char area[25];
double pincode;
struct student s;
}
a = {10, "Tiruttani", 631209, 1001, "Vimal", "CSE"};

int main() {
// Printing details of the nested structure
printf("Name = %s\n", a.s.name);
printf("Roll number = %d\n", a.s.rno);
printf("Department = %s\n", a.s.dept);
printf("Door No = %d\n", a.door);
printf("Area = %s\n", a.area);
printf("Pincode = %.0lf\n", a.pincode); // Correct format specifier for double

return 0;
}
lOMoARcPSD| 356 459 56

Output-1:
Name=Vimal
Roll number=1001
Department=CSE
Door No=10
Area=Tiruttani
Pincode=631209

Result:
Thus a C program to print student details using nested structure was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 9 STRUCTURE – Pointer to Structure


Date:
b) To print book details using nested structure

Aim:
To write a C program to print the book details with help of pointers to structure.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare structure pointer and assign the address of structure variable.
Step 5. Print the values using member selection operator 
Step 6: Stop the program

Program:
#include <stdio.h>
struct book {
char bname[20];
char auname[20];
float price;
int pages;
char publisher[20];
int pubyear;
};
struct book b = {"Pro C", "Kanetkar", 590.50, 696, "McGraw Hill", 2008};
int main() {
struct book *p;
p = &b;
printf("Book Name = %s\n", p->bname);
printf("Author Name = %s\n", p->auname);
printf("Book Price = %.2f\n", p->price); // Formatting the price to 2 decimal places
printf("Book Pages = %d\n", p->pages);
printf("Book Publisher = %s\n", p->publisher);
printf("Published Year = %d\n", p->pubyear);
return 0;
}

Output :

Book Name=Pro C Author


Nanme=Kanetkar Book
Price=590.500000 Book
Pages=696
Book Publisher=Mcgraw Hill
Published Year=2008

Result:
Thus a C program to print book details using pointer to structure was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 9 STRUCTURE – Arrays with Structure


Date:

(c) To generate EB bill using Arrays with structures

Aim:
To write a C program to generate EB bill using arrays with structure.
Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Declare structure variable with array with size.
Step 4: Read for no. of consumers, n
Step 5: Read the values for the members using structure variable using loop.
Step 6: Print the values using member selection operator 
Step 7: Stop the program

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

struct eb {
char consumer[20];
int consumerid;
int curread;
int prevread;
int totread;
float price;
} b[10];

int main() {
int i, n;

printf("Enter how many consumers: ");


scanf("%d", &n);

printf("\nEnter the details of the consumers:\n");

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


printf("\nEnter the consumer name: ");
scanf("%19s", b[i].consumer); // Prevent buffer overflow

printf("Enter the consumer ID: ");


scanf("%d", &b[i].consumerid);

printf("Enter the current reading: ");


scanf("%d", &b[i].curread);

printf("Enter the previous reading: ");


scanf("%d", &b[i].prevread);

b[i].totread = b[i].curread - b[i].prevread;


b[i].price = b[i].totread * 6 + 50; // Assuming cost per unit is 6 and base charge is 50
}
lOMoARcPSD| 356 459 56

printf("\nElectricity Bill Details are:\n");


for (i = 0; i < n; i++) {
printf("\nConsumer Name = %s", b[i].consumer);
printf("\nConsumer ID = %d", b[i].consumerid);
printf("\nCurrent Reading = %d", b[i].curread);
printf("\nPrevious Reading = %d", b[i].prevread);
printf("\nConsumed Units = %d", b[i].totread);
printf("\nTotal Bill = %.2f", b[i].price); // Displaying with 2 decimal places
printf("\n");
}

return 0;
}

Output :
Enter how many consumers:2

Enter the details of the consumers


Enter the consumer name:vimal
Enter the consumer id:1001
Enter the current reading:5234
Enter the previous reading:3254

Enter the consumer name:Kamal


Enter the consumer id:1002
Enter the current reading:6541
Enter the previous reading:4521

Electricity Bill Details are:


Consumer name=vimal
Consumer ID = 1001
Current Reading=5234
Previous Reading=3254
Consumed Units = 1980
Total Bill = 11930.000000

Consumer name=Kamal
Consumer ID = 1002
Current Reading=6541
Previous Reading=4521
Consumed Units = 2020
Total Bill = 12170.000000

Result:
Thus a C program to generate eb bill using arrays of structure was compiled and executed
successfully.
lOMoARcPSD| 356 459 56

Ex.No. 9 UNIONS
Date:
d) Demonstration on Union

Aim:
To write a C program to print the mark sheet of a student using union.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare structure pointer and assign the address of structure variable.
Step 5. Print the values using member selection operator 
Step 6: Stop the program

Program
#include <stdio.h>
union book {
char bname[20];
char auname[20];
float price;
} b;
int main() {
printf("Enter the Book Name: ");
scanf("%19s", b.bname); // Limit input to 19 characters
printf("Book Name = %s\n", b.bname);
printf("Enter the Author Name: ");
scanf("%19s", b.auname); // Limiting the input size
printf("Author Name = %s\n", b.auname);
printf("Enter the Book Price: ");
scanf("%f", &b.price); // Correct usage with '&'
printf("Book Price = %.2f\n", b.price); // Formatting with 2 decimal places
return 0;
}

Output :

Enter the Book Name: Pro C


Book Name=Pro C
Enter the Author Name: Kanetkar Author
Nanme=Kanetkar
Enter the Book Price:590.50
Book Price=590.500000

Result:
Thus a C program to print book details using union was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 10 FILES
Date:
a) Reading from a file

Aim:
To write a C program to read the content from the existing file.

Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “r”.
Step 4: Using fgetc(), read the each character from a file upto EOF.
Step 5: Print the content of the file
Step 6: Stop the program

Program
#include <stdio.h>
int main()
{ sample.txt
char ch;
FILE *fp; CS3271 PROGRAMMING IN C LABORATORY
fp=fopen(“sample.txt”. “r”);
if (fp = = NULL)
{
printf(“File does not exist”);
return 1;
}
while ((ch = getc(fp)) != EOF) {
printf("%c", ch);
}

fclose(fp);
return 0;
}

Output :

CS3271 ProgramMING IN C LABORATORY

Result:
Thus a C program to read the content of the existing file was compiled and executed
Successfully.
lOMoARcPSD| 356 459 56

Ex.No. 10 FILES
Date:

b) Writing into a file

Aim:
To write a C program to write the content into a file.
Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w”.
Step 4: Using fputc(), write the each character and press ctrl z to stop.
Step 5: Print the content of the text into the file.
Step 6: Stop the program

Program
#include <stdio.h>

int main() {
char ch;
FILE *fp;

fp = fopen("sample.txt", "w");
if (fp == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Enter the text (press Ctrl + Z to stop):\n");
while ((ch = getchar()) != EOF) {
fputc(ch, fp);
}

fclose(fp);
printf("\nText has been written to sample.txt successfully.\n");

return 0;
}

Output :

Enter the text (press Ctrl + Z to stop):


Hello, this is a sample text.
Ctrl + Z (Windows) or Ctrl + D (Linux/macOS) to stop.

Text has been written to sample.txt successfully.

Result:
Thus a C program to write the content in to the file was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 10 FILES
Date:
(b) Demonstration on File Pointer

Aim:
To write a C program to demonstrate on usage of file pointers.

Algorithm:

Step 1: Start the program


Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w”.
Step 4: Using fprintf(), write the data in to the file.
Step 5: Close the file pointer
Step 6: Stop the program

Program
#include <stdio.h>
int main() {
FILE *fp;
char name[20];
int regno;
char place[20];
fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Enter your Name, Reg No & Place: ");
scanf("%19s %d %19s", name, &regno, place);
fprintf(fp, "%s %d %s", name, regno, place);
printf("\nData written successfully");
fclose(fp);
return 0;
}

Output :
Enter your Name, Reg No & Place: Vimal 1001 Tiruttani

Data written successfully

example.txt

Vimal 1001 Tiruttani

Result:
Thus a C program to demonstration on file pointer was compiled and executed successfully.
lOMoARcPSD| 356 459 56

Ex.No. 10 FILES
Date:
b) Demonstration on File Operations

Aim:
To write a C program to demonstrate on usage of file operations (read, write & append)

Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Display menu for different file operations.
Step 4: Based on the option, open the file in specified mode using file pointer.
Step 5: Perform the operation on file pointer.
Step 6: Close the file pointer
Step 7: Stop the program.

Program:

#include <stdio.h>
int main() {
FILE *fp;
int regno, n;
char fname[20], name[40];

printf("FILE OPERATIONS\n");
printf("1. Read Operation\n");
printf("2. Write Operation\n");
printf("3. Append Operation\n");
printf("Choose any one of the options: ");
scanf("%d", &n);

switch (n) {
case 1:
printf("Enter the file name to read: ");
scanf("%19s", fname);

fp = fopen(fname, "r");
if (fp == NULL) {
printf("Error opening the file.\n");
break;
}

fscanf(fp, "%39s %d", name, &regno);


printf("%s %d\n", name, regno);
fclose(fp);
break;

case 2:
printf("Enter the file name to write: ");
scanf("%19s", fname);

fp = fopen(fname, "w");
if (fp == NULL) {
lOMoARcPSD| 356 459 56

printf("Error opening the file.\n");


break;
}

fprintf(fp, "Programming in C");


printf("Content written successfully\n");
fclose(fp);
break;

case 3:
printf("Enter the file name to write: ");
scanf("%19s", fname);

fp = fopen(fname, "a"); // Appends to the file


if (fp == NULL) {

printf("Error opening the file.\n");


break;
}

fprintf(fp, "\nProblem solving and Python programming");


printf("Content written successfully\n");
fclose(fp);
break;

default:
printf("Enter a correct choice.\n");
break;
}

return 0;
}
lOMoARcPSD| 356 459 56

Output-1

FILE OPERATIONS
1. Read Operation
2. Write Operation
3. Append Operation
Choose any one of the options: 1
Enter the file name to read: example.txt
Vimalraj 1001

Output-2

FILE OPERATIONS
1. Read Operation
2. Write Operation
3. Append Operation
Choose any one of the options: 2
Enter the file name to write: kamal.txt
Content written successfully

Output-3

FILE OPERATIONS
1. Read Operation
2. Write Operation
3. Append Operation
Choose any one of the options: 3
Enter the file name to write: kamal.txt
Content written successfully

Result:

Thus a C program to demonstrate on file operations was compiled and executed successfully
lOMoARcPSD| 356 459 56

Ex.No. 9 FILES

c) Demonstration on Random Access

Aim:
To write a C program to demonstrate on random access in a file.

Algorithm:

Step 1: Start the program


Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w+”.
Step 4: Using random access functions, fseek(), ftell(), rewind(), perform the operations
Step 5: Move the file pointer using Postion
Step 6: Print the position of the pointer.
Step 7: Close the file pointer.
Step 8: Stop the program.

Program:
#include <stdio.h>
int main() {
FILE *fp;
int c;
fp = fopen("file.txt", "w+");
if (fp == NULL) {
printf("Error opening the file.\n");
return 1;
}
fputs("This is study.com", fp);
fseek(fp, 7, SEEK_SET);
fputs(" C Programming", fp);
printf("The current position of the file pointer is: %ld\n", ftell(fp));
rewind(fp);
printf("The current position of the file pointer is: %ld\n", ftell(fp));
while (1) {
c = fgetc(fp);
if (feof(fp)) {
break;
}
printf("%c", c);
}

fclose(fp);
return 0;
}
lOMoARcPSD| 356 459 56

Output:

The current position of the file pointer is: 21


The current position of the file pointer is: 0
This is C Programming

Explanation:

Functions can be used to handle file operations only when you send the file pointer as a parameter to the
function. You can also send the file name as a parameter and handle the operations inside the function. The
common practice is to send the file pointer to a function for a specific purpose. This example has been
modified to use a function for displaying the contents of the file by passing the file pointer.

Result:

Thus to write a C program to perform the demonstration on Random file access was compiled and
executed successfully.
lOMoARcPSD| 356 459 56

Ex.No.10 FILES
Date:
d) Preprocessor Directives

Aim:
To write a C program to demonstrate on preprocessor directives

Algorithm:

Step 1: Start the program


Step 2: Define a macro
Step 3: Define a macro in another macro
Step 4: Use the macros inside the program
Step 5: Perform the manipulations
Step 6: Print the result
Step 7: Stop the program

Program:
#include <stdio.h>

#define A 10
#define B (A + 30) // Added parentheses for proper evaluation
#define C (A + B) // Added parentheses for proper evaluation

int main() {
int result;
result = A + B + C; // Calculation with macros
printf("Result = %d", result);

return 0;
}

Output :

Result = 90

Result:
Thus a C program to demonstration on preprocessor directives was compiled and executed
successfully.

You might also like