0% found this document useful (0 votes)
17 views

CS3271-PC Lab

Lab manual

Uploaded by

anitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CS3271-PC Lab

Lab manual

Uploaded by

anitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

CSI COLLEGE OF ENGINEERING, KETTI

(Affiliated to Anna University, Approved by AICTE)

THE NILGIRIS-643215

LABORATORY RECORD

DEPARTMENT OF INFORMATON TECHNOLOGY

CS3271-PROGRAMMING IN C LABORATORY

YEAR 2023-2024
CSI COLLEGE OF ENGINEERING
(Church of SouthIndia, Coimbatore Diocesan Council)
KETTI, THE NILGIRIS-643215.

Register No.....................................................................................
Department………………………………………………………………………………..

This is to Certified that Mr./Ms………………………………………….. with the register


no………………………………… has satisfactorily completed the practical course CS3271-
PROGRAMMING IN C Laboratory prescribed by Anna University, Chennai for …..
Semester B.TECH Examination during the academic year ……….. - ………...

Staff In-Charge Head of the Department

Submitted for the university ................... semester B.Tech Examination of the Anna University,
Chennai conducted on…………………….

Internal Examiner External Examiner


1. I/O STATEMENTS, OPERATORS, EXPRESSIONS.

1.a) C Program to display your personal details.

AIM:
To write a C Program to display your personal details.

ALGORITHM:
Step 1: Start
Step 2: Declare and initialize the variables for name, address,date of birth,
mobilenumber and age.
Step 3: Display name, address, date of birth, mobile number and age.
Step 4: End

PROGRAM:
#include<stdio.h>
void main()
{
char name[20]= "SAI RAM";
char address[80]= "west tambharam,chennai"; int date=20;
int month=10; int year=1990;
int mobile=987456321; int age=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");

1
}

OUTPUT:
NAME: SAI RAM
ADDRESS: west tambaram, chennai
DOB: 20:10:1990
MOBILE NUMBER: 987456321
AGE: 25

RESULT:
Thus the C Program to display your personal details has been done and the
output has been verified.
2
1.b)C Program to get the user details and display it.

AIM:
To write a C Program to get the user details and display it.

ALGORITHM:
Step 1: Start
Step 2: Declare the variables for name, address, date, month, year, mobile
number, age.
Step 3: Read values of name, address , date, month, year, mobile number, age
from the user.
Step 4: Display name, address, date, month, year, mobile number, age.
Step 5: End.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20]; char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
clrscr();
printf("\n ENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
3
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/%d",&date,&month,&year);
printf("\n ENTER YOUR AGE="); scanf("%d",&age);
printf("\n ENTER YOUR GENDER(MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld" ,&mobile);
printf("\n=====================");
printf("\n NAME: %s",name); printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year); printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
getch();
}

4
OUTPUT:
ENTER YOUR NAME:=karthikeyan
ENTER YOUR ADDRESS=west tambharam,chennai.
ENTER YOUR date/month/year=03/12/1992
ENTER YOUR AGE=28
ENTER YOUR GENDER(MALE/FEMALE)=MALE ENTER YOUR MOBILE
NUMBER=987654321
=====================
NAME: karthikeyan ADDRESS:west tambharam,chennai. DOB:3:12:1992
AGE:28 GENDER:MALE
MOBILE NUMBER:987654321
========================

RESULT:
Thus the C Program to get the user details and display it has been done and
the output has been verified.

5
1.c)C Program to calculate area and circumference of a circle.

AIM:
To write a C Program to calculate area and circumference of a circle

ALGORITHM:
Step 1: Start
Step 2: Declare the variables radius, area and circumference
Step 3: Read values of radius from the user.
Step 4: Find area and circumference and display it.
Step 5: Stop

PROGRAM:
#include <stdio.h>
#inlude<conio.h>
void main()
{
int radius;
float PI = 3.14, area, circumference;
printf("Enter the radius of circle: ");
scanf("%d", &radius);
area = PI * radius * radius;
printf("The Area of circle is: %f", area);
circumference = 2 * PI * radius;
printf("\nThe Circumference of circle is: %f", circumference);
getch();
}

6
OUTPUT:

Enter the radius of circle: 2

The Area of circle is: 12.560000


The Circumference of circle is: 12.560000

RESULT:
Thus the C Program to calculate area and circumference of a circle has been
executed and the output was verified.

7
2. DECISION – MAKING CONSTRUCTS:IF-ELSE, GOTO,
SWITCH- CASE, BREAK-CONTINUE

2.a) C program to find a given number is odd or even(if-else)

AIM:
To write a C program to find a given number is odd or even using if-else
statement.

ALGORITHM:
Step 1:Start
Step 2:Declare variable n
Step 3:Recieve the value of n from user
Step 4:Check the condition if the number is odd or even
Step 5:Print the number is odd or even
Step 6:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter the number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“Number is Even”);
else
printf(“Number is Odd”);
getch();
8
}

OUTPUT:
Enter the number:15
Number is Odd

RESULT:
Thus the C Program to find a given number is odd or even has been executed
and the output was verified.

9
2.b Program to input two numbers and display the maximum number(else if)

AIM:
To write a C program to input two numbers and display the maximum
number using else if

ALGORITHM:
Step 1:Start
Step 2:Declare the variables num1 and num2
Step 3:Get the values of num1 and num2 from the user
Step 4:Check the condition to find greatest number
Step 5:Print the greatest number
Step 6:End

