R23 C Programming Lab Record With Program(1)
R23 C Programming Lab Record With Program(1)
B23CSI101 - C PROGRAMMING
Name ………………………………………………………………………….
Batch …………………….. Reg. No. ………………….…….…
Branch ………………………. Year ……………………………….
KIT-Kalaignarkarunanidhi Institute of Technology
(An Autonomous Institution, Approved by AICTE & Affiliated to Anna University, Chennai)
Coimbatore – 641 402
Department of
………………………………………………………………………………………………
Record Work of
… ..................................................................................................................................Laboratory
Name:………………………………………………………………………………………………
Branch………………………………………………………………………………………………..
Date:
…………………………………………………
7. Shutdown the Computer properly and arrange chairs in order before leaving thelab.
8. The program should be written on the left side pages of the record work book.
9. The record workbook should be completed in all aspects and submitted in the nextclass
itself.
10. Experiment number with date should be written at the top left-hand corner of the
11. Strictly follow the uniform dress code for Laboratory classes.
13. Avoid eatables inside and maintain the cleanliness of the lab.
VISION
To produce intellectual graduates to excel in the field of Computer Science Engineering and Technologies.
MISSION
Providing excellent and intellectual inputs to the students through qualified faculty members.
Imparting technical knowledge in latest technologies through the industry institute interaction and thereby
making the graduates ready for the industrial environment.
Enriching the student’s knowledge for active participation in co-curricular and extracurricular activities.
Promoting research-based projects in contexts to social, legal and technical aspects.
PEO1: Graduate will be successful in their profession by taking part actively in the field of software and
technology.
PEO2: Graduate will be proficient in analyzing and facing the challenge in computer science and
engineering.
PEO3: Graduate will engage in lifelong learning activities by adapting to the advanced software
technologies for continuous professional development.
PROGRAM SPECIFIC OUTCOME (PSOs)
After the successful completion of the U.G. programme in Computer Science and Engineering, Graduates
will be able to:
PSO1: Categorize the basic engineering knowledge to solve the problems in Computer Science and
Engineering according to the environmental needs.
PSO2: Apply the modern tools to design and develop the software system ethically to the industrial needs.
COURSE OUTCOMES
At the end ofthis course, the student will be able to:
CO1 K2 3 2 1 - - - - - 2 1
1 1 1 1
CO2 K3 3 2 1 - 1 - - 1 2 - - 2 2 1
CO3 K3 3 2 1 - 1 - - 1 2 - - 2 2 1
CO4 K3 3 2 1 - 1 - - 1 2 - - 2 2 1
CO5 K3 3 2 1 - 1 - - 1 2 - - 2 2 1
Weighted
3 2 1 - 1 - - 1 2 - - 2 2 1
average
SYLLABUS
LIST OF EXPRIMENTS
1. Experiment with I/O statements, operators, expressions
2. Develop C programs for Decision Making Construct.
a) if-else b) switch-case c) goto, break-continue
3. Develop a Cprograms for Loop Control statements.
a) for b) Nested for c) while and do-while
4. Develop a C programs for Array
a) One Dimensional – Sorting and Searching b)Two Dimensional – Matrix Operations c) Traversal
5. Develop a C program to perform the pointers
Linear Search b) Binary Search c) Pointer Operation
6. Build a C programs for the recursive function
7. Implement a C programs for string operations
String operations using build in methods
8. Develop a C program to experiment with Pass by value and Pass by Reference
9. Develop a c program for structure and union
a) Payroll using structure and union b) Student records using structure and union.
10. Develop a C program to perform file operations
2(c) C program for decision making using goto, break and continue statement
3(a) C program to input a number and calculate its factorial using while loop
3(b)
3(
C program to input a number and calculate sum of digits using do-while loop.
C program to input a number and check whether the number is prime number or not
3(c)
using for loop.
3(d) C program to find the prime numbers from 2 to 50 using nested for loop.
4 (a)(i) C program to input elements in array and sort array elements in ascending order
4(a)(ii) C program to input elements in array and search whether an element exists in array or not.
4(c) C program to read square matrix of order n, transpose it using user defined function.
5 (c) C program to read two numbers from user and add them using pointers.
7 (a) C program to find the length of the string using strlen() function.
Date
Name of the Experiment
Page
Number
Aim &
Algorithm
(20 Marks)
Program
Signature of the Faculty Member
(25 Marks)
Output &
Inference (10
Marks)
Viva-Voce(20
Marks)
Total
(75Marks)
Signature of
the Faculty
Member
No.
Sl.
Model Exam Marks (25):
Date
Name of the Experiment
Page
Number
Aim
&Algorithm
(20 Marks)
Program
(25 Marks)
Signature of the Faculty Member
Output &
Inference
(10 Marks)
Viva-Voce
(20 Marks)
Total
(75
Marks)
Signature
ofthe
Faculty
Member
No.
Sl.
Model Exam Marks (25):
Date
Name of the Experiment
Page
Number
Aim
&Algorithm
(20 Marks)
Program
Signature of the Faculty Member
(25 Marks)
Output &
Inference
(10 Marks)
Viva-Voce
(20 Marks)
Total
(75Marks)
Signature of
the Faculty
Member
No.
Sl.
Model Exam Marks (25):
Date
Name of the Experiment
Page
Number
Aim
&Algorithm
(20 Marks)
Program
Signature of the Faculty Member
(25 Marks)
Output &
Inference
(10 Marks)
Viva-Voce
(20 Marks)
Total
(75Marks)
Signature of
the Faculty
Member
B23CSI101 C PROGRAMMING
AIM:
To write a C program for formatted and unformatted input and output statements.
ALGORITHM:
STEP 3: Get input values from the user output using formatted and unformatted input statement.
STEP 4: Display the output using formatted and unformatted output statement.
14
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
char name [50];
char ch;
int age;
float height;
printf("\nFormatted Output:\n");
printf ("Name: %s\n", name);
printf ("Age: %d\n", age);
printf ("Height: %.2f meters\n", height);
printf("\nUnformatted Output:\n");
putchar(ch); // Outputs a single character
printf("\n");
puts(name); // Outputs the string
return 0;
}
15
B23CSI101 C PROGRAMMING
OUTPUT:
Formatted Output:
Name: John
Age: 25
Height: 1.80 meters
Unformatted Output:
A
John Doe
16
B23CSI101 C PROGRAMMING
AIM:
To write a C program to perform operations on operators and expressions.
ALGORITHM:
17
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int a = 10, b = 20, c;
float x = 5.5, y = 2.5;
int result;
// Arithmetic Operators
printf("Arithmetic Operations:\n");
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("b / a = %d\n", b / a);
printf("b %% a = %d\n", b % a);
// Relational Operators
printf("\n Relational Operations:\n");
printf("a > b = %d\n", a > b);
printf("a < b = %d\n", a < b);
printf("a == b = %d\n", a == b);
printf("a != b = %d\n", a != b);
// Logical Operators
printf("\n Logical Operations:\n");
printf("(a > 5 && b > 15) = %d\n", (a > 5 && b > 15));
printf("(a > 15 || b > 15) = %d\n", (a > 15 || b > 15));
printf("!(a > 15) = %d\n", !(a > 15));
// Bitwise Operators
printf("\nBitwise Operations:\n");
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 2 = %d\n", a << 2); // Left shift
printf("a >> 2 = %d\n", a >> 2); // Right shift
// Assignment Operators
printf("\n Assignment Operations:\n");
c = a + b;
printf("c = a + b = %d\n", c);
c += a;
printf("c += a = %d\n", c);
c -= b;
printf("c -= b = %d\n", c);
c *= a;
printf("c *= a = %d\n", c);
c /= a;
18
B23CSI101 C PROGRAMMING
// Conditional Operator
printf("\n Conditional (Ternary) Operation:\n");
result = (a > b) ? a : b;
printf("Max of a and b = %d\n", result);
return 0;
}
19
B23CSI101 C PROGRAMMING
OUTPUT:
Arithmetic Operations:
a + b = 30
a - b = -10
a * b = 200
b/a=2
b%a=0
Relational Operations:
a>b=0
a<b=1
a == b = 0
a != b = 1
Logical Operations:
(a > 5 && b > 15) = 1
(a > 15 || b > 15) = 1
!(a > 15) = 1
Bitwise Operations:
a&b=0
a | b = 30
a ^ b = 30
~a = -11
a << 2 = 40
a >> 2 = 2
Assignment Operations:
c = a + b = 30
c += a = 40
c -= b = 20
c *= a = 200
c /= a = 20
c %= a = 0
20
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
RESULT:
21
B23CSI101 C PROGRAMMING
AIM:
To write a C program to check whether the given number is positive number or negative number using
if-else statement.
ALGORITHM:
STEP 4: Check the input value whether it is a positive number or negative number.
STEP 5: If the number is less than ZERO, then print the result as “NEGATIVE”. Otherwise display
22
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int number;
return 0;
}
23
B23CSI101 C PROGRAMMING
OUTPUT:
24
B23CSI101 C PROGRAMMING
Ex. No. 2(b) C PROGRAM FOR DECISION MAKING USING SWITCH - CASE
Date STATEMENT
AIM:
To write a C program to design a menu-based calculator to perform various basic arithmetic
operations like addition, subtraction, multiplication, division and modulo.
ALGORITHM:
STEP 3: Get two inputs from the user using scanf() function and store them in “a” and “b” respectively
STEP 4: Get the user option and based on the user options, perform the corresponding arithmetic
STEP 5: Store the result in a variable called “c” and display the value of “c” using printf()
25
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
// Input numbers
if (choice >= 1 && choice <= 4) {
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
}
return 0;
}
26
B23CSI101 C PROGRAMMING
OUTPUT:
Case 1: Addition
Select an operation to perform:
1. Addition
2. Subtraction
3. Multiplication
4. Division
27
B23CSI101 C PROGRAMMING
Ex. No. 2(c) C PROGRAM FOR DECISION MAKING USING GOTO, BREAK AND
Date CONTINUE STATEMENT
AIM
To write a C program for decision making using goto, break and continue statement.
ALGORITHM
STEP3: Goto: The for loop and a goto statement is used to skip printing the number 2. The label skip is
STEP 4: Break: The for loop and the break statement is used to exit the loop when i becomes 3.
STEP 5: Continue: The for loop and the continue statement is used to skip printing the number 2 and
28
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int choice, i;
int num;
while (1) {
// Display menu
printf("\nMenu:\n");
printf("1. Print numbers from 1 to 10 (using continue)\n");
printf("2. Check if a number is odd or even (using break)\n");
printf("3. Exit program (using goto)\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: // Use of 'continue'
printf("Numbers from 1 to 10 (skipping 5 using continue):\n");
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the number 5
}
printf("%d ", i);
}
printf("\n");
break;
default:
printf("Invalid choice. Please select 1, 2, or 3.\n");
}
}
end:
printf("Program terminated.\n");
return 0;
}
29
B23CSI101 C PROGRAMMING
OUTPUT:
30
B23CSI101 C PROGRAMMING
INFERENCE
VIVA QUESTIONS
1. What is the purpose of the goto statement in C?
RESULT
31
B23CSI101 C PROGRAMMING
AIM:
To write a C program to input a number and calculate its factorial using while loop.
ALGORITHM:
STEP 3: Read a number whose factorial is to be found. Store it in a variable (int num).
STEP 4: Set the while loop to the condition (i <= num) where initial value of i = 1
STEP 5: Inside the while loop, multiply the variable fact and variable i, and store the result in
32
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int number, factorial = 1, i;
return 0;
}
33
B23CSI101 C PROGRAMMING
OUTPUT:
Case 2: Zero
Enter a positive integer: 0
Factorial of 0 is 1
34
B23CSI101 C PROGRAMMING
Ex.No. 3(b)
C PROGRAM BY USING WHILE, DO-WHILE AND FOR LOOPS.
Date
AIM:
To write a C program to input a number and calculate sum of digits using do-while loop.
ALGORITHM:
STEP 3: Find last digit of the number. To get last digit modulo division the number by 10 i.e.,
lastDigit = num % 10.
STEP 4: Add last digit found above to sum i.e., sum = sum + lastDigit.
STEP 5: Remove last digit from number by dividing the number by 10 i.e., num = num / 10.
STEP 6: Repeat step 3-5 till number becomes 0. Finally, you will be left with the sum of digits in
sum.
STEP 7: Stop the program.
35
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int number, sum = 0, digit;
return 0;
}
36
B23CSI101 C PROGRAMMING
OUTPUT:
37
B23CSI101 C PROGRAMMING
Ex.No. 3(c)
C PROGRAM BY USING WHILE, DO-WHILE AND FOR LOOPS.
Date
AIM:
To write a C program to input a number and check whether the number is prime number or not
using for loop.
ALGORITHM:
STEP 3: Declare and initialize another variable say isPrime = 1. isPrime variable is used as a
notification or flag variable. Assigning 0 means number is composite and 1 means prime.
STEP 5: Check, divisibility of the number i.e., if(num%i == 0) then, the number is not prime. Set
isPrime = 0 indicating number is not prime and terminate from loop.
STEP 6: Outside the loop check the current value of isPrime. According to our assumption if it is
equal to 1 then the number is prime otherwise composite.
38
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int number, i, isPrime = 1;
return 0;
}
39
B23CSI101 C PROGRAMMING
OUTPUT:
40
B23CSI101 C PROGRAMMING
Ex.No. 3(d)
C PROGRAM BY USING WHILE, DO-WHILE AND FOR LOOPS.
Date
AIM:
To write a C program to find the prime numbers from 2 to 50 using nested for loop.
ALGORITHM:
STEP 3: Using nested for loop check for the condition. If the factor is found it is not prime
41
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int num, i, j, isPrime;
// Nested for loop to check if the number is divisible by any number between 2 and num/2
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // If num is divisible by i, it is not a prime number
break; // No need to check further
}
}
return 0;
}
42
B23CSI101 C PROGRAMMING
OUTPUT:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
43
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
4. What is the difference between the local variable and global variable in C?
RESULT:
44
B23CSI101 C PROGRAMMING
AIM:
To write a C program to input elements in array and sort array elements in ascending order.
ALGORITHM:
STEP 2: Input size and elements in array from user. Store it in some variable say size and arr.
STEP 3: To select each element from array, run an outer loop from 0 to size – 1.
STEP 4: Run another inner loop from i + 1 to size - 1 to place currently selected element at its correct
position.
STEP 5: Inside inner loop to compare currently selected element with subsequent element and swap
two array elements if not placed at its correct position. Which is if(arr[i] > arr[j]) then swap arr[i] with
arr[j].
STEP 6: After this nested loop gets executed, we get all the elements of the array sorted in ascending
order.
45
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int n, i, j, temp;
return 0;
}
46
B23CSI101 C PROGRAMMING
OUTPUT:
47
B23CSI101 C PROGRAMMING
AIM:
To write a C program to input elements in array and search whether an element exists in array or
not.
ALGORITHM:
STEP 2: Input size and elements in array from user. Store it in some variable say size and arr.
STEP 3: Input number to search from user in some variable say toSearch.
STEP 4: Define a flag variable as found = 0. which means initially we have assumed that searched
element does not exists in array.
STEP 6: Inside loop check if current array element is equal to searched number or not. Which is
if(arr[i] == toSearch) then set found = 1 flag and terminate from loop. Since element is found no need
to continue further.
STEP 7: Outside the loop check if(found == 1), if it is true means element is found, else element is
not found in the array
48
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int n, i, element, found = 0;
return 0;
}
49
B23CSI101 C PROGRAMMING
OUTPUT:
50
B23CSI101 C PROGRAMMING
AIM:
ALGORITHM:
STEP 1: Start
C[i][j]=A[i][j] + B[i][j]
Set j=j+1
STEP 7: Stop
51
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int m, n, i, j;
return 0;
}
52
B23CSI101 C PROGRAMMING
OUTPUT:
53
B23CSI101 C PROGRAMMING
AIM:
ALGORITHM:
STEP 1: Start
C[i][j]=A[i][j] - B[i][j]
Set j=j+1
STEP 7: Stop
54
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int m, n, i, j;
return 0;
}
55
B23CSI101 C PROGRAMMING
OUTPUT
56
B23CSI101 C PROGRAMMING
AIM
ALGORITHM:
STEP 1: Start
Set k=k+1
STEP 7: Stop
57
B23CSI101 C PROGRAMMING
PROGRAM
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k;
return 0;
}
59
B23CSI101 C PROGRAMMING
OUTPUT:
60
B23CSI101 C PROGRAMMING
AIM:
To Write a C program to read square matrix of order n, transpose it using user defined function and
display transposed matrix from main() function.
ALGORITHM:
STEP 2: Read the entered order of matrix store that value into the variable n.
STEP 3: Read the entered elements using scanf and store the entered elements into the matrix using
nested for loop.
STEP 4: From the main() function calls the transpose() function by passing an 2D array (i/p matrix),
size of the 2D array value as arguments.
STEP 5: In the function transpose(), loop through the matrix elements and convert its rows into
columns using tmp variable.
STEP 6: Finally, display the transposed matrix elements in the main() function.
61
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int n, i, j;
return 0;
}
62
B23CSI101 C PROGRAMMING
OUTPUT:
63
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
RESULT:
64
B23CSI101 C PROGRAMMING
Ex.No. 5(a)
GENERATE A SIMPLE APPLICATION USING POINTER
Date
AIM:
To implement a C program to search (linear) an element from an array using pointers.
ALGORITHM:
STEP 7: Check if an element is present in an array by traversing. If an element is found display "Yes",
otherwise "No".
65
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int n, target, result;
int arr[n];
return 0;
}
66
B23CSI101 C PROGRAMMING
OUTPUT:
67
B23CSI101 C PROGRAMMING
AIM:
To write a C program to perform binary search using pointers.
ALGORITHM:
STEP 6: Check if an element is present in an array by traversing. If an element is found display the
elementindex, otherwise print element not found.
68
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int binarySearch(int *arr, int size, int target) {
int *left = arr; // Pointer to the first element of the array
int *right = arr + size - 1; // Pointer to the last element of the array
while (left <= right) {
int *mid = left + (right - left) / 2; // Calculate middle element using pointer arithmetic
if (*mid == target) {
return mid - arr; // Return the index of the target element
}
else if (*mid < target) {
left = mid + 1; // Search in the right half
}
else {
right = mid - 1; // Search in the left half
}
}
int main() {
int n, target, result;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
return 0;
}
69
B23CSI101 C PROGRAMMING
OUTPUT:
70
B23CSI101 C PROGRAMMING
AIM:
To write a C program to read two numbers from user and add them using pointers.
ALGORITHM:
STEP 2: Declare three variables say num1, num2, and sum of int (integer) type
STEP 3: Here first two variable stores the two number entered by user at run-time.
STEP 4: And the third variable, sum will be used to store the summation of given two number using
pointer.
STEP 5: Declare two variables say ptr1 and ptr2 of int pointer (integer pointer) type. To declare any
variable as pointer type variable, just place * (star) before the variable without including any space.
STEP 6: Initialize the address of first variable (that holds first number) num1 to ptr1 using & (address
of) operator.
STEP 7: Initialize the address of second variable (that holds second number) num2 to ptr2 using &
(address of) operator.
STEP 8: Perform the operation (value at ptr1 + value at ptr2). using * (value at address) operator. As
both ptr1 and ptr2 holds the address of entered two number. And finally initialize it to the variable sum
that holds the addition result of given two number by user.
71
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int num1, num2, sum;
int *ptr1, *ptr2, *ptrSum;
return 0;
}
72
B23CSI101 C PROGRAMMING
OUTPUT:
TEST CASES:
Enter the first number: 15
Enter the second number: 25
The sum of 15 and 25 is: 40
73
B23CSI101 C PROGRAMMING
INFERENCE:
VIVAQUESTIONS:
RESULT:
74
B23CSI101 C PROGRAMMING
AIM:
To write a C program to find the factorial of a given number using recursion.
ALGORITHM:
STEP 4: From the main() function call factorial(n) function with the argument.
75
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
int num;
// Check if the number is negative, as factorial is not defined for negative numbers
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Call the recursive factorial function and display the result
printf("The factorial of %d is %d.\n", num, factorial(num));
}
return 0;
}
76
B23CSI101 C PROGRAMMING
OUTPUT:
Enter a number: 5
The factorial of 5 is 120.
Enter a number: -3
Factorial is not defined for negative numbers.
77
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
4. Can you return multiple values from a function using return statement?
RESULT:
78
B23CSI101 C PROGRAMMING
AIM:
To Write a C program to find the length of the string using strlen() function.
ALGORITHM:
79
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
#include <string.h> // Required for strlen() function
int main() {
char str[100]; // Declare a character array to store the input string
return 0;
}
80
B23CSI101 C PROGRAMMING
OUTPUT:
81
B23CSI101 C PROGRAMMING
AIM:
To write a C program to perform the string copy using strcpy() function.
ALGORITHM:
STEP 4: Copy the string from source string to destination string using strcpy function.
82
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
#include <string.h> // Required for strcpy() function
int main() {
char source[100], destination[100]; // Declare source and destination character arrays
return 0;
}
83
B23CSI101 C PROGRAMMING
OUTPUT:
84
B23CSI101 C PROGRAMMING
AIM:
To write a C Program to compare two strings using strcmp() Function.
ALGORITHM:
STEP 5: If the strings are equal then print “Strings are equal”.
STEP 6: If the strings are not equal then print “Strings are not equal”.
85
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
#include <string.h> // Required for strcmp() function
int main() {
char str1[100], str2[100]; // Declare two character arrays to store the strings
return 0;
}
86
B23CSI101 C PROGRAMMING
OUTPUT:
87
B23CSI101 C PROGRAMMING
AIM:
To write a C Program to concatenate two strings using strcat() function.
ALGORITHM:
STEP 4: Concatenate two strings (str1, str2) using strcat() function. It takes two strings as a parameter
and then appends the second string at the end of the first string.
88
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
#include <string.h> // Required for strcat() function
int main() {
char str1[100], str2[100]; // Declare two character arrays to store the strings
return 0;
}
89
B23CSI101 C PROGRAMMING
OUTPUT:
90
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
2. What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
3. How do you determine the length of a string value that was stored in a variable?
RESULT:
91
B23CSI101 C PROGRAMMING
AIM:
To write a C program to swap two numbers using call by value.
ALGORITHM:
92
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
// Printing swapped values inside the function (call by value does not affect the original values)
printf("Inside swap function:\n");
printf("a = %d, b = %d\n", a, b);
}
int main() {
int num1, num2;
// Printing values after calling swap function (values in main() will not change)
printf("\nAfter swap (inside main):\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
93
B23CSI101 C PROGRAMMING
OUTPUT:
Case 1:
Before swap:
num1 = 5, num2 = 10
Inside swap function:
a = 10, b = 5
94
B23CSI101 C PROGRAMMING
AIM:
To write a C Program to Sort the list of numbers using pass by reference.
ALGORITHM:
STEP 3: Read the Input for number of elements and each element.
STEP 5: Compare the elements in each pass till all the elements are sorted.
95
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to sort the array using Bubble Sort (pass by reference)
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(&arr[j], &arr[j+1]); // Swapping elements if they are in wrong order
}
}
}
}
// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n;
// Input the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
96
B23CSI101 C PROGRAMMING
OUTPUT:
Case 1:
97
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
RESULT:
98
B23CSI101 C PROGRAMMING
AIM:
To write a C Program to maintain students records using structure.
ALGORITHM:
STEP 2: Declare the Structure Student with data members such as no, name, m1, m2,
m3, m4, m5, total, avg and grade.
STEP 4.1: Enter the Student details such as Roll no, Name, Mark1, Mark2, Mark3, Mark4, Mark5.
STEP 5: Print Student Roll No, Name, Total, Avg and Grade.
99
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
struct Student student1; // Declare a variable of type struct Student
return 0;
}
100
B23CSI101 C PROGRAMMING
OUTPUT:
Case 1:
Enter student details:
Enter the student's name: John Doe
Enter the roll number: 101
Enter the marks: 85.5
101
B23CSI101 C PROGRAMMING
AIM:
To write a C Program to create payroll processing using union.
ALGORITHM:
STEP 2: Declare the Union Employee with data members such as name, eno, basic salary, net salary,
gross salary.
STEP 4: Enter the employee details such as Name, Emp No and Basic salary.
STEP 7: Print Employee salary details such as Name, Id, Basic, HRA, DA,GROSS.
102
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
int id;
char type; // 'S' for salaried, 'H' for hourly
union Payroll pay; // Union to store either salary or hourly wage
};
printf("Enter type of employee ('S' for Salaried, 'H' for Hourly): ");
getchar(); // Consume the newline character left by scanf
scanf("%c", &e->type);
int main() {
struct Employee emp;
return 0;
}
104
B23CSI101 C PROGRAMMING
OUTPUT:
Employee Details:
Name: John Doe
Employee ID: 101
Type: Salaried
Salary: 50000.00
Employee Details:
Name: Alice Smith
Employee ID: 102
Type: Hourly
Hourly Wage: 20.50
105
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
1. What is a structure?
3. Define union?
RESULT:
106
B23CSI101 C PROGRAMMING
AIM:
To write a C program to display the contents of a file.
ALGORITHM:
STEP 4: Repeat until end of the file and print the contents if the file.
107
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
FILE *file;
char ch;
return 0;
}
108
B23CSI101 C PROGRAMMING
OUTPUT:
Explanation:
The program uses fopen() to open the file. If the file cannot be opened (e.g., it doesn't exist), fopen()
returns NULL, and an error message is printed.
If the file is opened successfully, the program reads its contents character by character using fgetc() and
prints them to the screen using putchar()
109
B23CSI101 C PROGRAMMING
AIM:
To write a C program to copy the contents of one file to another.
ALGORITHM:
STEP 5: If source file is NULL pointer, then print source file cannot be open.
STEP 7: If destination file is NULL pointer, then print destination file cannot be open.
STEP 8: Read a character from source file and write to destination file until EOF.
110
B23CSI101 C PROGRAMMING
PROGRAM:
#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char sourceFileName[100], destinationFileName[100];
char ch;
return 0;
}
111
B23CSI101 C PROGRAMMING
OUTPUT:
Program Input:
Enter the name of the source file: source.txt
Enter the name of the destination file: destination.txt
Program Output:
File copied successfully.
Explanation of Output:
The message "File copied successfully" confirms that the operation completed without errors.
The content of source.txt was read and written to destination.txt, creating an exact copy.
112
B23CSI101 C PROGRAMMING
INFERENCE:
VIVA QUESTIONS:
RESULT:
113