[go: up one dir, main page]

0% found this document useful (0 votes)
24 views43 pages

Record 1

Uploaded by

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

Record 1

Uploaded by

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

EX.No.

1(a) PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

Date :

AIM:

To implement the program using I/O statements and expression using c language

ALGORITHM:
1. Start the program
1. Input the radius of the circle
2. Find the area and circumference of the circle using the formula
Area=3.14*r*r
Circum=2*3.14*r
3. Print the area and the circum of the circle
4. Display the output of the calculations.
5. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
Void main()
{
float rad,area,circum;
printf("\nEnter the radius of the circle");
scanf("%f",&r);
area=3.14*rad*r;
circum=2*3.14*r;
printf(“\nArea=%f”,area);
printf("\n Circumference=%f,circum);
}
OUTPUT:

Enter the radius of the circle : 5

AREA=78.500000

CIRCUMFERENCE=31.4000000

RESULT:

Thus the program for I/O statement and expression was executed
successfully and the result was verified.
EX.No.1(b) Program to print Regno,Name, and calculate the average of student

DATE :

AIM

To implement the program to print regno,name and calculate the averageof a student.

ALGORITHM

Step 1 : Start the program

Step 2 : Declare the variables

Step 3. Get Student Regno,Name,and marks.

Step 4 : Calculate the total and average

Step 5 : Print Regno,name, and average of the student

Step 6 : Stop the program

PROGRAM
#include <stdio.h>
int main()
{
int Regno,Size, i, sum = 0;
char name[20];
float avg = 0;
printf(“Enter your Regno”);
scanf(“%d”,&Regno);
printf(“Enter your name”);
scanf(“%s”,&name);
printf("Please Enter the Array size = ");
scanf("%d", &Size);
int arr[Size];
printf("Enter the Marks : ");
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < Size; i++)
{
sum = sum + arr[i];
}
avg = sum / Size;
printf(“Your Reg no is %d”,Regno);
printf(“Your name is %s”,name);
printf("\nThe Sum of marks = %d\n", sum);
printf("\nThe Average of student = %.f\n", avg);
}

OUTPUT :

Enter your Regno : 2415671

Enter your name : Shankar

Enter array size : 5

Enter the marks: 80 50 80 97 88

Your Regno is 2415671

Your name is Shankar

The sum of marks = 395

The average of student = 79.0

RESULT

Thus the program for print Regno ,Name and calculate Average has been implemented
successfully.
Ex.No. 2(a) PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS

AIM:
To implement the program to display biggest of two numbers.

ALGORITHM:

Step 1:Start

Step 2:Read three numbers A,B & C

Step 3:If A>B,then go to step 6

Step 4:If B>C,then print B & go to step 8

Step 5:print C is greatest & go to step 8

Step 6:If A>C,then print A is greatest & go to step 8

Step 7:Print C is greatest

Step 8:end

PROGRAM:

#include <stdio.h> void main()


{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B)
{
if(A>C)
{
printf(" %d is the Greatest Number \n",A);
}
Else
{
printf("%d is the greatest Number \n",C);
}
}
Else
{
if(B>C)
{

printf("%d is the greatest Number \n",B );


}
Else
{
printf("%d is the greatest Number \n", C);
}
}
}

OUTPUT:

Enter two numbers:


4.5
5.6
5.6 is the largest number.

RESULT:

Thus the program to input two numbers and display the maximum number was
implemented successfully.
EX.No.2(b) PROGRAM TO CHECK WHETHER A PERSON IS

DATE : ELIGIBLE TO VOTE OR NOT

AIM:

To implement the program to check whether a person is eligible to vote or not.

ALGORITHM:

1. Start the program

2. Declare the variable age and read the age.

3. Check the status age =18 or age>18


4. If its condition is true eligible for vote.

5. Otherwise the person is not eligible for vote.


6. Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50];
printf("\n Enter the name of the candidate:");
gets(name);
printf("\n Enter the age:");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligibile for vote",name);
else
printf("\n %s is not Eligibile for vote",name);
getch();
}
OUT PUT :

Enter the name of the candidate : Raja

Enter the age : 20

Raja is eligible to vote

Enter the name of the candidate : Rajesh

Enter the age : 15

Rajesh is not eligible to vote

RESULT:

Thus the program for checking eligibility for voting is executed successfully.
EX.NO.3(a) PROGRAM TO DO ARITHMETIC OPERATIONS

DATE :

AIM:

To generate a C program to do arithmetic operations.

ALGORTHIM:

1. Start the program

2. Enter (assign)value of the variable(addend)X.

3. Enter (assign)value of the variable(addend)Y.


4. Calculate Z (Z = X + Y)
5. Display result of addition, value of Z on the monitor with the message "The sum
of X and Y is:"
6. Stop the program.

PROGRAM :

# include <stdio.h>

void main()
{
int a,b,result,sq1,sq2,ch; float divide;
clrscr();
printf("enter two integers:");
scanf("%d%d",&a,&b);
printf("1.add,2.subtrat,3.multiply,4.divide,5.sqare"); printf("\
n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{

result=(a+b); printf("sum=%d\
n",result);
break;
}
case 2:
{
result=(a-b); printf("difference=%d\
n",result);
break;
}
case 3:
{
result=(a*b);
printf("mutiplication=%d\n",result);
break;
}
case 4:
{
result=a/(float)b;
printf("division=%2f\n",result);
break;
}
case 5:
{
sq1=(a*a);
printf("square=%d\n",sq1);
sq2=b*b;
printf("second square number=%\n",sq2);
break;
}
Default
{
Prinf(“You are Entered wrongly”);
Break;
}
}
getch();
}
OUTPUT :

Enter two integer :23 45


1.add 2.subraction,3.multiplication,4.division,5.square
Enter the choice: 1
Sum=68

Enter two integer :50 20


1.add 2.subraction,3.multiplication,4.division,5.square

Enter the choice: 2

Difference : 30

RESULT :

Thus the c program for arithmetic operation using switch case has been executed
successfully.
Ex.No. 4 (a) PROGRAM TO FIND THE GIVEN NUMBER IS

DATE: ARMSTRONG OR NOT

AIM:

To write C program for find the given number is Armstrong or not.

ALGORITHM:

1. Start the program.

2. Declare variable sum,temp,num.

3. Read number from user.

4. Initialize variable sum=0 and temp=num

5. Repeat until num>=0

6. If sum==temp

Print “Armstrong Number” Else

Print “not Armstrong number”

7. Stop the program.

PROGRAM:
#include<stdio.h>
#include<cono.h>
void main()
{
intn,orgnum,r,result=0;
printf("enter a three digit number:");

scanf("%d",&n);
orgnum=n;
while(orgnum!=0)
{
r=orgnum%10;
result+=r*r*r;
orgnum=orgnum/10;
}
if(result==n)
printf("%d is an armstrongnumber",n);
else
printf("%d is not an armstrongnumber",n);
getch();
}

OUTPUT :

Enter a three digit number: 371


371 is an Armstrong number
Enter a three digit number:401
401 is not an Armstrong number

RESULT:

The program for checking given number is Armstrong or not has been successfully
executed.
Ex.No. 4 (b) PROGRAM TO CHECK WHETHER THE ENTERED

DATE : CHARACTER IS VOWEL OR NOT(USE SWITCH CASE)

AIM :
To implement the Program to check whether the entered character is vowel or
not(Use switch case)

ALGORITHM :

Step 1: Start
Step 2: Declare and initialize the variables
Step 3: Get the input from the user and compare with each cases
Step 4: if match found, print vowel otherwise print consonant
Step 5: End

PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
break;
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}

return 0;
}

OUTPUT :

Enter a character : A

A is a Vowel

Enter a character : X

X is consonant

RESULT :
Thus the C Program check whether the entered character is vowel or
not (Use switch case) has been executed and the output was verified.
Ex.No.5(a) PROGRAM TO PRINT POSITIVE INTEGERS FROM 1 TO 100

DATE :
AIM :
To implement the program to print positive integers from 1 to 10.

ALGORITHM :

Step 1: Start
Step 2: Declare and the variables
Step 3: Execute the for loop
Step 4 : Print the integers from 1 to 10

PROGRAM :

#include<stdio.h>