PROGRAM:
// C program to find the maximum of the two numbers
#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2;
clrscr();
printf("Enter first number: ");
scanf("%d", & num1);
printf("Enter second number: ");
scanf("%d", & num2);
if (num1 > num2)
{
printf("%d is largest", num1);
}
10
else if (num1 < num2)
{
printf("%d is largest", num2);
}
else
{
printf("Both numbers are equal");
}
getch();
}

OUTPUT:
Enter first number:9

Enter second number:3

9 is largest

RESULT:

Thus the a C program to input two numbers and display the maximum
number using else ifhas been executed and the output has been verified.

11
2.c) program that finds equivalent Grade of your obtained score using if -else
ladder

AIM:
To write a C program to find the equivalent grade for the obtained score
using if-else ladder.

ALGORITHM:
Step 1:Start
Step 2:Declare the variable m
Step 3:Get the marks from the user
Step 4:Find the grade according to the marks obtained and print.
Step 5:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
printf(“Enter the marks:”);
scanf(“%d”,&m);
if(m>=75)
printf(“Grade:Distinction”);
else if(m>=60)
printf(“Grade:First Class”);
else if(m>=50)
printf(“Grade:Second Class”);
else if(m>=40)
printf(“Grade:Pass Class”);
elseprintf(“Grade : Fail”);
12
}

OUTPUT:
Enter the marks:52
Grade:Second Class

RESULT:
Thus the C program to find the equivalent grade for the obtained score
using if-else ladder has been executed and the output has been verified.

13
2.d)C Program to find whether a character is consonant or vowel using switch
statement.

AIM:
To write a C program to find whether a character is consonant or vowel using
switch statement.

ALGORITHM:
Step 1:Start
Step 2:Declare the variable c
Step 3:Get the value of c from the user
Step 4:Using switch statement find whether the given character is consonant
or vowel
Step 5:Print the character is consonant or vowel
Step 6:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main() {
char c;
clrscr();
printf("Enter an Alphabet\n");
scanf("%c", &c);
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
14
case 'o':
case 'O':
case 'u':
case 'U': printf("%c is VOWEL", c);
break;
default: printf("%c is CONSONANT", c);
}

getch();
}

OUTPUT:
Enter an Alphabet
O
O is VOWEL

Or

// C program to find whether a character is consonant or vowel using switch


statement.

#include <stdio.h>

#include<conio.h>

void main()

char ch;

clrscr();
printf("Enter any alphabet: ");
scanf("%c", &ch);

15
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':

16
printf("Vowel");
break;
default:
printf("Consonant");
}

getch();
}

OUTPUT
Enter any alphabet: E
Vowel

RESULT:
Thus the C program to find whether a character is consonant or vowel using
switch statement has been executed and the output has been verified.

17
3)e) C program to check whether given number is odd or even using goto
statement(goto)

AIM:
To write a C Program to check whether given number is odd or even using
goto.

ALGORITHM:
Step 1:Start
Step 2:Initialise the variable num and pass the value to the function
checkEvenOrNot()
Step 3:Check whether the number is even or odd
Step 4:If number is even goto even , print num is even
Step 5:If the number is odd goto odd , print num is odd
Step 6:End

PROGRAM :
#include <stdio.h>
#include<conio.h>
void checkEvenOrNot(int num)
{
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf("%d is even", num);
// return if even
return;
18
odd:
printf("%d is odd", num);
}
void main()
{
int num = 26;
clrscr();
checkEvenOrNot(num);
getch();
}

OUTPUT:
26 is even

RESULT:
Thus the C Program to check whether given number is odd or even using
goto has been executed and the output has been verified.

19
2.f) C program to implement continue statement(continue)

AIM:
To write a C program to implement continue statement

ALGORITHM:
Step 1:Start
Step 2:Initialise the variable i to 1
Step 3:Use the for loop to print value 1-10
Step 4:Check for the condition i=5 and continue
Step 5:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf("%d \n",i);
}
getch();
}

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

RESULT:
Thus the C program to implement continue statement has been executed and
the output has been verified.

21
3. LOOPS: FOR, WHILE, DO-WHILE

3.a) C program to generate Fibonacci series(for loop).

AIM:
To write a C program to find the Fibonacci series using for loop.

ALGORITHM:
Step 1:Start
Step 2:Declare the necessary variables
Step 3:Initialise t1 to 0 and t2 to 1
Step 4:Get the number of terms from the user
Step 5:Calculate and print the Fibonacci series
Step 6:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
clrscr();
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
22
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
getch();
}

OUTPUT:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

RESULT:
Thus the C program find the Fibonacci series using for loop has been executed
and the output has been verified.

23
3.b) C program to find the factorial of a number(for loop)

AIM:
To Write a C program to find the factorial of the number using for loop.

ALGORITHM:
Step 1:Start
Step 2:Declare the variables n,i and fact
Step 3:Recieve the value of n from the user
Step 4:Find the factorial of the number and print the factorial
Step 5:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for (i = 1; i <= n; i++)
{
fact *= i;
}
printf("Factorial of %d = %d", n, fact);
}
24
getch();
}

OUTPUT:
Enter an integer:5
Factorial of 5 = 120

RESULT:
Thus the C program to find the factorial of the number using for loop has
been executed and the output has been verified.

25
3.c) C program to check whether a number is palindrome or not(while loop).

AIM:
To write a C program to check whether a number is palindrome or not
using while loop.

ALGORITHM:
Step 1:Start
Step 2:Declare the necessary variables
Step 3:Get the value of n from the user
Step 4:Check whether the number is palindrome or not
Step 5:Print the number is palindrome or not
Step 6:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, reversed = 0, remainder, original;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if original and reversed are equal
26
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
getch();
}

