0% found this document useful (0 votes)
2 views46 pages

Practical_File_C_programming

The document is a practical file for a C Programming course (24CSE0107) for the Bachelor of Engineering (CSE) batch of 2024 at Chitkara University. It includes an index of 31 experiments covering fundamental programming concepts such as input/output operations, arithmetic calculations, control structures, functions, arrays, and structures. Each experiment outlines the aim, concepts used, program code, and output screenshots.

Uploaded by

jenniekim11198
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views46 pages

Practical_File_C_programming

The document is a practical file for a C Programming course (24CSE0107) for the Bachelor of Engineering (CSE) batch of 2024 at Chitkara University. It includes an index of 31 experiments covering fundamental programming concepts such as input/output operations, arithmetic calculations, control structures, functions, arrays, and structures. Each experiment outlines the aim, concepts used, program code, and output screenshots.

Uploaded by

jenniekim11198
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 46

Fundamental ofC Programming

(24CSE0107)

Practical File
of
Fundamentals of C Programming
(24CSE0107)

Batch-2024

Bachelor of Engineering (CSE)

Submitted To: Submitted By:


Dr.Chander Prabha Sakshi Singla
Assistant Professor 2410991653
Chitkara University, Punjab G3

Department of
Computer Science and Engineering,
Chitkara University School of Engineering and Technology,
Chitkara University, Punjab, India
Fundamental ofC Programming
(24CSE0107)

Index

Sr. Experiments Page No. Date Signatur


No. e

1. Install C compiler (GCC/Code::Blocks), set up


IDE, compile and run the first "Hello, World!" 1
program.
2. Write a Program to show the use to input (Scanf)/
output (Printf) statements and block structure of C- 2
program by highlighting the features of "stdio.h".
3. Write a program to add two numbers and display
3
the sum.
4. Write a program to calculate the area and the
circumference of a circle by using radius as the 4
input provided by the user.
5. Write a Program to perform addition, subtraction,
division and multiplication of two numbers given 5
as input by the user.
6. Write a program to evaluate each of the following
equations.
6
(i) V = u + at. (ii) S = ut+1/2at2 (iii) T=2*a+√b+9c
(iv) H=√b2+p2
7. Write a program to swap two variables:
a) By using temporary variable. 7
b) Without using temporary variable
8. Write a Program to find the greatest among three
numbers using:
8
1. Conditional Operator
2. If-Else statement
9. Write the following programs using switch case
statement:
1. To check that an input alphabet is vowel or
9
consonant
2. To check whether a number is positive,
negative or zero
10. Write a program using while loop to print the sum
10
of first n natural numbers.
11. Write a program to check a number is Armstrong
11
or not using For loop.

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

12. Write the program to count the digits in a number


12
and then print the reverse of the number also.
13. Write a program to generate the Fibonacci series. 13
14. Write a program to print the following patterns:
a) *
**
*** 14
****
*****
******
15. Write the program to print the following pattern:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18 16
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
16. Write a program to check that the given number is
prime, Armstrong or perfect using the concept of 17
functions.
17. Write a program to calculate the area and
19
circumference of a circle using functions.
18. Write a program to swap two variables using the
20
concept of call by value and call by reference.
19. Write a program to perform the following
operations on 1D-Array:
1. Insert
2. Update 21
3. Delete
4. Display
5. Search
20. Write a program to calculate the sum of array
25
elements by passing it to a function.
21. Write a program to show the use of passing pointer
26
as arguments to the functions.
22. Write a program matrix multiplication using the
27
concept of 2D array
23. Write a program to transpose a given matrix. 29

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

24. Write a program to find the factorial of a number


30
by using the concept of recursion.
25. Write a menu driven C program to show the use of
in-built string functions like strlen, strcat, strcpy, 31
strcmp, strrev etc.
26. Write a Program in C to display the total number
of appearances of a substring provided as input by 34
the user in a given string.
27. Write a program to display the sum of the digits of
35
a number by using the concept of recursion.
28. Write a C program to add two distances in inch &
36
feet using the concept of structures.
29. Write a C program to add two complex numbers
37
using the concept of structures in C.
30. Write a program in C to store the information of
five employees using both concepts i.e. array of 38
structure and array within structure.
31. Write a Program in C to find and replace a specific
string in a file and also display the total number of 39
appearances of that string.

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment No. 1