int main()
{
int i;

for(i = 1; i <= 100; i++)

printf("%d\t ",i);

return 0;

OUTPUT : 1 2 3 4 5 6 7 8 9 10

RESULT :

Thus the program to print integers from 1 to 100 is successfully implemented and verified.
EX.NO.5(b) GET THE ELEMENTS INTO AN ARRAY AND PRINT THE ELEMENTS
DATE : FIND THE LARGEST AND SMALLEST OF THE GIVEN ARRAY

AIM:
To write the program to find the largest and smallest number of given array

ALGORITHM:

1. Start the program

2. Enter the size of array

3. Enter the elements of the array

4. Print the array elements

5. Initialize the large and small is equal to the first element of the array

6. Set a loop up to the array size

7. Check the next element greater than the larger.

8. If greater then assign next element to the large

9. Check the next element smaller than the larger. If smaller then assign next
element to the small

10. Print the value of large and small after the execution of the loop

11. Stop the program

PROGRAM :
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[100],I,small,large,no;
printf(“In how many numbers you want to insert….”);
scanf(“%d”,&no);
printf(“Enter the elements of the array…”);
for(i=0;i<no;i++)
scanf(“%d”,&a[i]);
printf(“\nThe
elements of the
array”);
for(i=0;i<no;i++)
printf(“\n%d”,a[i]);
small=a[0];
large=a[0];
for(i=1;i<no;i++)
{
If(a[i]>large

large=a[i];

else if(a[i]<small)
small=a[i];
}
Printf(“\nThe largest of the given array is %d”,large);

Printf(“\nThe largest of the given array is %d”,small);


}

OUTPUT :
In how many numbers you want to insert….5
Enter the elements of the array….
12 34 56 87 43
The elements of the array 12 34 56 87 43
The largest of the given array is 87
The smallest of the given array is 12

RESULT:

Thus the largest and smallest number of the given array using c program has been
executed successfully.
Ex.No.6(a) FIND THE ADDITION OF TWO MATRIXES

DATE :

AIM:

To implement a program to add two matrices.

ALGORITHM:

1. Start the program

2. Enter the row and column of the matrix

3. Enter the elements of the A matrix

4. Enter the elements of the B matrix

5. Print the A matrix in the matrix form

6. Print the B matrix in the matrix form

7. Set a loop up to the row

8. Set a inner loop up to the column

9. Add the elements of A and B in column wise and store the result in C matrix

10. After the execution of the two loops Print the value of C matrix

11. Stop the program


PROGRAM :

#include<stdio.h>

#include <conio.h>

Void main()

int a[25][25],b[25][25],c[25][25],I,j,m,n;

printf(“Enter the rows and columns of two matrixes…\n”); scanf(“%d”,&m,&n);

printf(“\nEnter the elements of A matrix…”);


for(i=0;i<&m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
}
printf(“\nEnter the elements of B matrix….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
}
printf(“\nThe elements of A matrix”); 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”); for(i=0;i<m;i+


+)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,b[i][j]);
}
printf(“\n The addition of two matrix”);
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 :

Enter the rows and column of two matrix : 3 3

Enter the elements of A matrix…1 2 3 4 5 6 7 8 9


Enter the elements of B matrix…1 2 3 4 5 6 7 8 9
The elements of A matrix
1 2 3

4 5 6

7 8 9

The elements of B matrix


1 2 3
4 5 6
7 8 9

The addition of two matrices are

2 4 6
8 10 12
14 16 18

RESULT:
Thus the program to add two matrix by using C program has been executed successfully.
EX.NO.6(b) PROGRAM TO REVERSE THE ARRAY ELEMENTS

DATE:

AIM

To implement the program to reverse the array elements.

ALGORITHM

Step1 : Start the program

Step2 : Declare the variables

Step 3 : Get the size of the array

Step4 : Get the elements

Step 5 : Using for loop to reverse the elements

Step 6: Print the reversed elements

PROGRAM :

#include<stdio.h>
int main()
{
int n, arr[n], i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int rev[n], j = 0;
for(i = n-1; i >= 0; i--)
{
rev[j] = arr[i];
j++;
}
printf("The Reversed array: ");
for(i = 0; i < n; i++)
{
printf("%d ", rev[i]);
}
}

OUTPUT :

Enter the size of the array :5

Enter the elements :12345

The Reversed array :54321

RESULT :

Thus the program to reverse the array elements can be implemented successfully
Ex.No.7(a) PROGRAM TO FIND WHETHER THE GIVEN STRING IS