OUTPUT:
Enter an integer: 1001
1001 is a palindrome.

RESULT:
Thus the C program to check whether a number is palindrome or not using
while loop has been executed and the output has been verified.

27
3.d) C program to print positive integers from 1 to 10(do while)

AIM:
To write a C program to print positive integers from 1 to 10 using do while.

ALGORITHM:
Step 1:Start
Step 2:Declare the variable i
Step 3:Print the values from 1 to 10 using do while
Step 4:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1
do
{
printf("%d\t",i);
i++;
}while(i<=10);
getch();
}

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

RESULT:
Thus the C program to print positive integers from 1 to 10 using do while
has been executed and the output has been verified.

29
4. ARRAYS:1D AND 2D , MULTI-DIMENSIONAL ARRAYS,
TRAVERSAL

4.a)C program to insert 5 elements into an array and print the elements of the
array.

AIM:
To write a C program to insert 5 elements into an array and print the elements
of the array.

ALGORITHM:
Step 1:Start
Step 2:Declare the variables arr,i and alement
Step 3:Initialise arr of size 10
Step 4:Get the array elements and the element to be inserted from the user
Step 5:Print the new array
Step 6:End

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10], i, element;
clrscr();
printf("Enter 5 Array Elements: ");
for(i=0; i<5; i++)
scanf("%d", &arr[i]);
printf("\nEnter Element to Insert: ");
scanf("%d", &element);
arr[i] = element;
30
printf("\nThe New Array is:\n");
for(i=0; i<6; i++)
printf("%d ", arr[i]);
getch();
}
OUTPUT :
Enter 5 Array Elements: 10
20
30
40
50
Enter Element to Insert: 60
The New Array is :
10 20 30 40 50 60

RESULT:
Thus the C program to insert 5 elements into an array and print the elements
of the array has been executed and the output has been verified.

31
4.b)C program to reverse the array of elements .

AIM:
To write a C program to reverse the array of elements

ALGORITHM:
Step 1:Start
Step 2:Declare the necessary variables
Step 3:Initialise the size of arr and j to 0
Step 4:Get the size and the elements of array from the user
Step 5:Reverse the array elements and print the reversed array
Step 6:End

PROGRAM:
#include<stdio.h>
void main()
{
int n, arr[100], i;
int rev[n], j = 0;
clrscr();
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for(i = n-1; i >= 0; i--)
{
rev[j] = arr[i];
j++;
32
}
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: 1 2 3 4 5
The Reversed array: 5 4 3 2 1

RESULT:
Thus the C program to reverse the array of elements has been executed and
the output has been verified.

33
4.c) C Program to access an element in 2-D Array.

AIM:
To write a C program to access an element in 2-D array.

ALGORITHM:
Step 1:Start
Step 2:Declare the 2-D array arr[3][3]
Step 3:Get the elements of array for each row and column from the user
Step 4:Print the elements in the array
Step 5:End

PROGRAM:
#include <stdio.h>
int main()
{
int i, j, arr[3][3];
printf("Enter the element for each row and column:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("arr[%d][%d] = ", i, j);
scanf("%d", &arr[i][j]);
}
}
//Display array
printf("\nElement present in an Array:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
34
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
return (0);
}

OUTPUT:
Enter the element for each row and column:
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 4
arr[1][1] = 5
arr[1][2] = 6
arr[2][0] = 7
arr[2][1] = 8
arr[2][2] = 9
Element present in an Array:
123
456
789

RESULT:
Thus the C program to access an element in 2-D array has been executed and
the output has been verified.

35
4.d)C Program to multiply two 3 X 3 Matrices

AIM:
To write a C program to multiply two 3 X 3 matrix.

ALGORITHM:
Step 1:Start
Step 2:Declare the 2-D arrays a[3][3], b[3][3], ans[3][3]
Step 3:Get the elements of the two arrays from the user
Step 4:Multiply both the arrays and print the product
Step 5:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], ans[3][3];
int i, j, x;

printf("Enter the first 3 by 3 matrix.\n\n");


for (i = 0; i<3; i++)
for (j = 0; j<3; j++)
scanf("%d", &a[i][j]);
printf("\n\nEnter the second 3 by 3 matrix.\n\n");
for (i = 0; i<3; i++)
for (j = 0; j<3; j++)
scanf("%d", &b[i][j]);

for (x = 0; x<3; x++)


{
36
for (i = 0; i<3; i++)
{
ans[x][i] = 0;
for (j = 0; j<3; j++)
ans[x][i] = ans[x][i] + a[x][j] * b[j][i];
}
}

printf("\n\nProduct of the matrices is.\n\n");


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

getch();

37
OUTPUT:

RESULT:
Thus the C program to multiply two 3 X 3 matrix has been executed and the
output has been verified.
38
4.e)C Program to access an element in 3-D Array

AIM:
To write a C program to access an element in 3-D array.

ALGORITHM:
Step 1:Start
Step 2:Declare the 3-D array arr[3][4][2]
Step 3:Initialise the elements of 3-D array
Step 4:Get the elements of the array from the user
Step 5:Print the elements of the array
Step 6:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, k;
int arr[3][4][2] = {
{ {2, 4}, {7, 8}, {3, 4}, {5, 6} },
{ {7, 6}, {3, 4}, {5, 3}, {2, 3} },
{ {8, 9}, {7, 2}, {3, 4}, {5, 1} }
};
clrscr();
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
for(k=0; k<2; k++)
printf("%d ", arr[i][j][k]);
39
printf("\n");
}
printf("\n");
}
getch();
}

