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

Cs3271 Programming in C Lab Manual

The document provides the syllabus and list of programs for a Programming in C Laboratory course, including objectives to familiarize students with C programming constructs, develop programs using arrays, strings, pointers, functions, and structures, and develop applications using file processing; it lists 10 experiments covering basic C programming concepts like I/O, decision making, loops, arrays, strings, functions, pointers, structures, and file handling.

Uploaded by

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

Cs3271 Programming in C Lab Manual

The document provides the syllabus and list of programs for a Programming in C Laboratory course, including objectives to familiarize students with C programming constructs, develop programs using arrays, strings, pointers, functions, and structures, and develop applications using file processing; it lists 10 experiments covering basic C programming concepts like I/O, decision making, loops, arrays, strings, functions, pointers, structures, and file handling.

Uploaded by

PRAVEENAT CSE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

SURYA GROUP OF INSTITUTIONS

SCHOOL OF ENGINEERING AND TECHNOLOGY

Department Of Computer Science & Engineering

LABORATORY MANUAL

Course Code: CS3271


Course Name: Programming in C Laboratory
Year / Semester: I Year / II Semester
Regulations: AU-R2021
Academic year: 2023-24- EVEN
Syllabus

Course Objectives:

 To familiarize with C programming constructs.


 To develop programs in C using basic constructs.
 To develop programs in C using arrays.
 To develop applications in C using strings, pointers, functions.
 To develop applications in C using structures.
 To develop applications in C using file processing.

List of Programs

1. I/O statements, operators, expressions


2. Decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing
arrays to function.
7. Recursion
8. Pointers: Pointers to functions, Arrays,Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10. Files: reading and writing, File pointers, file operations, random access,
processor directives.

Platform Needed:

Turbo C for Windows

TOTAL: 60 PERIODS
Table of Contents

Ex.
Name of the Experiment
No.

1a. Write a C program to use various IO statements in C

1b. Write a C program to use operators and expressions.

2a Decision making constructs: if-else and goto

2b Decision making constructs: switch statements, break-continue


Write a C program to calculate the sum of digits of a number entered by the
3a
user using while loop
3b Write a C program to print table for the given number using do while loop

3c Write a C program to calculate the factorial of a given number using for loop

4a Write a C program to find out the average of 4 integers using array.


Write a C program to Storing elements in a matrix and printing it using 2d
4b
array
4c writing a program in C to perform traverse operation on an array

5 String operations in C Programming

6 Sorting using pass by reference


7 Recursive function

8a Swapping two numbers using Pointers

8b Pointers to Pointers

8c Array of pointers to character to store a list of strings

9a Nested structure
9b An array of structures

9c Functioning of pointers to structures.

9d Union in c
10 File handling
Ex: No 1a Write a C program to use various IO statements in C

Aim:
To write C programs to demonstrate the uses of various formatted and unformatted input
and output functions, expressions and functional blocks.

Algorithm:

STEP 1:Start the program.


STEP 2:Declare all required variables and initialize them.
STEP 3:Get input values from the user for arithmetic operation.
STEP 4:Use various IO functions like printf(), scanf(),getchar(),putchar() functions
STEP 5:Stop the program.
Program:

#include <stdio.h>
#include<conio.h>
void main()
{
// using printf() and scanf() for multiple inputs
char gender;
int age;
int c;
char str[100];
clrscr();
// Taking and display character using getchar() and putchar()
printf("\nEnter a character");
c = getchar();
putchar(c);
printf("\nEnter your age and then gender(M, F or O): ");
scanf("%d %c", &age, &gender);
printf("\nYou entered: %d and %c", age, gender);
getch();
}

Sample Output:

Enter a Character G
G
Enter your age and then gender(M,F,O) 21 F
You entered 21 and F

Result:

Thus a C program to use IO statements was executed successfully.


Ex: No:1b Write a C program to use operators and expressions.

Aim:
To write C programs to demonstrate the functions of operators and expressions

Algorithm:

STEP 1:Start the program.


STEP 2:Declare all required variables and initialize them.
STEP 3:Perform the various operations using the corresponding C operators.
STEP 4:Obtain the desired output.
STEP 5:Stop the program.
Program:

#include <stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20;
int Total=0,i;
int m=40,n=20;
int x=1, y;
clrscr();
//ARITHMETIC OPERATORS
printf("ARITHMETIC OPERATORS\n");
printf("Addition of a, b is : %d\n", a+b);
printf("Subtraction of a, b is : %d\n", a-b);
printf("Multiplication of a, b is : %d\n", a*b);
printf("Division of a, b is : %d\n", a/b);
printf("Modulus of a, b is : %d\n", a%b);

