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

C Programming Lab Manual I Year

The document is a lab manual for the course 'Programming for Problem Solving using C' at Rohini College of Engineering and Technology, detailing the vision, mission, program outcomes, course objectives, syllabus, and specific experiments. It outlines the skills students will develop, such as problem-solving techniques, programming in C, and using data structures. The manual includes various programming exercises and algorithms to enhance students' understanding of C programming concepts.

Uploaded by

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

C Programming Lab Manual I Year

The document is a lab manual for the course 'Programming for Problem Solving using C' at Rohini College of Engineering and Technology, detailing the vision, mission, program outcomes, course objectives, syllabus, and specific experiments. It outlines the skills students will develop, such as problem-solving techniques, programming in C, and using data structures. The manual includes various programming exercises and algorithms to enhance students' understanding of C programming concepts.

Uploaded by

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

ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

24CS201 - PROGRAMMING FOR PROBLEM SOLVING USING C

LAB MANUAL

SEMESTER: I REGULATION:2024

PREPARED BY APPROVED BY

A.HASEENA BEEVI DR.I. MICHAEL REVINA


Assistant Professor, Associate Professor & Head
Department of Computer Science
Department of AI&DS
& Engineering

Revision No: DATE


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

VISION
 To be a centre of excellence in engineering and technology
 To produce technocrats who are technically competent, ethically strong for
advancement of the society.

MISSION
 To provide quality education in emerging technologies in accordance with industrial trends.
 To build good research capabilities and support new innovations.

DEPARTMENT OF COMPUTER SCINENCE AND ENGINEERING

Vision
 To create young software professionals to compete the global challenges in the field of
computerscience and engineering and be researcher to meet the need of society.

Mission
 To provide quality education to develop software for real time problem in scientific and
business application for various needs of industry.
 To provide learning ambience to enhance innovations, problem solving skill, leadership
qualities, team spirit and ethical responsibility to serve the society.

PEO
 PEO1- The graduates will be able to design and to adapt modern tools to innovate ideas and
develop computational solution for technological problem.
 PEO2- The graduates will be able to develop professional skills for employment and
understandthe need of lifelong learning for a successful professional career.
 PEO3- To develop an ability to become successful professional, entrepreneur and urge for
pursuinghigher studies.
Program outcomes

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals andan engineering specialization to the solution of complex engineering problems.

2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

3. Design/development of solutions: Design solutions for complex engineering problems and


design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

4. Conduct investigations of complex problems: Use research-based knowledge and research


methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.

5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding ofthe limitations.

6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.

7. Environment and sustainability: Understand the impact of the professional engineering


solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of theengineering practice.

9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverseteams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.

11. Project management and finance: Demonstrate knowledge and understanding of the
engineering andmanagement principles and apply these to one‘s own work, as a member and leader
in a team, to manage projects and in multidisciplinary environments.

12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
COURSE OBJECTIVES

1. To gain knowledge on problem solving techniques


2. To learn how to write simple and modular C programs

3. To develop C programs using arrays and strings

4. To learn the usage of pointers in accessing and manipulating memory

5. To develop applications in C using structures & files

SYLLABUS
24CS201 - PROGRAMMING FOR PROBLEM SOLVING USING C

S.No. Name of the Experiment


1 Steps in Problem Solving Drawing flowchart using yEd tool/Raptor Tool programs using simple
statements
2 Programs to illustrate the use of user-defined data types
3 Programs using decision making statements
4 Programs using looping statements
5 Programs using user defined functions and recursive functions
6 Build programs using arrays and array-based operations.
7 Programs using one dimensional and two dimensional arrays.
8 Implementations involving array usage for string operations.
9 Implementations involving pointers for dynamic memory allocation
10 Implementations involving pointers for string manipulation.
11 Programs to implement structures
12 Programs to implement union
13 Programs to implement various file operations
COURSE OUTCOMES:
[Cognitive Level K1- remember, K2- Understand, K3- Apply, K4 - Analyze,
K5- Evaluate, K6- Synthesize]
On completion of the course, the student will have the ability to:

COs Course Outcomes K Level

CO1 Make use of problem solving techniques to solve real world problems & outline K3
the structure of C program
CO2 Identify the appropriate looping and control statements in C and develop K3
applications using these statements
CO3 Make use of arrays & strings in development of simple applications K3

CO4 Apply the concepts of pointers and develop C programs using pointer K3

CO5 Develop programs for storing, retrieving and processing data using structures K3
and files.
CONTENTS
SL.NO NAME OF THE EXPERIMENT

1 Problem Solving by Drawing flowchart using yEd tool/Raptor Tool for programs
using simple statements