OUTPUT:
2 4
7 8
3 4
5 6

7 6
3 4
5 3
2 3

8 9
7 2
3 4
5 1

RESULT:
Thus the C program to access an element in 3-D array has been executed and
the output has been verified.

40
5. STRING: OPERATIONS

5.a) C program to concatenate two strings

AIM:

To write a C program to concatenate two strings.

ALGORITHM:
Step 1:Start
Step 2:Declare the variables s and s1
Step 3:Initialise the string s to “hello” and s1 to “world”
Step 4:Concatenate the string using strcat(s, s1)
Step 5:Print the concatenated string
Step 6:End

PROGRAM:

Using Strcat() Function


#include <stdio.h>
#include <string.h>
void main()
{
char s[] = "hello";

char s1[] = "world";

clrscr();

// concatenating the string

41
strcat(s, s1);

printf("Final string is: %s ", s);

getch();

OUTPUT:

Final string is: hello world

Concatenate Two Strings Without Using strcat()

AIM:

To write a C program to concatenate two strings without using strcat()

ALGORITHM:

Step 1:Start

Step 2:Declare the variables s1 and s2

Step 3:Initialise the string s1 to “programming” and s2 to “is awesome”

Step 4:Concatenate the string without using strcat()

Step 5:Print the concatenated string

Step 6:End

PROGRAM:
#include <stdio.h>
void main()
42
{
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
clrscr();
// store length of s1 in the length variable
length = 0;
while (s1[length] != '\0')
{
++length;
}
// concatenate s2 to s1
for (j = 0; s2[j] != '\0'; ++j, ++length)
{
s1[length] = s2[j];
}
// terminating the s1 string
s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
getch();
}

OUTPUT :
After concatenation: programming is awesome

RESULT:
Thus the C program to concatenate two strings with and without using
strcat() has been executed and the output has been verified.

43
5.b)C program to compare two strings without using string library functions.

AIM:
To write a C program to compare two strings without using string library
functions.

ALGORITHM:
Step 1:Start
Step 2:Declare the strings str1 and str2 and the function
compare(char[],char[])
Step 3:Get the values of the strings str1 and str2 from the user
Step 4:Write the definition of the function compare(char[],char[])
Step 5:Print whether the strings are same or not
Step 6:End

PROGRAM :
#include <stdio.h>
#include<conio.h>
int compare(char[],char[]);
void main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int c;
clrscr();
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
c= compare(str1,str2); // calling compare() function
44
if(c==0)
printf("strings are same");
else
printf("strings are not same");
getch();
}
// Comparing both the strings.
int compare(char a[],char b[])
{
int flag=0,i=0; // integer variables declaration
while(a[i]!='\0' &&b[i]!='\0') // while loop
{
if(a[i]!=b[i])
{
flag=1;
break;
}
i++;
}
if(flag==0)
return 0;
else
return 1;
}

45
OUTPUT :
Enter the first string : hello
Enter the second string : hello
strings are same
2nd output:
Enter the first string : hello
Enter the second string : world
strings are not same

RESULT:
Thus the C program to compare two strings without using string library
functions has been executed and the output has been verified.

46
5.c) C program in C to copy one string to another string

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

ALGORITHM:
Step 1:Start
Step 2:Declare the variables str1 and str2
Step 3:Get the values of str1 and str2 from the user
Step 4:Copy one string to another using strcpy(str2, str1)
Step 5:Print both the string
Step 6:End

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
clrscr();
printf("Enter the string: ");
gets(str1);
printf("\nString 1 = %s", str1);
strcpy(str2, str1);
printf("\nString 2 = %s", str2);
getch();
}

47
OUTPUT:
Enter the string: hello world
String 1 = hello world
String 2 = hello world

RESULT:
Thus the C program copy one string to another string has been executed and
the output has been verified.

48
5.d) C program to accept a string and count the number of vowels present in this
string.

AIM:
To write a C program to accept a string and count the number of vowels
present in the string.

ALGORITHM:
Step 1:Start
Step 2: Declare necessary variables.
Step 3: Get the input string from the user.
Step 4: Check the conditions for checking vowels and count the no of vowels
using for loop.
Step 5: Display number of vowels.
Step 6: Stop

PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,vow=0;
clrscr();
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' ||
a[i]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')
49
vow=vow+1;
}
printf("\nTHERE ARE %d VOWELS IN THE STRING",vow);
getch();
}

OUTPUT:
ENTER A STRING: hi how are you
THERE ARE 6 VOWELS IN THE STRING

RESULT:
Thus the C program to accept a string and count the number of vowels
present in the string has been executed and the output has been verified.

50
6. FUNCTIONS: CALL, RETURN, PASSING BY(VALUES,REFERENCE),
PASSING ARRAYS TO FUNCTION

6.a) C program to add, subtract, multiply and divide two integers using user
defined type function with return type

AIM:
To write a C program to add, subtract, multiply and divide two integers using
user defined type function with return type

ALGORITHM:
Step 1: Start
Step 2: Declare four functions.
Step 3: Get two values from the user.
Step 4: Display addition, subtraction, multiplication and division of two
numbers by calling respective functions.
Step 5: Write definition for functions to perform addition, subtraction,
multiplication and division.
Step 6: Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
void main()
{
int num1, num2;
clrscr();
51
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("%d + %d = %d\n", num1, num2, add(num1, num2));
printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
printf("%d / %d = %d\n", num1, num2, divide(num1, num2));
getch();
}
int add(int n1, int n2)
{
int result;
result = n1 + n2;
return result;
}
int subtract(int n1, int n2)
{
int result;
result = n1 - n2;
return result;
}
int multiply(int n1, int n2)
{
int result;
result = n1 * n2;
return result;
}
int divide(int n1, int n2)
{
int result;
result = n1 / n2;

52
return result;
}