//ASSIGNMENT OPERATORS
printf("ASSIGNMENT OPERATORS\n");

for(i=0;i<=10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("\nThe sum of first 10 numbers are = %d", Total);

//RELATIONAL OPERATORS
printf("\nRELATIONAL OPERATORS\n");
if (!(m>n && m !=0))
{
printf("!(%d>%d && %d !=0) is true",m,n,m);
}
else
{
printf("!(%d>%d && %d !=0) is false",m,n,m);
}
//CONDITIONAL OPERATORS
printf("\nCONDITIONAL OPERATORS\n");
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
getch();
}
Output:

ARITHMETIC OPERATORS
Addition of a,b is :60
Subtraction of a,b is :60
Multiplication of a,b is :60
Division of a,b is :60
Modulus of a,b is :60
ASSIGNMENT OPERATORS
The sum of first 10 numbers are 55
RELATIONAL OPERATORS
!(40>20 && 40!=0) is false
CONDITIONAL OPERATORS
Xvalue is 1
Y value is 2

Result:

Thus a C program to demonstrate the functions of operators and expressions was

written and executed successfully.


2. a) Decission making constructs: if-else and goto.

Aim:
To write C programs to use the decision making constructs if else and goto.

(i)if else statements

Algorithm:

STEP 1:Start the program.


STEP 2:Declare a variable and get the input from the user.
STEP 3:Check wheter the given number is even or not.
STEP 4:Obtain the desired output.
STEP 5:Stop the program.

Program:

include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
Output:

Enter a number 4
4 is even number
(ii ) goto statements

Algorithm:

STEP 1:Start the program.


STEP 2:Declare a variable.
STEP 3:Use the goto staments to print the numbers
STEP 4:Obtain the desired output.
STEP 5:Stop the program.

Program:

void printNumbers()
{
int n = 1;
label:
printf("%d ",n);
n++;
if (n <= 10)
goto label;
}
// Driver program to test above function
int main() {
printNumbers();
return 0;
}
Output:

1 2 3 4 5 6 7 8 9 10

Result:

Thus a C program to use the decision making constructs if else and goto is written

and executed successfully.


2. b) Decission making constructs: switch statements, break-continue

Aim:
To write C programs to use the decision making constructs switch statements

(i)switch statements

Algorithm:

STEP 1:Start the program.


STEP 2:Declare two variables
STEP 3:Declare switch statements and use arithmetic operators in each case.
STEP 4:Obtain the desired output.
STEP 5:Stop the program.
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
char operator;
int num1,num2;
clrscr();
printf(“\n Enter the operator (+, -, *, /):”);
scanf(“%c”,&operator);
printf(“\n Enter the Two numbers:”);
scanf(“%d%d”,&num1,&num2);
switch (operator)
{
case ‘+’:
printf(“%d+%d=%d”,num1,num2,num1+num2);
break;
case ‘-‘:
printf(“%d-%d=%d”,num1,num2,num1-num2);
break;
case ‘*’:
printf(“%d*%d=%d”,num1,num2,num1*num2);
break;
case ‘/’:
printf(“%d / %d = %d”,num1,num2,num1/num2);
break;
default:
printf(“\n Enter the operator only”);
break;
}
}
Output:
Enter the operator (+, -,*,/): +
Enter two numbers: 10 2
10+2=12
(ii)break-continue statements

Algorithm:

STEP 1:Start the program.


STEP 2:Declare two variables
STEP 3:Declare switch statements and use arithmetic operators in each case.
STEP 4:Obtain the desired output.
STEP 5:Stop the program.
a) Program:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
// break condition
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
1234
b) Program:
// loop to print numbers 1 to 10 except 4
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
// if i is equal to 4 , continue to next iteration without printing 4.
if (i == 4) {
continue;
}
else{
// otherwise print the value of i.
printf("%d ", i);
}
}
return 0;
}
Output:
1 2 3 5 6 7 8 9 10

Result:
Thus C programs to use the decision making constructs switch statements and break
statement is executed successfully.
3 .a) Write a program in C to display n terms of natural number and their sum.

Aim:
To write C programs to to display n terms of natural number and their sum.

Algorithm:

STEP 1:Start the program.


STEP 2:Get a natural number as input from the user.
STEP 3:Use while loop to find the sum of the digits
STEP 4:Obtain the desired output.
STEP 5:Stop the program.
Program:
#include<stdio.h>
int main()
{
int n, num, sum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &n);
num = n;
while( n > 0 )
{
remainder = n % 10;
sum += remainder;
n /= 10;
}
printf("Sum of digits of %d is %d", num, sum);
return 0;
}

