C LAB
C LAB
Aim:
To write a C program to find area and circumference of a circle.
Algorithm:
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
Aim:
Algorithm:
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
Result:
Thus the C program to swap two numbers without using temporary variable has been created
successfully and verified.
lOMoARcPSD| 356 459 56
Aim:
Algorithm:
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
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:
Result:
Thus the C program to find largest of three numbers has been created successfully and verified.
lOMoARcPSD| 356 459 56
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
Aim:
To write a C program for demonstrating arithmetic operations using switch case statement.
Algorithm:
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
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
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.
#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:
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
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
(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:
int main() {
int i, n;
printf("Enter the limit: ");
scanf("%d", &n);
return 0;
}
Output:
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:
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);
Output: - 1
Output: -2
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
(c)To print the even numbers up to given limit (using do..while loop)
Aim:
Algorithm:
#include <stdio.h>
int main() {
int i = 0, n, sum = 0;
do {
i = i + 2;
sum = sum + i;
printf("%d\n", i);
} while (i <= n);
return 0;
}
Output: - 1
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
Algorithm:
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;
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
}
Output:-1
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
Aim:
To write a C program for matrix Addition.
Algorithm:
void main() {
int a[25][25], b[25][25], c[25][25], i, j, m, n;
printf("\t%d", b[i][j]);
}
}
Output:
Result:
Thus a C program to implement matrix addition was compiled and executed successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to print the position of elements stored in Multi-dimensional array.
Algorithm:
#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
Aim:
To write a C program to demonstrate on the traversal of one dimensional array.
Algorithm:
int main() {
int a[6] = {10, 20, 30, 40, 50, 60};
int 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
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;
Output:
Result:
Thus a C program to find the length of the given string was compiled and executed successfully.
lOMoARcPSD| 356 459 56
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
return 0;
}
Output:
Result:
Thus a C program to copy from one string to another string was compiled and executed successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to concatenate two strings.
Algorithm:
Program
#include <stdio.h>
#include <string.h>
int main() {
char str1[40], str2[20];
printf("Enter the string1: ");
scanf("%s", str1);
Output:
Result:
Thus a C program to concatenate two strings was compiled and executed successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to compare two strings are same or not.
Algorithm:
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
Aim:
To write a C program to reverse the given string.
Algorithm:
Program
#include<stdio.h>
#include<string.h>
int main() {
char str1[20], rec[20];
int len, i, j;
len = strlen(str1);
for(i = 0, j = len - 1; j >= 0; i++, j--) {
rec[i] = str1[j];
}
rec[i] = '\0';
return 0;
}
Output
Result:
Thus a C program to find the reverse of the given string was compiled and executed successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to search an element in a given array using linear search.
Algorithm:
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);
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:
OUTPUT-2:
Result:
Thus a C program to search an element using linear search was compiled and executed
successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to find square of a given number.
Algorithm:
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);
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
Aim:
To write a C program to find cube for a given number using pass by value.
Algorithm:
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
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
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
Aim:
To write a C program to sort the numbers in ascending order using bubble sort.
Algorithm:
Program
#include<stdio.h>
int main() {
printf("Enter number of elements: ");
scanf("%d", &n);
return 0;
}
}
}
Output
Result:
Thus a C program to sort the numbers in ascending order using bubble sort was compiled and
executed successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to find factorial of a given number using recursion.
Algorithm:
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
Result:
Thus a C program to find factorial of a given number using recursion was compiled and executed
successfully.
lOMoARcPSD| 356 459 56
Aim:
Algorithm:
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
Aim:
Algorithm:
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;
return 0;
}
lOMoARcPSD| 356 459 56
Output
Result:
Thus a C program to print the array values using pointers was compiled and executed
successfully.
lOMoARcPSD| 356 459 56
Aim:
Algorithm:
Program
#include<stdio.h>
#include<stdlib.h>
int main() {
char Name[] = "Dinesh";
char* ptrname = Name;
system("cls");
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
Aim:
Algorithm:
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
Aim:
Algorithm:
Program
#include <stdio.h>
const int MAX = 3;
int main() {
int var[] = {10, 100, 200};
int i, *ptr[MAX];
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
Aim:
To write a C program to print the student details with help of nested structure.
Algorithm:
Program
#include <stdio.h>
// 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
Aim:
To write a C program to print the book details with help of pointers to structure.
Algorithm:
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 :
Result:
Thus a C program to print book details using pointer to structure was compiled and executed
successfully.
lOMoARcPSD| 356 459 56
Aim:
To write a C program to generate EB bill using arrays with structure.
Algorithm:
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;
return 0;
}
Output :
Enter how many consumers:2
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:
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 :
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 :
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:
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 :
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:
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, ®no, 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
example.txt
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;
}
case 2:
printf("Enter the file name to write: ");
scanf("%19s", fname);
fp = fopen(fname, "w");
if (fp == NULL) {
lOMoARcPSD| 356 459 56
case 3:
printf("Enter the file name to write: ");
scanf("%19s", fname);
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
Aim:
To write a C program to demonstrate on random access in a file.
Algorithm:
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:
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:
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.