OUTPUT:
Enter two numbers: 20 5
20 + 5 = 25
20 – 5 = 15
20 * 5 = 100
20 / 5 = 4

RESULT:
Thus the C program to add, subtract, multiply and divide two integers using
user defined type function with return type has been executed and the output has
been verified.

53
6.b) Write a C program to swap two integers using call by value and call by
reference methods of passing arguments to a function.

AIM:
To write a C program to swap two integers using call by value and call by
reference methods of passing arguments to a function.

ALGORITHM:
Step 1: Start
Step 2: Get two values from the user
Step 3: Write function definitions to swap two values
Step 4: Pass the arguments to function either values or references based on
call by value or call by reference concept.
Step 5: Display the results of before sapping and after swapping.
Step 6: Stop

PROGRAM:
Call by value:
#include <stdio.h>
void swap(int x, int y){
int temp = x;
x = y;
y = temp;
}

int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y);
printf("Values after swap: x = %d, y = %d", x,y);
54
}

OUTPUT:
Values before swap: x = 10, y = 11
Values after swap: x = 11, y = 10

Call by reference:
#include <stdio.h>
#include<conio.h>
void swap (int *, int *);
void main()
{
int a, b;
clrscr();
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
void swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

55
OUTPUT:
Enter value of a & b: 10 20

Before Swapping:

a = 10

b = 20

After Swapping:

a = 20

b = 10

RESULT:
Thus the C program to swap two integers using call by value and call by
reference methods of passing arguments to a function has been executed and the
output has been verified.

56
6.c) Write a C program to check whether a number is prime, Armstrong or perfect
number using functions.

AIM:
To write a C program to check whether a number is prime, Armstrong or
perfect number using functions.

ALGORITHM:
Step 1: Start
Step 2: Declare three functions to check for given number is prime ,Armstrong
and perfect .
Step 3: Get the number from the user.
Step 4: Call three functions that will check the given no is prime ,Armstrong
and perfect and return the result and display the results.
Step 5: Write definition for three functions.
Step 6: Stop.

PROGRAM:
#include <stdio.h>
#include <math.h>
int Check_Armstrong (int Number);
int Perfect_Number(int Number);
int Prime_Number(int Number);
void main()
{
int Number;
printf("\nPlease Enter Number to Check whether it is an Armstrong,
Prime, or Perfect : ");
scanf("%d", &Number);
if (Check_Armstrong(Number))
printf("\n %d is an Armstrong Number.", Number);
57
else
printf("\n %d is not an Armstrong Number.", Number);

if(Prime_Number(Number) )
printf("\n %d is a Prime Number", Number);
else
printf("\n %d is Not a Prime Number", Number);

if (Perfect_Number(Number) )
printf("\n %d is a Perfect Number", Number) ;
else
printf("\n %d is not a Perfect Number", Number) ;

getch();
}
int Check_Armstrong (int Number)
{
int Temp, Reminder, Times = 0, Sum = 0;
Temp = Number;

while (Temp != 0)
{
Times = Times + 1;
Temp = Temp / 10;
}

for(Temp = Number; Temp > 0; Temp = Temp /10 )


{
Reminder = Temp % 10;
Sum = Sum + pow(Reminder, Times);

58
}
if ( Number == Sum )
return 1;
else
return 0;
}

int Perfect_Number(int Number)


{
int i, Sum = 0 ;

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


{
if(Number % i == 0)
Sum = Sum + i ;
}

if (Sum == Number)
return 1;
else
return 0;
}
int Prime_Number(int Number)
{
int i, Count = 0;

for (i = 2; i <= Number/2; i++)


{
if(Number%i == 0)
{

59
Count++;
}
}
if(Count == 0 && Number != 1 )
return 1;
else
return 0;
}

OUTPUT:
Please Enter Number to Check whether it is an Armstrong, Prime, or Perfect :
11
11 is not an Armstrong Number
11 is a Prime Number
11 is not a Perfect Number

RESULT:
Thus the C program to check whether a number is prime, Armstrong or
perfect number using functions has been executed and the output has been verified.
60
7.RECURSION

7.a) C program to calculate factorial of a number using recursion.

AIM:
To write a C program to calculate the factorial of a number using recursion.

ALGORITHM:
Step 1:Start
Step 2:Declare the variables x and n
Step 3:Get the number to find the factorial from the user
Step 4:call the function fact(n)
Step 5:call the same function fact(n-1) to return n*fact(n-1)
Step 6:display the factorial of n value
Step 7:End
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int x,n;
clrscr();
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
getch();
}
int fact(int n)
{
61
if(n==0)
return(1);
return(n*fact(n-1));
}

OUTPUT:
Enter the Number to Find Factorial :5
Factorial of 5 is 120

RESULT:
Thus the C program to calculate the factorial of a number using recursion has
been executed and the output has been verified.

62
7.b) Write a C program to read an integer number and print the reverse of that
number using recursion.

AIM:
To write a C program to read an integer number and to print the reverse of
that number using recursion