Output:
Enter a number: 222
Sum of digits of 222 is 6

Result:
Thus C programs to display n terms of natural number and their sum using while
loop was executed successfully.
3. b) Write a C program to print table for the given number using do while loop

Aim:
To write C programs to print table for the given number using do while loop.

Algorithm:

STEP 1:Start the program.


STEP 2:Get a number as input from the user.
STEP 3:Use do while loop to print the table of the given number.
STEP 4:Run the program to get the desired output.
STEP 5:Stop the program.
Program:

#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
}
Output:
Enter a number: 5
5
10
15
20
25
30
35
40
45
50

Result:
Thus C programs to print table for the given number using do while loop was executed
successfully.
3. c) Write a C program to calculate the factorial of a given number using for loop

Aim:
To write C programs to calculate the factorial of a given number using for loop.

Algorithm:

STEP 1:Start the program.


STEP 2:Get a number as input from the user.
STEP 3:Use for loop to find the factorial of the given number.
STEP 4:Run the program to get the desired output.
STEP 5:Stop the program.
Program:

#include <stdio.h>
void main(){
int i,f=1,num;
printf("Input the number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("The Factorial of %d is: %d\n",num,f);
}

Output:
Input the number : 5
The Factorial of 5 is: 120

Result:

Thus C programs to calculate the factorial of a given number using for loop was executed
successfully.
4.a) Write a C program to find out the average of 4 integers using array.
Aim:
To write a C program to find out the average of 4 integers using array.
Algorithm:

STEP 1:Start the program.


STEP 2:Declare an array with size 4.
STEP 3:Use for loop to get the elements of the array
STEP 4:Use for loop to perform the average of the array.
STEP 5:Run the program to get the desired output.
STEP 6:Stop the program.
Program:

#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;

int num[4];

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


{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
for (x=0; x<4;x++)
{
sum = sum+num[x];
}

avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output:

Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20

Result:
Thus a C program to find out the average of 4 integers using array was executed successfully.
4.b) Write a C program to Storing elements in a matrix and printing it using 2d array

Aim:
To write a C program to storing elements in a matrix and printing it using 2d array

Algorithm:
STEP 1:Start the program.
STEP 2:Declare a 2d array.
STEP 3:Use for loop to get the elements of the array
STEP 4:Use for loop to print the elements of the array.
STEP 5:Run the program to get the desired output.
STEP 6:Stop the program.
Program:

#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}

Output:
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34

Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....


56 10 30
34 21 34
45 56 78

Result:
Thus a C program to storing elements in a matrix and printing it using 2d array was executed
successfully.
4.c) writing a program in C to perform traverse operation on an array

Aim:
To writing a program in C to perform traverse operation on an array

Algorithm:
STEP 1:Start
STEP 2: [Initialize counter variable. ] Set i = LB.
STEP 3:Repeat for i = LB to UB.
STEP 4:Apply process to arr[i].
STEP 5: [End of loop. ]
STEP 6:Stop

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
size=sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\narr[%d]= %d", i, arr[i]);
}

Output:
The array elements are:
arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3

Result:
Thus a C program to perform traverse operation on an array was executed successfully.
5) String operations in C Programming
Aim:

To writing a program in C to perform string operations.

Algorithm:

STEP 1:Start
STEP 2: Declare variables
STEP 3: Read the text.
STEP 4: Display the menu options
STEP 5: Compare each character with tab char ‘\t’ or space char ‘ ‘ to count no of words
STEP 6: Find the first word of each sentence to capitalize by checks to see if a character is a
punctuation mark used to denote the end of a sentence. (! . ?)
STEP 7: Replace the word in the text by user specific word if match.
STEP 8: Display the output of the calculations
STEP 9: Repeat the step 4 till choose the option stop.
STEP 10:Stop

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace (char *, char *, char *);
int main()
{
char choice.str[200];
int i, words;
char s_string[200], r_string[200];
/* Input text from user */
printf("Enter any text:\n ");
gets(str);
do
{
printf("\n1. Find the total number of words \n");
printf("2. Capitalize the first word of each sentence \n");
printf("3. Replace a given word with another word \n");
printf("4. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' :
i = 0;
words = 1;
/* Runs a loop till end of text */
while(str[i] != '\0')
{
/* If the current character(str[i]) is white space */
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("\nTotal number of words = %d", words);
break;
case '2' :
i = 0;
/* Runs a loop till end of text */
while(str[i] != '\0')
{
if(str[i]=='!' || str[i]=='.' || str[i]=='?')
{
i++;
while(str[i]!=' ' || str[i]!='\n' || str[i]!='\t || str[i] != '\0'’)
{
putchar (toupper(str[++i]));
i++;
}
}
else
putchar (str[i]);
i++;
}
break;
case '3' :
printf("\nPlease enter the string to search: ");
fflush(stdin);
gets(s_string);
printf("\nPlease enter the replace string ");
fflush(stdin);
gets(r_string);
replace(str, s_string, r_string);
puts(str);
break;
case '4' :
exit(0);
}
printf("\nPress any key to continue....");
getch();
}while(choice!=’4’);
return 0;
}
void replace(char * str, char * s_string, char * r_string)
{
char buffer[200];
char * ch;
if(!(ch = strstr(str, s_string)))
return;
strncpy(buffer, str, ch-str);
buffer[ch-str] = 0;
sprintf(buffer+(ch -str), "%s%s", r_string, ch + strlen(s_string));
str[0] = 0;
strcpy(str, buffer);
return replace(str, s_string, r_string);
}

Output:

Enter any text:


I like C and C++ programming!
1. Find the total number of words
2. Capitalize the first word of each sentence
3. Replace a given word with another word
4. Stop
Enter your choice : 1
Total number of words = 6
Press any key to continue....
1. Find the total number of words
2. Capitalize the first word of each sentence
3. Replace a given word with another word
4. Stop
Enter your choice : 4

Result:
Thus a C program to perform string operations was executed successfully.
6 .Sorting using pass by reference

Aim:

To write a C Program to Sort the list of numbers using pass by reference.


Algorithm:
STEP 1:Start
STEP 2: Declare variables and create an array
STEP 3: Read the Input for number of elements and each element.
STEP 4: Develop a function to sort the array by passing reference
STEP 5: Compare the elements in each pass till all the elements are sorted.
STEP 6: Display the output of the sorted elements .
STEP 7:Stop
Program:

#include <stdio.h>
#include <conio.h>
void main()
{

int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : "); scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");

for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void sortarray(int* arr,int num)
{

int i,j,temp;

for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];

arr[j] = temp; } }

Output:
Enter the Number of Elements in an array : 5
Enter the Array elements 33
67
21
45
11
After Sorting....
11
21
33
45
67

Result:
Thus a C Program Sorting using pass by reference was executed and the output was obtained.
7. Recursive function

Aim:
To write C programs to find the Fibonacci series of a given number using recursive
function.

Algorithm:

STEP 1:Start the program.


STEP 2:Get a number as input from the user.
STEP 3:Use recursive function to find the Fibonacci series of the given number.
STEP 4:Run the program to get the desired output.
STEP 5:Stop the program.

Program:

#include <stdio.h>
int fibonacci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d\t\n", fibonacci(i));
}
return 0;
}
Output:

0
1
1
2
3
5
8
13
21
34
Result:
Thus a C Program to find Fibonacci series was executed and the output was obtained.
8.a) Swapping two numbers using Pointers
Aim:
To write C programs to swap two numbers using pointers.

Algorithm:

STEP 1:Start the program.


STEP 2:Declare two numbers
STEP 3:Write swap function and pass the reference of the declared variables to it.
STEP 4:Do the swapping function by creating a temp variable.
STEP 5:Run the program to get the desired output.
STEP 6:Stop the program.

Program:

#include <stdio.h>
void swapnum(int *num1, int *num2)
{
int tempnum;
tempnum = *num1;
*num1 = *num2;
*num2 = tempnum;
}
int main( )
{
int v1 = 11, v2 = 77 ;
printf("Before swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2);

/*calling swap function*/


swapnum( &v1, &v2 );

printf("\nAfter swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2);
}
Output:
Before swapping:
Value of v1 is: 11
Value of v2 is: 77
After swapping:
Value of v1 is: 77
Value of v2 is: 11
Result:
Thus a C Program to swap two numbers using pointers was executed and the output was
obtained.
8.b) Pointers to Pointers
Aim:
To write C programs to print a variable using double pointer.

Algorithm:

STEP 1:Start the program.


STEP 2:Declare the required variables.
STEP 3:Use the printf function to print the double pointer variable.
STEP 4:Run the program to get the desired output.
STEP 5:Stop the program.

Program:

#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); //value stoted at the address contained by p i.e.10 will be printed
printf("value stored at pp: %d\n",**pp);
//value stored at the address contained by the pointer stoyred at pp
}

Output:
address of a: d26a8734
address of p: d26a8738
value stored at p: 10
value stored at pp: 10