2
Programs to illustrate the use of user-defined data types

3 Programs using decision making statements

4 Programs using looping statements

5 Programs using user defined functions and recursive functions

6 Build programs using arrays and array-based operations.

7 Programs using one dimensional and two dimensional arrays.

8 Implementations involving array usage for string operations.

9 Implementations involving pointers for dynamic memory allocation

10 Implementations involving pointers for string manipulation.

11 Programs to implement structures

12 Programs to implement union

13 Programs to implement various file operations


EX.NO.1. PROBLEM SOLVING BY DRAWING FLOWCHART USING YED TOOL

/ RAPTOR TOOL

Aim:

To develop Flowcharts for simple problem solving using yED and Raptor Graphics tools

Procedure

1. Define the Problem:

Identify and clearly define the problem you need to solve. Understand the inputs, outputs, and
process involved.

2. Open yEd Tool:

Launch the yEd tool on your computer.

3. Create a New Document:

Start a new flowchart by creating a new document or canvas.

4. Add Shapes for Process Steps:

(i) Use shapes to represent different steps in the process. Common shapes include:

 Oval for Start/End


 Rectangle for Process/Action
 Diamond for Decision
 Parallelogram for Input/Output

(ii) Drag and drop these shapes onto your canvas.

5. Connect Shapes:

Use arrows to connect the shapes, indicating the flow of the process. The direction of the
arrows shows the order of operations.

6. Label Shapes:

Click on each shape to add text describing the action or decision represented by that shape.

7. Review and Adjust:

1
Ensure the flowchart logically represents the problem-solving process. Adjust shapes and
connections as needed for clarity.

8. Save and Export:

Save your flowchart. You can also export it in various formats like PNG, PDF, etc., for
sharing or printing.

Using Raptor Tool

1. Define the Problem:

Clearly understand and define the problem and the steps needed to solve it.

2. Open Raptor Tool:

Start the Raptor tool on your computer.

3. Create a New Flowchart:

Open a new flowchart document or workspace in Raptor.

4. Add Flowchart Symbols:

 Start Symbol: Use the oval to denote the start of the process.
 Process Symbol: Use rectangles for actions or steps in the process.
 Decision Symbol: Use diamonds for decision-making points.
 Input/Output Symbol: Use parallelograms for input or output operations.

Drag and drop these symbols onto the workspace.

5. Connect Symbols:

Draw lines or arrows to connect the symbols, showing the flow from one step to the next.

6. Add Descriptions:

Click on each symbol to enter the descriptions or actions for that step.

7. Test the Flowchart:

Use Raptor's simulation feature to test your flowchart and ensure it executes correctly.

8. Review and Refine:


2
Check for any logical errors or improvements needed. Refine your flowchart to ensure
accuracy and clarity.

9. Save and Export:

Save your work. Raptor allows you to export your flowchart in various formats for
documentation or sharing purposes.

1.To develop a flowchart to Add two numbers entered by the user.


Algorithm:
STEP 1: Start the program.
STEP 2: Read the values of ‘a’ & ’b’.
STEP 3: Compute the sum of the entered numbers ‘a’, ’b’, c= a+b.
STEP 4: Print the value of ‘c’.
STEP 5: Stop the program.

Flowchart

2. To Develop a flowchart to calculate the simple interest.


Algorithm:
Step 1: Start
Step 2: Read principal amount as ‘p’, time as ‘t’ and rate of interest as ‘r’
Step 3: SI = p*t*r/100

3
Step 4: Display SI
Step 5: Stop

Flowchart:

3. To develop a Flowchart to convert temperature in Celsius to Fahrenheit


Algorithm
Step 1: Start
Step 2: Read temperature in Celsius as ‘C’
Step 3: F = 9 / 5 * C + 32 [Convert to Fahrenheit]
Step 4: Display F
Step 5: Stop

4
Flowchart:

4. To Develop a flow chart to find whether a year is a Leap Year or not.


Algorithm
Step1: Start the program
Step2: Declare year as int data type
Step3: Read the year
Step4: Check if (ye%4==0)
STEP4.1: Print “It is a leap year”
Step5: Else Step5.1: Print “It is not a leap year”
Step6: stop the program.

5
5. Draw the flow chart for finding largest of three numbers
Algorithm
1. Start the program
2. Input the values of three numbers as A,B,C
3. Check If (A>B) and (A>C) then print “A is greater”.
Else if (B>A) and (B>C) then print “B is greater”.
Else print “C is greater”.
4. Stop the program

6
Flowchart