ALGORITHM:
Step 1:Start
Step 2:Declareand initialise the variable the sum to 0
Step 3:Write the definition for the function revNumFunc(int num) to reverse
the given number by calling the same function for revNumFunc(num/10) and
return the result
Step 4:In main function we display the result by calling revNumFunc(num)
Step 5:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
static int sum=0,rem;
int revNumFunc(int num){
if(num){
rem=num%10;
sum=sum*10+rem;
revNumFunc(num/10);
}
return sum;
}
void main()
{
63
int num = 1357 ,revNum;
clrscr();
revNum=revNumFunc(num);
printf("The number after reversing is :%d",revNum);
getch();
}

OUTPUT:
The number after reversing is :7531

RESULT:
Thus the C program to read an integer number and to print the reverse of that
number using recursion has been executed and the output has been verified.

64
7.c) C program to find power of any number using recursion.

AIM:
To write a C program to find the power of any number using recursion.

ALGORITHM:
Step 1:Start
Step 2: Define the function powRec(int a,int b) and return a*powRec(a,b-1) by
calling the same function
Step 3: print a ^b by calling powRec(a,b)
Step 4:End

PROGRAM:
#include <stdio.h>
#include <math.h>
#include<conio.h>
int powRec(int a,int b)
{
if(b==0)
return 1;
return a*powRec(a,b-1);
}
void main()
{
int a = 2,b=3;
clrscr();
printf("The number %d to the power %d is %d",a,b,powRec(a,b));
getch();
}

65
OUTPUT:
The number 2 to the power 3 is 8

RESULT:
Thus the C program to find the power of any number using recursion has
been executed and the output has been verified.

66
7.d) C program to generate Fibonacci series using recursive function

AIM:
To write a C program to generate Fibonacci series using the recursive
function.

ALGORITHM:
Step 1: Start
Step 2:declare the function fibonacci(int term).
Step 3:Get the number of terms from Fibonacci series from the user
Step 4:Print fibanacci series by calling the function fibonacci(counter))
Step 5:write the definition for the function fibonacci(int term),it return
fibonacci(term-1) + fibonacci(term-2) by calling the same function.
Step 6:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
int fibonacci(int term);
void main(){
int terms, counter;
clrscr();
printf("Enter number of terms in Fibonacci series: ");
scanf("%d", &terms);
printf("Fibonacci series till %d terms\n", terms);
for(counter = 0; counter < terms; counter++)
{
printf("%d ", fibonacci(counter));
}
getch();
}
67
int fibonacci(int term){
/* Exit condition of recursion*/
if(term < 2)
return term;
return fibonacci(term-1) + fibonacci(term-2);
}

OUTPUT:
Enter number of terms in Fibonacci series: 9
Fibonacci series till 9 terms
0 1 1 2 3 5 8 13 21

RESULT:
Thus the C program to generate Fibonacci series using the recursive function
has been executed and the output has been verified.

68
8. POINTERS: POINTERS TO FUNCTION, ARRAYS, STRINGS,
POINTERS TO POINTERS, ARRAY OF POINTERS.

8. a) a C program to find biggest among three numbers using pointer

AIM:
To write a C program to find the biggest among three numbers using pointer.

ALGORITHM:
Step 1:Start
Step 2:Declare three variables a,b,c and three pointer variables p1,p2,p3
Step 3: Get the values of a,b and c from the user
Step 4:Assign address of a,b,c to the pointer variables p1,p2 and p3.
Step 5:Check the condition to find the greatest among three numbers and
print it.
Step 6: End

PROGRAM:
#include < stdio.h >
#include<conio.h>
void main( )
{
int a, b, c ;
int *p1, *p2, *p3 ;
clrscr();
printf(" Enter the first number : ");
scanf("%d",&a);
printf("\n Enter the second number : ");
scanf("%d",&b);
printf("\n Enter the third number : ");
scanf("%d",&c);
69
p1=&a;
p2=&b;
p3=&c;
if((*p1>=*p2)&&(*p1>=*p3))
printf("\n The Greatest Number is : %d ",*p1) ;
else
if((*p2>=*p1)&&(*p2>=*p3))
printf("\n The Greatest Number is : %d ",*p2) ;
else
printf("\n The Greatest Number is : %d ",*p3) ;
getch();
}

OUTPUT:
Enter the first number : 9
Enter the second number : 7
Enter the third number : 8
The Greatest Number is : 9

RESULT:
Thus the C program to find the biggest among three numbers using pointer
has been executed and the output has been verified

70
8.b) Write a C program to input and print array elements using pointer.

AIM:
To write a C program to find the input and print the array elements using
pointer.
ALGORITHM:
Step 1: Start
Step2: Declare the array variable data.
Step3: Get the elements of an array data from the user.
Step4: Print the array elements using pointer
Step5: Stop

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main() {
int data[5],i;
clrscr();
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
getch();
}

71
OUTPUT:
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4

RESULT:
Thus C program to find the input and print the array elements using pointer
has been executed and the output has been verified.

72
8.c) C program to pass pointers to function

AIM:
To write a C program to pass pointers to function.

ALGORITHM:
Step 1: Start
Step 2: Write definition to the function addOne(int* ptr) to increment ptr
variable
Step 3: In main function initialize i to 10 and p to &i
Step4: call the function addOne(p) and print the value of p.
Step5:Stop

PROGRAM:
#include <stdio.h>
#include<conio.h>
void addOne(int* ptr)
{
(*ptr)++; // adding 1 to *ptr
}
void main()
{
int* p, i = 10;
clrscr();
p = &i;
addOne(p);
printf("Value=%d", *p); // 11
getch();
}

73
OUTPUT:
Value=11

RESULT:
Thus C program to pass pointers to function has been executed and the
output has been verified.