DATE : PALINDROME OR NOT

AIM:

To write the c program to find whether the given string is palindrome or not

ALGORITHM:

1. Start the program


2. Enter the string
3. Find the string length using thr strlen() function
4. Print the string length
5. Set a loop up to the half of the string length
6. Compare every charcter above the middle character with the below character
of the middle character.
7. If any character equal prints the given string is palindrome
8. If the character is not equal then print the given string is not a palindrome
9. Stop the program.
PROGRAM:
#include <stdio.h>
#include<conio.h>
Void main()
{
int len=0,i,j;
char name[25];
printf(“Enter the string…”);
scanf(“%s”,name);

while(name[len]!=’\0’)
len++;
printf(“\n%d”,len);
for(i=0,j=len-1;i<len/2;i++,j--);
{
If(name[i]!=name[j])
{
Printf(“\nThe given string is not a
palindrome”); Exit(0);
}
}
Printf(“\n The given string is a palindrome”);
}

OUTPUT :

Enter the string : Malayalam


9
The given string is palindrome

RESULT :
Thus the program for palindrome has been implemented successfully.
Ex.no:7(b)

Date : PROGRAM TO COMPARE TWO STRINGS

AIM:

To implement the program for compare two strings.

ALGORITHM :

Step 1 : Start the program

Step2 : Declare the string variables

Step 3 : Get the strings from user

Step 4 : Compare two strings

Step 5 : If two strings are equal then print the strings are equal.

PROGRAM:

#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
SAMPLE OUTPUT :

Enter string1: Computer


Enter string 2: programming
strings are not equal

Enter string1: c programming


Enter string2: c programming
strings are equal

RESULT :

Thus the C Program for compare two strings have been successfully implemented.
Ex.no.8(a) PROGRAM TO GENERATE FIBONACCI SERIES

Date:

AIM

To implement the program for generate Fibonacci series.

ALGORITHM

Step 1 : Start the program

Step 2 : Initialise 0 and 1 for variables.

Step3 : Get the number of elements to generate Fibonacci series

Step 4 : Print 0 and 1.

Step 5 : Execute the for loop and add the result with previous number.

Step 6 : Print the Fibonacci series

PROGRAM

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2); //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
OUTPUT :

Enter the number of elements:15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

RESULT :

Thus the program to generate Fibonacci series has been implemented successfully.
EX.NO.8(b) PROGRAM TO GENERATE FIBONACCI SERIES USING
DATE: RECURSION

AIM

To implement the program for generate Fibonacci series using recursion.

ALGORITHM

Step 1 : Start the program

Step 2 : Initialise 0 and 1 for variables.

Step3 : Get the number of elements to generate Fibonacci series using recursion

Step 4 : Print 0 and 1.

Step 5 : Execute the for loop and add the result with previous number.

Step 6 : Print the Fibonacci series

PROGRAM

#include<stdio.h>
void Fibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
Fibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");

printf("%d %d ",0,1);
Fibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}

OUTPUT:

Enter the number of elements:15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

RESULT :

Thus the program to generate Fibonacci series has been implemented successfully.
Ex.no:9(a)

DATE: SWAPPING TWO VALUES BY USING CALL BY VALUE

AIM

To implement the program by swapping two values by using call by value

ALGORITHM

Step 1 : Start the program

Step 2 : Declare the function swap

Step 3 : Print Before swapping values

Step 4 : Call the function swap(a,b)

Step 5 : Execute the swapping and function and print the exchanging values

of the variables.

PROGRAM

#include <stdio.h>
void swap(int , int); //prototype of the function

int main()
{
int a = 10; int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
}

void swap (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);
}

OUTPUT :

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

RESULT :

Thus the program for swapping two values by using call by value has been implemented
successfully.
EX.NO:9(b) SWAPPING TWO VALUES BY USING

DATE: CALL BY REFERENCE

AIM

To implement the program by swapping two values by using call by value

ALGORITHM

Step 1 : Start the program

Step 2 : Declare the function swap

Step 3 : Print Before swapping values

Step 4 : Call the function swap(a,b)

Step 5 : Execute the swapping and function and print the exchanging values

of the variables.

PROGRAM :

#include <stdio.h>
void swap(int *, int *); //prototype of the function