Aim: Install C compiler (GCC/Code::Blocks), set up IDE, compile and run the first "Hello,
World!" program.

Concept Used:
• Compiler: Converts the high-level programming language code into machine code
that the computer's processor can execute.

• IDE: Integrated Development Environment that provides comprehensive facilities to


programmers for software development, including editing, compiling, and debugging.

Program:
#include <stdio.h>

int main () {
printf ("Hello, World! \n");
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment No. 2
Aim: Write a Program to show the use to input (Scanf)/output (Printf) statements and block
structure of C-program by highlighting the features of "stdio.h".

Concept Used:

• Header File (#include <stdio.h>): This header file includes the definitions of
functions like scanf and printf which are used for input and output operations.

• Standard Input/Output Functions: scanf reads formatted input from the standard
input (keyboard), and printf sends formatted output to the standard output (screen).

Program:
#include <stdio.h>

int main () {
char name [50];
int age;
printf ("Enter your name: ");
scanf ("%49s", name);
printf ("Enter your age: ");
scanf ("%d", &age);
printf ("Hello, %s! You are %d years old.\n", name, age);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 3

Aim: Write a program to add two numbers and display the sum.

Concept Used:
• Header File (#include <stdio.h>): This header file includes the definitions of
functions like scanf and printf, which are used for input and output operations.
• Variables: Variables are used to store data that can be manipulated by the program.
• Input/Output Functions: scanf is used to read input from the user, and printf is used
to display output to the user.
• Arithmetic Operations: Arithmetic operators are used to perform mathematical
operations such as addition.

Program:
#include <stdio.h>

int main () {
int a, b;
printf ("Enter the first number: ");
scanf ("%d", &a);
printf ("Enter the second number: ");
scanf ("%d", &b);
printf ("The sum of %d and %d is %d.\n", num1, num2, a,b,a+b);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 4

Aim: Write a program to calculate the area and the circumference of a circle by using radius
as the input provided by the user.

Concept Used:
• Variables: Variables are used to store data that can be manipulated by the program.
• Input/Output Functions: scanf is used to read input from the user, and printf is used
to display output to the user.
• Mathematical Operations: Uses the formula for calculating the area and
circumference of a circle.
• Constants: Defines the mathematical constant π (Pi) for calculations.

Program:
#include <stdio.h>

int main() {
float r, a, c;
printf ("Enter the radius of the circle: ");
scanf ("%f", &r);
a = 3.14 * r * r;
c = 2 * 3.14 * r;
printf ("The area of the circle is: %.2f\n", a);
printf ("The circumference of the circle is: %.2f\n", c);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 5

Aim: Write a Program to perform addition, subtraction, division and multiplication of two
numbers given as input by the user.

Concept Used:
• Variables: Variables are used to store data that can be manipulated by the program.
• Input/Output Functions: scanf is used to read input from the user, and printf is used
to display output to the user.
• Arithmetic Operations: Arithmetic operators are used to perform mathematical
operations such as addition, subtraction, multiplication, and division.
• Control Flow: The sequence in which individual statements, instructions, or function
calls are executed or evaluated.

Program:
#include <stdio.h>

int main() {
float a, b;
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
printf("The sum of %.2f and %.2f is %.2f.\n", a, b, a + b);
printf("The difference between %.2f and %.2f is %.2f.\n",a,b,a-b);
printf("The product of %.2f and %.2f is %.2f.\n", a, b, a*b);
printf("The quotient of %.2f divided by %.2f is %.2f.\n", a, b, a/b);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 6

Aim: Write a program to evaluate each of the following equations.


(i) V = u + at. (ii) S = ut+1/2at2 (iii) T=2*a+√b+9c (iv) H=√b2+p2

Concept Used:
• Variables: Variables are used to store data that can be manipulated by the program.
• Input/Output Functions: scanf is used to read input from the user, and printf is used
to display output to the user.
• Mathematical Operations: Uses arithmetic and mathematical operations to evaluate
the given equations.
• Constants: Defines the mathematical constant π (Pi) for calculations.

Program:
#include <stdio.h>
#include <math.h>
int main() {
float u, a, t, b, c, p;
printf ("Enter the value of u, a , t , b, c and p : ");
scanf ("%f %f %f %f %f %f", &u, &a, &t, &b, &c, &p);
printf ("The value of V (u + at) is: %.2f\n", u+(a*t));
printf ("The value of S (ut + 1/2at^2) is: %.2f\n", (u * t) + (0.5 * a * t * t));
printf ("The value of T (2*a + √b + 9c) is: %.2f\n", (2 * a) + sqrt(b) + (9 * c));
printf ("The value of H (√ (b^2 + p^2)) is: %.2f\n", sqrt ((b * b) + (p * p));
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 7

Aim: Write a program to swap two variables:


a) By using temporary variable.
b) Without using temporary variable

Concept Used:
• Temporary Variable: A temporary variable is used to hold the value of one of the
variables so that we don't lose it during the swap.
• Arithmetic Operations: The second method uses arithmetic operations to swap the
values without needing an additional storage variable.

Program:
(a) #include <stdio.h>
int main () {
int A,B,TB;
printf ("Enter A and B=");
scanf ("%d %d”, &A, &B);
TB=A;
A=B;
B=TB;
printf ("\n Now A=%d B=%d", A, B);
return 0;
}
Output Screenshot:

(b) #include <stdio.h>


int main(){
int x , y;
printf("Enter x and y = ");
scanf("%d %d" ,&x, &y);
x=x+y
y=x-y
x=x-y
printf ("\n Now A=%d B=%d" , x ,y);
Return}Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 8

Aim: Write a Program to find the greatest among three numbers using:
1. Conditional Operator
2. If-Else statement

Concept Used:
The concept revolves around comparing three numbers using conditional (ternary)
operator and if-else statements.

Program:
(a) #include <stdio.h>
int main(){
float a,b,c,max;
printf("Enter A B and C=");
scanf("%f %f %f", &a, &b, &c);
max=(a>b? a>c ? a: c: b >c? b: c);
printf("Max of 3 Numbers = %f ",max);
}

Output Screenshot:

(b) #include <stdio.h>


int main() {
int a, b, c, greatest;
printf("Enter A B and C = ");
scanf("%d %d %d", &a, &b, &c);
if(a > b && a > c)
greatest = a;
else if(b > c)
greatest = b;
else
greatest = c;
printf("The greatest number is %d\n", greatest);

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Output Screenshot:

Experiment no. 9
Aim: Write the following programs using switch case statement:
1. To check that an input alphabet is vowel or consonant
2. To check whether a number is positive, negative or zero

Concept Used:
• To check if an input alphabet is a vowel or consonant using a switch case, we rely on
enumerating the possible vowel characters ('a', 'e', 'i', 'o', 'u') and making the default
case handle consonants.
• To check whether a number is positive, negative, or zero using a switch case, we can
create conditions that switch based on the value of the number after comparing it to
zero.
Program:
1. #include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf("%c", &ch);
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case
'O': case 'U':
printf("%c is a vowel\n", ch);
break;
default:
printf("%c is a consonant\n", ch);
}
}
Output Screenshot:

2. #include <stdio.h>
int main () {
float N;
printf("\n\tEnter the Number = ");
scanf("%f",&N);

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

if(N>0) printf ("Number is +ve");


else if(N==0) printf ("Number is Zero");
else printf ("Number is -ve");
}
Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 10

Aim: Write a program using while loop to print the sum of first n natural numbers.
Concept Used:
The concept revolves around initializing a counter variable and a sum variable, then
iteratively adding the counter to the sum variable until the counter exceeds the value of
n.
Program:
#include <stdio.h>
int main ()
{
int N, Sum=0, C;
printf("Enter N=");
scanf("%d",&N);
for (C=1; C<=N; C++)
{
printf("%d\n",C);
Sum=Sum+C;
}
printf("Sum=%d",Sum);
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 11

Aim: Write a program to check a number is Armstrong or not using For loop.

Concept Used:
An Armstrong number (or Narcissistic number) is a number that is equal to the sum of
its own digits each raised to the power of the number of digits. For example, 153 is an
Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Program:
#include <stdio.h>
#include <math.h>
int main(){
int r, N, count=0,N1,pw,sum=0;
printf("Enter N = ");
scanf("%d",&N);
N1=N;
while(N1!=0)
{
count++;
N1=N1/10;
}
N1=N;
while(N1!=0)
{
r=N1%10;
pw=pow(r,count);
sum=sum+pw;
N1=N1/10;
}
if(sum==N)
printf("Yes Armstrong");
else
printf("Not an Armstrong");
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 12

Aim: Write the program to count the digits in a number and then print the reverse of the
number also.
Concept Used:
• Counting the Digits: We repeatedly divide the number by 10 until it becomes zero.
Each division gives us a new digit, which we count.
• Reversing the Number: We repeatedly take the last digit of the number (using
modulus operation), add it to a new number that represents the reverse, and update
this reversed number by multiplying it by 10 and adding the last digit.

Program:
#include <stdio.h>
int main() {
int n,n1,count = 0, reverse = 0;
printf("Enter an integer: ");
scanf("%d", &num);
n1 = n;
while (n1 != 0) {
n1 /= 10;
++count;
}
n1 = n;
while (n1 != 0) {
r = n1 % 10;
reverse = reverse * 10 + r;
n1 /= 10;
}
printf("Number of digits: %d\n", count);
printf("Reversed number: %d\n", reverse);
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 13

Aim: Write a program to generate the Fibonacci series.

Concept Used:
The Fibonacci series is a sequence of numbers where each number is the sum of the
two preceding ones, usually starting with 0 and 1. In this sequence, F(n) = F(n-1) +
F(n-2), where F (0) = 0, and F (1) = 1.

Program:
#include <stdio.h>
int main()
{
int A=0, B=1, C=0, N;
printf("Enter N =");
scanf("%d",&N);
while(C<=N)
{
A=B;
B=C;
printf("%d ",C);
C=A+B;
}

}
Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 14
Aim: Write a program to print the following patterns:
a) *
**
***
****
*****
******

Concept Used:
• For Pattern a: This pattern uses a simple nested loop. The outer loop runs from 1 to
the number of rows, and the inner loop prints '*' followed by a space for each column.
• For Pattern b: This pattern also uses nested loops. The outer loop runs from 1 to the
number of rows, and the inner loop prints leading spaces to align the stars. The
amount of leading spaces decreases as the number of stars in the row increases.

Program:
(a.) #include <stdio.h>
int main()
{ int r,c;
for(r=1;r<=6;r++)
{
for(c=1;c<=r;c++)
{
printf("* ");
}
printf("\n");
}
}
Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

(b.) #include <stdio.h>


int main()
{ int r,c,s;
for(r=1;r<=6;r++)
{
for(s=1;s<=6-r;s++)
{
printf(" ");
}
for(c=1;c<=r;c++)
{
printf("* ");
}
printf("\n");
}
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 15
Aim: Write the program to print the following pattern:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36

Concept Used:
• Nested loops: The outer loop iterates over the rows, and the inner loop iterates over
the columns.
• Multiplication: Each element in the table is the product of the row and column
indices.
• Formatting: The print(f"{i*j:>4}", end=" ") ensures that each number is right-aligned
and separated by spaces for a neat appearance.

Program:
#include <stdio.h>
int main() {
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
printf("%4d ", i * j);
}
printf("\n");
}
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 16
Aim: Write a program to check that the given number is prime, Armstrong or perfect using
the concept of functions.

Concept Used:
• Functions: Breaking down the problem into smaller, manageable tasks using
functions isPrime(), isArmstrong(), and isPerfect().
• Prime number: A number greater than 1 that has no divisors other than 1 and itself.
• Armstrong number: A number that is equal to the sum of its own digits each raised
to the power of the number of digits.
• Perfect number: A positive integer that is equal to the sum of its proper divisors
(excluding itself).

Program:
#include <stdio.h>
#include <math.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return 0;
}
return 1;
}
int isArmstrong(int n) {
int n1, r, count = 0,rev;
n1 = n;
while (n1 != 0) {
n1 /= 10;
++count;
}
n1 = n;
while (n1 != 0) {
r = n1 % 10;
rev += pow(r, count);
n1 /= 10;
}

return rev == n;
}
int isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) sum += i;
}
return sum == num;
}

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

int main () {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

if (isPerfect(num))
printf("%d is a perfect number.\n", num);
else
printf("%d is not a perfect number.\n", num);

return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 17
Aim: Write a program to calculate the area and circumference of a circle using functions.

Concept Used:
• Functions: Breaking down the problem into smaller tasks using functions
calculateArea() and calculateCircumference().
• Mathematical constants: Using the constant PI to ensure accuracy in calculations.
• Math calculations:
• Area: Area=π×radius2\text{Area} = \pi \times \text{radius}^2
• Circumference: Circumference=2×π×radius

Program:
#include <stdio.h>
#define PI 3.14159
double calculateArea(double radius) {
return PI * radius * radius;
}
double calculateCircumference(double radius) {
return 2 * PI * radius;
}

int main() {
double radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = calculateArea(radius);
circumference = calculateCircumference(radius);
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 18
Aim: Write a program to swap two variables using the concept of call by value and call by
reference.

Concept Used:
• Call by Value: In this approach, the actual value of the argument is passed to the
function. Modifying the parameters within the function does not affect the actual
variables. This is demonstrated by the swapByValue function, where the values of x
and y remain unchanged after the function call.
• Call by Reference: In this approach, the address of the argument is passed to the
function. Modifying the parameters within the function affects the actual variables.
This is demonstrated by the swapByReference function, where the values of x and y
are swapped after the function call using pointers.

Program:
#include <stdio.h>
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("Enter the values of x and y: ");
scanf("%d %d", &x, &y);
swapByValue(x, y);
printf("After swapByValue: x = %d, y = %d\n", x, y);
swapByReference(&x, &y);
printf("After swapByReference: x = %d, y = %d\n", x, y);
return 0;
}
Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 19
Aim: Write a program to perform the following operations on 1D-Array:
1. Insert
2. Update
3. Delete
4. Display
5. Search

Concept Used:
• Array Manipulation: Operations like insert, update, delete, and search require
manipulating the array elements.
• Loops: Used for traversing the array elements.
• Switch-case: Used for handling user choices in the menu.
• Conditional Statements: Used for validating user inputs and checking array
boundaries.

Program:
#include <stdio.h>
void insert(int arr[], int *n, int capacity, int value, int pos) {
if (*n == capacity) {
printf("\nArray is full! Cannot insert.\n");
return;
}
if (pos < 0 || pos > *n) {
printf("\nInvalid position!\n");
return;
}
for (int i = *n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
(*n)++;
printf("\nValue %d inserted at position %d\n", value, pos);
}
void update(int arr[], int n, int pos, int newValue) {

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

if (pos < 0 || pos >= n) {


printf("\nInvalid position!\n");
return;
}
arr[pos] = newValue;
printf("\nUpdated value at position %d to %d\n", pos, newValue);
}
void deleteElement (int arr[], int *n, int pos) {
if (pos < 0 || pos >= *n) {
printf("\nInvalid position!\n");
return;
}
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
printf("\nDeleted element at position %d\n", pos);
}
int search(int arr[], int n, int value) {
for (int i = 0; i < n; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
void display(int arr[], int n) {
printf("\nArray elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[100], n, choice, value, pos, capacity = 100;

printf("Enter number of elements in the array: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

for (int i = 0; i < n; i++) {


scanf("%d", &arr[i]);
}
while (1) {
printf("\nArray Operations Menu:\n");
printf("1. Insert\n");
printf("2. Update\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Search\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value and position: ");
scanf("%d %d", &value, &pos);
insert(arr, &n, capacity, value, pos);
break;
case 2:
printf("Enter position and new value: ");
scanf("%d %d", &pos, &value);
update(arr, n, pos, value);
break;
case 3:
printf("Enter position to delete: ");
scanf("%d", &pos);
deleteElement(arr, &n, pos);
break;
case 4:
display(arr, n);
break;
case 5:
printf("Enter value to search: ");
scanf("%d", &value);
pos = search(arr, n, value);
if (pos != -1)
printf("\nValue found at position %d\n", pos);
else
printf("\nValue not found!\n");

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

break;
case 6:
printf("\nExiting...\n");
return 0;
default:
printf("\nInvalid choice! Try again.\n");
}
}
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 20
Aim: Write a program to calculate the sum of array elements by passing it to a function.

Concept Used:
• Function Passing Array - Arrays in C are passed to functions by reference,
meaning modifications inside the function affect the original array.
• Looping Through Array - A loop is used to iterate over all elements and calculate
the sum.
• Returning Sum - The function returns the computed sum to the main function.

Program:
#include <stdio.h>
int calculateSum(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Sum of array elements: %d\n", calculateSum(arr, n));
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 21
Aim: Write a program to show the use of passing pointer as arguments to the functions.

Concept Used:
• Call by Reference: Instead of passing a copy of the variable, a pointer to the variable
is passed, allowing direct modification of its value.
• Dereferencing (*ptr): The pointer is used to access and modify the actual value
stored at the referenced memory location.
• Memory Efficiency: Passing pointers avoids copying large data structures, improving
performance.

Program:
#include <stdio.h>
void updateValue(int *ptr) {
*ptr = *ptr + 10;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
updateValue(&num);
printf("Updated value: %d\n", num);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 22
Aim: Write a program matrix multiplication using the concept of 2D array.

Concept Used:
• 2D Arrays: Matrices are represented using two-dimensional arrays.
• Nested Loops: Iteration over rows and columns to perform multiplication.
• Matrix Multiplication Rule: The number of columns in the first matrix must match
the number of rows in the second matrix.
• Row-wise and Column-wise Traversal: Elements of the first matrix are multiplied
with corresponding elements of the second matrix and summed to get the resulting
matrix.
• User Input & Output Handling: Taking input for matrices and displaying the
resultant matrix.

Program:
#include <stdio.h>
int main () {
int r, c, RA, RB, CA, CB, sum, uw ;
printf("Enter Order of A[RA][CA] =");
scanf("%d%d",&RA,&CA);
printf("Enter Order of B[RB][CB] =");
scanf("%d%d",&RB,&CB);
if(CA!=RB)
printf("Invalid Order");
int A[RA][CA],B[RB][CB],AB[RA][CB];
for(r=0;r<RA;r++)
{
for(c=0;c<CA;c++)
{
printf("Enter the Value at A[%d][%d] =",r,c);
scanf("%d",&A[r][c]);
}
}
for(r=0;r<RB;r++)
{
for(c=0;c<CB;c++)
{
printf("Enter the Value at B[%d][%d] =",r,c);

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

scanf("%d",&B[r][c]);
}
}
for(r=0;r<RA;r++)
{
for(c=0;c<CB;c++)
{ sum=0;
for(uw=0;uw<CA;uw++)
{
sum=sum+A[r][uw]*B[uw][c];
}
AB[r][c]=sum;
}
}
printf("\nMultiplied Matrix =\n");
for(r=0;r<RA;r++)
{
for(c=0;c<CB;c++)
{
printf("%d ",AB[r][c]);
}
printf("\n");
}
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 23
Aim: Write a program to transpose a given matrix.

Concept Used:
• 2D Arrays: Used to store the original and transposed matrices.
• Matrix Transposition Rule: Rows become columns and vice versa.
• Nested Loops: Used for reading, transposing, and printing the matrix.

Program:
#include <stdio.h>
int main() {
int r, c;
scanf("%d %d", &r, &c);
int mat[r][c], transposed[c][r];
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++)
scanf("%d", &mat[i][j]);
}
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++)
transposed[j][i] = mat[i][j];
}
printf("Transposed Matrix:\n");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
printf("%d ", transposed[i][j]);
printf("\n");
}
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 24
Aim: Write a program to find the factorial of a number by using the concept of recursion.

Concept Used:
• Recursion: The function calls itself to compute the factorial.
• Base Case & Recursive Case:
• Base Case: Factorial of 0 or 1 is 1.
• Recursive Case: n!=n×(n−1)!n! = n \times (n-1)!n!=n×(n−1)!
• Function Calling: The function repeatedly calls itself until it reaches the base case.

Program:
#include <stdio.h>
int main()
{
int FACTO(int N);
int N,fact;
printf("Enter N =");
scanf("%d",&N);
fact=FACTO(N);
printf("\nFACTORIAL OF %d = %d",N,fact);
}
int FACTO(int N)
{
if(N==1)
return(1);
int resp=FACTO(N-1);
int fact=resp*N;
return(fact);
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 25
Aim: Write a menu driven C program to show the use of in-built string functions like strlen,
strcat, strcpy, strcmp, strrev etc.

Concept Used:
• Strings in C: Handling character arrays (char[]) with '\0' termination.
• String Functions (from <string.h>):
o strlen() – Finds the length of a string.
o strcat() – Concatenates two strings.
o strcpy() – Copies one string to another.
o strcmp() – Compares two strings.
o strrev() – Reverses a string (not standard in some compilers, so a custom
function is used).
• Switch Case in C: To create a menu-driven program.
• Loop Control: Using do-while to keep the menu running until the user exits.

Program:
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str1[100], str2[100];
int choice;
do {
printf("\nMenu:");
printf("\n1. Find String Length");
printf("\n2. Concatenate Strings");
printf("\n3. Copy String");
printf("\n4. Compare Strings");
printf("\n5. Reverse String");
printf("\n6. Exit");
printf("\nEnter your choice: ");

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
printf("Enter a string: ");
gets(str1);
printf("Length of string: %ld\n", strlen(str1));
break;
case 2:
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
break;
case 3:
printf("Enter source string: ");
gets(str1);
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
break;
case 4:
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
break;
case 5:
printf("Enter a string: ");
gets(str1);
reverseString(str1);
printf("Reversed string: %s\n", str1);
break;

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 6);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 26
Aim: Write a Program in C to display the total number of appearances of a substring
provided as input by the user in a given string.

Concept Used:
• Strings in C: Handling character arrays (char []) with '\0' termination.
• String Traversal: Iterating through a string to find occurrences of a substring.
• strstr() Function: Used to find the first occurrence of a substring within a string.
• Looping and Pointers: Using loops and pointer manipulation to count multiple
occurrences.
• User Input Handling: Accepting a main string and substring from the user.

Program:
#include <stdio.h>
#include <string.h>
int countSubstringOccurrences(char str[], char sub[]) {
int count = 0;
char *pos = str;
while ((pos = strstr(pos, sub)) != NULL) {
count++;
pos++; // Move pointer ahead to check for next occurrence
}
return count;
}
int main() {
char str[1000], sub[100];
printf("Enter the main string: ");
gets(str);
printf("Enter the substring to search: ");
gets(sub);
int occurrences = countSubstringOccurrences(str, sub);
printf("Total occurrences of \"%s\": %d\n", sub, occurrences);
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 27
Aim: Write a program to display the sum of the digits of a number by using the concept of
recursion.

Concept Used:
• Recursion: A function calls itself to break the problem into smaller subproblems.
• Base Case & Recursive Case: The base case stops recursion when the number
becomes 0.
• Modulus (%) Operator: Extracts the last digit of the number.
• Integer Division (/) Operator: Removes the last digit to reduce the number.

Program:
#include <stdio.h>
int sumOfDigits(int num) {
if (num == 0)
return 0;
return (num % 10) + sumOfDigits(num / 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits: %d\n", sumOfDigits(num));
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 28
Aim: Write a C program to add two distances in inch & feet using the concept of structures.

Concept Used:
• Structures (struct) – To define a user-defined data type for storing distance in feet
and inches.
• Functions with Structures – To perform operations on structured data.
• Basic Arithmetic Operations – Adding inches and feet separately while handling
carry-over.
• Conditional Statements – To adjust the total inches if it exceeds 12 (since 12 inches
= 1 foot).

Program:
#include <stdio.h>
struct Distance {
int feet;
int inches;
};
int main() {
struct Distance d1, d2, sum;
printf("Enter first distance (feet inches): ");
scanf("%d %d", &d1.feet, &d1.inches);
printf("Enter second distance (feet inches): ");
scanf("%d %d", &d2.feet, &d2.inches);
sum.feet = d1.feet + d2.feet;
sum.inches = d1.inches + d2.inches;
if (sum.inches >= 12) {
sum.feet += sum.inches / 12;
sum.inches %= 12;
}
printf("Total Distance: %d feet %d inches\n", sum.feet, sum.inches);
return 0;
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 29
Aim: Write a C program to add two complex numbers using the concept of structures in C.

Concept Used:
• Structures (struct) – To define a data type for storing complex numbers.
• Basic Arithmetic Operations – Adding the real and imaginary parts separately.

Program:
#include <stdio.h>
struct Complex
{
int R;
int I;
};
int main()
{
struct Complex C1,C2,C3;
printf("Enter C1 =");
scanf("%d%d",&C1.R,&C1.I);
printf("Enter C2 =");
scanf("%d%d",&C2.R,&C2.I);
C3.R=C1.R+C2.R;
C3.I=C1.I+C2.I;
printf("Sum of 2 Complex Numbers =%d+%di",C3.R,C3.I);
}

Output Screenshot:

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Experiment no. 30
Aim: Write a program in C to store the information of five employees using both concepts
i.e. array of structure and array within structure.

Concept Used:
• Array of Structures – To store multiple employee records using an array of struct.
• Array Within Structure – To store additional information (e.g., department name as
a character array).

Program:
#include <stdio.h>
struct Employee {
int id;
char name[50];
char department[50];
float salary;
};
int main() {
struct Employee employees[5];
for (int i = 0; i < 5; i++) {
printf("\tEnter details of employee %d (ID, Name, Department, Salary):", i + 1);
scanf("%d", &employees[i].id);
scanf("%s", &employees[i].name);
scanf("%s", &employees[i].department);
scanf("%f", &employees[i].salary);
}
printf("\n\tEmployee Details:\n");
for (int i = 0; i < 5; i++) {
printf ("\tID: %d, Name: %s, Department: %s, Salary: %.2f\n",employees[i].id,
employees[i].name, employees[i]. department, employees[i].salary);
}
}

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

Output Screenshot:

Experiment no. 31
Aim: Write a Program in C to find and replace a specific string in a file and also display the
total number of appearances of that string.

Concept Used:
• File Handling in C (fopen, fgets, fprintf, fseek)
• String Manipulation (strstr, strcpy, strlen, strncpy)
• Searching and Replacing a Substring
• Counting Occurrences of a Word in a File
• Using a Temporary File for Modifications

Program:
#include <stdio.h>
#include <string.h>
int main() {
char filename[50], search[50], replace[50];
printf("Enter file name: ");
scanf("%s", filename);
printf("Enter the string to search: ");
scanf("%s", search);
printf("Enter the replacement string: ");
scanf("%s", replace);
FILE *file = fopen(filename, "r");
if (!file) {
printf("Error opening file.\n");
return 1;
}
FILE *tempFile = fopen("temp.txt", "w");
if (!tempFile) {
printf("Error creating temporary file.\n");

2410991653 Sakshi G3
Fundamental ofC Programming
(24CSE0107)

fclose(file);
return 1;
}
char line[1000];
int count = 0;
int searchLen = strlen(search);
int replaceLen = strlen(replace);
while (fgets(line, sizeof(line), file)) {
char *pos = line;
while ((pos = strstr(pos, search)) != NULL) {
count++;
char buffer[1000];
int prefixLen = pos - line;
strncpy(buffer, line, prefixLen);
buffer[prefixLen] = '\0';
strcat(buffer, replace);
strcat(buffer, pos + searchLen);
strcpy(line, buffer);
pos += replaceLen;
}
fputs(line, tempFile);
}
fclose(file);
fclose(tempFile);
remove(filename);
rename("temp.txt", filename);
printf("Total occurrences of '%s': %d\n", search, count);
printf("Replacement completed successfully!\n");
return 0;
}

Output Screenshot:

2410991653 Sakshi G3

You might also like