Result:
Thus the flowcharts were drawn using Yed and Raptor graphics tools
7
EX.NO.2 PROGRAMS TO ILLUSTRATE THE USE OF USER-DEFINED DATA TYPES
Aim:
To develop the c programs by using simple and user defined data types.
1) Sum of two numbers
Algorithm:
STEP 1: Start the program.
STEP 2: Read the values of ‘a’&’b’.
STEP 3: Compute the sum of the numbers by using the formula c=a+b.
STEP 4: Print the value of ‘c’.
STEP 5: Stop the program.

Program:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
int sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}

Output:
Enter two integers: 5
6
sum:11

8
2) Program to print a student details using User-defined Data Types
Algorithm:
STEP 1: Start the program.
STEP 2: Declare variables for student details in a structure using the keyword struct
STEP 3: Create an object for the structure.
STEP 4: Define the values for the structure members
STEP 5: Print the student details by accessing it with structure object.
STEP 6: Stop the program.

Program
#include <stdio.h>
typedef struct
{
char name[30];
int age;
} Person;
int main() {
Person p1 = {"abinesh", 25};
printf("Name: %s, Age: %d\n", p1.name, p1.age);
return 0;
}

Output :

Result:
Thus the C programs for using basic and user defined datatypes were written executed and the
output was verified.

9
Ex.No :3 PROGRAMS USING DECISION MAKING STATEMENTS
1) Program to check even or odd number
Aim:
Write a c program to check for even or odd number using decision making statements.

Algorithm:
STEP 1: Start the program.
STEP 2: Read a number
STEP 3: Divide a number by 2 and get the remainder.
STEP 4: Check if the remainder is 0, then print the number is even
STEP 5: Else print the number is odd
STEP 6: Stop the program.
Program:
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("Even number\n");
}
else {
printf("Odd number\n");
}
return 0;
}

10
Output:

2) Write a program to find whether a character is consonant or vowel using switch statement.
Aim:
Write a c program to check a character is consonant or vowel using decision making statements.
Algorithm:
STEP 1: Start the program.
STEP 2: Read a character.
STEP 3: Construct a switch case to compare the character with vowels “a,e,i,o,u” or “A,E,I,O,U”
STEP 4: Check the character whether it is matched with switch cases.
STEP 5: If matches occurs print the character as vowel.
STEP 6: If all cases not matches, print the given character is Consonant by default case.

Program :
#include <stdio.h>
void main()
{
char ch;
printf(“Enter any alphabet:”); //input alphabet from user
scanf(“%c”, &ch);
switch(ch)
{
case ‘a’:
case ‘A’:
printf(“Vowel”);
break;
case ‘e’:

11
case ‘E’:
printf(“Vowel”);
break;
case ‘i’:
case ‘I’:
printf(“Vowel”);
break;
case ‘o’:
case ‘O’:
printf(“Vowel”);
break;
case ‘u’:
case ‘U’:
printf(“Vowel”);
break;
default: printf(“Consonant”);
}
}

Result:
Thus the C programs by using decision making statements were written executed and the
output was verified.
12
Ex NO :4 PROGRAMS USING LOOPING STATEMENTS

1) Program to print n numbers using for loop


Aim:
Write C Programs to print n numbers using for loop.

Algorithm:

STEP 1: Start the program.


STEP 2: Set the range of numbers as a condition in for loop
STEP 3: Use the counter, initialize the counter by 1.
STEP 4: Print the value of counter variable
STEP 5: Increment the counter variable
STEP 6: Repeat the steps 4 and 5 with the help of for loop
STEP 7: Stop the program

Program:

#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++)
{
printf("Number: %d\n", i);
}
return 0;
}
Output

13
2) Program to calculate sum of n numbers Using While loop
Aim:
Write a c program to find the sum of integers using while loop.

Algorithm:
STEP 1: Start the program.
STEP 2: Initialize the value of sum as 0
STEP 3: Read a number from the user (integer range)
STEP 4: Initialize i as iteration variable and set initial value to 1.
STEP 5: Calculate sum = sum+i
STEP 6: increment the value of i
STEP 7: Repeat the steps 5 and 6 until i becomes larger than n (integer range)
STEP 8: Stop the program.
Program:
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}
Output:
Enter a positive integer: 100
Sum = 5050

14
3) Write a program to display the following pattern.
*
**
***
****
*****
Aim:
Write a c program to display the given pattern
Algorithm:

STEP 1: Start the program.


STEP 2: Set up a for loop with range of number of stars in a last row.
STEP 3: Construct a nested for loop to iterate for increasing row positions.
STEP 4: Print *
STEP 5: Repeat the steps 4 until condition becomes false
STEP 7: Stop the program

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1; i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
15
}

