PC Lab Manual PDF
PC Lab Manual PDF
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
PROGRAMMING IN C
LAB MANUAL
1
PAGE
Ex. No. NAME OF THE EXPERIMENTS
NO
I/O STATEMENTS
ARRAYS
2
POINTERS
STRUCTURES
FILES
3
Ex .No. 1(a)
Find the Sum and Average of Three Numbers
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : Declare integer variables a, b, c , sum and avg
Step 3 : Read values a, b and c
Step 4 : Add a,b and c and assign the result to sum
Step 5 : find average of a,b and c
Step 6 : display sum and average
Step 7 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
int sum,avg;
clrscr();
printf("enter three integer:");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
avg=sum/3;
printf("sum=%d and avg=%d of 3 integers:",sum,avg);
getch();
return 0;
}
4
OUTPUT
RESULT
Thus the C program to find sum the average of three numbers has been executed
successfully and the result was verified.
5
Ex .No. 1(b)
Calculate the Sum of Individual Digits of Positive Integer
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : Read Number n
Step 3 : Initialize the sum=0
Step 4 : if n>0 then calculate remainder r =n%10
Step 5 : Add remainder r to sum
Step 6: store n/10 value to n
Step 7 : if n=0 then display sum
Step 8 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
clrscr();
printf("Enter a positive integer\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("The sum of individual digits of a positive integer is %d",sum);
getch();
}
6
OUTPUT
234
RESULT
Thus the C program to find the sum of individual digits of positive integer has been
executed successfully and the result was verified.
7
Ex .No. 1(c)
Evaluate Area of Triangle with 3 Sides given
Date :
AIM:
To write a c program to find the evaluate area of a triangle with 3 sides given.
ALGORITHM
Step3: s=(a+b+c)/2
Step4: A=sqrt(s*(s-a)(s-b)(s-c))
Step5: Print A
FLOWCHART
START
Input a,b,c
s=(a+b+c)/2
A=sqrt (s*(s-a)(s-b)(s-c))
Display A
STOP
8
PROGRAM
#include<stdio.h>
#include<math.h>
void main()
int a,b,c;
float s,area;
clrscr();
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
getch();
OUTPUT
20
30
RESULT
Thus the C program to evaluate area of triangle with 3 sides given has been executed
successfully and the result was verified.
9
Ex .No. 2(a)
Calculate the Arithmetic Operations using Switch Statement
Date :
AIM
ALGORITHM
Step 1: Start.
Step 2 : Accept two numbers a and b from the user.
Step 3: Display the menu and select the choice
Step 4 : if choice = ‘+’ Perform addition and store the result in c.
Step 5 : if choice = ‘-’ Perform subtraction and store the result in c.
Step 6 : if choice = ‘*’ Perform multiplication and store the result in c.
Step 7 : if choice = ‘/’ Perform subtraction and store the result in c.
Step 8 : else the default statement is execute.
Step 9 : if choice =’e’ exit from the loop
Step 10: End
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char op;
clrscr();
printf("enter a and b:");
scanf("%d%d",&a,&b);
do
{
printf("\n\n MENU\n");
printf("+ Addition\n");
printf("- Subtraction\n");
printf("* Multiplication\n");
printf("/ Division\n");
printf("% Remainder\n");
printf("E Exit\n");
printf("Enter your choice:");
op=getchar();
if (op=='E' || op=='e')
exit(1);
10
switch(op)
{
case '+':
printf("Addition\n");
c=a+b;
printf("sum=%d\n",c);
break;
case '-':
printf("Subtraction\n");
c=a-b;
printf("Difference=%d\n",c);
break;
case '*':
printf("Multiplication\n");
c=a*b;
printf("Product=%d\n",c);
break;
case '/':
printf("Division\n");
c=a/b;
printf("Quotient=%d\n",c);
break;
case '%':
printf("Remainder\n");
c=a%b;
printf("Remainder=%d\n",c);
break;
default:
printf("Invalid Option\n");
break;
}
}
while(1);
}
11
OUTPUT
MENU
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
E Exit
Enter your choice: +
Addition
sum=30
MENU
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
E Exit
Enter your choice: E
RESULT
12
Ex .No.2(b)
Implement Different String Operations
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : get the text for string1 and string2
Step 3 : compare string1 and string 2 are equal or different using string comparison function
and display the values
Step 4 : add string1 and string2 using string concatenation function and display the values
Step 5 : copy from string1 to string2 using string copy function and print string2
Step 6 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("Enter string 1\n");
scanf("%s",&s1);
printf("Enter string 2\n");
scanf("%s",&s2);
if (strcmp(s1,s2)==0)
{
printf("string comparison\n");
printf("string1 and string2 are equal\n");
}
else
{
printf("string1 and string2 are differernt\n");
}
strcat(s1,s2);
printf("Output string after concatenation : %s \n",s1);
strcpy(s1,s2);
printf("String copy from string2 to string1 is : %s \n",s1);
13
getch();
}
OUTPUT
Enter string 1
computer
Enter string 2
program
RESULT
Thus the C Program to implement different string operations has been executed
successfully and the result was verified.
14
Ex .No. 3(a)
Check whether the number is Even or Odd
Date :
AIM
ALGORITHM
Step 1: Start
Step 2: Receive data From keyboard
Step 3: If Num Percent 2 == 0. Even Integer Should Be Printed.
Step 4: Else, Print An Odd Number
Step 5: Print An Odd Number
Step 6: End
PROGRAM
#include<stdio.h>
#include<stdio.h>
int main()
{
int number=0;
clrscr();
printf("enter a number");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}
getch();
return 0;
}
15
OUTPUT
enter a number
5 is odd number
RESULT
Thus the C program to find whether the given number is even or odd number has been
executed successfully and the result was verified.
16
Ex .No. 3(b)
Even Numbers using Break Statement
Date :
AIM
ALGORITHM
step 1: start
step 2: print even numbers from 2 to 50 using for loop
step 3: if i==10, break the loop
step 4 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
clrscr();
for(i=2; i<=50; i=i+2)
{
if(i==10)
{
break;
}
printf("%d ",i);
}
getch();
return 0;
}
17
OUTPUT
2468
RESULT
Thus the C program to print even numbers using break statement has been executed
successfully and the result was verified.
18
Ex .No. 3(c)
Date : Find and display multiplication table of a particular number
AIM
ALGORITHM
Step 1: Start
Step 2: Input num, the number for which multiplication table is to be printed.
Step 3: Print num x i = num * i
Step 4: process step 3 until i<=10 using goto statement
Step 5: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i=1;
clrscr();
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%dx%d=%d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
getch();
return 0;
}
19
OUTPUT
6x1=6
6x2=12
6x3=18
6x4=24
6x5=30
6x6=36
6x7=42
6x8=48
6x9=54
6x10=60
RESULT
Thus the C program to find and display multiplication table of a particular number has
been executed successfully and the result was verified.
20
Ex .No. 4(a)
Construct a Fibonacci Series upto N Terms
Date :
AIM
Algorithm
step 1: start
step 2: declare variable t1=0, t2=1, next n, i
step 3: read n
step 4: repeat this step until i <= n:
printf t1
next=t1+t2;
t1=t2;
t2=next;
step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,t1=0,t2=1,next;
clrscr();
printf("Enter the number");
scanf("%d",&n);
printf("Fibanacci series");
for(i=1;i<=n;++i)
{
printf("%d\t",t1);
next=t1+t2;
t1=t2;
t2=next;
}
getch();
return 0;
}
21
OUTPUT
Fibanacci series 0 1 1 2 3
RESULT
Thus the C program to construct a Fibonacci series up to n terms has been executed
successfully and the result was verified.
22
Ex .No. 4(b)
Find the Given Number is Prime or Not
Date :
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int m,n,i, flag=0;
clrscr();
printf("\n Enter a positive integer value:");
scanf("%d",&n);
m=n/2;
i=2;
do
{
if((n!=2) && (n%i==0))
{
flag=1;
break;
}
i++;
}while(i<=m);
if((flag==0) && (n!=1))
printf("\n %d is a prime number.",n);
else
23
printf("\n%d is not a prime number.",n);
getch();
}
OUTPUT
7 is a prime number.
RESULT
Thus the C program to find the given number is prime or not has been executed
successfully and the result was verified.
24
Ex .No. 4(c)
Find the Given Number is Armstrong or Not
Date :
AIM
To write a c program to find the given number is Armstrong or not.
ALGORITHM
step 1 : input the number.
step 2 : initialize sum=0 and temp=number.
step 3 : find the total number of digits in the number.
step 4 : repeat until (n> 0)
step 5 : r = n % 10
step 6 : sum = sum + (r*r*r)
step 7 : n = n/10
step 8 : if (temp == sum)
step 9 : display "armstrong"
step 10: else
step 11: display "not armstrong"
step 12: stop
PROGRAM
#include<stdio.h>
#include<conio.h>;
int main()
{
int n,r,sum=0,temp;
clrscr ();
printf("Enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number");
else
printf("not armstrong number");
getch ();
return 0;
25
}
OUTPUT
armstrong number
RESULT
Thus the C program to check whether a given number is Armstrong or not has been
executed successfully and the output was verified.
26
Ex .No. 5(a)
Calculate the Area of Square
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : call the function area
Step 3 : Input square_side
Step 3 : calculate square_area = squre_side * square_side
Step 4 : print square_area
Step 5 : stop
PROGRAM
#include <stdio.h>
#include<conio.h>
void area();
void main()
{
clrscr();
area();
getch();
}
void area()
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
printf("\nArea of Square = %d",square_area);
}
27
OUTPUT
Area of Square = 16
RESULT
Thus the C program to calculate the area of square has been executed successfully and
the result was verified.
28
Ex .No. 5(b)
Find the Bubble Sort using Array to Function
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : input n
Step 3 : enter element one by one until n
Step 4 : call bubble sort function
Step 5 : Starting with the first element(index = 0), compare the current element with the next
element of the array.
Step 6 : If the current element is greater than the next element of the array, swap them.
Step 7 : If the current element is less than the next element, move to the next element.
Repeat Step 5.
Step 8 : display all sorted values
Step 9 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void bubble_sort(long [], long);
void main()
{
long array[100], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%ld", &n);
printf("Enter %ld elements one by one\n", n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%ld\n", array[c]);
getch();
}
29
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
OUTPUT
RESULT
Thus the C program to find the bubble sort using array to function has been executed
successfully and the result was verified.
30
Ex .No. 5(c) Find Maximum and Minimum Between Two Numbers using
Date : Functions
AIM
To write a c program to find maximum and minimum between two numbers using
functions.
ALGORITHM
Step 1 : start
Step 2 : input num1 and num2
Step 3 : call the maximum function
Step 4 : if num1 is maximum return num1 otherwise return num2
Step 5 : call the minimum function
Step 4 : if num2 is minimum return num2 otherwise return num1
Step 5 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int max(int num1, int num2);
int min(int num1, int num2);
void main()
{
int num1, num2, maximum, minimum;
clrscr();
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
maximum = max(num1, num2);
minimum = min(num1, num2);
printf("\nMaximum = %d\n", maximum);
printf("Minimum = %d", minimum);
getch();
}
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
int min(int num1, int num2)
{
return (num1 > num2 ) ? num2 : num1;
}
31
OUTPUT
Maximum = 7
Minimum = 2
RESULT
Thus the C Program to find the maximum and minimum number between two
numbers using function has been executed successfully and the output was verified.
32
Ex .No. 6(a)
Swapping Numbers Using Call by Value
Date :
AIM
ALGORITHM
Step 1: Start
Step 2: input x and y
Step 3: Call the function swap(&x,&y)
Step 4: Start fuction
Step 5 : move x and y value to and b
Step 6: Assign temp ← *b
Step 7: Assign *b ← *a
Step 8: Assign *a ← temp
Step 9: Assign *b ← temp
Step 10: End function
Step 11: Print x and y
PROGRAM
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",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);
}
33
OUTPUT
RESULT
Thus the C Program to swap two numbers using functions has been executed
successfully and the result was verified.
34
Ex .No. 6(b)
Swapping Numbers using Call by Reference
Date :
AIM
ALGORITHM
Step 1: Start
Step 2: input x and y
Step 3: Call the function swap(&x,&y)
Step 4: Start fuction
Step 5 : move x and y value to and b
Step 6: Assign temp ← *b
Step 7: Assign *b ← *a
Step 8: Assign *a ← temp
Step 9: End function
Step 10: Print x and y
Step 11: Stop
PROGRAM
#include<stdio.h>
#include<stdio.h>
void swap(int*, int*);
void main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch();
}
OUTPUT
RESULT
Thus the C Program to swap two numbers using functions has been executed
successfully and the result was verified.
36
Ex .No. 6(c)
Find the Factorial of a Given Number using Recursion
Date :
AIM
ALGORITHM
Step 1 : start
Step 2 : read number
Step 3 : call factorial(n)
Step 4 : if n==0 return 1
Step 5 : else return (n*factorial(n-1) to fact
Step 6 : display fact
Step 7 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
getch();
}
37
OUTPUT
Enter a number: 5
Factorial of 5 is 120
RESULT
Thus the C Program to find the factorial of a given number has been executed
successfully and verified.
38
Ex .No. 7(a)
Search an Element in an Array
Date :
AIM
ALGORITHM
Step 1 : Start
Step 2 : Iterate the array using the loop.
Step 3 : Check whether the given key present in the array i.e. a[i] == key.
Step 4 : If yes, print "Search Found".
Step 5 : Else print "Search Not Found".
Step 6 : Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,n,key;
clrscr();
printf("Enter size of the array:");
scanf("%d",&n);
printf("Enter elements in array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element found %d in the position=%d",key,i+1);
getch();
return 0;
}
}
printf("Element %d not found",key);
getch();
return 0;
}
39
OUTPUT
RESULT
Thus the C program to search an element in an array has been executed successfully
and the result was verified.
40
Ex .No. 7(b)
Addition of Two Matrix using Two Dimensional Array
Date :
AIM
ALGORITHM
Step 1 : Start
Step 2 : Get the number of rows and columns
Step 3 : Calculate the number of rows and columns present in the array a & b (as
dimensions of both the arrays are same) and store it in variables rows and cols
respectively.
Step 4 : Declare another array sum with the similar dimensions.
Step 5: Loop through the arrays a and b, add the corresponding elements
e.g a11 + b11 = sum11
Step 6 : Display the elements of array sum
Step 7 : Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,a[10][10],b[10][10],sum[10][10],i,j;
clrscr();
printf("Enter the number of rows (between 1 to 10): ");
scanf("%d",&r);
printf("Enter the number of columns (between 1 to 10): ");
scanf("%d",&c);
printf("\n Enter elements of 1st matrix:\n\n");
for(i=0;i<r;++i)
for(j=0; j<c;++j)
{
printf("Enter element of a %d row %d column: ", i+1,j+1);
scanf("%d",&a[i][j]);
}
41
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
OUTPUT
RESULT
Thus the C program to add two matrix using two dimensional array has been executed
successfully and the result was verified.
42
Ex .No. 7(c) Update the Elements of 3D Array Either by Specifying the Element
to be Replaced or by Specifying the Position where Replacement has
Date :
to done
AIM
ALGORITHM
Step 1 : Start
Step 2 : Get and print the values of 3 dimensional array
Step 3: Enter the row and column number for update
Step 4: get the new value for update
Step 5 : update old value to new value of that particular row and column
Step 6 : Print the all values of 3 dimensional array
Step 7 : Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int i,j,k;
int num;
void main()
{
int arr[2][3][3];
printf("Enter the values in the array:\n\n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("the value at arr[%d][%d][%d]:",i,j,k);
scanf("%d",&arr[i][j][k]);
}
}
}
printf("\nprinting the values in array:\n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
43
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
printf("\nenter the block row and column number:\n");
scanf("%d%d%d",&i,&j,&k);
printf("enter the new number you want to update with:");
scanf("%d",&num);
arr[i][j][k]=num;
printf("\narray after updating:\n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
getch();
}
44
OUTPUT
987
654
321
987
654
321
RESULT
Thus the C program to update the elements of 3D array either by specifying the
element to be replaced or by specifying the position where replacement has to done has been
executed successfully and the result was verified.
45
Ex .No. 8(a)
Area of a Rectangle using Function Pointer
Date :
AIM
To write a c program to function pointer to call the function that returns the area of a
rectangle in c.
ALGORITHM
Step 1 : start
Step 2 : input length and breath
Step 3 : call function through pointer
Step 4 : calculate area of rectangle
Step 5 : display area of rectangle
Step 6 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int areaRectangle(int, int);
int (*fp)(int, int);
int main()
{
int length, breadth, area;
clrscr();
fp = areaRectangle;
46
OUTPUT
RESULT
Thus the C program to create a function pointer to call the function that returns the
area of a rectangle has been executed successfully and the result was verified.
47
Ex .No. 8(b)
Address and Value using Pointer to Pointer
Date :
AIM
To write a c program to print the address and value using pointer to pointer.
ALGORITHM
Step 1 : start
Step 2 : declare a=10, *p1, **P2
Step 3 : address of a is stored in pointer p1
Step 4 : address of p1 is stored in double pointer p2
Step 5 : print address of a and p1 and p2
Step 6 : print value at the address stored by p2 and p1
Step 7 : print value of **p
Step 8 : stop
PROGRAM
#include <stdio.h>
#include<conio.h>
int main() {
int a = 10;
int *p1;
int **p2;
clrscr();
p1 = &a;
p2 = &p1;
printf("Address of a = %u\n", &a);
printf("Address of p1 = %u\n", &p1);
printf("Address of p2 = %u\n\n", &p2);
printf("Value at the address stored by p2 = %u\n", *p2);
printf("Value at the address stored by p1 = %d\n\n", *p1);
printf("Value of **p2 = %d\n", **p2);
getch();
return 0;
}
48
OUTPUT
Address of a = 65524
Address of p1 = 65522
Address of p2 = 65520
Value of **p2 = 10
RESULT
Thus the C program to print the address and value using pointer to pointer has been
executed successfully and the result was verified.
49
Ex .No. 8(c)
Address and Value using Array Pointers
Date :
AIM
To write a c program to print address and value using array pointers.
ALGORITHM
Step 1 : start
Step 2 : declare and initialize p=40 ,q=60 ,r=90 *arr[3]
Step 3 : store address of p,q and r to the array
Step 4 : print values from the array of pointer
Step 5 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int main()
{
int *arr[3];
int p = 40, q = 60, r = 90, i;
clrscr();
arr[0] = &p;
arr[1] = &q;
arr[2] = &r;
for(i = 0; i < 3; i++)
{
printf("For the Address = %d\t the Value would be = %d\n", arr[i], *arr[i]);
}
getch();
return 0;
}
50
OUTPUT
RESULT
Thus the C program to print address and value using array of pointers has been
executed successfully and the result was verified.
51
Ex .No. 9(a)
Date : Display the Book Details using Nested Structure
AIM
To write a c program to display the book details using nested structure.
ALGORITHM
Step 1 : start
Step 2 : define one structure to hold the details of an person. This structure can hold
the pointer variable firstname and lastname for author of the book.
Step 3 : define another structure to hold the details of an book. This structure can hold
the id and pointer of title for book
Step 4 : define variables of above two structures
Step 5 : print out the content using nested structure
Step 6 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
struct person
{
char *firstname, *lastname;
}
struct book
{
int id;
char *title;
struct person author;
float price;
}b2, b1 = {1, "book1", {"KAMATHENU","N"}, 220.5};
clrscr();
b2.id = 2;
b2.title = "book2";
b2.author.firstname = "BALA";
b2.author.lastname = "GURURUSAMY";
b2.price = 335.0;
printf("\nbook id\ttitle \t auther \t price \n");
printf("%d\t%s\t%s %s\t%f\n", b1.id, b1.title, b1.author.firstname, b1.author.lastname,
b1.price);
printf("%d\t%s\t%s %s\t%f\n", b2.id, b2.title, b2.author.firstname, b2.author.lastname,
52
b2.price);
getch();
}
OUTPUT
RESULT
Thus the C Program to display the book details using nested structure has been
executed successfully and the result was verified.
53
Ex .No. 9(b)
Display the Students Percentage using Array of Structures
Date :
AIM
To write a c program to display the student’s percentage using array of structures.
ALGORITHM
Step 1 : start
Step 2 : define one structure to hold the details of an student. This structure can hold
the name and percentage for array of structure.
Step 3 : define variables of this structure
Step 4 : ask the user to enter the details one by one and store them in the variable details
Step 5 : print out the content using array of record
Step 6 : stop
PROGRAM
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
}
int main()
{
int i;
struct student record[2];
record[0].id=1;
strcpy(record[0].name, "Bhanu");
record[0].percentage = 86.5;
record[1].id=2;
strcpy(record[1].name, "Priya");
record[1].percentage = 90.5;
record[2].id=3;
strcpy(record[2].name, "Hari");
record[2].percentage = 81.5;
for(i=0; i<3; i++){
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %.2f\n\n",record[i].percentage);
54
}
getch();
return 0;
}
OUTPUT
Records of STUDENT : 1
Id is: 1
Name is: Bhanu
Percentage is: 86.50
Records of STUDENT : 2
Id is: 2
Name is: Priya
Percentage is: 90.50
Records of STUDENT : 3
Id is: 3
Name is: Hari
Percentage is: 81.50
RESULT
Thus the C program to display the students percentage using array of structures has
been executed successfully and the result was verified.
55
Ex .No. 9(c)
Show the Detail of the Students using Pointer to Structure
Date :
AIM
To write a c program to show the detail of the students using pointer to structure.
ALGORITHM
Step 1 : start
Step 2 : define one structure to hold the details of an student. This structure can hold
the sno, sname, and marks for student pointer.
Step 3 : define variables of this structure
Step 4 : address of the s variable is stored in the another variable st
Step 5 : ask the user to enter the details one by one and store them in the variable details
Step 6 : print out the content
Step 7 : stop
PROGRAM
#include<stdio.h>
#include<conio.h>
struct student
{
int sno;
char sname[30];
int marks;
}
void main ( )
{
struct student s;
struct student *st;
clrscr();
printf("enter sno, sname, marks:");
scanf ("%d%s%d", & s.sno, &s.sname, &s. marks);
st = &s;
printf ("details of the student are\n");
printf ("SNumber = %d\n", st->sno);
printf ("name = %s\n", st->sname);
printf ("marks =%d\n", st->marks);
getch ( );
}
56
OUTPUT
enter sno, sname, marks: 101 Rani 80
details of the student are
SNumber = 101
name = Rani
marks =80
RESULT
Thus the C program to show the detail of the students using pointer to structure has
been executed successfully and the result was verified.
57
Ex .No. 10(a)
Copy File from One File to Another
Date :
AIM
ALGORITHM
Step 1: Start
Step 2: open source file in read mode
Step 3: if NULL pointer, then print cannot open file
Step 4: open destination file in write mode
Step 5: if NULL pointer, then print cannot open file
Step 6 : read a character from source file and write to destination file until EOF
Step 7: close source file and destination file
Step 8: Stop
PROGRAM
#include <stdio.h>
#include <stdlib.h> // For exit()
#include <conio.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
clrscr();
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
OUTPUT
RESULT
Thus the C program to copy file from one file to another has been executed
successfully and the result was verified.
59
Ex .No. 10(b)
File Pointer using the Random Access Techniques
Date :
AIM
To write a c program to point the file pointer using the random access techniques.
ALGORITHM
Step 1 : start
Step 2 :Open file. txt in read and write mode.
Step 3 : write text in file.txt
Step 4 : find the current position of the file pointer
Step 5 : display the current position of the file pointer
Step 7 : Close file
Step 8 :stop
PROGRAM
#include <stdio.h>
#include<conio.h>
int main ()
{
FILE *fp;
int c;
clrscr();
fp = fopen("file.txt","w+");
fputs("This is study.com", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming", fp);
printf("The current position of the file pointer is: %ld\n", ftell(fp));
rewind(fp);
printf("The current position of the file pointer is: %ld\n", ftell(fp));
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break;
}
printf("%c", c);
}
fclose(fp);
getch();
return(0);
}
60
OUTPUT
This is C Programming
RESULT
Thus the C program to point the file pointer using the random access techniques has
been executed successfully and the result was verified.
61
62