74
8.d) C program to demonstrate pointer to pointer

AIM:
To write C Program to demonstrate pointer to pointer.

ALGORITHM:
Step1:Start
Step2:declare the variable var,pinter variable ptr2,double pointer variable
ptr1.
Step3:initialize ptr to &var and ptr1 to &ptr2.
Step4:Display the values of var, *ptr2 and **ptr1

PROGRAM:
#include <stdio.h>
void main()
{
int var = 789;
// pointer for var
int* ptr2;
// double pointer for ptr2
int** ptr1;
clrscr();
// storing address of var in ptr2
ptr2 = &var;
// Storing address of ptr2 in ptr1
ptr1 = &ptr2;
// Displaying value of var using
// both single and double pointers
printf("Value of var = %d\n", var);
printf("Value of var using single pointer = %d\n", *ptr2);
75
printf("Value of var using double pointer = %d\n", **ptr1);
getch();
}

OUTPUT:
Value of var = 789
Value of var using single pointer = 789
Value of var using double pointer = 789

RESULT:
Thus C Program to demonstrate pointer to pointer has been executed and the
output has been verified.

76
8.e) C Program to print array of pointers

AIM:
To write C Program to print Array of Pointers.

ALGORITHM:
Step1:Start
Step2:Declare array of pointer arr[] instead of 2D array
Step3:Initailize array of pointer var arr[]
Stpe4:Print the array elements
Step5:End

PROGRAMS:
void main()
{
char *arr[] = {"xxxx", "yyyyy", "zzzzz"};
printf("String array Elements are:\n");

for (int i = 0; i < 3; i++)


{
printf("%s\n", arr[i]);
}
getch();
}

77
OUTPUT:
String array Elements are:
xxxx
yyyyy
zzzzz

RESULT:
Thus C Program to print Array of Pointers has been executed and the output
has been verified.

78
9. STRUCTURES:NESTED STRUCTURES, POINTERS TO STRUCTURES,

ARRAY OF STRUCTURES AND UNIONS.

9.a)C program to store information of 5 students in structure and display it(Array


of Structure).

AIM:
To write a C program to store information of 5 students in structure and
display it.

ALGORITHM:
Step 1: Start

Step 2: Create a structure students with three fields for name,roll number and
marks.

Step 3: Declare array structure variable s[5].

Step 4: Get the information for structure variable(s[0],s[1],..,s[5]) from the


user.

Step 5: Display Students information

Step 6: Stop

PROGRAM:

#include <stdio.h>

#include<conio.h>

struct student {

char firstName[50];

int roll;

float marks;

} s[5];

79
void main() {

int i;

clrscr();

printf("Enter information of students:\n");

// storing information

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

s[i].roll = i + 1;

printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

printf("Enter marks: ");

scanf("%f", &s[i].marks);

printf("Displaying Information:\n\n");

// displaying information

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

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");

puts(s[i].firstName);

printf("Marks: %.1f", s[i].marks);

printf("\n");

getch();

80
OUTPUT:

Enter information of students:

For roll number1,

Enter name: Tom

Enter marks: 98

For roll number2,

Enter name: Jerry

Enter marks: 89

Displaying Information:

Roll number: 1

Name: Tom

Marks: 98

RESULT:

Thus the C program to store information of 5 students in structure and


display it has been done and the output has been verified.
81
9.b)C program to create ,declare and initialize structure

AIM:

To write a C program to create ,declare and initialize structure.

ALGORITHM:
Step 1: Start

Step 2: Create structure employee

Step 3: Initialize structure variable emp

Step 4: Display the values of emp variable

Step 5: Stop

PROGRAM:

#include <stdio.h>

#include<conio.h>

/*structure declaration*/

struct employee {

char name[30];

int empId;

float salary;

};

void main()

/*declare and initialization of

structure variable*/

struct employee emp = { "xxxx", 1120, 76909.00f };

clrscr();
82
printf("\n Name: %s", emp.name);

printf("\n Id: %d", emp.empId);

printf("\n Salary: %f\n", emp.salary);

getch();

OUTPUT:

Name: xxxx

Id: 1120

Salary: 76909.000000

RESULT:

Thus the C program to create ,declare and initialize structure has been done
and the output has been verified.

83
9.c) C program to demonstrate nested structure.

AIM:
To write a C program to demonstrate nested structure.

ALGORITHM:

Step 1: Start

Step 2: Create structure Complex.

Step 3: Create structure number and declare complex structure variable comp
to implement nested structure.

Step 4: Display values of Complex structure using the number structure


variable num.

PROGRAM:

#include <stdio.h>
#include<conio.h>
struct complex
{
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
void main()
{
// initialize complex variables
num1.comp.imag = 11;
num1.comp.real = 5.25;
// initialize number variable
num1.integer = 6;
clrscr();
// print struct variables
printf("Imaginary Part: %d\n", num1.comp.imag);
84
printf("Real Part: %.2f\n", num1.comp.real);
printf("Integer: %d", num1.integer);
getch();
}

OUTPUT:
Imaginary Part: 11
Real Part: 5.25
Integer: 6

RESULT:
Thus the C program to demonstrate nested structure has been done and the
output has been verified.

85
9.d) C program To access members of a structure using pointers(pointers to
structures)

AIM:
To write a C program To access members of a structure using pointers
(pointers to structures)

ALGORITHM:
Step 1: Start
Step 2: Declare Structure person
Step 3: Declare one pointer variable personPtr to store the address of structure
variable person1
Step 4: Get the values for person1 by using Pointervariable and -> operator
Step 5: Display the values
Step 6: Stop