Output:
*
**
***
****
*****

Result:
Thus the C programs by using looping statements were written executed and the output was
verified.

16
Ex.No : 5 PROGRAMS USING USER DEFINED FUNCTIONS AND RECURSIVE
FUNCTIONS
1) Program to check a given number a prime number or not using user defined function
Aim:
Write a c program to check a number is prime or not using user defined functions.
Algorithm:

STEP 1: Start the program


STEP 2: Read a number as n1
STEP 3: Define a function to check for prime number.
STEP 4: Iterate a “for” loop from 2 to num/2.
STEP 5: If num is divisible by loop iterator, check the remainder.
STEP 6: If the remainder is equal to 0, Return 0 as return value from function else return 1.
STEP 7: Call the function and check for return value if it is 1
STEP 8: Print the given number is prime
STEP 9: Else print the given number is not a prime number.
Program:
#include<stdio.h>
int PrimeOrNot(int);
int main()
{
int n1,prime;
printf(" Input a positive number : ");
scanf("%d",&n1);
rvalue = PrimeOrNot(n1);
if(rvalue==1)
printf(" The number %d is a prime number.\n",n1);
else
printf(" The number %d is not a prime number.\n",n1);
return 0;
}
int PrimeOrNot(int n1)
17
{
int i=2;
while(i<=n1/2)
{
if(n1%i==0)
return 0;
else
i++;
}
return 1;
}
2) Program to find factorial of a number using recursion
Aim:
Write a c program to find factorial of a number using recursion.
Algorithm:

Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f

Program
#include <stdio.h>
int factorial(int n) {
if (n == 0)
18
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

Result:
Thus the C programs by using user defined and Recursive functions were written executed and
the output was verified.

19
Ex.No :6 PROGRAMS USING ARRAYS AND ARRAY-BASED OPERATIONS
Aim:
Write a c program to add All elements in an array
Algorithm:

STEP1: Initialize an array arr and a variable sum.


STEP2: Set the value of sum=0.
STEP3: Start a for loop from index 0 to the length of the array – 1.
STEP4: In every iteration, perform sum = sum + arr[i].
STEP5: After the termination of the loop, print the value of the sum.
Program
#include <stdio.h>
int main()
{
int arr[5], sum = 0;
printf("Enter 5 integers:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
for (int i = 0; i < 5; i++)
{
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
Output

20
Result:
Thus the C programs for Array operations were written executed and the output was verified.

21
Ex.No :7 ONE DIMENSIONAL AND TWO DIMENSIONAL ARRAYS
1) Program to perform linear search using one dimensional array.
Aim:
Write a c program to perform linear search using single dimensional array.

Algorithm:
STEP 1: Start the program
STEP 2: Start traversing from the start of the array.
STEP 3: Compare the current element with the key (element to be searched).
STEP 4: If the element is equal to the key, return index.
STEP 5: Else, increment the index and repeat the step 2 and 3.
STEP 6: If we reach the end of the Array without finding the element equal to the key, return some
value to represent that the element is not found.
STEP 7: Stop the program

Program
#include <stdio.h>
int linearSearch(int* arr, int n, int key)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
return i;
}
}
return -1;
}
int main()
{
int arr[] = { 10, 50, 30, 70, 80, 60, 20, 90, 40 };

22
int n = sizeof(arr)
int key = 30;
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);

return 0;
}

2) Matrix multiplication using two dimensional array.


Aim:
Write a c program to perform linear search using single dimensional array.

Algorithm:
Step 1: Start
Step 2: Declare matrix mat1[row1][col1]; and matrix mat2[row2][col2]; and matrix mul[row1][col2];
row= no. of rows, col= no. of columns
Step 3: Read mat1[row1][col1] and mat2[row2][col2]
Step 4: Declare variable i=0, j=0
Step 5: Set a loop from i=0 to i=row1.
Step 6: Set an inner loop for the above loop from j=0 to j=col2
Initialise the value of the element (i, j) of the new matrix (mul[row1][col2]) to 0.
Set an inner loop inside the above loop from k=0 to k=row1.
Using the add and assign operator (+=) store the value of mat1[i][k] * mat2[k][j] in the third
matrix, mul[i][j].
Print the third matrix.
Step 7: Stop

23
Program :
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
} }
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
} }
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)

24
{
mul[i][j]+=a[i][k]*b[k][j];
} } }
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
Output:

enter the number of row=3


enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

Result:
Thus the C programs by using one dimensional and two dimensional array were written
executed and the output was verified.

25
Ex.No :8 ARRAY USAGE FOR STRING OPERATIONS
Aim:
Write a c program to concatenate two character arrays as strings
Algorithm:

