CS3271 - PROGRAMMING IN C - RECORD (2) (1)
CS3271 - PROGRAMMING IN C - RECORD (2) (1)
INDEX
PAGE MARKS MARKS
Ex.No DATE DESCRIPTION OF THE CONTENT ALLOTTED OBTAINED SIGNATURE
NO.
1A Program to get student details 1 10
Program to implement Arithmetic
1B Operators, Relational Operators and 3 10
Bitwise Operators
2A Greatest of 3 Numbers 5 10
4B Matrix Multiplication 13 10
5A String Concatenation 15 10
5B Reverse a String 17 10
7A Factorial of a Number 22 10
7B Fibonacci Series 24 10
9D Implementation of Union 37 10
Observation (5)
Record (5)
Total (10)
Ex.No: 1 PROGRAM TO GET STUDENT DETAILS
DATE:
AIM
To write a C program for getting student details.
ALGORITHM
Step 1: Start the program.
Step 2: Declare the variables.
Step3: Input the details of students using printf & scanf.
Step 4: Display the student details.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
int main()
{
char name[30], dept[20];
int rollno;
float cutoff;
printf("\nEnter the name of the student:");
scanf("%s",name);
printf("\nEnter the Roll Number:");
scanf("%d",&rollno);
printf("\nEnter the Department:");
scanf("%s",dept);
printf("\nEnter the cutoff:");
scanf("%f",&cutoff);
printf("\n===============");
printf("\nSTUDENT DETAILS");
printf("\n===============");
printf("\nName:%s",name);
printf("\nRoll No:%d",rollno);
printf("\nDepartment:%s",dept);
printf("\nCut-off:%.2f",cutoff);
return 0;
}
1
OUTPUT
RESULT
Thus, C program to get student details was written, executed and output is verified
successfully.
2
Ex.No:1B PROGRAM TO IMPLEMENT ARITHMETIC OPERATORS,
DATE: RELATIONAL OPERATORS AND BITWISE OPERATORS
AIM
To write a C program for implementing Arithmetic operators, Relational Operators and
Bitwise Operators.
ALGORITHM
Step 1: Start the program.
Step 2: Declare and initialize values to the variables.
Step 3: Display the result of arithmetic operations.
Step 4: Display the result of relational operations.
Step 5: Display the result of bitwise operations.
Step 6: Stop the program.
PROGRAM
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
int x = 5, y = 5, z = 10;
c = a+b;
printf("ARITHMETIC OPERATIONS");
printf("\n=====================================\n");
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
printf("=====================================\n");
printf("\nRELATIONAL OPERATIONS");
printf("\n======================\n");
printf("%d == %d is %d \n", x, y, x == y);
3
printf("%d > %d is %d \n", x, z, x > z);
printf("%d < %d is %d \n", x, y, x < y);
printf("%d != %d is %d \n", x, y, x != y);
printf("%d >= %d is %d \n", x, y, x >= y);
printf("%d <= %d is %d \n", x, z, x <= z);
printf("======================\n");
printf("\nBITWISE OPERATIONS");
printf("\n====================\n");
printf("a&b = %d", a&b);
printf("\na|b = %d", a|b);
printf("\na^b = %d", a^b);
printf("\na<<b = %d", a<<b);
printf("\na>>b = %d", a>>b);
printf("\n====================\n");
return 0;
}
OUTPUT
RESULT
Thus, C program to implement arithmetic, relation and bitwise operators was written,
executed and output is verified successfully.
4
Ex.No:2A GREATEST OF 3 NUMBERS
DATE:
AIM
To write a C program for finding greatest of 3 numbers.
ALGORITHM
Step 1: Start the program.
Step 2: Declare 3 variables and get the values from user.
Step 3: Using if – else if statement find the greatest value and display it.
Step 4: Stop the program.
PROGRAM
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
return 0;
}
OUTPUT
RESULT
Thus, C program to find greatest of 3 numbers was written, executed and output is
verified successfully.
5
Ex.No:2B MENU DRIVEN CALCULATOR
DATE:
AIM
To write a C program for implementing menu-driven calculator.
ALGORITHM
Step 1: Start the program.
Step 2: Input 2 values from user.
Step 3: Display the menu and get the choice from user.
Step 4: Using Switch Case, based on the choice perform the operation and display the
result.
Step 5: Stop the program.
PROGRAM:
#include <stdio.h>
int main()
{
int num1,num2,opt;
printf("Enter the first Integer :");
scanf("%d",&num1);
printf("Enter the second Integer :");
scanf("%d",&num2);
printf("\nInput your option :\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("The Addition of %d and %d is: %d\n",num1,num2,num1+num2);
break;
case 2:
printf("The Substraction of %d and %d is: %d\n",num1,num2,num1-num2);
break;
case 3:
printf("The Multiplication of %d and %d is: %d\n",num1,num2,num1*num2);
break;
6
case 4:
if(num2==0) {
printf("The second integer is zero. Devide by zero.\n");
} else {
printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2);
}
break;
case 5:
break;
default:
printf("Input correct option\n");
break;
}
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Menu-Driven Calculator was written, executed and
output is verified successfully.
7
Ex.No:3A DISPLAY FIRST ‘N’ NATURAL NUMBERS USING FOR LOOP
DATE:
AIM
To write a C program for displaying first ‘n’ natural numbers using for loop.
ALGORITHM
Step 1: Start the program.
Step 2: Input the limit.
Step 3: Using for loop display the values till the limit.
Step 4: Stop the program.
PROGRAM
#include <stdio.h>
int main()
{
int i, n;
printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d : \n", n);
for(i=1; i<=n; i++)
{
printf("%d\t", i);
}
return 0;
}
OUTPUT
RESULT
Thus, C program to display ‘n’ natural numbers using For Loop was written executed
and output is verified successfully.
8
Ex.No:3B DISPLAY FIRST ‘N’ NATURAL NUMBERS USING WHILE &
DATE: DO-WHILE LOOP
AIM
To write a C program for displaying first ‘n’ natural numbers using while & Do-While
loop.
ALGORITHM
Step 1: Start the program.
Step 2: Input the limit.
Step 3: Using while & do-while loop display the values till the limit.
Step 4: Stop the program.
PROGRAM
#include <stdio.h>
int main()
{
int i=1,j=1,n;
printf("Enter the Limit: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d using while loop: \n", n);
while(i<=n)
{
printf("%d\t", i);
i++;
}
printf("\nNatural numbers from 1 to %d using do-while loop: \n", n);
do
{
printf("%d\t", j);
j++;
}while(j<=n);
return 0;
}
9
OUTPUT
RESULT
Thus, C program to display ‘n’ natural numbers using While & Do-While Loop was
written executed and output is verified successfully.
10
Ex.No:4A SUM & AVERAGE OF ‘N’ NUMBERS
DATE:
AIM
To write a C program for finding sum & average of ‘n’ numbers.
ALGORITHM
Step 1: Start the program.
Step 2: Input the limit from user.
Step 3: Input the values of the array based on the limit.
Step 4: Calculate the sum using for loop.
Step 5: Calculate the average based on the sum.
Step 6: Display the sum & average.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
total+=array[i];
11
}
average = total / num;
printf("\n Sum of all numbers = %.2f\n", total);
printf("\n Average of all input numbers = %.2f\n", average);
return 0;
}
OUTPUT
RESULT
Thus, C program to find sum & average of array was written executed and output is
verified successfully.
12
Ex.No:4B MATRIX MULTIPLICATION
DATE:
AIM
To write a C program for finding product of 2 matrices of size 3*3.
ALGORITHM
Step 1: Start the program.
Step 2: Input 2 matrices.
Step 3. Using for loop find the product of two matrices and store it in 3rd matrix array.
Step 4: Display the result.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int mat1[3][3],mat2[3][3],mul[3][3],i,j,k;
printf("enter the first matrix element=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat2[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<3;i++)
{
13
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
RESULT
Thus, C program to find product of 3*3 matrices was written executed and output is
verified successfully.
14
Ex.No:5A STRING CONCATENATION
DATE:
AIM
To write a C program for concatenating 2 strings.
ALGORITHM
Step 1: Start the program.
Step 2: Input 2 strings from user.
Step 3: Using strcat() combine 2 strings and display it.
Step 4: Using while loop add the characters of 2nd string to the end of 1st string and
display it.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100],str3[100],str4[100];
int i=0,j=0;
printf("Enter the first string\n");
gets(str1);
printf("Enter the second string\n");
gets(str2);
printf("Enter the third string\n");
gets(str3);
printf("Enter the fourth string\n");
gets(str4);
strcat(str1,str2);
printf("String obtained on concatenation using strcat() is %s\n",str1);
while(str3[i]!='\0')
i++;
while(str4[j]!='\0')
{
str3[i]=str4[j];
j++;
15
i++;
}
str3[i]='\0';
printf("\nConcatenated String without using strcat() is %s",str3);
return 0;
}
OUTPUT
RESULT
Thus, C program to concatenate 2 strings was written executed and output is verified
successfully.
16
Ex.No:5B REVERSE A STRING
DATE:
AIM
To write a C program for reversing a given string.
ALGORITHM
Step 1: Start the program.
Step 2: Input a string.
Step 3: Calculate the length of the string using stlen().
Step 4: Using for loop display the string in reverse.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100];
int len,i;
printf("\nEnter a string:");
gets(str1);
len=strlen(str1);
printf("\nReverse of the given string is:");
for(i=len;i>=0;i--)
printf("%c",str1[i]);
return 0;
}
OUTPUT
RESULT
Thus, C program to reverse a given string was written executed and output is verified
successfully.
17
Ex.No:6A PASS BY VALUE & PASS BY REFERENCE
DATE:
AIM
To write a C program for implementing pass by value & reference.
ALGORITHM
Step 1: Start the program.
Step 2: Declare 2 functions.
Step 3: Initialize 4 values in main().
Step 4: Display first 2 values and then call swap1().
Step 5: In swap1() using temp variable exchange 2 values and display it.
Step 6: Display the value of first 2 after returning from swap1().
Step 7: Display 3rd & 4th values and then call swap2().
Step 8: In swap2() using temp variable exchange 2 values and display it.
Step 9: Display the value of 3rd & 4th after returning from swap2().
Step 10: Stop the program.
PROGRAM
#include <stdio.h>
void swap1(int , int);
void swap2(int *,int *);
int main()
{
int a = 10;
int b = 20;
int c = 30;
int d = 40;
printf("\nCALL BY VALUE");
printf("\n===================================================");
printf("\nBefore swapping the values in main a = %d, b = %d\n",a,b);
swap1(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
printf("===================================================");
printf("\nCALL BY REFERENCE");
printf("\n===================================================");
printf("\nBefore swapping the values in main c = %d, d = %d\n",c,d);
18
swap2(&c,&d);
printf("After swapping values in main c = %d, d = %d\n",c,d);
printf("===================================================");
return 0;
}
void swap1(int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
void swap2(int *x, int *y)
{
int temp1;
temp1 = *x;
*x=*y;
*y=temp1;
printf("After swapping values in function c = %d, d = %d\n",*x,*y);
}
OUTPUT
RESULT
Thus, C program to implement pass by value and pass by reference was written
executed and output is verified successfully.
19
Ex.No:6B PASSING ARRAYS TO FUNCTION
DATE:
AIM
To write a C program for implementing passing array to a function.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a function calculateSum().
Step 3: In main(), initialize the array num[] with 6 values.
Step 4: Call the calculateSum().
Step 5: In calculateSum(), using for loop find the sum of the array and return the sum
to main().
Step 6: Display the sum.
Step 7: Stop the program.
PROGRAM
#include <stdio.h>
float calculateSum(float num[]);
int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float num[])
{
float sum = 0.0;
for (int i = 0; i < 6; ++i)
{
sum += num[i];
}
return sum;
}
20
OUTPUT
RESULT
Thus, C program to implement passing array to a function was written executed and
output is verified successfully.
21
Ex.No:7A FACTORIAL OF A NUMBER
DATE:
AIM
To write a C program for implementing factorial of a number using recursion.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a function factorial().
Step 3: In main(), get the value from user.
Step 4: Call the factorial().
Step 5: In factorial(), check whether value is equal to 0, if so return value 1 else
repeatedly call factorial(). Finally return the value calculated.
Step 6: in main(), display the value.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
long factorial(int n);
int main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
22
OUTPUT
RESULT
Thus, C program to implement factorial of a number using recursion was written
executed and output is verified successfully.
23
Ex.No:7B FIBONACCI SERIES
DATE:
AIM
To write a C program for implementing Fibonacci Series using recursion.
ALGORITHM
Step 1: Start the program.
Step 2: Declare printFibonacci().
Step 3: In main(), input the limit from user.
Step 4: Display the first 2 values and call printFibonacci().
Step 5: In printFibonacci(), using if repeatedly call the call and display the series.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
void printFibonacci(int n);
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);
return 0;
}
void printFibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
24
}
}
OUTPUT
RESULT
Thus, C program to implement Fibonacci Series using recursion was written executed
and output is verified successfully.
25
Ex.No:8A IMPLEMENTATION OF FUNCTION POINTER
DATE:
AIM
To write a C program for implementing Function Pointer.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a function add().
Step 3: In main(), declare 2 integer variables and a pointer variable for add().
Step 4: Input 2 values from user.
Step 5: Assign the function to pointer.
Step 6: Call the function using the pointer.
Step 7: In add(), add the values and return the result.
Step 8: In main(), display the result.
Step 9: Stop the program.
PROGRAM
#include <stdio.h>
int add(int,int);
int main()
{
int a,b;
int (*ip)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);
return 0;
}
int add(int a,int b)
{
int c=a+b;
return c;
}
26
OUTPUT
RESULT
Thus, C program to implement Function Pointer was written executed and output is
verified successfully.
27
Ex.No:8B IMPLEMENTATION OF POINTER TO ARRAY
DATE:
AIM
To write a C program for implementing pointer to array.
ALGORITHM
Step 1: Start the program.
Step 2: Declare an array an initialize it.
Step 3: Assign the array to a pointer.
Step 4: Using for loop and pointer display the values of the array.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
int main ()
{
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = balance;
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ )
{
printf("*(p + %d) : %.2f\n", i, *(p + i) );
}
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Pointer to an Array was written executed and output is
verified successfully.
28
Ex.No:8C IMPLEMENTATION OF DOUBLE POINTER
DATE:
AIM
To write a C program for implementing double pointer.
ALGORITHM
Step 1: Start the program.
Step 2: Initialize a variable.
Step 3: Assign the address of the variable to a pointer.
Step 4: Assign the address of the pointer to another pointer.
Step 5: Display the address and value using the pointers.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Double Pointer was written executed and output is
verified successfully.
29
Ex.No:8D IMPLEMENTATION OF ARRAY OF POINTERS
DATE:
AIM
To write a C program for implementing array of pointers.
ALGORITHM
Step 1: Start the program.
Step 2: Define a constant MAX as 3.
Step 3: Initialize an array var[].
Step 4: Using for loop assign the address of each value of var[] to pointer array.
Step 5: Display the array value using pointer 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];
for ( i = 0; i < MAX; i++)
{
ptr[i] = &var[i];
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Array of Pointers was written executed and output is
verified successfully.
30
Ex.No:9A IMPLEMENTATION OF NESTED STRUCTURE
DATE:
AIM
To write a C program for implementing Nested Structure.
ALGORITHM
Step 1: Start the program.
Step 2: Define a structure address.
Step 3: Define a structure employee and declare a variable add for structure address.
Step 4: In main(), declare a variable for structure employee.
Step 5: Using the both structure variables input and display the values.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
int main()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
printf("Printing the employee information....\n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
return 0;
}
31
OUTPUT
RESULT
Thus, C program to implement Nested Structures was written executed and output is
verified successfully.
32
Ex.No:9B IMPLEMENTATION OF POINTERS TO STRUCTURES
DATE:
AIM
To write a C program for implementing pointers to structures.
ALGORITHM
Step 1: Start the program.
Step 2: Define a structure person.
Step 3: In main(), declare pointer to the structure and a structure variable.
Step 4: Assign the address of structure variable to pointer.
Step 5: Using the pointer input the values and display it.
Step 6: Stop the program.
PROGRAM
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
33
OUTPUT
RESULT
Thus, C program to implement Pointer to Structure was written executed and output is
verified successfully.
34
Ex.No:9C IMPLEMENTATION OF ARRAY OF STRUCTURES
DATE:
AIM
To write a C program for implementing array of structures.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a structure student.
Step 3: In main(), declare a variable for structure.
Step 4: Using structure variable and for loop get the details.
Step 5: Display the details.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
35
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Array of structures was written executed and output is
verified successfully.
36
Ex.No:9D IMPLEMENTATION OF UNION
DATE:
AIM
To write a C program for implementing Union.
ALGORITHM
Step 1: Start the program.
Step 2: Define a union and declare 2 variables and a variable for union.
Step 3: In main(), using union variable assign value to ‘a’.
Step 4: Display the value of a and b.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
union abc
{
int a;
char b;
}var;
int main()
{
var.a = 66;
printf("\n a = %d", var.a);
printf("\n b = %d", var.b);
}
OUTPUT
RESULT
Thus, C program to implement Union was written executed and output is verified
successfully.
37
Ex.No:10A IMPLEMENTATION OF FILE READ & WRITE
DATE:
AIM
To write a C program for implementing File Read & Write.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a string and File Pointer.
Step 3: Using fopen() open Sample.txt for writing content.
Step 4: Check whether file pointer is null.
Step 5: Using fprintf(), write any number of lines to the file.
Step 6: Close the file.
Step 7: Using fopen() open the sample file in read mode.
Step 8: Using while loop and fgets(), read the contents of file and display it.
Step 9: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[50];
FILE *fptr;
fptr = fopen("Sample.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"Welcome to C\n File Concepts");
fclose(fptr);
if ((fptr = fopen("Sample.txt","r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
38
while(fgets(str,sizeof(str),fptr))
{
printf("%s",str);
}
fclose(fptr);
return 0;
}
OUTPUT
RESULT
Thus, C program to implement File Read & Write was written executed and output is
verified successfully.
39
Ex.No:10B IMPLEMENTATION OF RANDOM ACCESS
DATE:
AIM
To write a C program for implementing Random Access.
ALGORITHM
Step 1: Start the program.
Step 2: Declare a file pointer.
Step 3: Using fopen(), open Sample.txt in read mode.
Step 4: Using fseek() set the file pointer to 7th position.
Step 5: Display the file content from 7th position.
Step 6: Using ftell() display the current position of file.
Step 7: Using rewind() move the file pointer to starting position of the file.
Step 8: Display the current position of file.
Step 9: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[50];
int len;
FILE *fptr;
fptr = fopen("Sample.txt","r");
fseek(fptr,7,SEEK_SET);
while(fgets(str,sizeof(str),fptr))
{
printf("%s",str);
}
len=ftell(fptr);
printf("\nCurrent File Position is:%d",len);
rewind(fptr);
len=ftell(fptr);
printf("\nCurrent File Position is:%d",len);
fclose(fptr);
40
return 0;
}
OUTPUT
RESULT
Thus, C program to implement Random File Access was written executed and output
is verified successfully.
41