C Programming Lab Manual I Year
C Programming Lab Manual I Year
LAB MANUAL
SEMESTER: I REGULATION:2024
PREPARED BY APPROVED BY
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.
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
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.
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.
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
SYLLABUS
24CS201 - PROGRAMMING FOR PROBLEM SOLVING USING C
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
/ RAPTOR TOOL
Aim:
To develop Flowcharts for simple problem solving using yED and Raptor Graphics tools
Procedure
Identify and clearly define the problem you need to solve. Understand the inputs, outputs, and
process involved.
(i) Use shapes to represent different steps in the process. Common shapes include:
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.
1
Ensure the flowchart logically represents the problem-solving process. Adjust shapes and
connections as needed for clarity.
Save your flowchart. You can also export it in various formats like PNG, PDF, etc., for
sharing or printing.
Clearly understand and define the problem and the steps needed to solve it.
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.
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.
Use Raptor's simulation feature to test your flowchart and ensure it executes correctly.
Save your work. Raptor allows you to export your flowchart in various formats for
documentation or sharing purposes.
Flowchart
3
Step 4: Display SI
Step 5: Stop
Flowchart:
4
Flowchart:
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
Algorithm:
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:
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
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:
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;
}
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:
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:
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:
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:
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:
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