PROGRAM:
#include <stdio.h>
#include<conio.h>
struct person
{
int age;
char name[10];
};
void main()
{
struct person *personPtr,person1;
personPtr=&person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter name: ");
scanf("%s", &personPtr->name);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("Name: %s", personPtr->name);
getch();
}

86
OUTPUT:
Enter age: 35
Enter name: xxyyzz
Displaying:
Age: 35
Name: xxyyzz

RESULT:
Thus the C program To access members of a structure using pointers
(pointers to structures) has been done and the output has been verified.

87
9.e) C program To create and access members of a union.

AIM:
To write a C program to create and access members of a union.

ALGORITHM:
Step 1: Start
Step 2: Create Union Data
Step 3: Declare and initialize union variable t.
Step 4: Display values of t.
Step 5: Stop

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

union data
{
int var1;
double var2;
char var3;
};

void main()
{
union data t;

t.var1 = 10;
printf("t.var1 = %d\n", t.var1);

t.var2 = 20.34;
printf("t.var2 = %f\n", t.var2);

t.var3 = 'a';
printf("t.var3 = %c\n", t.var3);

printf("\nSize of Union: %d", sizeof(t));

getch();
}
88
OUTPUT:

t.var1 = 10
t.var2 = 20.340000
t.var3 = a

Size of union: 8

RESULT:
Thus the C program to create and access members of a union has been done
and the output has been verified.

89
10.FILES : READING AND WRITING, FILE POINTERS, FILE OPERATIONS,
RANDOM ACCESS, PROCESSOR DIRECTIVES

10.a) C program to create a file called emp.rec and store information about a
person, in terms of his name, age and salary.

AIM:
To write a program to create a file called emp.rec and store information about
a person, in terms of his name, age and salary.

ALGORITHM:
Step 1: Start
Step 2: create a file called emp.rec in write mode
Step 3:Check for the condition for file existence
Step 4: if file exist get the details of person(name, age and salary) from users
and write these details in the file emp.rec using fprintf().
Step5:End

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();

fptr = fopen ("emp.rec", "w"); /*open for writing*/

if (fptr == NULL)
{
printf("File does not exists\n");
return;
}
printf("Enter the name\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
90
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
getch();
}

OUTPUT:
Enter the name
Padmanabhan
Enter the age
45
Enter the salary
32000
-------------------------------------
Note: Please note that you have to open the file called emp.rec in the
directory(C:\TC\BIN) and check these details are written in that file

Name = Padmanabhan
Age = 45
Salary = 32000.00

RESULT:

Thus the C program to create a file called emp.rec and store information
about a person, in terms of his name, age and salary has been done and the output
has been verified.

91
10.b) C program to list all files and sub-directories in a directory

AIM:

To write a C program to list all files and sub-directories in a directory

ALGORITHM:
Step 1: Start
Step 2: Declare pointer variable for directory entry
Step 3: Declare and initialize pointer variable dr by using opendir(“.”)
Step 4:check for the condition to open directory
Step 5:If can open a directory, print all files and sub-directories in a directory
by using de->d_name to access the files
Step 6: Stop

PROGRAM:

#include <stdio.h>
#include<conio.h>
#include <dirent.h>

void main(void)
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
clrscr();
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );

}
while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);

closedir(dr);
getch();
}

92
OUTPUT:-(All Files and Subdirectory of Current Directory)

RESULT:

Thus the C program to list all files and sub-directories in a directory has been
done and the output has been verified.
93
10.c) C Program to print contents of file.

AIM:

To write a C Program to print contents of file.

ALGORITHM:
Step 1: Start

Step 2: declare file pointer variable fp

Step 3: Get the filename from the user to read

Step 4: Open a file in read mode using fopen();

Step 5: Check for the condition for file existence

Step 6: If file exist,read the content of file using fscanf() and print the content
of file.

Step 7: Stop

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

char fname[20], str[500];

FILE *fp;

clrscr();

printf("Enter the Name of File: ");

gets(fname);

fp = fopen(fname, "r");

if(fp==NULL)

94
printf("Error Occurred while Opening the File!");

else

fscanf(fp, "%[^\0]", str);

printf("\nContent of File is:\n\n");

printf("%s", str);

fclose(fp);

getch();

95
OUTPUT:

Note : Enter an existing file name to read the content of file and display.

RESULT:

Thus the C program to write a C Program to print contents of file has been
done and the output has been verified.

96
10.d)C program to demonstrate random access file using fseek().

AIM:
To write a C program to demonstrate random access file using fseek().

ALGORITHM:
Step1: Start
Step 2: Declare file pointer variable f1
Step 3: open a function example.txt file in read mode
Step 4: read the contents of file using fgets()and print that content.
Step 5: use fseek() function to access randomly and print according to
required.
Step 6: Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char str[80] , s;
int n;
clrscr();
f1=fopen("example.txt","r");
fgets(str,80,f1);
printf("%s",str);
fseek(f1,2,0);
/*File pointer jumped to 3rd character from beginning*/
s=getc(f1);
printf("\n%c",s);
fseek(f1,2,1);
/*File pointer jumped to 3rd character from current
position*/
s=getc(f1);
printf("\n%c",s);
fseek(f1,0,2);
s=getc(f1);

97
printf("\n%c",s);
fclose(f1);
getch();
}

OUTPUT :

Note: contents in example.txt

RESULT:

Thus the C program to demonstrate random access file using fseek() has been
done and the output has been verified.

98

You might also like