STEP 1: Start the program.


STEP 2: Declare strings as character arrays
STEP 3: Get the string values from the user
STEP 4: Concatenate the string using strcat() function.
STEP 5: Print the Concatenated String
STEP 6: Stop the program

Program
#include <stdio.h>
#include <string.h>
int main() {
char str1[20], str2[20];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
strcat(str1, str2); // Concatenating strings
printf("Concatenated String: %s\n", str1);
return 0;
}
Output:

Result:
Thus the C Program Array based string operations were written executed and the output was
verified.
26
Ex.No : 9 POINTERS FOR DYNAMIC MEMORY ALLOCATION
Aim:
Write a c program to perform dynamic memory allocation using pointers.
Algorithm:
STEP 1: Start the program.
STEP 2: Read the number of elements for an array
STEP 3: Allocate the memory for array dynamically using malloc function
STEP 4: Assign the memory to array using pointer variable
STEP 5: Get the array elements and store it into allocated memory.
STEP 6: Print the array elements
STEP 7: Deallocate the memory using free() method.
STEP 8: Stop the program

Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *ptr = (int *)malloc(sizeof(int) * n);
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
27
Output:

Result:
Thus the C programs by using pointers for dynamic memory allocations was written executed
and the output was verified.

28
Ex.No : 10 POINTERS FOR STRING MANIPULATION

Aim:
Write a c program to perform string manipulation using pointers.
Algorithm:

STEP 1: Start the program.


STEP 2: Declare a string variable
STEP 3: Get the string data from the user
STEP 4: Assign the String to the pointer.
STEP 5: Print the characters of string using pointer sequentially by incrementing the pointer.
STEP 6: Stop the program

Program:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
char *ptr = str;
printf("String characters: ");
while (*ptr != '\0') {
printf("%c", *ptr);
ptr++;
}
printf("\n");
return 0;
}

29
Output:

Result:
Thus the C programs by using pointers for string manipulations was written executed and the
output was verified.

30
Ex.No : 11 PROGRAMS TO IMPLEMENT STRUCTURES
Aim:
Write a c program to process the student details using structures
Algorithm:

STEP 1: Start the program.


STEP 2: Define a structure to declare student details as members
STEP 3: Define a structure object
STEP 4: Get the values of Structure members from the user.
STEP 5: Print the student details by accessing the member values using structure object
STEP 6: Stop the program

Program:
#include <stdio.h>
struct Student {
char name[50];
int roll_no;
float marks;
};
int main() {
struct Student s;
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
if (s.name[strlen(s.name) - 1] == '\n')
{
s.name[strlen(s.name) - 1] = '\0';
}
printf("Enter roll number: ");
scanf("%d", &s.roll_no);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Name: %s, Roll No: %d, Marks: %.2f\n", s.name, s.roll_no, s.marks);
31
return 0;
}

Result:
Thus the C programs for implementing structures was written executed and the output was
verified.

32
Ex.No : 12 PROGRAM TO IMPLEMENT UNION
Aim:
Write a c program to process the data using union data type.

Algorithm:
STEP 1: Start the program.
STEP 2: Define a union to declare data members like integer, float, string data members
STEP 3: Define a union object
STEP 4: Get the values of union members from the user.
STEP 5: Print the datas by accessing the member values using union object
STEP 6: Stop the program Program:
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.2f\n", data.f);
strcpy(data.str, "C Programming");
printf("data.str: %s\n", data.str);
return 0;
}

33
Output:

Result:
Thus the C programs for implementing union was written executed and the output was
verified.

34
Ex.No : 13 PROGRAM TO IMPLEMENT VARIOUS FILE OPERATIONS
Aim:
Write a c program to perform various file operations like file open, read, write and close.
Algorithm:

STEP 1: Start the program.


STEP 2: Create a file with a name
STEP 3: Define a string to be used as a file content
STEP 4: Write the content into a file.
STEP 5: Open the file
STEP 6: Print the contents of the file in the screen.
STEP 7: Stop the program.

Program:
#include <stdio.h>
int main() {
FILE *fptr;
char filename[] = "sample.txt";
char content[] = "Welcome to C Programming";
fptr = fopen(filename, "w");
fprintf(fptr, "%s\n", content);
fclose(fptr);
char buffer[100];
fptr = fopen(filename, "r");
fgets(buffer, 100, fptr);
printf("File Content: %s\n", buffer);
fclose(fptr);
return 0;
}

35
Output:
"Welcome to C Programming"

Result:
Thus the C programs for implementing various file operations was written executed and the
output was verified.

36

You might also like