int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (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);
}

OUTPUT :

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

RESULT :

Thus the program for swapping two values by using call by reference has been implemented
successfully.
Ex.No.10 (a) PROGRAM TO READ THE STUDENT DATA AND

CALCULATE THETOTAL MARKS USING ARRAY AND STRUCTURE

DATE :

AIM:

To read the student data and calculate the total marks


ALGORITHM:

Step 1: Start the program

Step 2: Get the details of the 10 students in five subjects

Step 3: Calculate the total marks of each student

Step 4 : Calculate the student who got the highest total marks

Step 5: Display the results

Step 6: Stop the Program


PROGRAM :

#include<stdio.h>

struct student

int sub1;

int sub2;

int sub3;

int sub4;

int sub5; };

void main()

Struct student s[10];


int i,total=0;

clrscr();

for(i=0;i<=9;i++)

printf("\nEnter Marks in Five Subjects = ");

scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);

total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;

printf("\nTotal marks of s[%d] Student= %d",i,total);

getch();

OUTPUT:

Enter Marks in Five Subjects 80 70 90 80 98

Total Marks of 1 student = 83.6

RESULT :

Thus the C program for create a structure and find out the average of a student marks was

implemented successfully.
Ex.no:10(b)

Date: PROGRAM TO DECLARE AND INITIALIZE AN UNION

AIM

To implement the program to declare and initialize by using union.

ALGORITHM

Step 1 : Start the program

Step 2 : Create pack as union variable

Step 3 : Declare the members in union

Step 4 : create a variable for pack and access the value of members.

Step 5: Print the value of members.

Step 6 : Stop the program

PROGRAM :

# include < stdio.h >


# include < conio.h >
union pack
{
char a ;
int b ;
double c ;
};

int main( )
{
pack p ;
printf("\n Occupied size by union pack: %d",sizeof(pack) ) ;
p.a = 'G' ;
printf("\n Value of a:%c", p.a ) ;
p.b = 20 ;
printf("\n Value of b:%d",p.b ) ;
p.c = 2545.6250 ;
printf("\n Value of c:%f",p.c ) ;
p.a = 'G' ;
p.b = 20 ;
p.c = 2545.6250 ;
printf("\n Value of a:%c, b:%d, c:%f",p.a,p.b,p.c ) ;
return 0 ;

OUTPUT :

Occupied size by union pack: 8

Value of a : G

Value of b: 20

Value of c : 2545.625

Value of a : ‘G’, b: 20, c: 2545.625000

RESULT :

Thus the program by using union was implemented successfully.


EX.NO.11(a) PROGRAM TO CONCATENATE TWO STRINGS

DATE:

AIM

To implement the program to concatenate two strings.

ALGORITHM

Step1: Start
Step 2: Get the two Strings to be concatenated.
Step 3: Declare a new String to store the concatenated String.
Step 4: Insert the first string in the new string.
Step 5: Insert the second string in the new string.
Step 6: Print the concatenated string.
Step 7: Stop

PROGRAM
#include <stdio.h>
#include<string.h>
int main()
{
char s1[20]; // declaration of char array
char s2[20]; // declaration of char array
printf("Enter the first string : ");
scanf("%s", s1);
printf("\nEnter the second string :");
scanf("%s",s2);
strcat(s1,s2);
printf("The concatenated string is : %s",s1);
return 0;
}
OUTPUT :

Enter the String1 : Software

Enter the String2 : Engineer

The concatenated string is SoftwareEngineer

RESULT :

Thus the Program for concatenation of two strings has been implemented successfully.

EX.No.11(b) PROGRAM TO READ THE CONTENT OF FILE


DATE :

AIM

To implement the program to read the content of file

ALGORITHM

Step1: Start the program

Step2 : Get the file name to read

Step 3: Using File function

fopen() is used to open a file.

Fclose() is used to close a file.

Step4 : Display the content of file by using file pointer fptr.

Step 5: Close the file

Step 6: Stop the program

PROGRAM

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *fptr;

char filename[100], c;

printf("Enter the filename to open \n");


scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}

// Read contents from file


c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}

fclose(fptr);
return 0;
}

OUTPUT :

Enter the filename to open


a.txt
/*Contents of a.txt*/

RESULT :

Thus the program to read the content of a file

You might also like