Result:
Thus a C Program to print a variable using double pointer was executed and the output
was obtained.
8.c) Array of pointers to character to store a list of strings
Aim:
To write C programs to store a list of strings using Array of pointers to character

Algorithm:

STEP 1:Start the program.


STEP 2:Declare a pointer array variable and initialize it with characters.
STEP 3:Use for loops to print the names.
STEP 4:Run the program to get the desired output.
STEP 5:Stop the program.

Program:

#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
for ( i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}

Output:
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

Result:
Thus a C Program to store a list of strings using Array of pointers to character was
executed and the output was obtained.
9.a) Nested structure

Aim:
To write C programs to nest two structures.

Algorithm:

STEP 1: Start the program.


STEP 2: Declare two structures complex and number.
STEP 3: Use (.) operator to access the structures.
STEP 4: Run the program to get the desired output.
STEP 5: Stop the program.

Program:

include <stdio.h>
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {

// initialize complex variables


num1.comp.imag = 11;
num1.comp.real = 5.25;

// initialize number variable


num1.integer = 6;

// print struct variables


printf("Imaginary Part: %d\n", num1.comp.imag);
printf("Real Part: %.2f\n", num1.comp.real);
printf("Integer: %d", num1.integer);

return 0;
}
Output:
Imaginary Part: 11
Real Part: 5.25
Integer: 6
Result:
Thus a C Program to nest two structures was executed and the output was obtained.
9.b) An array of structures

Aim:
To write C programs that stores information of 5 students and prints it using An array of
structures.

Algorithm:

STEP 1: Start the program.


STEP 2: Declare a structure which also has array variable.
STEP 3: Use (.) operator to access the structures.
STEP 4: Run the program to get the desired output.
STEP 5: Stop the program.

Program:

#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}

Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
Result:
Thus a C Program that stores information of 5 students and prints it using an array of
structures was executed and the output was obtained.
9.c) Functioning of pointers to structures.

Aim:
To write C programs to store person information using pointer as a reference variable to
structures.

Algorithm:

STEP 1: Start the program.


STEP 2: Declare a structure with required variables.
STEP 3: Declare a pointer variable to refer structre in main function.
STEP 4: Use (->) operator to access the structures.
STEP 5: Run the program to get the desired output.
STEP 6: Stop the program.

Program:

#include<stdio.h>
struct person{
int age;
float weight;
};
int main(){
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}

Output:
Let us run the above program that will produce the following result −
Enter age: 45
Enter weight: 60
Displaying:
Age: 45
weight: 60.000000

Result:
Thus a C Program to store person information using pointer as a reference variable to
structures was executed and the output was obtained.
9.d) Union in C
Aim:
To write C programs to store employee details using union.

Algorithm:

STEP 1: Start the program.


STEP 2: Declare an union with required variables.
STEP 3: Declare a reference variable to union in main function.
STEP 4: Use (.) operator to access the union members.
STEP 5: Run the program to get the desired output.
STEP 6: Stop the program.

Program:

#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;

int main() {
j.salary = 12.3;

// when j.workerNo is assigned a value,


// j.salary will no longer hold 12.3
j.workerNo = 100;

printf("Salary = %.1f\n", j.salary);


printf("Number of workers = %d", j.workerNo);
return 0;
}

Output:

Salary = 0.0
Number of workers = 100
Result:
Thus a C Program to store to store employee details using union was executed and the
output was obtained.
10. File handling
Aim:
To write a C program to read name and marks of n number of students from and store them in a
file. If the file previously exits, add the information to the file.
Algorithm:

STEP 1: Start the program.


STEP 2: Declare text file student and stored in the directory
STEP 3: Use the FILE operation like fopen to open the file.
STEP 4: Add the student name and marks by using for loop.
STEP 4: Run the program to get the desired output.
STEP 5: Stop the program.

Program:

#include <stdio.h>
#include<conio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;

printf("Enter number of students: ");


scanf("%d", &num);

fptr = (fopen("C:\\student.txt", "a"));


if(fptr == NULL)
{
printf("Error!");
exit(1);
}

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


{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);

printf("Enter marks: ");


scanf("%d", &marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);


}

fclose(fptr);
return 0;
}

Output:
Enter number of students 2
For student1
Enter name:pravi
Enter marks:100
For Student 2
Enter name: Theeran
Enter marks:100

Student.txt
Name: pravi
Marks=100
Name: Theeran
Marks=100
Result:
Thus a C Program to store to read name and marks of n number of students from and store them
in a file. If the file previously exits, add the information to the file was executed and the output was
obtained.

You might also like