Lab Manuals C-1
Lab Manuals C-1
Year / Semester: I / 02
lOMoARcPSD|356 459 56
LIST OF EXPERIMENTS
5. Strings: operations
7. Recursion
10. Files: reading and writing, File pointers, file operations, random access,
processor directives.
TOTAL: 60 PERIODS
lOMoARcPSD|356 459 56
RESULT:
Thus the C program to find the area and circumference of the circle has been created successfully
and verified.
lOMoARcPSD|356 459 56
Algorithm:
Step 1: Start the program.
Step 2: Input the first number and second number.
Step 3: Swap the numbers without using temporary variable
num1=num1+num2
num2=num1-num2
num1=num1-num2
Step 4: Print the first number and second number.
Step 5: Stop the Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
clrscr();
printf(“\n Enter the first number:”);
scanf(“%d”,&num1);
printf(“\n Enter the second number:”)
scanf(“%d”,&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf(“\n The first number is %d”,num1);
printf(“\n The second number is %d”,num2);
}
Output:
Enter the first number: 50
Enter the Second number: 75
RESULT:
Thus the C program to swap two numbers without using temporary variable has been created
successfully and verified.
lOMoARcPSD|356 459 56
Algorithm:
Step 1: Start the program.
Step 2: Input the temperature in fahrenheit.
Step 3: Calculate the temperature in Celsius using below formula
celsius=(0.56)*(Fahrenheit-32);
Step 4: Print the temperature in degree celsius.
Step 5: Stop the Program
#include<stdio.h>
#include<conio.h>
void main()
{
float fahrenheit,celsius;
clrscr();
printf(“\n Enter the temperature in Fahrenheit: );;;;;
scanf(“%f”,&fahrenheit);
celsius=(0.56)*(Fahrenheit-32);
printf(“\n Temperature in Degree Celsius is %f “,celsius);
getch();
}
Output:
Enter the temperature in Fahrenheit: 32
RESULT:
Thus the C program to convert Fahrenheit into Degree Celsius has been created successfully and
verified.
lOMoARcPSD|356 459 56
Aim:
To write a C program to find largest of three numbers.
Algorithm:
Step 1: Start the program
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: if a>b & If a>c
Display a is the largest number.
else
Display c is the largest number.
else If b>c
Display b is the largest number.
else
Display c is the greatest number.
Step 5: Stop the program.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the values of A,B and C: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
printf("A is Greater than B and C");
}
else if (b > a && b > c)
{
printf("B is Greater than A and C");
}
else if (c > a && c > b)
{
printf("C is Greater than A and B");
}
else
{
printf("all are equal or any two values are equal");
}
return 0;
}
Output:
Enter the values of A,B and C: 3 5 8
C is Greater than A and B
RESULT:
Thus the C program to find largest of three numbers has been created successfully and verified.
lOMoARcPSD|356 459 56
AIM:
To write a C Program to check for age eligibility on voting.
ALGORITHM:
Step 1: Start the program
Step 2: Read age.
Step 3: Check the condition, if age>18
Step 4: if true, then go to the label, “yes” and print the statement.
Step 5: Else, go to the label, “no” and print the statement.
Step 6: Stop the program.
Output:-1
RESULT:
Thus the C program to demonstrate on goto label has been created successfully and verified.
lOMoARcPSD|356 459 56
AIM:
To write a C program for demonstrating arithmetic operations using switch case statement.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, n;
clrscr();
printf(“1. Addition\n”);
printf(“2. Subtraction\n”);
printf(“3. Multiplication\n”);
printf(“4. Division\n”);
printf(“0. Exit\n”);
printf(“Enter your choice : “);
scanf(“%d”,&n);
printf(“Enter the two numbers :”);
scanf(“%d,%d”,&a,&b);
switch(n)
{
case 1:
c = a + b;
printf(“Addition :%d\n”,c);
break;
case 2:
c = a – b;
printf(“Subtraction :%d\n”,c);
break;
case 3:
c = a * b;
printf(“Multiplication :%d\n”,c);
break;
case 4:
c = a / b;
printf(“Division :%d\n”,c);
break;
case 0:
lOMoARcPSD|356 459 56
exit(0);
break;
}
getch();
}
OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
Result: Thus to write a C program for demonstrating arithmetic operations using switch case statement was
compiled and executed successfully.
lOMoARcPSD|356 459 56
AIM:
To write a C Program to take the inputs until entering zero using break statement.
ALGORITHM:
Step 1: Start the program
Step 2: Using while loop till false, Read the value, a
Step 4: Check the condition, if a==0 then
Step 5: if true, exit from the executions
Step 6: Else, ask for reading the value
Step 7: Stop the program.
#include <stdio.h>
int main ()
{
int a;
while (1)
{
printf("Enter the number:");
scanf("%d", &a);
if ( a == 0 )
printf(“Bye”);
break;
}
return 0;
}
Output:
RESULT:
Thus the C program to take the inputs until entering zero using break statement has been created
successfully and verified.
lOMoARcPSD|356 459 56
AIM:
To write a C Program to print sum of odd numbers upto 10 using continue statement.
ALGORITHM:
Step 1: Start the program
Step 2: Initialize a=0, sum=0
Step 3: Using for loop with condition a<10
Step 4: check the condition a%2==0 then
Step 5: If true, move to the beginning of the for loop
Step 6: Else, calculate sum=sum + a
Step 7: Repeat the steps 4 to 6, until for loop is satisfied
Step 8: Print the sum
Step 9: Stop the program.
#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{
if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("Sum = %d",sum);
return 0;
}
Output:
Sum = 25
RESULT:
Thus the C program to print sum of odd numbers upto 10 using continue statement has been created
successfully and verified.
Demonstration on Looping Statements
Ex.No. 3(a) To print the number series up to the given limit (using for loop) Date:
Aim:
To write a C program to print number series upto given limit(using for loop).
ALGORITHM:
#include <stdio.h>
int main()
{
int i, n;
printf(“Enter the limit:”);
scanf(“%d”,&n);
for (i=1; i<=n; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Result: Thus a C program to print the number series up to the given limit n was compiled and executed
successfully.
Ex.No. 3(b) Demonstration on Looping Statements Date:
To print the sum of series up to given limit (using while loop)
AIM:
ALGORITHM:
Step 1: Start the program
Step 2: Read the limit, n
Step 3: Using while loop, initialize i =0 check i<n
Step 4: if true, sum = sum + i, increment i = i + 1
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Print the value of Sum
Step 7: Stop the program
PROGRAM (To Print the sum of number series up to the given limit n)
#include <stdio.h>
int main()
{
int i=0, n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
while (i<=n)
{
sum=sum+i;
i= i + 1;
}
printf(“Sum of series upto %d is %d”,n,sum);
return 0;
}
Output: - 1
Output: -
Result: Thus a C program to print the sum of number series up to the given limit n was compiled and
executed successfully.
Demonstration on Looping Statements
Ex.No. 3(c) To print the number series up to the given limit (using do…while loop) Date:
Aim:
To write a C program to print number series upto given limit(using for loop).
ALGORITHM:
#include <stdio.h>
int main()
{
int i=0, n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
do
{
i= i + 2;
print(i);
}while(i<=n);
printf(“Sumofseriesupto%d is%d”,n,sum);
return0;
}
Output: - 1
2
4
6
8
10
12
14
Result: Thus a C program to print the even numbers up to the given limit n was compiled and executed
successfully.
lOMoARcPSD|356 459 56
ALGORITHM:
PROGRAM (To Print sum of given array elements up to the given limit n)
#include <stdio.h>
int main()
{
int a[100],i=0,n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
for (i=0;i<=n;i++)
{
printf(“Enter the number: ”);
scanf(“%d”,&a[i]);
sum = sum + a[i];
i = i + 1;
}
printf(“Sum of the given array numbers is %d”,sum);
return 0;
}
Output:
Result: Thus a C program to print sum of given array elements up to the given limit n was compiled and
executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program for matrix Addition.
Algorithm:
#include<stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n;
clrscr();
printf("Enter the rows and column of two matrix:\n");
scanf("%d%d",&m,&n);
printf("\n Enter 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]);
lOMoARcPSD|356 459 56
}
printf("\nThe addition of two matrices");
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]);
}
}
getch();
}
OUTPUT:
Result: Thus a C program to implement matrix addition was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to print the position of elements stored in Multi dimensional array.
Algorithm:
#include<stdio.h>
int main()
{
int tables, rows, columns;
int Employees[2][2][3] = { { {9, 99, 999}, {8, 88, 888} },
{ {225, 445, 665}, {333, 555, 777} }
};
for (tables = 0; tables < 2; tables++)
{
for (rows = 0; rows < 2; rows++)
{
for (columns =0; columns < 3; columns++)
{
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns,
Employees[tables][rows][columns]);
}
}
}
return 0;
}
lOMoARcPSD|356 459 56
OUTPUT:
Employees [0][0][0] = 9
Employees [0][0][1] = 99
Employees [0][0][2] = 999
Employees [0][1][0] = 8
Employees [0][1][1] = 88
Employees [0][1][2] = 888
Employees [0][2][0] = 225
Employees [0][2][1] = 445
Employees [0][2][2] = 665
Employees [0][0][0] = 333
Employees [0][0][0] = 555
Employees [0][0][0] = 777
Result: Thus a C program to print the position of elements stored in Multi dimensional array was compiled
and executed successfully.
lOMoARcPSD|356 459 56
(d) To traverse the one dimensional array (Reaching all the elements)
Aim:
To write a C program to demonstrate on the traversal of one dimensional array.
Algorithm:
#include <stdio.h>
int main()
{
int a[6]={10,20,30,40,50,60};
int i;
for (i=0; i<6; i++)
{
printf(“Elementinposition[%d]is%d”,i+1,a[i]);
} return 0;
}
OUTPUT:
Element in position[1] is 10
Element in position[2] is 20
Element in position[3] is 30
Element in position[4] is 40
Element in position[5] is 50
Element in position[6] is 60
Result:
Thus a C program to traverse the one dimensional array was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to find the length of the given string.
Algorithm:
PROGRAM
#include<stdio.h>
void main()
{
char str1[20];
int len;
printf("Enter the string: ");
scanf("%s",&str1);
len=strlen(str1);
printf("Length of the given string %s is %d",str1,len);
}
OUTPUT:
Result:
Thus a C program to find the length of the given string was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to copy the one string to another string.
Algorithm:
PROGRAM
#include<stdio.h>
void main()
{
char str1[20],str2[20];
int len;
printf("Enter the string");
scanf("%s",&str1);
strcpy(str2,str1);
printf("Copied New String is %s",str2);
}
OUTPUT:
Result:
Thus a C program to copy from one string to another string was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to concatenate two strings.
Algorithm:
Step 1: Start the program
Step 2: Read two strings separately.
Step 3: Using the builtin function, strcat(), join the strings
Step 4: print the concatenated string
Step 5: Stop the program
PROGRAM
#include<stdio.h>
void main()
{
char str1[20],str2[20];
int len;
printf("Enter the string1: ");
scanf("%s",&str1);
printf("Enter the string2: ");
scanf("%s",&str2);
strcat(str1,str2);
printf("Copied New String is %s",str1);
}
OUTPUT:
Result:
Thus a C program to concatenate two strings was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to compare two strings are same or not.
Algorithm:
PROGRAM
#include<stdio.h>
void main()
{
char str1[20],str2[20];
int comp;
printf("Enter the string1: ");
scanf("%s",&str1);
printf("Enter the string2: ");
scanf("%s",&str2);
comp=strcmp(str1,str2);
if (comp==0)
{
printf("Two strings are same");
}
else
{
printf("Two strings are not same");
}
}
OUTPUT:-1
Enter the string1: Python
Enter the string2: Python
Two strings are same
OUTPUT:-2
Enter the string1: Python
Enter the string2: C
strings are not same
Result:
Thus a C program to compare two strings to check same or not was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to reverse the given string.
Algorithm:
PROGRAM
#include<stdio.h>
void main()
{
char str1[20],rec[20];
printf("Enter the string: ");
scanf("%s",&str1);
printf("Reverse of the given string is %s”,strrev(str));
}
OUTPUT:
Result:
Thus a C program to find the reverse of the given string was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to search an element in a given array using linear search.
Algorithm:
PROGRAM
#include<stdio.h>
void linear(int [ ], int, int);
main()
{
int i, n, x, a[20];
printf(“Enter how many elements:”);
scanf(“%d”,&n);
printf(“\n Enter the elements”);
for ( i=0; i<n; i++)
{
scanf(“%d\n”,&a[i]);
}
printf(“\n Enter the element to search:”);
scanf(“%d”,&x);
linear(a.n,x); // Function call
}
void linear(int a[ ], int n, int x)
{
int i, flag=0;
for(i=0; i<n; i++)
{
if ( x == a[i])
{
flag = 1;
break;
}
}
if (flag = = 1)
{
lOMoARcPSD|356 459 56
OUTPUT-1:
OUTPUT-2:
Result:
Thus a C program to search an element using linear search was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to find square of a given number.
Algorithm:
PROGRAM
#include<stdio.h>
int square(int);
void main()
{
int sq, n;
printf(“ Enter the number””);
scanf(“%d”,&n);
sq = square( n ); // Function Call
printf(“Square of %d is %d”,n,sq);
}
int square(int n)
{
return n * n;
}
OUTPUT:
Enter the number: 9
Square of 9 is 81
Result:
Thus a C program to find square of a number was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to find cube for a given number using pass by value.
Algorithm:
PROGRAM
#include<stdio.h>
int cube(int);
void main()
{
int c, n;
printf(“ Enter the number””);
scanf(“%d”,&n);
c = cube( n ); // Function Call
printf(“Cube of %d is %d”,n,c);
}
int cube(int n)
{
return n * n * n;
}
OUTPUT:
Enter the number: 5
Cube of 5 is 125
Result:
Thus a C program to find cube of a number using pass by value was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to add two numbers using pass by reference
Algorithm:
Step 1: Start the program
Step 2: Read the two values.
Step 3: Call and Pass the values to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Add the two values
Step 3.3: Return the values
PROGRAM
#include<stdio.h>
int add(int *, int *);
void main()
{
int a,b,c;
printf(“ Enter the two numbers””);
scanf(“%d%d”,&a,&b);
c = add(&a, &b); // Function Call
printf(“Addition is %d”,c);
}
int add(int *x, int *y)
{
int z;
z = *x + *y;
return z;
}
OUTPUT:
Enter the two numbers: 5 10
Addition is 15
Result:
Thus a C program to add two numbers using pass by reference was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to sort the numbers in ascending order using bubble sort.
Algorithm:
Step 1: Start the program
Step 2: Read no. of elements and enter the values.
Step 3: Call the function, bubble() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the array elements
Step 4.2: Using for loop in i, j, check the condition if a[i] > a[j]
Step 4.3: if true, swap the elements
Step 4.4: return the elements after the loop.
PROGRAM
#include<stdio.h>
void bubble(int a[],int n);
int a[25], i, j, n, temp;
void main()
{
printf("Enter no.of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a, n); // function call
printf("The sorted elements are: ");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
void bubble(int a[], int n)
{
for(i=0;i<=n-1;i++)
for(j=i+1;j<=n-1;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
lOMoARcPSD|356 459 56
OUTPUT:
Result:
Thus a C program to sort the numbers in ascending order using bubble sort was compiled and
executed successfully.
lOMoARcPSD|356 459 56
Ex.No. 7 Recursion
Aim:
To write a C program to find factorial of a given number using recursion.
Algorithm:
Step 1: Start the program
Step 2: Read the number
Step 3: Call the function, fact() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the value
Step 4.2: Using recursive function, calculate the value
Step 4.3: return the value after the recursive condition.
PROGRAM
#include<stdio.h>
int fact(int );
main()
{
int n;
int f;
printf("\nEnter a number:");
scanf("%d",&num);
f=fact(num);
printf("\nThefactorialvalueof%dis%d\n",num,f);
}
int fact(int n)
{
if(n==1||n==0)
return(1);
else
return(n*fact(n-1));
}
OUTPUT
Enter a number:6
Result:
Thus a C program to find factorial of a given number using recursion was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
Algorithm:
PROGRAM
#include<stdio.h>
int swap(int *, int *);
void main()
{
int a,b,c;
printf(“ Enter the two numbers”);
scanf(“%d%d”,&a,&b);
swap(&a, &b); // Function Call
printf(“Now A value is %d”,a);
printf(“Now B value is %d”,b);
}
int swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
}
OUTPUT:
Enter the two numbers: 5 10
Now A value is 10
Now B value is 5
Result:
Thus a C program to swap two numbers with passing pointers to functions was compiled and
executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to print the array values using pointers (Address)
Algorithm:
Step 1: Start the program
Step 2: Initialize array values and declare a pointer variable.
Step 3: Assign array variable to the pointer variable.
Step 4: Using for loop, access the elements with help of pointers
Step 5: Print the value
Step 6: Stop the program
PROGRAM
#include <stdio.h>
int main ()
{
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = &balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ )
{
printf("*(p + %d) : %f\n", i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ )
{
printf("*(balance + %d) : %f\n", i, *(balance + i) );
}
return 0;
}
OUTPUT:
Array values using pointer
*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000
Array values using balance as address
lOMoARcPSD|356 459 56
*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000
Result:
Thus a C program to print the array values using pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
Algorithm:
PROGRAM
#include<stdio.h>
int main()
{
char Name[] = "Dinesh";
char* ptrname = Name;
clrscr();
printf("Name = %s\n",Name);
printf("Name via ptrname = %s\n", ptrname);
return 0;
}
OUTPUT:
Name = C program
Name via ptrname = C program
Result:
Thus a C program to print the string using pointers was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
Algorithm:
Step 1: Start the program
Step 2: Declare pointers with * and **
Step 3: Intialize a value with a variable
Step 4: Assign pointer address for the variable.
Step 5: Print the values with pointer with pointersStep 6: Stop the program
PROGRAM
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr; var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */ printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);return 0;
}
OUTPUT:
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Result:
Thus a C program to print the values using pointers to pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
Algorithm:
Step 1: Start the program
Step 2: Declare pointer variable and variable with one dimensional values
Step 3: Initialize a value for one dimensional array
Step 4: Using loop assign the address for the array values
Step 5: Using loop print the values of the array.
Step 6: Stop the program
PROGRAM
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
OUTPUT:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
Result:
Thus a C program to print the values using array of pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to print the student details with help of nested structure.
Algorithm:
PROGRAM
#include <stdio.h>
struct student
{
int rno;
char name[25];
char dept[10];
};
struct address
{
int door;
char area[25];
double pincode;
struct student s; //Nested Structure
}a={10,"Tiruttani",631209,1001,"Vimal","CSE"};
int main()
{
printf("Name=%s",a.s.name);
printf("\nRoll number=%d",a.s.rno);
printf("\nDepartment=%s",a.s.dept);
printf("\nDoor No=%d",a.door);
printf("\nArea=%s",a.area);
printf("\nPincode=%2.lf",a.pincode);
return 0;
}
OUTPUT-1:
Name=Vimal
Roll number=1001
Department=CSE
Door No=10
Area=Tiruttani
Pincode=631209
Result:
Thus a C program to print student details using nested structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to print the book details with help of pointers to structure.
Algorithm:
PROGRAM
#include <stdio.h>
struct book
{
char bname[20];
char auname[20];
float price;
int pages;
char publisher[20];
int pubyear;
}
OUTPUT :
Book Name=Pro C
Author Nanme=Kanetkar
Book Price=590.500000
Book Pages=696
Book Publisher=Mcgraw Hill
Published Year=2008
Result:
Thus a C program to print book details using pointer to structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to generate EB bill using arrays with structure.
Algorithm:
PROGRAM
#include <stdio.h>
#include<stdlib.h>
struct eb
{
char consumer[20];
int consumerid;
int curread;
int prevread;
int totread;
float price;
}b[10];
int main()
{
int i,n;
printf("Enter how many consumers:");
scanf("%d",&n);
printf("\nEnter the details of the consumers");
for(i=0;i<n;i++)
{
printf("\nEnter the consumer name:");
scanf("%s",b[i].consumer);
printf("\nEnter the consumer id:");
scanf("%d",&b[i].consumerid);
printf("\nEnter the current reading:");
scanf("%d",&b[i].curread);
printf("\nEnter the previous reading:");
scanf("%d",&b[i].prevread);
b[i].totread = b[i].curread - b[i].prevread;
b[i].price = b[i].totread * 6 + 50;
}
printf("\nElectricity Bill Details are:");
for(i=0;i<n;i++)
{
printf("\nCounumer name=%s",b[i].consumer);
lOMoARcPSD|356 459 56
printf("\nCosumer ID = %d",b[i].consumerid);
printf("\nCurrent Reading=%d",b[i].curread);
printf("\nPrevious Reading=%d",b[i].prevread);
printf("\n Consumed Units = %d",b[i].totread);
printf("\n Total Bill = %f",b[i].price);
printf("\n");
}
OUTPUT :
Enter how many consumers:2
Consumer name=vimal
Consumer ID = 1001
Current Reading=5234
Previous Reading=3254
Consumed Units = 1980
Total Bill = 11930.000000
Consumer name=Kamal
Consumer ID = 1002
Current Reading=6541
Previous Reading=4521
Consumed Units = 2020
Total Bill = 12170.000000
Result:
Thus a C program to generate eb bill using arrays of structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Ex.No. 9 UNIONS
Aim:
To write a C program to print the mark sheet of a student using union.
Algorithm:
PROGRAM
#include <stdio.h>
union book
{
char bname[20];
char auname[20];
float price;
}b;
int main()
{
printf(“\nEnter the Book Name:”);
scanf(“%s”,b.bname);
printf("Book Name=%s",b.bname);
printf(“\nEnter the Author Name:”);
scanf(“%s”,&b.auname);
printf("\nAuthor Nanme=%s",b.auname);
printf(“\nEnter the Book Price:”);
scanf(“%f”,b.price);
printf("\nBook Price=%f",b.price);
return 0;
}
OUTPUT :
Result:
Thus a C program to print book details using union was compiled and executed successfully.
lOMoARcPSD|356 459 56
Ex.No. 10 FILES
Aim:
To write a C program to read the content from the existing file.
Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “r”.
Step 4: Using fgetc(), read the each character from a file upto EOF.
Step 5: Print the content of the file
Step 6: Stop the program
PROGRAM
#include <stdio.h>
int main()
{ sample.txt
char ch;
FILE *fp; CS3271 PROGRAMMING IN C LABORATORY
fp=fopen(“sample.txt”. “r”);
if (fp = = NULL)
{
printf(“File does not exist”);
}
while (fp !=EOF)
{
ch= getc(fp);
printf(“%c”,ch);
}
fclose(fp);
return 0;
}
OUTPUT :
Result:
Thus a C program to read the content of the existing file was compiled and executed
Successfully.
lOMoARcPSD|356 459 56
Ex.No. 10 FILES
Aim:
To write a C program to write the content into a file.
Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w”.
Step 4: Using fputc(), write the each character and press ctrl z to stop.
Step 5: Print the content of the text into the file.
Step 6: Stop the program
PROGRAM
#include <stdio.h>
int main()
{
char ch;
FILE *fp;
fp=fopen(“sample.txt”,“w”);
printf(“Enter the text & press ctrl Z to stop”);
while ((ch==getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
return 0;
}
OUTPUT :
Enter the text & press ctrl Z to stop
CS3271 PROGRAMMING IN C LABORATORY
sample.txt
Result:
Thus a C program to write the content in to the file was compiled and executed successfully.
lOMoARcPSD|356 459 56
Ex.No. 10 FILES
Aim:
To write a C program to demonstrate on usage of file pointers.
Algorithm:
PROGRAM
#include <stdio.h>
int main()
{
FILE *fp;
char name[20];
int regno;
char place[20];
fp = fopen("example.txt","w");
printf("Enter your Name, Reg No & Place:");
scanf("%s%d%s",&name,®no,&place);
fprintf(fp,"%s %d %s",name,regno,place);
printf("\nData written successfully");
fclose(fp);
return 0;
}
OUTPUT :
Enter your Name, Reg No & Place: Ram 1001 Erode
example.txt
Result:
Thus a C program to demonstration on file pointer was compiled and executed successfully.
lOMoARcPSD|356 459 56
Aim:
To write a C program to demonstrate on usage of file operations (read, write & append)
Algorithm :
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Display menu for differentfile operations.
Sep 4: Based on the option, open the file in specified mode using file pointer. Step 5:
Perform the operation on file pointer.
Step 6: Close the file pointer Step 7: Stop
the program.
PROGRAM
#include <stdio.h>
int main()
{
FILE *fp; int
regno,n;
char fname[20],name[40];
printf("FILE OPERATIONS");
printf("\nRead Opertion"); printf("\nWrite
Operation"); printf("\nAppend operation");
printf("\nChoose any one of the option:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter the file name to read:");
scanf("%s",fname); fp=fopen(fname,"r");
fscanf(fp,"%s%d",&name,®no);
printf("%s %d",name,regno); fclose(fp);
break;
case 2:
printf("\nEnter the file name to write:");
scanf("%s",fname); fp=fopen(fname,"w");
fprintf(fp,"Programming in C");
printf("\nContent written successfully");
fclose(fp);
break;
case 3:
printf("\nEnter the file name to write:");
scanf("%s",fname); fp=fopen(fname,"a");
fprintf(fp,"\nProblem solving and Python programming");
printf("\nContent written successfully");
fclose(fp);
break;
default:
printf("Enter correct choice");
break;
} return 0;
}
OUTPUT :-1
FILE OPERATIONS
Read Opertion
lOMoARcPSD|356 459 56
Write Operation
Append operation
Choose any one of the option:1
Enter the file name to read:example.txt
Vimalraj 1001
OUTPUT :-2
FILE OPERATIONS kamal.txt (New file)
Read Opertion Programming in C
Write Operation
Append operation
Choose any one of the option:2
OUTPUT :-3
FILE OPERATIONS sample.txt (Existing file)
Read Opertion Programming in C
Write Operation Problem Solving and Python Programming
Append operation
Choose any one of the option:3
Result:
Thus a C program to demonstration on file operations was compiled and executed
successfully.
lOMoARcPSD|356 459 56
Ex.No. 9 FILES
Aim:
To write a C program to demonstrate on random access in a file.
Algorithm:
PROGRAM
#include <stdio.h>
int main () {
FILE *fp;
int c;
fp = fopen("file.txt","w+");
puts("This is study.com", fp);
// We are using fseek() to shift the file pointer to the 7th position.
fseek( fp, 7, SEEK_SET );
//Now we overwrite C programming in the 7th position
fputs(" C Programming", fp);
//Now we print the current position of the file pointer using ftell()
printf("The current position of the file pointer is: %ld\n", ftell(fp));
//We take the file pointer to the beginning of the file.
rewind(fp);
//Now we verify if rewind() worked using ftell().
printf("The current position of the file pointer is: %ld\n", ftell(fp));
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
lOMoARcPSD|356 459 56
OUTPUT
Explanation:
Functions can be used to handle file operations only when you send the file pointer as a parameter to the
function. You can also send the file name as a parameter and handle the operations inside the function. The
common practice is to send the file pointer to a function for a specific purpose. This example has been
modified to use a function for displaying the contents of the file by passing the file pointer.
Result:
Thus to write a C program to perform the demonstration on Random file access was compiled and
executed successfully.
lOMoARcPSD|356 459 56
Ex.No.10 FILES
(f)Preprocessor Directives
Aim:
To write a C program to demonstrate on preprocessor directives
Algorithm:
Step
1:
Start
the
progr
am
Step
2:
Defin
ea
macr
o
Step 3: Define a
macro in another
macro Step 4: Use
the macros inside
the program Step
5: Perform the
manipulations
Ste
p
6:
Pri
nt
the
res
ult
Ste
p
7:
St
op
the
pr
og
ra
m
lOMoARcPSD|356 459 56
PROGRAM
#
i
n
c
l
u
d
e
<
s
t
d
i
o
.
h
>
#
d
e
f
i
n
e
A
1
0
#
d
e
f
i
n
e
B
A
+
3
0
#
d
e
lOMoARcPSD|356 459 56
f
i
n
e
C
A
+
B
v
o
i
d
m
a
i
n
(
)
{
int result;
resul
t =A
+B+
C;
printf
(“Res
ult =
%d”,
result
);
OUTPUT :
Result = 90
Result:
Thus to write a C program to perform the Preprocessor Directives has been Compiled and
executed successfully.