[go: up one dir, main page]

0% found this document useful (0 votes)
128 views143 pages

CLab Manual Final B

Uploaded by

harirathan2008
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)
128 views143 pages

CLab Manual Final B

Uploaded by

harirathan2008
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/ 143

CS25C02-PROGRAMMING IN C LABORATORY

Velammal College of Engineering and Technology, Madurai

(Autonomous)

Department of Electronics and Communication Engineering

Academic Year: 2025-2026

VCET Regulation 2025

LAB MANUAL

CS25C02-PROGRAMMING IN C LABORATORY

Prepared by Verified by

Mrs.M.Bhavadharani AP/AIDS Dr. S.Sasikalai, HoD/AIDS


CS25C02-PROGRAMMING IN C LABORATORY

INDEX

Exp. Page Staff


Name of the Experiment
No. No. Marks Sign
1. Basics of C Programming

A) Identification and solving of simple real life or scientific or technical


problems, and developing flow charts and Pseudo code for the same
(Electricity Billing, Retail shop billing)

B) Write a program to print sample strings like “hello world”, “Welcome


to C Programming” with different formats using escape sequences.

C) Write a Program to print different data types in ‘C’ and their ranges.

D) Write a Program to initialize, assignment & printing variables of


different data types

2. Operators
A) Write a Program to demonstrate arithmetic, logical and relational
operators.

B) Write a Program to demonstrate pre increment and post increment


(++a, a++), pre decrement and post decrement (--a, a--)

C) Write a Program to read radius value from the keyboard and calculate
the area of circle and print the result in both floating and exponential
notation.

D) Write a Program to calculate simple interest.

E) Write a Program to convert temperature from Fahrenheit –Centigrade


and vice-versa.

3. Control Statements
A) Write a Program to read marks of a student in six subjects and print
whether pass or Fail. (using if-else).
B)Write a Program to calculate roots of quadratic equation
.
C) Write a Program to calculate electricity bill. Read starting and ending
meter reading.

The charges are as follows.


Usage Slab( in Units) Rate per unit(Rs)
Upto 400 4.80
401-500 6.45
CS25C02-PROGRAMMING IN C LABORATORY

500-600 8.55
601-800 9.65
800-1000 10.70
>1000 11.80

D) Write a Program to perform arithmetic operations using case control


statement.

E) Write a Program to display names of days in a Week using case control


statement.
4. Looping Operations
A) Write a program to calculate sum of individual digits of a given
number.
B) Write a program to check whether given number is palindrome or not.
C) Write a program to print prime numbers in the given range.
D) Write a program to print the Fibonacci series for given ‘N’ value.
E) Write a program to print the following formats

5. 1-D and 2-D arrays


A) Write a program to store 10 elements in a 1-D array and print sum of
the array, maximum and minimum element in an array.

B) Write a program to count no. of positive numbers, negative numbers


and zeros in an array.

C) Write a program to count all subsets of given array with sum equal to
given sum.

D) Write a program to search an element using linear search algorithm.

E)Write a program to sort the given elements using bubble sort algorithm.
CS25C02-PROGRAMMING IN C LABORATORY

F)Write a program to perform matrix addition, subtraction and


multiplication

6. Strings
A)Write a program to perform various string manipulations using built-in
functions.

B) Write a program to verify the given string is palindrome or not.

C) Write a program to Check if two strings are anagrams of each other.

D) Write a program to concatenate two strings using arrays.

E) Write a program to print the given strings in ascending order.

7. Non recursive and recursive functions

A) Write an application to simulate basic calculator ( +, -, *, /) using


functions.

B) Write a program to find nth Fibonacci number using recursive and non-
recursive number.

C) Write a program to find factorial of a number using recursive and non-


recursive number.

D) Write a program to swap two numbers using Call by Value and Call by
Reference

8. Pointers

A)Write a program to illustrate Pointers to array, strings, Pointers to


Pointers, Array of Pointers

B) Write a program to sort an array using pointers.

C) Write a program to perform matrix multiplication using pointers.

9. Structures and Union


a. Write a program to create structure for an account holder in a bank with
following Fields: name, account number, address, balance and display the
details of ‘n’ account holders.

b. Write a program to find total marks of individual student and average


marks for ‘n’ students using structures.

c. Write a program to illustrate the functions of union.

10. Mini Project


CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:1 Basics of C Programming


Date:
1A) Identification and solving of simple real life or scientific or technical problems, and
developing flow charts and Pseudo code for the same (Electricity Billing, Retail shop
billing)
AIM:

To develop a flowchart for electricity billing.

ALGORITHM:

1. Step1: Start.
2. Step2: Read the previous unit and current unit.
3. Step 3: Calculate the used units by subtracting the current unit and
4. previous unit.
5. Step4: Calculate the Electricity bill from the used units.
6. Step 5: Print the amount of electricity bill.
7. Step6: Stop.

PSEUDOCODE:

• READ Previous unit, Current Unit


• CALCULATE Used Unit = Current Unit – Previous Unit
• Electricity Bill=Used Unit x 2
• PRINT Electricity Bill
CS25C02-PROGRAMMING IN C LABORATORY

FLOWCHART

Start

Read Previous Units


and Current Units

Used Units=Current Units –Previous units

Electricity bill=Used units x 2

Print Electricity bill

Stop
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

Result:
Thus the flow chart and Pseudocode for electricity billing is developed.
CS25C02-PROGRAMMING IN C LABORATORY

AIM:

To develop a flowchart for the retail shop billing.

ALGORITHM:

Step1: Start.
Step2: Read the barcode of the product.
Step3: Display the product name and the amount.
Step 4: Check if more products is available, if available go to Step 2, otherwise go to Step 5.
Step5: Calculate the total cost of the products.
Step6: Print total cost.
Step7: Stop.

PSEUDOCODE:

IF more products available THEN


READ bar code
DISPLAY Product name, amount
ELSE
ENDIF CALCULATE Total Cost
PRINT Total Cost

FLOWCHART
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
CS25C02-PROGRAMMING IN C LABORATORY

RESULT:

Thus the flowchart for retail shop billing is developed and executed successfully.
1B) Write a program to print sample strings like “hello world”, “Welcome to C
Programming” with different formats using escape sequences.

AIM:

To write a C program to print sample strings using escape sequences.

ALGORITHM:

1. Start the program.


2. Print the string "hello world" using:
o A newline character (\n)
o A tab character (\t)
o Quotation marks using escape character (\")
o A backspace character (\b)
o A carriage return (\r)
3. Print the string "Welcome to C Programming" using:
o A newline after the full sentence
o Newlines between each word
o Tab spaces between each word
o Quotation marks using escape characters
o An alert sound (\a)
4. Wait for a key press using getch() (optional for pausing the output).
5. End the program.
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>
#include <conio.h> // Used for getch()

void main( )
{
// Print "hello world" in different formats
printf("hello world\n"); // \n - Newline
printf("\thello world\n"); // \t - Horizontal tab
printf("\"hello world\"\n"); // \" - Double quote
printf("hello\b world\n"); // \b - Backspace
printf("hello world\rWELCOME\n"); // \r - Carriage return

// Print "Welcome to C Programming" in different formats


printf("Welcome to C Programming\n"); // Normal
printf("Welcome\nto\nC\nProgramming\n"); // Each word on a new line
printf ("Welcome\tto\tC\tProgramming\n"); // Words separated by tabs
printf("\"Welcome to C Programming\"\n"); // Quoted text
printf("Welcome to C Programming\a\n"); // \a - Alert (bell sound)

getch(); // Waits for a key press before closing (useful in Turbo C/C++)
}

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
CS25C02-PROGRAMMING IN C LABORATORY

RESULT:

Thus the sample strings using escape sequences in C program is executed successfully.

C) Write a C Program to print different data types in ‘C’ and their ranges

AIM:
To write a C program to print different data types in ‘C’ and their ranges

ALGORITHM:

1. Start the program.


2. Include the required header files:
3. Begin the main function.
4. For each data type, do the following:
5. Use sizeof() to find and print the size of the data type.
6. Use predefined macros (CHAR_MIN, CHAR_MAX, INT_MIN, etc.) to print the
minimum and maximum range.
7. Display the size and range of: char,short,int,long,float.
8. End the program.

PROGRAM:

#include <stdio.h>
#include <limits.h> // For integer type limits
#include <float.h> // For floating point type limits
CS25C02-PROGRAMMING IN C LABORATORY

int main() {
// Integer types
printf("Data Type: char\n");
printf("Size: %lu byte\n", sizeof(char));
printf("Range: %d to %d\n\n", CHAR_MIN, CHAR_MAX);

printf("Data Type: unsigned char\n");


printf("Size: %lu byte\n", sizeof(unsigned char));
printf("Range: 0 to %u\n\n", UCHAR_MAX);

printf("Data Type: short\n");


printf("Size: %lu bytes\n", sizeof(short));
printf("Range: %d to %d\n\n", SHRT_MIN, SHRT_MAX);

printf("Data Type: unsigned short\n");


printf("Size: %lu bytes\n", sizeof(unsigned short));
printf("Range: 0 to %u\n\n", USHRT_MAX);

printf("Data Type: int\n");


printf("Size: %lu bytes\n", sizeof(int));
printf("Range: %d to %d\n\n", INT_MIN, INT_MAX);

printf("Data Type: unsigned int\n");


printf("Size: %lu bytes\n", sizeof(unsigned int));
printf("Range: 0 to %u\n\n", UINT_MAX);

printf("Data Type: long\n");


printf("Size: %lu bytes\n", sizeof(long));
printf("Range: %ld to %ld\n\n", LONG_MIN, LONG_MAX);

printf("Data Type: unsigned long\n");


printf("Size: %lu bytes\n", sizeof(unsigned long));
printf("Range: 0 to %lu\n\n", ULONG_MAX);

// Floating point types


printf("Data Type: float\n");
printf("Size: %lu bytes\n", sizeof(float));
printf("Range: %e to %e\n\n", FLT_MIN, FLT_MAX);

printf("Data Type: double\n");


printf("Size: %lu bytes\n", sizeof(double));
printf("Range: %e to %e\n\n", DBL_MIN, DBL_MAX);

printf("Data Type: long double\n");


printf("Size: %lu bytes\n", sizeof(long double));
printf("Range: %Le to %Le\n\n", LDBL_MIN, LDBL_MAX);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
CS25C02-PROGRAMMING IN C LABORATORY

Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully displays the size and range value of various data types in
C, including integer, floating-point, and character types
D) Write a C Program to initialize, assignment & printing variables of different data
types.

AIM:

To write a C Program to initialize, assignment & printing variables of different data


types.

ALGORITHM:

1. Start the program.


2. Include the necessary header file stdio.h.
3. Begin the main() function.
4. Declare and initialize variables of different data types:
o Declare an int variable and assign a value.
o Declare an unsigned int variable and assign a value.
o Declare a short variable and assign a value.
o Declare a long variable and assign a value.
o Declare a float variable and assign a value.
o Declare a double variable and assign a value.
o Declare a long double variable and assign a value.
o Declare a char variable and assign a value.
5. Print the values of all variables using printf() and appropriate format specifiers.
6. End the main() function.
7. Stop the program.
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
// Initialization and Assignment of different data types

// Integer types
int a = 10;
unsigned int b = 20;
short c = -5;
long d = 100000L;

// Floating-point types
float e = 3.14f;
double f = 2.718281828;
long double g = 1.6180339887L;

// Character type
char h = 'A';

// Printing all variables


printf("Integer (int) a = %d\n", a);
printf("Unsigned Integer (unsigned int) b = %u\n", b);
printf("Short Integer (short) c = %d\n", c);
printf("Long Integer (long) d = %ld\n", d);

printf("Float e = %.2f\n", e);


printf("Double f = %lf\n", f);
printf("Long Double g = %Lf\n", g);

printf("Character h = %c\n", h);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully initializes variables of different data types, assigns values to
them, and prints their contents.

EX:NO:2 OPERATORS
Date:

2A) Write a Program to demonstrate arithmetic, logical and relational operators.

AIM:

To write a C Program to demonstrate arithmetic, logical and relational operators.

ALGORITHM:

1. Start the program.


2. Include the header file stdio.h for input/output functions.
3. Begin the main() function.
4. Declare integer variables a and b, and initialize them with values (e.g., a = 10, b =
5).
CS25C02-PROGRAMMING IN C LABORATORY

5. Perform Arithmetic Operations:

• Compute a + b (Addition)
• Compute a - b (Subtraction)
• Compute a * b (Multiplication)
• Compute a / b (Division)
• Compute a % b (Modulus)
• Print all arithmetic results

6. Perform Relational Operations:

• Check a == b (Equal to)


• Check a != b (Not equal to)
• Check a > b (Greater than)
• Check a < b (Less than)
• Check a >= b (Greater than or equal to)
• Check a <= b (Less than or equal to)
• Print all relational results (output will be 1 for true, 0 for false)

7. Declare two more integer variables x = 1, y = 0 to demonstrate logical operations.


8. Perform Logical Operations:

• Evaluate x && y (Logical AND)


• Evaluate x || y (Logical OR)
• Evaluate !x (Logical NOT)
• Evaluate !y (Logical NOT)
• Print all logical results

9. End the main() function.


10. Stop the program.

PROGRAM:

#include <stdio.h>

int main() {
int a = 10, b = 5;

// Arithmetic Operators
printf("Arithmetic Operators:\n");
printf("a + b = %d\n", a + b); // Addition
printf("a - b = %d\n", a - b); // Subtraction
printf("a * b = %d\n", a * b); // Multiplication
printf("a / b = %d\n", a / b); // Division
printf("a %% b = %d\n\n", a % b); // Modulus (Note: use %% to print %)
CS25C02-PROGRAMMING IN C LABORATORY

// Relational Operators
printf("Relational Operators:\n");
printf("a == b: %d\n", a == b); // Equal to
printf("a != b: %d\n", a != b); // Not equal to
printf("a > b: %d\n", a > b); // Greater than
printf("a < b: %d\n", a < b); // Less than
printf("a >= b: %d\n", a >= b); // Greater than or equal to
printf("a <= b: %d\n\n", a <= b);// Less than or equal to

// Logical Operators
int x = 1, y = 0;

printf("Logical Operators:\n");
printf("x && y = %d\n", x && y); // Logical AND
printf("x || y = %d\n", x || y); // Logical OR
printf("!x = %d\n", !x); // Logical NOT
printf("!y = %d\n", !y); // Logical NOT

return 0;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully performs arithmetic, logical, and relational operations on
given operands and displays the results.

B) Write a C Program to demonstrate pre increment and post increment (++a, a++), pre
decrement and post decrement (--a, a--)
CS25C02-PROGRAMMING IN C LABORATORY

AIM:

To write a C Program to demonstrate pre increment and post increment (++a, a++), pre
decrement and post decrement (--a, a--).

ALGORITHM:

1. Start the program.


2. Include the header file stdio.h.
3. Begin the main() function.
4. Declare two integer variables: a and b.
5. Initialize a with the value
6. Apply pre-increment,Post-Increment,Pre-Decrement and Post-Decrement
7. Print the values.
8. Stop the program.

PROGRAM:

#include <stdio.h>

int main() {
int a = 5, b;

// Pre-increment
b = ++a; // a is incremented first, then assigned to b
printf("After pre-increment (++a): a = %d, b = %d\n", a, b);

// Reset a
a = 5;

// Post-increment
b = a++; // a is assigned to b first, then incremented
printf("After post-increment (a++): a = %d, b = %d\n", a, b);

// Reset a
a = 5;

// Pre-decrement
b = --a; // a is decremented first, then assigned to b
printf("After pre-decrement (--a): a = %d, b = %d\n", a, b);

// Reset a
a = 5;

// Post-decrement
b = a--; // a is assigned to b first, then decremented
printf("After post-decrement (a--): a = %d, b = %d\n", a, b);
CS25C02-PROGRAMMING IN C LABORATORY

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program correctly demonstrates the behavior of pre-increment, post-increment,


pre-decrement, and post-decrement operators on integer variables.

C) Write a Program to read radius value from the keyboard and calculate the area of
circle and print the result in both floating and exponential notation
CS25C02-PROGRAMMING IN C LABORATORY

AIM:

To write a C Program to read radius value and calculate the area of circle.

ALGORITHM:

1. Start
2. Declare variables:
3. Prompt the user to enter the radius of the circle.
4. Read the radius value from the keyboard.
5. Calculate the area of the circle using the formula:
area = PI × radius × radius
6. Display the area in floating-point notation (e.g., using %.2f format).
7. Display the area in exponential notation (e.g., using %.2e format).
8. End

PROGRAM:

#include <stdio.h>

int main() {
float radius, area;
const float PI = 3.14159;

// Read radius from the user


printf("Enter the radius of the circle: ");
scanf("%f", &radius);

// Calculate area
area = PI * radius * radius;

// Display area in floating-point notation


printf("Area of the circle (floating-point): %.2f\n", area);

// Display area in exponential notation


printf("Area of the circle (exponential): %.2e\n", area);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads the radius value from the user, calculates the area of the
circle using the formula area = π × radius × radius, and displays the result.

D) Write a Program to calculate simple interest.


CS25C02-PROGRAMMING IN C LABORATORY

AIM:

To write a C program to calculate simple interest.

ALGORITHM:

1. Start
2. Declare variables:

• principal (P)
• rate (R)
• time (T)
• simpleInterest (SI)

3. Prompt the user to enter the principal amount


4. Read the value of principal
5. Prompt the user to enter the rate of interest (per annum)
6. Read the value of rate
7. Prompt the user to enter the time (in years)
8. Read the value of time
9. Calculate Simple Interest using the formula:

SI=P×R×T100\text{SI} = \frac{P \times R \times T}{100}SI=100P×R×T

10. Display the value of simpleInterest


11. Stop

PROGRAM:

#include <stdio.h>

int main() {
float principal, rate, time, simpleInterest;

// Input
printf("Enter the principal amount: ");
scanf("%f", &principal);

printf("Enter the rate of interest (per annum): ");


scanf("%f", &rate);

printf("Enter the time (in years): ");


scanf("%f", &time);

// Calculate Simple Interest


simpleInterest = (principal * rate * time) / 100;
CS25C02-PROGRAMMING IN C LABORATORY

// Output
printf("Simple Interest = %.2f\n", simpleInterest);

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads the principal, rate of interest, and time from the
user, calculates the simple interest using the formula SI = (P × R × T) / 100, and displays the
result.
CS25C02-PROGRAMMING IN C LABORATORY

E) Write a Program to convert temperature from Fahrenheit –Centigrade and vice-


versa.

AIM:

To write a C Program to convert temperature from Fahrenheit to centigrade and vice-versa.

ALGORITHM:

1. Start
2. Display the title: "Temperature Conversion Program"
3. Display options:
4. Press 1 for Fahrenheit to Centigrade conversion
5. Press 2 for Centigrade to Fahrenheit conversion
6. Read the user's choice
7. If choice is 1, then: Prompt the user to enter temperature in Fahrenheit
8. Read the temperature value
9. Convert using the formula: Centigrade = (Fahrenheit - 32) × 5 / 9
10. Display the result in Centigrade,Else if choice is 2, then: Prompt the user to enter
temperature in Centigrade
11. Read the temperature value
12. Convert using the formula:
Fahrenheit = (Centigrade × 9 / 5) + 32
13. Display the result in Fahrenheit
14. Else
15. Display "Invalid choice"
16. Stop

PROGRAM:

#include <stdio.h>

int main() {
int choice;
float temp, converted;

printf("Temperature Conversion Program\n");


printf("1. Fahrenheit to Centigrade (Celsius)\n");
printf("2. Centigrade (Celsius) to Fahrenheit\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);

if (choice == 1) {
// Fahrenheit to Centigrade
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temp);
converted = (temp - 32) * 5 / 9;
printf("Temperature in Centigrade (Celsius): %.2f °C\n", converted);
} else if (choice == 2) {
CS25C02-PROGRAMMING IN C LABORATORY

// Centigrade to Fahrenheit
printf("Enter temperature in Centigrade (Celsius): ");
scanf("%f", &temp);
converted = (temp * 9 / 5) + 32;
printf("Temperature in Fahrenheit: %.2f °F\n", converted);
} else {
printf("Invalid choice. Please run the program again.\n");
}

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully converts temperature values from Fahrenheit to


Centigrade and vice versa using the appropriate formulas.
CS25C02-PROGRAMMING IN C LABORATORY

Ex: No: 3 Control Statements


Date:

A) Write a Program to read marks of a student in six subjects and print whether pass
or Fail. (using if-else).

AIM:

To write a C Program to read marks of a student in six subjects and print whether pass or
Fail. (using if-else).
Description:

ALGORITHM:

• Start
• Declare six integer variables: s1, s2, s3, s4, s5, s6 for storing subject marks.
• Display a message to the user: "Enter marks for 6 subjects".
• Input marks for each subject one by one.
• Check condition:
• If all six marks are greater than or equal to 35:
o Display: "Result: PASS"
• Else:

o Display: "Result: FAIL"


• End

PSEUDOCODE:

• START
• Declare integers s1, s2, s3, s4, s5, s6
• Print "Enter marks for 6 subjects"
• Read s1, s2, s3, s4, s5, s6
• IF s1 >= 35 AND s2 >= 35 AND s3 >= 35 AND s4 >= 35 AND s5 >= 35 AND s6 >=
35 THEN
• Print "Result: PASS"
• ELSE
• Print "Result: FAIL"
• ENDIF
• END

PROGRAM:
#include <stdio.h>

int main() {
int s1, s2, s3, s4, s5, s6;

// Read marks for 6 subjects


printf("Enter marks for 6 subjects (out of 100):\n");
CS25C02-PROGRAMMING IN C LABORATORY

printf("Subject 1: ");
scanf("%d", &s1);
printf("Subject 2: ");
scanf("%d", &s2);
printf("Subject 3: ");
scanf("%d", &s3);
printf("Subject 4: ");
scanf("%d", &s4);
printf("Subject 5: ");
scanf("%d", &s5);
printf("Subject 6: ");
scanf("%d", &s6);

// Check if student passed all subjects


if (s1 >= 35 && s2 >= 35 && s3 >= 35 && s4 >= 35 && s5 >= 35 && s6 >= 35) {
printf("Result: PASS\n");
} else {
printf("Result: FAIL\n");
}

return 0;
}
OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
CS25C02-PROGRAMMING IN C LABORATORY

Thus the program successfully reads marks for six subjects, checks each against the
passing criteria using if-else, and displays whether the student has passed or failed

.B) Write a Program to calculate roots of quadratic equation.

AIM:

To write a C Program to calculate roots of quadratic equation. (using if-else).

DESCRIPTION:

A quadratic equation is of the form:

ax^2 + bx + c = 0

The roots are calculated using the discriminant (D):

D = b^2 - 4ac

• If D > 0 → two real and distinct roots


• If D == 0 → two real and equal roots
• If D < 0 → two complex (imaginary) roots

ALGORITHM:

1. Start
2. Input coefficients a, b, c
3. Calculate the discriminant D = b² - 4ac
4. If D > 0, compute two real and distinct roots
5. Else if D == 0, compute one real repeated root
6. Else if D < 0, compute two complex roots
7. Display the roots
8. Stop

PSEUDOCODE:

• START
Input a, b, c
Compute discriminant D = b^2 - 4*a*c
• IF D > 0 THEN
root1 = (-b + sqrt(D)) / (2*a)
root2 = (-b - sqrt(D)) / (2*a)
• Print "Roots are real and different"
• Print root1, root
• ELSE IF D == 0 THEN
root = -b / (2*a)
• Print "Roots are real and equal"
• Print root
CS25C02-PROGRAMMING IN C LABORATORY

• ELSE
realPart = -b / (2*a)
imagPart = sqrt(-D) / (2*a)
• Print "Roots are complex and imaginary"
• Print root1 = realPart + imagPart i
• Print root2 = realPart - imagPart i
• STOP

PROGRAM:

#include <stdio.h>
#include <math.h>

int main() {
float a, b, c;
float discriminant, root1, root2, realPart, imagPart;

// Input coefficients
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);

// Calculate discriminant
discriminant = b * b - 4 * a * c;

// Using if-else to determine the nature of roots


if (discriminant > 0) {
// Two real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root1 = %.2f\n", root1);
printf("Root2 = %.2f\n", root2);
}
else if (discriminant == 0) {
// Two real and equal roots
root1 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root1 = Root2 = %.2f\n", root1);
}
else {
// Complex roots
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and imaginary.\n");
printf("Root1 = %.2f + %.2fi\n", realPart, imagPart);
printf("Root2 = %.2f - %.2fi\n", realPart, imagPart);
}

return 0;
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program correctly reads the coefficients of a quadratic equation, computes the
discriminant, and uses if-else to determine and display real, equal, or complex roots.
CS25C02-PROGRAMMING IN C LABORATORY

C) Write a Program to calculate electricity bill. Read starting and ending meter
reading.

The charges are as follows.


Usage Slab( in Units) Rate per unit(Rs)
Upto 400 4.80
401-500 6.45
500-600 8.55
601-800 9.65
800-1000 10.70
>1000 11.80

AIM:

To write a C Program to calculate electricity bill by reading starting and ending meter
readings given by usage slab charges.

ALGORITHM:

1. Step 1: Start
2. Step 2: Read the starting and ending meter readings
3. Step 3: Check if the ending reading is greater than or equal to starting reading
• If not, print an error and exit
4. Step 4: Calculate units consumed = ending reading - starting reading
5. Step 5: Determine the rate per unit based on the slab:
6. If units ≤ 400, rate = ₹4.80
7. If 401 ≤ units ≤ 500, rate = ₹6.45
8. If 501 ≤ units ≤ 600, rate = ₹8.55
9. If 601 ≤ units ≤ 800, rate = ₹9.65
10. If 801 ≤ units ≤ 1000, rate = ₹10.70
11. If units > 1000, rate = ₹11.80
12. Step 6: Calculate the bill amount = units × rate
13. Step 7: Display the units consumed and total bill
14. Step 8: Stop

PSEUDOCODE:

• START
• INPUT startReading
• INPUT endReading
• IF endReading < startReading THEN
• PRINT "Invalid readings"
• EXIT
units ← endReading - startReading
• IF units ≤ 400 THEN
• rate ← 4.80
• ELSE IF units ≤ 500 THEN
rate ← 6.45
• ELSE IF units ≤ 600 THEN
rate ← 8.55
CS25C02-PROGRAMMING IN C LABORATORY

• ELSE IF units ≤ 800 THEN


rate ← 9.65
• ELSE IF units ≤ 1000 THEN
rate ← 10.70
• ELSE
rate ← 11.80
billAmount ← units × rate
• PRINT "Units Consumed: ", units
• PRINT "Total Bill: ", billAmount
• STOP

PROGRAM:

#include <stdio.h>

int main() {
int startReading, endReading, units;
float billAmount;

// Input meter readings


printf("Enter starting meter reading: ");
scanf("%d", &startReading);

printf("Enter ending meter reading: ");


scanf("%d", &endReading);

// Calculate units consumed


if (endReading < startReading) {
printf("Invalid readings. Ending reading should be greater than starting reading.\n");
return 1; // Exit program with error
}

units = endReading - startReading;

// Calculate bill amount based on units consumed


if (units <= 400) {
billAmount = units * 4.80;
}
else if (units <= 500) {
billAmount = units * 6.45;
}
else if (units <= 600) {
billAmount = units * 8.55;
}
else if (units <= 800) {
billAmount = units * 9.65;
}
else if (units <= 1000) {
billAmount = units * 10.70;
}
CS25C02-PROGRAMMING IN C LABORATORY

else {
billAmount = units * 11.80;
}

// Display result
printf("\nElectricity Bill\n");
printf("----------------------\n");
printf("Units Consumed : %d\n", units);
printf("Total Bill (Rs): %.2f\n", billAmount);

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads the starting and ending meter readings, calculates
total units consumed, applies the correct rate based on the usage slab, and displays the
electricity bill amount.
CS25C02-PROGRAMMING IN C LABORATORY

E) Write a Program to perform arithmetic operations using case control statement.

AIM:

To write a C Program to perform arithmetic operations using case control statement.

ALGORITHM:

• Start
• Read two numbers: num1, num2
• Read an arithmetic operator: +, -, *, or /
• Use switch statement on the operator:
• If +, compute sum
• If -, compute difference
• If *, compute product
• If /, check divisor ≠ 0 and compute division
• Else, print invalid operator
• Print the result
• Stop

PSEUDOCODE:

• START
INPUT num1, num2
INPUT operator
• SWITCH (operator)
• CASE '+':
result = num1 + num2
• PRINT result
• CASE '-':
result = num1 - num2
• PRINT result
• CASE '*':
result = num1 * num2
• PRINT result
• CASE '/':
IF num2 ≠ 0 THEN
result = num1 / num2
• PRINT result
• ELSE
• PRINT "Division by zero error"
• DEFAULT:
• PRINT "Invalid operator"
• STOP
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int num1, num2;
char operator;
float result;

// Input two numbers and the operator


printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &operator); // Space before %c to consume previous newline

// Switch-case to perform operation


switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;

case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;

case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;

case '/':
if (num2 != 0) {
result = (float)num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;

default:
printf("Invalid operator.\n");
}

return 0;
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully performs addition, subtraction, multiplication, and division
based on the user’s choice using a switch case control statement.
CS25C02-PROGRAMMING IN C LABORATORY

F) Write a Program to display names of days in a Week using case control statement.

AIM:

To write a C Program to display names of days in a Week using case control statement.

ALGORITHM:

1. Start
2. Read an integer between 1 and 7
3. Use switch statement:
4. Case 1 → Sunday
5. Case 2 → Monday
6. Case 7 → Saturday
7. Default → Invalid input message
8. Stop

PROGRAM:

#include <stdio.h>

int main() {
int day;

// Input day number


printf("Enter day number (1 to 7): ");
scanf("%d", &day);

// Switch-case to display day name


switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
CS25C02-PROGRAMMING IN C LABORATORY

printf("Saturday\n");
break;
default:
printf("Invalid day number. Please enter between 1 and 7.\n");
}

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
The program successfully displays the name of the day corresponding to the user’s input
(1–7) using a switch case control statement.
CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:4
Date Looping operations

A. Write a program to calculate sum of individual digits of a given number.

AIM:

To write a C program to calculate sum of individual digits of a given number.

ALGORITHM:

1. Start
2. Declare variables num, digit, and sum = 0
3. Read an integer number from the user and store it in num
4. If num is negative, convert it to positive
5. Repeat the following steps while num is not 0:
6. Extract the last digit: digit = num % 10
7. Add digit to sum
8. Remove the last digit from num: num = num / 10
9. Display the value of sum
10. Stop

PSEUDOCODE:
• Begin
• Initialize sum ← 0
• Read num
• If num < 0 then
• num ← -num
• While num ≠ 0 do
• digit ← num mod 10
• sum ← sum + digit
• num ← num / 10
• End while
• Display sum
• End

PROGRAM:

#include <stdio.h>

int main() {
int num, digit, sum = 0;

// Read a number from user


printf("Enter an integer: ");
scanf("%d", &num);

// Make sure to work with positive number


CS25C02-PROGRAMMING IN C LABORATORY

if (num < 0)
num = -num;

// Extract digits and compute sum


while (num != 0) {
digit = num % 10; // Get last digit
sum += digit; // Add to sum
num = num / 10; // Remove last digit
}

// Display the result


printf("Sum of digits = %d\n", sum);

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads an integer from the user, extracts each digit, calculates
the sum of all digits, and displays the result.
CS25C02-PROGRAMMING IN C LABORATORY

B. Write a program to check whether given number is palindrome or not.

AIM:

To write a C program to check whether given number is palindrome or not.

DESCRIPTION:

A number is called a palindrome if it reads the same forward and backward.


Examples:

• 121 → Palindrome
• 123 → Not a palindrome

ALGORITHM:

1. Start
2. Declare variables: num, temp, digit, rev = 0
3. Read number from user and store it in num
4. Store original number in temp
5. Repeat until num becomes 0:
6. Get last digit: digit = num % 10

• Reverse number: rev = rev * 10 + digit


• Remove last digit: num = num / 10

7. Compare rev with temp

• If equal → number is a palindrome


• Else → number is not a palindrome

8. Stop

PSEUDOCODE:

• Begin
• Initialize rev ← 0
• Read num
• temp ← num
• While num ≠ 0 do
• digit ← num mod 10
• rev ← rev * 10 + digit
• num ← num / 10
• End while
• If temp = rev then
• Print "Palindrome"
• Else
• Print "Not a palindrome"
• End
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int num, temp, digit, rev = 0;

// Read input
printf("Enter an integer: ");
scanf("%d", &num);

temp = num; // Store original number

// Reverse the number


while (num != 0) {
digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

// Check palindrome
if (temp == rev)
printf("%d is a Palindrome number.\n", temp);
else
printf("%d is Not a Palindrome number.\n", temp);

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads an integer, reverses its digits, and compares the
reversed number with the original to determine if it is a palindro
CS25C02-PROGRAMMING IN C LABORATORY

C. Write a program to print prime numbers in the given range.

AIM:

To write a C program to print prime numbers in the given range.

ALGORITHM:

1. Start
2. Read start and end (the range values from the user)
3. Loop from i = start to i = end

• If i <= 1, skip it (0 and 1 are not prime)


• Initialize a flag is Prime = 1 (assume i is prime)
• For j = 2 to √I If i % j == 0, set is Prime = 0 and break (i is not prime)
• If is Prime == 1, print i

4. End loop
5. Stop

PSEUDOCODE:

• BEGIN
• INPUT start, end
• FOR i FROM start TO end DO
• IF i <= 1 THEN,CONTINUE to next i
• END IF is Prime ← 1
• FOR j FROM 2 TO square_root(i) DO
o IF i MOD j == 0 THEN
o isPrime ← 0
o BREAK
o END IF
• END FOR
• IF isPrime == 1 THEN
o PRINT i
• END IF
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int start, end, i, j, isPrime;

// Input range
printf("Enter the starting number: ");
scanf("%d", &start);
CS25C02-PROGRAMMING IN C LABORATORY

printf("Enter the ending number: ");


scanf("%d", &end);

printf("Prime numbers between %d and %d are:\n", start, end);

// Loop through each number in the range


for(i = start; i <= end; i++) {
if (i <= 1)
continue; // 0 and 1 are not prime

isPrime = 1; // Assume i is prime

// Check for factors


for(j = 2; j * j <= i; j++) {
if(i % j == 0) {
isPrime = 0;
break;
}
}

if(is Prime)
printf("%d ", i);
}

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads the start and end values of a range, checks each
number for primality, and displays all prime numbers within the range.
CS25C02-PROGRAMMING IN C LABORATORY

D. Write a program to print the Fibonacci series for given ‘N’ value.

AIM:
To write a C Program to print the Fibonacci series for given ‘N’ value.

ALGORITHM

1. Start
2. Read the value of N (number of terms)
3. Initialize:
o first = 0
o second = 1
4. Repeat for i = 0 to N - 1:
o If i == 0, print 0
o Else if i == 1, print 1
o Else:
▪ Calculate next = first + second
▪ Update first = second
▪ Update second = next
▪ Print next
5. Stop

PSEUDOCODE:

• sql
• CopyEdit
• BEGIN
• INPUT N
• first ← 0
• second ← 1
• FOR i ← 0 TO N-1 DO
• IF i == 0 THEN, PRINT 0
• ELSE IF i == 1 THEN, PRINT 1
• ELSE
o next ← first + second
o PRINT next
o first ← second
o second ← next
• END IF
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int n, i;
CS25C02-PROGRAMMING IN C LABORATORY

int first = 0, second = 1, next;

// Input number of terms


printf("Enter the number of terms (N): ");
scanf("%d", &n);

printf("Fibonacci Series up to %d terms:\n", n);

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


if(i == 0)
next = 0;
else if(i == 1)
next = 1;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads the value of N and generates the Fibonacci series up to
the Nth term.
CS25C02-PROGRAMMING IN C LABORATORY

E. Write a program to print the following formats.

AIM:

To write a C Program to print the various types of Pyramids.

1. Right Half Pyramid (n = 5)

ALGORITHM:

1. Start
2. Input number of rows n
3. Loop i from 1 to n
o Loop j from 1 to i
▪ Print "*"
o Print new line
4. End

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• FOR j ← 1 TO i DO
o PRINT "* "
CS25C02-PROGRAMMING IN C LABORATORY

• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

2. Left Half Pyramid

ALGORITHM:

1. Start
2. Input n
3. Loop i from 1 to n
o Print (n - i) spaces
o Print i stars
o Print newline
4. End

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• FOR j ← 1 TO (n - i) DO
o PRINT " "
• END FOR
• FOR j ← 1 TO i DO
o PRINT "* "
• END FOR
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n - i; j++) {
printf(" "); // Two spaces for alignment
}
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;

3. Full Pyramid

ALGORITHM:

1. Start
2. Input n
3. Loop i from 1 to n
o Print (n - i) spaces
o Print (2 * i - 1) stars
o Print newline
4. End

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• FOR j ← 1 TO (n - i) DO
o PRINT " "
• END FOR
• FOR j ← 1 TO (2 * i - 1) DO
o PRINT "* "
CS25C02-PROGRAMMING IN C LABORATORY

• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= n - i; j++) {
printf(" ");
}
for(j = 1; j <= 2 * i - 1; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

4. Inverted Right Half Pyramid

ALGORITHM:

1. Start
2. Input n
3. Loop i from n down to 1
o Loop j from 1 to i
▪ Print "*"
o Print newline
4. End

PSEUDOCODE:

• pgsql
• Copy Edit
• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
CS25C02-PROGRAMMING IN C LABORATORY

• FOR j ← 1 TO i DO
o PRINT "* "
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

5. Inverted Left Half Pyramid

ALGORITHM:

1. Start
2. Input n
3. Loop i from n down to 1
o Print (n - i) spaces
o Print i stars
o Print newline
4. End

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
• FOR j ← 1 TO (n - i) DO
o PRINT " "
• END FOR
• FOR j ← 1 TO i DO
o PRINT "* "
• END FOR
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


for(j = 1; j <= n - i; j++) {
printf(" ");
}
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

6. Inverted Full Pyramid

ALGORITHM:

1. Input n
2. For i from n to 1
o Print (n - i) spaces
o Print 2*i - 1 stars
o Print newline

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
• FOR j ← 1 TO n - i DO
o PRINT " "
• END FOR
• FOR j ← 1 TO 2*i - 1 DO
o PRINT "* "
• END FOR
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


for(j = 0; j < n - i; j++)
printf(" ");
for(j = 0; j < 2 * i - 1; j++)
printf("* ");
printf("\n");
}

return 0;
}

7. Rhombus Pattern

ALGORITHM:

1. Input n
2. For i from 1 to n
o Print (n - i) spaces
o Print n stars
o Print newline

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• FOR j ← 1 TO n - i DO
o PRINT " "
• END FOR
• FOR j ← 1 TO n DO
o PRINT "* "
• END FOR
• PRINT newline
• END FOR
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter size of Rhombus: ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= n; j++)
printf("* ");
printf("\n");
}

return 0;
}

8. Diamond Pattern

ALGORITHM:

1. Input n
2. For i from 1 to n
o Print n - i spaces
o Print 2*i - 1 stars
3. For i from n-1 down to 1
o Repeat same as above for lower half

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• PRINT (n - i) spaces
• PRINT (2*i - 1) stars
• PRINT newline
• END FOR
• FOR i ← n - 1 TO 1 DO
• PRINT (n - i) spaces
• PRINT (2*i - 1) stars
• PRINT newline
• END FOR
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter size of Diamond: ");
scanf("%d", &n);

// Upper half
for(i = 1; i <= n; i++) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("* ");
printf("\n");
}

// Lower half
for(i = n - 1; i >= 1; i--) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("* ");
printf("\n");
}

return 0;
}

9. Hourglass Pattern

ALGORITHM:

1. Input n
2. For i = n down to 1
o Print n - i spaces
o Print 2*i - 1 stars
3. For i = 2 to n
o Repeat same logic

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
• PRINT (n - i) spaces
• PRINT (2*i - 1) stars
• PRINT newline
• END FOR
CS25C02-PROGRAMMING IN C LABORATORY

• FOR i ← 2 TO n DO
• PRINT (n - i) spaces
• PRINT (2*i - 1) stars
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

// Top half
for(i = n; i >= 1; i--) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("* ");
printf("\n");
}

// Bottom half
for(i = 2; i <= n; i++) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("* ");
printf("\n");
}

return 0;
}

10. Hollow Square Pattern

ALGORITHM:

1. Input n
2. For i from 1 to n
o For j from 1 to n
▪ If i == 1 || i == n || j == 1 || j == n → print *
▪ Else → print space
CS25C02-PROGRAMMING IN C LABORATORY

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• FOR j ← 1 TO n DO
o IF i == 1 OR i == n OR j == 1 OR j == n THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter size of square: ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

11. Hollow Full Pyramid

ALGORITHM:

1. Input n
2. For i from 1 to n
o Print n - i spaces
o For j from 1 to 2*i - 1
▪ Print * if j == 1 or j == 2*i - 1 or i == n
▪ Else print space
CS25C02-PROGRAMMING IN C LABORATORY

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 OR i == n THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

for(i 1; i <= n; i++) {


for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1 || i == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

12. Hollow Inverted Full Pyramid

ALGORITHM:

1. Input n
2. For i from n down to 1
o Print n - i spaces
o For j from 1 to 2*i - 1
▪ Print * if j == 1 or j == 2*i - 1 or i == n
▪ Else print space

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 OR i == n THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1 || i == n)
printf("* ");
else
printf(" ");

}
printf("\n");
}
CS25C02-PROGRAMMING IN C LABORATORY

return 0;
}

13. Hollow Diamond Pyramid

ALGORITHM:

1. Input n
2. For i = 1 to n
o Print spaces and stars (only at boundaries)
3. For i = n-1 down to 1
o Repeat the same logic

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← 1 TO n DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• FOR i ← n - 1 TO 1 DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
rintf("Enter size of diamond: ");
CS25C02-PROGRAMMING IN C LABORATORY

scanf("%d", &n);

// Upper half
for(i = 1; i <= n; i++) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1)
printf("* ");
else
printf(" ");
}
printf("\n");
}

// Lower half
for(i = n - 1; i >= 1; i--) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

14. Hollow Hourglass Pattern

ALGORITHM:

1. Input n
2. Print upper inverted hollow pyramid
3. Print lower upright hollow pyramid

PSEUDOCODE:

• BEGIN
• INPUT n
• FOR i ← n TO 1 DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 OR i == n THEN
o PRINT "* "
o ELSE
o PRINT " "
CS25C02-PROGRAMMING IN C LABORATORY

o END IF
• END FOR
• PRINT newline
• END FOR
• FOR i ← 2 TO n DO
• PRINT (n - i) spaces
• FOR j ← 1 TO (2*i - 1) DO
o IF j == 1 OR j == 2*i - 1 OR i == n THEN
o PRINT "* "
o ELSE
o PRINT " "
o END IF
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

// Upper half
for(i = n; i >= 1; i--) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1 || i == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}

// Lower half
for(i = 2; i <= n; i++) {
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++) {
if(j == 1 || j == 2 * i - 1 || i == n)
printf("* ");
else
printf(" ");
}
CS25C02-PROGRAMMING IN C LABORATORY

printf("\n");
}

return 0;
}

15. Floyd’s Triangle

ALGORITHM:

1. Input n
2. Initialize num = 1
3. For i = 1 to n
o For j = 1 to i
▪ Print num++

PSEUDOCODE:

• BEGIN
• INPUT n
• num ← 1
• FOR i ← 1 TO n DO
• FOR j ← 1 TO i DO
o PRINT num
o num ← num + 1
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int main() {
int i, j, n, num = 1;
printf("Enter number of rows: ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= i; j++) {
printf("%d ", num++);
}
printf("\n");
}

return 0;
CS25C02-PROGRAMMING IN C LABORATORY

16. Pascal’s Triangle

ALGORITHM:

1. Input n
2. For each row i from 0 to n-1
o Print spaces for formatting
o For j = 0 to i, compute and print C(i, j) using factorial

PSEUDOCODE:

• FUNCTION factorial(n)
• IF n == 0 OR n == 1 THEN RETURN 1
• ELSE RETURN n * factorial(n - 1)
• BEGIN
• INPUT n
• FOR i ← 0 TO n - 1 DO
• PRINT (n - i - 1) spaces
• FOR j ← 0 TO i DO
o PRINT factorial(i) / (factorial(j) * factorial(i - j))
• END FOR
• PRINT newline
• END FOR
• END

PROGRAM:

#include <stdio.h>

int fact(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}

int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);

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


for(j = 0; j <= n - i - 1; j++)
printf(" ");
for(j = 0; j <= i; j++)
printf("%d ", fact(i) / (fact(j) * fact(i - j)));
CS25C02-PROGRAMMING IN C LABORATORY

printf("\n");
}

return 0;
}

OUTPUT:

Right Half Pyramid

*
* *
* * *
* * * *
* * * * *

Left Half Pyramid


*
* *
* * *
* * * *
* * * * *

Full Pyramid
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

Inverted Right Half Pyramid

* * * * *
* * * *
* * *
* *
*

Inverted Left Half Pyramid

* * * * *
* * * *
* * *
* *
*
CS25C02-PROGRAMMING IN C LABORATORY

Inverted Full Pyramid (n = 5)

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Rhombus Pattern (n = 5)

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Diamond Pattern (n = 5)

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Hourglass Pattern (n = 5)

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

Hollow Square Pattern (n = 5)

* * * * *
* *
* *
* *
* * * * *

Hollow Full Pyramid (n = 5)

*
CS25C02-PROGRAMMING IN C LABORATORY

* *
* *
* *
* * * * * * * * *

Hollow Inverted Full Pyramid (n = 5)

* * * * * * * * *
* *
* *
* *
*

Hollow Diamond Pyramid (n = 5)

*
* *
* *
* *
* *
* *
* *
* *
*

Hollow Hourglass Pattern (n = 5)

* * * * * * * * *
* *
* *
* *
*
* *
* *
* *
* * * * * * * * *

Floyd’s Triangle (n = 5)

2 3
4 5 6
7 8 9 10
11 12 13 14 15

Pascal’s Triangle (n = 5)

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully prints different types of pyramid patterns (such as full,
half, and inverted) based on the given number of rows.
CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:5
Date 1-D and 2-D arrays

A. Write a program to store 10 elements in a 1-D array and print sum of the array,
maximum and minimum element in an array.

AIM:

To write a C program to store 10 elements in a 1-D array and print sum of the array,
maximum and minimum element in an array.

ALGORITHM:

1. Start
2. Declare an array of size 10
3. Initialize sum = 0, max = arr[0], min = arr[0]
4. Input 10 elements into the array
5. For each element:
o Add to sum
o If element > max, update max
o If element < min, update min
6. Print sum, max, and min
7. End

PSEUDOCODE

• BEGIN
• DECLARE array[10], i, sum, max, min
• sum ← 0
• PRINT "Enter 10 elements:"
• FOR i ← 0 TO 9 DO
• READ array[i]
• END FOR
• max ← array[0]
• min ← array[0]
• FOR i ← 0 TO 9 DO
• sum ← sum + array[i]
• IF array[i] > max THEN
• max ← array[i]
• END IF
• IF array[i] < min THEN
• min ← array[i]
• END IF
• END FOR
• PRINT "Sum =", sum
• PRINT "Maximum =", max
• PRINT "Minimum =", min
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int arr[10], i;
int sum = 0, max, min;

// Input 10 elements
printf("Enter 10 elements:\n");
for(i = 0; i < 10; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Initialize max and min


max = min = arr[0];

// Calculate sum, max, and min


for(i = 0; i < 10; i++) {
sum += arr[i];

if(arr[i] > max)


max = arr[i];
if(arr[i] < min)
min = arr[i];
}

// Display results
printf("\nSum of array elements: %d\n", sum);
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully stores 10 elements in a 1-D array, calculates the sum of all
elements, and determines the maximum and minimum values.
CS25C02-PROGRAMMING IN C LABORATORY

B.Write a program to count no. of positive numbers, negative numbers and zeros in an
array.

AIM:

To write a C Program to count number of positive numbers, negative numbers and zeros
in an array.

ALGORITHM:

1. Start
2. Declare an array and variables: positive, negative, and zero to count respective values
3. Input the size of the array n
4. Input n elements into the array
5. For each element in the array:
o If the element is > 0, increment positive
o Else if < 0, increment negative
o Else (== 0), increment zero
6. Display the counts of positive, negative, and zero
7. End

PSEUDOCODE:

• BEGIN
• DECLARE array[100], n, i
• DECLARE positive ← 0, negative ← 0, zero ← 0
• PRINT "Enter number of elements:"
• READ n
• PRINT "Enter n elements:"
• FOR i ← 0 TO n-1 DO
• READ array[i]
• IF array[i] > 0 THEN
o positive ← positive + 1
• ELSE IF array[i] < 0 THEN
o negative ← negative + 1
• ELSE
o zero ← zero + 1
• END IF
• END FOR
• PRINT "Number of Positive elements = ", positive
• PRINT "Number of Negative elements = ", negative
• PRINT "Number of Zeros = ", zero
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int arr[100], n;
int i, positive = 0, negative = 0, zero = 0;

// Input size of array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);

// Count based on value


if(arr[i] > 0)
positive++;
else if(arr[i] < 0)
negative++;
else
zero++;
}

// Output results
printf("\nNumber of Positive elements = %d\n", positive);
printf("Number of Negative elements = %d\n", negative);
printf("Number of Zeros = %d\n", zero);

return 0;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

RUBRICS

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
The program successfully reads elements into an array, counts the number of positive
numbers, negative numbers, and zeros, and displays the count.
CS25C02-PROGRAMMING IN C LABORATORY

C. Write a program to count all subsets of given array with sum equal to given sum.

AIM:

To write a C Program to count all subsets of given array with sum equal to given sum.

ALGORITHM:

1. Start
2. Input the size of the array n
3. Input n elements into the array
4. Input the target sum
5. Define a recursive function countSubsets(arr[], n, target) that:
o Returns 1 if target == 0 (found valid subset)
o Returns 0 if n == 0 and target != 0 (no subset)
o If arr[n-1] > target, call function excluding current element
o Else, call function:
▪ Once excluding current element
▪ Once including current element (subtract it from target)
6. Call countSubsets(arr, n, target) and store the result
7. Output the result
8. End

PSEUDOCODE:

• FUNCTION countSubsets(array, n, target)


• IF target = 0 THEN
• RETURN 1
• ELSE IF n = 0 THEN
• RETURN 0
• END IF
• IF array[n - 1] > target THEN
• RETURN countSubsets(array, n - 1, target)
• ELSE
• RETURN countSubsets(array, n - 1, target) +
o countSubsets(array, n - 1, target - array[n - 1])
• END IF
• END FUNCTION
• MAIN
• DECLARE array[100], n, target, i, result
• PRINT "Enter the number of elements:"
• READ n
• PRINT "Enter n elements:"
• FOR i ← 0 TO n-1 DO
• READ array[i]
• END FOR
• PRINT "Enter the target sum:"
• READ target
CS25C02-PROGRAMMING IN C LABORATORY

• result ← countSubsets(array, n, target)


• PRINT "Total subsets with sum =", target, "is", result
• END MAIN

PROGRAM:

#include <stdio.h>

// Function to count subsets with given sum


int countSubsets(int arr[], int n, int target) {
// Base case
if (target == 0)
return 1; // Found a valid subset
if (n == 0)
return 0; // No elements left

// If current element > target, exclude it


if (arr[n - 1] > target)
return countSubsets(arr, n - 1, target);

// Include or exclude current element


return countSubsets(arr, n - 1, target) +
countSubsets(arr, n - 1, target - arr[n - 1]);
}

int main() {
int arr[100], n, target, i;

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


scanf("%d", &n);

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


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter the target sum: ");


scanf("%d", &target);

int result = countSubsets(arr, n, target);


printf("Total subsets with sum %d = %d\n", target, result);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully generates all possible subsets of the given array, counts
those whose elements sum to the specified value, and displays the count.
CS25C02-PROGRAMMING IN C LABORATORY

D. Write a program to search an element using linear search algorithm.

AIM:

To write a C Program to search an element using linear search algorithm.

ALGORITHM:

1. Start
2. Input the size of the array n
3. Input n elements into the array
4. Input the search element to find
5. Initialize a flag variable found = 0
6. Traverse the array from index 0 to n-1:
o If arr[i] == search, set found = 1 and break
7. If found == 1, print the index where the element was found
8. Else, print "Element not found"
9. End

PSEUDOCODE:

• BEGIN
• DECLARE array[100], n, i, search, found ← 0
• PRINT "Enter number of elements:"
• READ n
• PRINT "Enter n elements:"
• FOR i ← 0 TO n - 1 DO
• READ array[i]
• END FOR
• PRINT "Enter the element to search:"
• READ search
• FOR i ← 0 TO n - 1 DO
• IF array[i] = search THEN
o found ← 1
o BREAK
• END IF
• END FOR
• IF found = 1 THEN
• PRINT "Element found at position ", i + 1
• ELSE
• PRINT "Element not found"
• END IF
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int arr[100], n, i, search, found = 0;

// Input size of array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Input the element to be searched


printf("Enter the element to search: ");
scanf("%d", &search);

// Linear search logic


for(i = 0; i < n; i++) {
if(arr[i] == search) {
found = 1;
break;
}
}

// Output result
if(found)
printf("Element %d found at position %d.\n", search, i + 1); // Position is 1-based
else
printf("Element %d not found in the array.\n", search);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads an array and a target element, searches for the element
using the linear search algorithm, and displays its position if found.
CS25C02-PROGRAMMING IN C LABORATORY

E.Write a program to sort the given elements using bubble sort algorithm.

AIM:

To write a C program to sort the given elements using bubble sort algorithm.

ALGORITHM:

1. Start
2. Input the number of elements n
3. Input n elements into an array arr[]
4. Repeat the following steps for i = 0 to n - 2:
a. For j = 0 to n - i - 2:
i. If arr[j] > arr[j + 1], then swap arr[j] and arr[j + 1]
5. After all passes, the array is sorted in ascending order
6. Output the sorted array
7. End

PSEUDOCODE

• BEGIN
• DECLARE array[100], n, i, j, temp
• PRINT "Enter number of elements:"
• READ n
• PRINT "Enter n elements:"
• FOR i ← 0 TO n - 1 DO
• READ array[i]
• END FOR
• FOR i ← 0 TO n - 2 DO
• FOR j ← 0 TO n - i - 2 DO
o IF array[j] > array[j + 1] THEN
o temp ← array[j]
o array[j] ← array[j + 1]
o array[j + 1] ← temp
o END IF
• END FOR
• END FOR
• PRINT "Sorted array:"
• FOR i ← 0 TO n - 1 DO
• PRINT array[i]
• END FOR
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

// Input number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Bubble Sort algorithm


for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
// Swap
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// Output the sorted array


printf("Sorted array in ascending order:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully sorts the given elements in ascending order using the bubble
sort algorithm and displays the sorted list.
CS25C02-PROGRAMMING IN C LABORATORY

F.Write a program to perform matrix addition, subtraction and multiplication.

AIM:

To write a C Program to perform matrix addition, Subtraction and multiplication.

ALGORITHM:

Input:

• Two matrices A[r1][c1] and B[r2][c2]


• User's choice of operation: addition, subtraction, or multiplication

1. Start
2. Input number of rows and columns for Matrix A: r1, c1
3. Input number of rows and columns for Matrix B: r2, c2
4. Input elements of Matrix A
5. Input elements of Matrix B
6. Ask user to choose operation:
o 1 for addition
o 2 for subtraction
o 3 for multiplication
7. If operation is addition or subtraction:
o Check if r1 == r2 and c1 == c2
o If yes:
▪ Loop through each element:
result[i][j] = A[i][j] ± B[i][j]
o Else:
▪ Display error: dimensions do not match
8. If operation is multiplication:
o Check if c1 == r2
o If yes:
▪ Loop: result[i][j] = sum(A[i][k] * B[k][j]) for k = 0 to c1-1
o Else:
▪ Display error: columns of A ≠ rows of B
9. Display the resulting matrix
10. End

PSEUDOCODE:

• BEGIN
• DECLARE matrices A[10][10], B[10][10], result[10][10]
• DECLARE integers r1, c1, r2, c2, i, j, k, choice
• PRINT "Enter rows and columns for Matrix A"
• READ r1, c1
• PRINT "Enter rows and columns for Matrix B"
• READ r2, c2
• PRINT "Enter elements of Matrix A"
• FOR i = 0 to r1 - 1 DO
CS25C02-PROGRAMMING IN C LABORATORY

• FOR j = 0 to c1 - 1 DO
o READ A[i][j]
• END FOR
• END FOR
• PRINT "Enter elements of Matrix B"
• FOR i = 0 to r2 - 1 DO
• FOR j = 0 to c2 - 1 DO
o READ B[i][j]
• END FOR
• END FOR
• PRINT "Enter choice: 1-Add, 2-Subtract, 3-Multiply"
• READ choice
• IF choice = 1 THEN
• IF r1 = r2 AND c1 = c2 THEN
o FOR i = 0 to r1 - 1 DO
o FOR j = 0 to c1 - 1 DO
▪ result[i][j] = A[i][j] + B[i][j]
o END FOR
o END FOR
o PRINT result matrix
• ELSE
o PRINT "Addition not possible"
• END IF
• ELSE IF choice = 2 THEN
• IF r1 = r2 AND c1 = c2 THEN
o FOR i = 0 to r1 - 1 DO
o FOR j = 0 to c1 - 1 DO
▪ result[i][j] = A[i][j] - B[i][j]
o END FOR
o END FOR
o PRINT result matrix
• ELSE
o PRINT "Subtraction not possible"
• END IF
• ELSE IF choice = 3 THEN
• IF c1 = r2 THEN
o FOR i = 0 to r1 - 1 DO
o FOR j = 0 to c2 - 1 DO
▪ result[i][j] = 0
▪ FOR k = 0 to c1 - 1 DO
▪ result[i][j] = result[i][j] + A[i][k] * B[k][j]
▪ END FOR
o END FOR
o END FOR
o PRINT result matrix
• ELSE
o PRINT "Multiplication not possible"
• END IF
• ELSE
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT "Invalid choice"


• END IF
• END

PROGRAM:

#include <stdio.h>

int main() {
int A[10][10], B[10][10], result[10][10];
int i, j, k, r1, c1, r2, c2;
int choice;

printf("Enter rows and columns of Matrix A: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of Matrix B: ");


scanf("%d %d", &r2, &c2);

// Input Matrix A
printf("Enter elements of Matrix A:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &A[i][j]);
}
}

// Input Matrix B
printf("Enter elements of Matrix B:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &B[i][j]);
}
}

printf("\nChoose operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n");
scanf("%d", &choice);

switch(choice) {
case 1:
if(r1 == r2 && c1 == c2) {
printf("\nResult of Matrix Addition:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
result[i][j] = A[i][j] + B[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
CS25C02-PROGRAMMING IN C LABORATORY

}
} else {
printf("Matrix addition not possible. Dimensions do not match.\n");
}
break;

case 2:
if(r1 == r2 && c1 == c2) {
printf("\nResult of Matrix Subtraction (A - B):\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
result[i][j] = A[i][j] - B[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
} else {
printf("Matrix subtraction not possible. Dimensions do not match.\n");
}
break;

case 3:
if(c1 == r2) {
printf("\nResult of Matrix Multiplication (A x B):\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
printf("%d ", result[i][j]);
}
printf("\n");
}
} else {
printf("Matrix multiplication not possible. Columns of A must equal rows of
B.\n");
}
break;

default:
printf("Invalid choice.\n");
}

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully reads two matrices from the user, performs addition,
subtraction, and multiplication operations, and displays the results.
CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:6
Date Strings

A. Write a program to perform various string manipulations using built-in


functions.

AIM:

To write a C Program to perform various string manipulations using built-in functions.


Algorithm:

ALGORITHM:

1. Start
2. Declare three character arrays: str1, str2, str3
3. Read the first string into str1
4. Read the second string into str2
5. Calculate the length of str1 using strlen() and display it
6. Copy str1 into str3 using strcpy()
7. Display the copied string str3
8. Concatenate str2 to str1 using strcat() and display the result
9. Compare str1 and str2 using strcmp():
o If result is 0: strings are equal
o Else: strings are not equal
10. Reverse str3 using strrev() and display it
11. End

PSEUDOCODE:

• BEGIN
• DECLARE str1[100], str2[100], str3[200]
• DECLARE length
• PRINT "Enter first string:"
• READ str1
• PRINT "Enter second string:"
• READ str2
• // String length
• SET length = strlen(str1)
• PRINT "Length of first string: ", length
• // String copy
• COPY str1 into str3 using strcpy()
• PRINT "Copied string (str3): ", str3
• // String concatenation
• CONCATENATE str2 to str1 using strcat()
• PRINT "Concatenated string: ", str1
• // String comparison
• IF strcmp(str1, str2) == 0 THEN
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT "Strings are equal"


• ELSE
• PRINT "Strings are not equal"
• END IF
• // String reverse
• REVERSE str3 using strrev()
• PRINT "Reversed copied string: ", str3
• END

PROGRAM:

#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100], str3[200];
int length;

// Input two strings


printf("Enter first string: ");
gets(str1); // Note: gets() is unsafe, prefer fgets() in real applications

printf("Enter second string: ");


gets(str2);

// 1. String Length
length = strlen(str1);
printf("Length of first string: %d\n", length);

// 2. String Copy
strcpy(str3, str1);
printf("Copied string into str3: %s\n", str3);

// 3. String Concatenation
strcat(str1, str2);
printf("After concatenation (str1 + str2): %s\n", str1);

// 4. String Comparison
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal.\n");
} else {
printf("str1 and str2 are not equal.\n");
}

// 5. String Reverse
strrev(str3); // reverse the copied string (str3)
printf("Reverse of copied string: %s\n", str3);

return 0;
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Enter first string: Hello


Enter second string: World
Length of first string: 5
Copied string into str3: Hello
After concatenation (str1 + str2): HelloWorld
str1 and str2 are not equal.
Reverse of copied string: olleH

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
CS25C02-PROGRAMMING IN C LABORATORY

B. Write a program to verify the given string is palindrome or not.

AIM:

To write a C Program to verify the given string is palindrome or not.

ALGORITHM:

1. Start
2. Declare two character arrays: str and rev
3. Read a string from the user into str
4. Find the length of the string using strlen()
5. Reverse the string manually by copying characters from str to rev in reverse order
6. Null-terminate the reversed string
7. Compare str and rev using strcmp():
o If equal, it is a palindrome
o Else, it is not a palindrome
8. Display the result
9. End

PSEUDOCODE

• BEGIN
• DECLARE str[100], rev[100]
• DECLARE length, i
• PRINT "Enter a string:"
• READ str
CS25C02-PROGRAMMING IN C LABORATORY

• SET length = strlen(str)


• FOR i = 0 to length - 1 DO
• rev[i] = str[length - i - 1]
• END FOR
• rev[length] = '\0' // Add null character at the end
• IF strcmp(str, rev) == 0 THEN
• PRINT "The string is a palindrome."
• ELSE
• PRINT "The string is NOT a palindrome."
• END IF
• END

PROGRAM:

#include <stdio.h>
#include <string.h>

int main() {
char str[100], rev[100];
int length, i, isPalindrome = 1;

// Input the string


printf("Enter a string: ");
gets(str); // Note: Use fgets(str, sizeof(str), stdin) in safer implementations

length = strlen(str);

// Create reversed string


for(i = 0; i < length; i++) {
rev[i] = str[length - i - 1];
}
rev[length] = '\0'; // Null-terminate the reversed string

// Check for palindrome


if(strcmp(str, rev) == 0) {
printf("The string is a palindrome.\n");
} else {
printf("The string is NOT a palindrome.\n");
}

return 0;
}

OUTPUT:

Input:

Enter a string: madam


CS25C02-PROGRAMMING IN C LABORATORY

Output:

The string is a palindrome.

Input:

Enter a string: hello

Output:

The string is NOT a palindrome.

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

C. Write a program to Check if two strings are anagrams of each other.

AIM:

To write a C Program to check if two strings are anagrams of each other.

ALGORITHM:

1. Start
2. Read two strings: str1 and str2
3. Convert both strings to lowercase to ensure case-insensitive comparison
4. If the lengths of str1 and str2 are not equal:
o Print "Strings are NOT anagrams" and stop
5. Sort both strings alphabetically
6. Compare the sorted versions:
o If they are equal: print "Strings are anagrams"
o Else: print "Strings are NOT anagrams"
7. End

PSEUDOCODE:

• BEGIN
• DECLARE str1[100], str2[100]
• PRINT "Enter first string:"
• READ str1
• PRINT "Enter second string:"
CS25C02-PROGRAMMING IN C LABORATORY

• READ str2
• // Convert both strings to lowercase
• FOR each character in str1
• CONVERT character to lowercase
• END FOR
• FOR each character in str2
• CONVERT character to lowercase
• END FOR
• IF length of str1 != length of str2 THEN
• PRINT "Strings are NOT anagrams"
• EXIT
• END IF
• // Sort both strings
• CALL sortString(str1)
• CALL sortString(str2)
• // Compare sorted strings
• IF str1 == str2 THEN
• PRINT "Strings are anagrams"
• ELSE
• PRINT "Strings are NOT anagrams"
• END IF
• END
• Procedure sortString(str):
• FOR i = 0 to length - 1
• FOR j = i + 1 to length
• IF str[i] > str[j]
o SWAP str[i] and str[j]
• END IF
• END FOR
• END FOR

PROGRAM:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void sortString(char str[]) {


int i, j;
char temp;
int len = strlen(str);
for(i = 0; i < len - 1; i++) {
for(j = i + 1; j < len; j++) {
if(str[i] > str[j]) {
// Swap characters
temp = str[i];
str[i] = str[j];
str[j] = temp;
CS25C02-PROGRAMMING IN C LABORATORY

}
}
}
}

int main() {
char str1[100], str2[100];

printf("Enter first string: ");


gets(str1); // Use fgets() for safety in real use

printf("Enter second string: ");


gets(str2);

// Convert to lowercase for case-insensitive comparison


for (int i = 0; str1[i]; i++)
str1[i] = tolower(str1[i]);
for (int i = 0; str2[i]; i++)
str2[i] = tolower(str2[i]);

// If lengths are not equal, not anagrams


if (strlen(str1) != strlen(str2)) {
printf("Strings are NOT anagrams.\n");
return 0;
}

// Sort both strings


sortString(str1);
sortString(str2);

// Compare sorted strings


if (strcmp(str1, str2) == 0)
printf("Strings are anagrams.\n");
else
printf("Strings are NOT anagrams.\n");

return 0;
}

OUTPUT:

Enter first string: listen


Enter second string: silent

OUTPUT:
Strings are anagrams.
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

D. Write a program to concatenate two strings using arrays.

AIM:

To write a C Program to concatenate two strings using arrays

ALGORITHM:

1. Start
2. Declare variables:
str1[100] → to store the first string
str2[100] → to store the second string
result[200] → to store the concatenated result
3. Integers i = 0, j = 0 for indexing
4. Prompt user to enter the first string.
5. Read the first string (str1).
6. Prompt user to enter the second string.
7. Read the second string (str2).
8. Copy characters from str1 into result:
9. Repeat while str1[i] is not '\0' (null character):
a. Assign result[i] = str1[i]
b. Increment i
10. Append characters from str2 to result:
11. Repeat while str2[j] is not '\0':
a. Assign result[i] = str2[j]
b. Increment both i and j
CS25C02-PROGRAMMING IN C LABORATORY

12. Null-terminate the concatenated string by setting result[i] = '\0'.


13. Display the concatenated string (result).
14. Stop

PROGRAM:

#include <stdio.h>
int main() {
char str1[100], str2[100], result[200];
int i = 0, j = 0;

printf("Enter first string: ");


gets(str1); // Use fgets() in real applications for safety

printf("Enter second string: ");


gets(str2);

// Copy str1 into result


while (str1[i] != '\0') {
result[i] = str1[i];
i++;
}

// Append str2 to result


while (str2[j] != '\0') {
result[i] = str2[j];
i++;
j++;
}

result[i] = '\0'; // Null-terminate the result string

printf("Concatenated string: %s\n", result);

return 0;
}

OUTPUT:

Input:

Enter first string: Hello


Enter second string: World

Output:

Concatenated string: HelloWorld


CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

E. Write a program to print the given strings in ascending order.

AIM:

To write a C Program to print the given strings in ascending order.

ALGORITHM:

1. Start
2. Declare a 2D array str[n][max_len] to hold n strings
3. Read the number of strings n
4. Loop to read n strings from the user
5. Use a nested loop to compare each pair of strings:
o For each i = 0 to n-2:
▪ For each j = i+1 to n-1:
▪ If str[i] > str[j] (i.e., comes later in dictionary order):
▪ Swap str[i] and str[j]
6. After sorting, loop through the array and print the sorted strings
7. End

PSEUDOCODE:

• BEGIN
• DECLARE str[n][max_len], temp[max_len]
CS25C02-PROGRAMMING IN C LABORATORY

• DECLARE i, j, n
• PRINT "Enter number of strings:"
• READ n
• FOR i ← 0 TO n-1 DO
• PRINT "Enter string ", i+1
• READ str[i]
• END FOR
• FOR i ← 0 TO n-2 DO
• FOR j ← i+1 TO n-1 DO
o IF str[i] > str[j] THEN
o temp ← str[i]
o str[i] ← str[j]
o str[j] ← temp
o END IF
• END FOR
• END FOR
• PRINT "Strings in ascending order:"
• FOR i ← 0 TO n-1 DO
• PRINT str[i]
• END FOR
• END

PROGRAM:

#include <stdio.h>
#include <string.h>

int main() {
char str[10][100], temp[100];
int i, j, n;

printf("Enter number of strings: ");


scanf("%d", &n);
getchar(); // Clear newline from input buffer

// Input strings
printf("Enter %d strings:\n", n);
for(i = 0; i < n; i++) {
fgets(str[i], sizeof(str[i]), stdin);
str[i][strcspn(str[i], "\n")] = '\0'; // Remove newline
}

// Sorting strings
for(i = 0; i < n-1; i++) {
for(j = i+1; j < n; j++) {
if(strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
CS25C02-PROGRAMMING IN C LABORATORY

strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}

// Output sorted strings


printf("\nStrings in ascending order:\n");
for(i = 0; i < n; i++) {
printf("%s\n", str[i]);
}

return 0;
}

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
CS25C02-PROGRAMMING IN C LABORATORY

Online Quiz/Viva 5
Total 20

RESULT:

Ex:No:7
Date Non recursive and recursive functions

A. Write an application to simulate basic calculator ( +, -, *, /) using functions.

AIM:

To write a C Program to simulate basic calculator ( +, -, *, /) using functions

ALGORITHM:

1. Start
2. Display calculator options: addition, subtraction, multiplication, division
3. Read the user's operation choice (1–4)
4. Read two float numbers num1 and num2
5. Use a switch-case or if-else to handle the operation:
o If choice is 1 → call add(num1, num2)
o If choice is 2 → call subtract(num1, num2)
o If choice is 3 → call multiply(num1, num2)
o If choice is 4:
▪ If num2 == 0, print error (cannot divide by zero)
▪ Else, call divide(num1, num2)
6. Print the result with the correct operator
7. End
CS25C02-PROGRAMMING IN C LABORATORY

PSEUDOCODE:

• BEGIN
• DECLARE float num1, num2, result
• DECLARE char operator
• DECLARE int choice
• DISPLAY "----- Basic Calculator -----"
• DISPLAY "1. Addition (+)"
• DISPLAY "2. Subtraction (-)"
• DISPLAY "3. Multiplication (*)"
• DISPLAY "4. Division (/)"
• PROMPT "Enter your choice (1-4): "
• READ choice
• PROMPT "Enter two numbers: "
• READ num1, num2
• SWITCH choice
• CASE 1:
o result ← add(num1, num2)
o operator ← '+'
o BREAK
• CASE 2:
o result ← subtract(num1, num2)
o operator ← '-'
o BREAK
• CASE 3:
o result ← multiply(num1, num2)
o operator ← '*'
o BREAK
• CASE 4:
o IF num2 = 0 THEN
o DISPLAY "Error: Cannot divide by zero"
o EXIT
o ELSE
o result ← divide(num1, num2)
o operator ← '/'
o END IF
o BREAK
• DEFAULT:
o DISPLAY "Invalid choice"
o EXIT
• END SWITCH
• DISPLAY "Result: num1 operator num2 = result"
• END
• FUNCTION add(a, b)
• RETURN a + b
• END FUNCTION
• FUNCTION subtract(a, b)
• RETURN a - b
CS25C02-PROGRAMMING IN C LABORATORY

• END FUNCTION
• FUNCTION multiply(a, b)
• RETURN a * b
• END FUNCTION
• FUNCTION divide(a, b)
• RETURN a / b
• END FUNCTION

PROGRAM:

#include <stdio.h>

// Function declarations
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);

int main() {
float num1, num2, result;
char operator;
int choice;

printf("----- Basic Calculator -----\n");


printf("Operations:\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");

printf("\nEnter your choice (1-4): ");


scanf("%d", &choice);

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

switch(choice) {
case 1:
result = add(num1, num2);
operator = '+';
break;
case 2:
result = subtract(num1, num2);
operator = '-';
break;
case 3:
result = multiply(num1, num2);
operator = '*';
break;
case 4:
CS25C02-PROGRAMMING IN C LABORATORY

if (num2 == 0) {
printf("Error: Cannot divide by zero.\n");
return 1;
}
result = divide(num1, num2);
operator = '/';
break;
default:
printf("Invalid choice.\n");
return 1;
}

printf("Result: %.2f %c %.2f = %.2f\n", num1, operator, num2, result);


return 0;
}

// Function definitions
float add(float a, float b) {
return a + b;
}

float subtract(float a, float b) {


return a - b;
}

float multiply(float a, float b) {


return a * b;
}

float divide(float a, float b) {


return a / b;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

B. Write a program to find nth Fibonacci number using recursive and non-recursive
number.

AIM:

To write a C program to find nth Fibonacci number using recursive and non-recursive
number

ALGORITHM:

1. Start
2. Read integer n from the user
CS25C02-PROGRAMMING IN C LABORATORY

3. Call the recursive function:


o If n == 0, return 0
o If n == 1, return 1
o Else, return fibonacci(n - 1) + fibonacci(n - 2)
4. Call the iterative function:
o Initialize a = 0, b = 1
o If n == 0, return a
o Loop from 2 to n:
▪ next = a + b
▪ Update a = b, b = next
o Return b
5. Print both results
6. End

PSEUDOCODE:

• FUNCTION fibonacci_recursive(n)
• IF n == 0 THEN
• RETURN 0
• ELSE IF n == 1 THEN
• RETURN 1
• ELSE
• RETURN fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
• END IF
• END FUNCTION
• FUNCTION fibonacci_iterative(n)
• SET a ← 0
• SET b ← 1
• IF n == 0 THEN
• RETURN a
• FOR i ← 2 TO n DO
• next ← a + b
• a←b
• b ← next
• END FOR
• RETURN b
• END FUNCTION

• BEGIN MAIN
• DECLARE n
• PROMPT "Enter the value of n: "
• READ n
• result_recursive ← fibonacci_recursive(n)
• result_iterative ← fibonacci_iterative(n)
• DISPLAY "Using Recursion: Fibonacci(n) = ", result_recursive
• DISPLAY "Using Iteration: Fibonacci(n) = ", result_iterative
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

// Recursive function to calculate nth Fibonacci number


int fibonacci_recursive(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
}

// Iterative function to calculate nth Fibonacci number


int fibonacci_iterative(int n) {
int a = 0, b = 1, next, i;
if (n == 0) return a;
for (i = 2; i <= n; i++) {
next = a + b;
a = b;
b = next;
}
return b;
}

int main() {
int n;

printf("Enter the value of n: ");


scanf("%d", &n);

printf("\nUsing Recursion: Fibonacci(%d) = %d\n", n, fibonacci_recursive(n));


printf("Using Iteration: Fibonacci(%d) = %d\n", n, fibonacci_iterative(n));

return 0;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

C. Write a program to find factorial of a number using recursive and non-


recursive number.

AIM:

To write a C program to find factorial of a number using recursive and non-recursive


number.

ALGORITHM:
CS25C02-PROGRAMMING IN C LABORATORY

1. Start
2. Declare a variable n to store the input number.
3. Prompt the user to enter a number.
4. Read the input and store it in n.
5. If n is negative:
o Display "Factorial is not defined for negative numbers."
6. Else:
o Call the recursive factorial function and store the result.
o Call the iterative factorial function and store the result.
o Display both results.
7. End

PSEUDOCODE:

• FUNCTION factorial_recursive(n):
• IF n == 0 OR n == 1 THEN
• RETURN 1
• ELSE
• RETURN n * factorial_recursive(n - 1)
• ENDIF
• END FUNCTION
• FUNCTION factorial_iterative(n):
• SET fact ← 1
• FOR i ← 2 TO n DO
• fact ← fact × i
• ENDFOR
• RETURN fact
• END FUNCTION
• BEGIN MAIN
• DECLARE n, result_rec, result_iter AS INTEGER or LONG
• PRINT "Enter a number:"
• READ n
• IF n < 0 THEN
• PRINT "Factorial is not defined for negative numbers."
• ELSE
• SET result_rec ← factorial_recursive(n)
• SET result_iter ← factorial_iterative(n)

• PRINT "Using Recursion: ", n, "! = ", result_rec


• PRINT "Using Iteration: ", n, "! = ", result_iter
• ENDIF
• END MAIN

PROGRAM:

#include <stdio.h>

// Recursive function to find factorial


CS25C02-PROGRAMMING IN C LABORATORY

long long factorial_recursive(int n) {


if (n == 0 || n == 1)
return 1;
else
return n * factorial_recursive(n - 1);
}

// Iterative function to find factorial


long long factorial_iterative(int n) {
long long fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;
}

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

if (n < 0)
printf("Factorial is not defined for negative numbers.\n");
else {
printf("\nUsing Recursion: %d! = %lld\n", n, factorial_recursive(n));
printf("Using Iteration: %d! = %lld\n", n, factorial_iterative(n));
}

return 0;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

D. Write a program to swap two numbers using Call by Value and Call by Reference.
CS25C02-PROGRAMMING IN C LABORATORY

AIM:

To write a C program to swap two numbers using Call by Value and Call by
Reference.

ALGORITHM:

1. Start
2. Declare two integer variables x and y.
3. Prompt the user to enter two numbers.
4. Read the input values into x and y.
5. Display the values before swapping.
6. Call swapByValue(x, y) to attempt swapping by value.
7. Display the values after swapByValue() (they remain unchanged).
8. Call swapByReference(&x, &y) to swap by reference.
9. Display the values after swapByReference() (they are swapped).
10. End

PSEUDOCODE:

• FUNCTION swapByValue(a, b)
• temp ← a
• a←b
• b ← temp
• PRINT "[Inside swapByValue] a = ", a, ", b = ", b
• END FUNCTION
• FUNCTION swapByReference(a_ref, b_ref)
• temp ← value at a_ref
• value at a_ref ← value at b_ref
• value at b_ref ← temp
• PRINT "[Inside swapByReference] a = ", value at a_ref, ", b = ", value at b_ref
• END FUNCTION
• BEGIN MAIN
• DECLARE x, y AS INTEGER
• PRINT "Enter two numbers:"
• READ x, y
• PRINT "Before swapping: x = ", x, ", y = ", y
• CALL swapByValue(x, y)
• PRINT "After swapByValue: x = ", x, ", y = ", y
• CALL swapByReference(address of x, address of y)
• PRINT "After swapByReference: x = ", x, ", y = ", y
• END MAIN

PROGRAM:
CS25C02-PROGRAMMING IN C LABORATORY

#include <stdio.h>

// Function to swap using Call by Value


void swapByValue(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
printf("\n[Inside swapByValue] a = %d, b = %d", a, b);
}

// Function to swap using Call by Reference


void swapByReference(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("\n[Inside swapByReference] a = %d, b = %d", *a, *b);
}

int main() {
int x, y;

printf("Enter two numbers: ");


scanf("%d %d", &x, &y);

printf("\nBefore swapping (Original): x = %d, y = %d", x, y);

// Call by Value
swapByValue(x, y);
printf("\nAfter swapByValue (No change): x = %d, y = %d", x, y);

// Call by Reference
swapByReference(&x, &y);
printf("\nAfter swapByReference (Swapped): x = %d, y = %d\n", x, y);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT

Ex:No:8
CS25C02-PROGRAMMING IN C LABORATORY

Date Pointers

A. Write a program to illustrate Pointers to array, strings, Pointers to Pointers, Array


of Pointers

AIM:

• Description:
• Pointer to Array:
int *p_arr = arr; allows access to each array element via pointer arithmetic.
• Pointer to String:
char *str = "Hello"; points to the first character of a string literal.
• Pointer to Pointer:
int **pptr = &ptr; points to a pointer variable that in turn points to an integer.
• Array of Pointers:
const char *names[] = {...}; is an array where each element is a pointer to a string.

1. Pointer to Array

ALGORITHM:

1. Start
2. Declare an integer array arr of size 5.
3. Initialize the array with 5 integers.
4. Declare a pointer p_arr and assign it the address of arr.
5. Use a loop to access array elements using pointer arithmetic.
6. Print values using both arr[i] and *(p_arr + i).
7. End

PSEUDOCODE:

• BEGIN
• DECLARE arr[5] ← {10, 20, 30, 40, 50}
• DECLARE p_arr AS pointer TO integer ← arr
• FOR i FROM 0 TO 4 DO
• PRINT arr[i], *(p_arr + i)
• ENDFOR
• END

2. Pointer to String

ALGORITHM:

1. Declare a character pointer str and assign a string literal.


2. Print the full string using %s.
3. Use a loop to print each character using pointer arithmetic.
4. End

PSEUDOCODE:
CS25C02-PROGRAMMING IN C LABORATORY

• BEGIN
• DECLARE str AS pointer TO char ← "Hello, World!"
• PRINT str
• FOR i FROM 0 TO length of str DO
• PRINT *(str + i)
• ENDFOR
• END

3. Pointer to Pointer:

ALGORITHM:

1. Declare an integer variable x and assign a value.


2. Declare a pointer ptr and assign the address of x.
3. Declare a double pointer pptr and assign the address of ptr.
4. Print the value of x using:
o x
o *ptr
o **pptr
5. End

PSEUDOCODE :

• BEGIN
• DECLARE x AS INTEGER ← 100
• DECLARE ptr AS pointer TO INTEGER ← address of x
• DECLARE pptr AS pointer TO pointer TO INTEGER ← address of ptr
• PRINT x, *ptr, **pptr
• END

4. Array of Pointers (Strings)

ALGORITHM:

1. Declare an array names of character pointers and initialize with string literals.
2. Use a loop to iterate through the array.
3. Print each string.
4. End

PSEUDOCODE:

• BEGIN
• DECLARE names[4] ← {"Alice", "Bob", "Charlie", "Diana"}
• FOR i FROM 0 TO 3 DO
• PRINT names[i]
• ENDFOR
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

int main() {
// 1. Pointer to Array
int arr[5] = {10, 20, 30, 40, 50};
int *p_arr = arr; // Pointer to array

printf("1. Pointer to Array:\n");


for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d, *(p_arr + %d) = %d\n", i, arr[i], i, *(p_arr + i));
}

// 2. Pointer to String
char *str = "Hello, World!";

printf("\n2. Pointer to String:\n");


printf("String: %s\n", str);
for (int i = 0; str[i] != '\0'; i++) {
printf("Character %d: %c\n", i, *(str + i));
}

// 3. Pointer to Pointer
int x = 100;
int *ptr = &x;
int **pptr = &ptr;

printf("\n3. Pointer to Pointer:\n");


printf("Value of x: %d\n", x);
printf("Value using *ptr: %d\n", *ptr);
printf("Value using **pptr: %d\n", **pptr);

// 4. Array of Pointers
const char *names[] = {"Alice", "Bob", "Charlie", "Diana"};

printf("\n4. Array of Pointers (Strings):\n");


for (int i = 0; i < 4; i++) {
printf("names[%d] = %s\n", i, names[i]);
}

return 0;
}

OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
CS25C02-PROGRAMMING IN C LABORATORY

B. Write a program to sort an array using pointers.

AIM:

To write a C program to sort an array using pointers.

ALGORITHM:

1. Start
2. Declare an array arr and an integer n for number of elements.
3. Prompt the user to enter the number of elements n.
4. Read n elements into the array using a loop.
5. Pass the base address of the array (arr) and the size (n) to the sorting function.
6. In the sorting function:
o Use two nested loops (i, j) to compare elements.
o Use pointer arithmetic: *(arr + i) and *(arr + j) to access elements.
o If *(arr + i) > *(arr + j), swap the two values using a temporary variable.
7. After sorting, print the array using pointer access.
8. End

PSEUDOCODE:

• FUNCTION sortArray(arr, size)


• FOR i FROM 0 TO size - 2 DO
• FOR j FROM i + 1 TO size - 1 DO
o IF *(arr + i) > *(arr + j) THEN
o temp ← *(arr + i)
o *(arr + i) ← *(arr + j)
o *(arr + j) ← temp
o ENDIF
• ENDFOR
• ENDFOR
• END FUNCTION
• BEGIN MAIN
• DECLARE arr[100],
• PRINT "Enter the number of elements:"
• READ n
• PRINT "Enter n integers:"
• FOR i FROM 0 TO n - 1 DO
• READ arr[i]
• ENDFOR
• CALL sortArray(arr, n)
• PRINT "Sorted array:"
• FOR i FROM 0 TO n - 1 DO
• PRINT *(arr + i)
• ENDFOR
• END MAIN
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>

void sortArray(int *arr, int size) {


int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
// Use pointer arithmetic to compare and swap
if (*(arr + i) > *(arr + j)) {
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}

int main() {
int arr[100], n;

printf("Enter number of elements: ");


scanf("%d", &n);

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


for (int i = 0; i < n; i++) {
scanf("%d", arr + i); // same as &arr[i]
}

sortArray(arr, n);

printf("\nSorted array in ascending order:\n");


for (int i = 0; i < n; i++) {
printf("%d ", *(arr + i));
}
printf("\n");

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

II method:

C Program: Sort an Array Using Pointers

#include <stdio.h>

// Function to sort array using pointers


void sortArray(int *arr, int size) {
int i, j, temp;

for (i = 0; i < size - 1; i++) {


for (j = i + 1; j < size; j++) {
if (*(arr + i) > *(arr + j)) {
// Swap using pointers
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}

int main() {
int arr[100], n;

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


scanf("%d", &n);

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


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

sortArray(arr, n);
CS25C02-PROGRAMMING IN C LABORATORY

printf("Sorted array:\n");
for (int i = 0; i < n; i++)
printf("%d ", *(arr + i));
printf("\n");

return 0;
}

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
CS25C02-PROGRAMMING IN C LABORATORY

C. Write a program to perform matrix multiplication using pointers

AIM:

To write a C program to perform matrix multiplication using pointers.

ALGORITHM:

1. Start
2. Declare 2D arrays A, B, and Result.
3. Read number of rows and columns for matrix A: r1, c1.
4. Read number of rows and columns for matrix B: r2, c2.
5. If c1 != r2, print error and terminate.
6. Else:
o Read elements of Matrix A using pointer arithmetic.
o Read elements of Matrix B using pointer arithmetic.
7. Perform matrix multiplication:
o For each row i in A and column j in B:
▪ Initialize Result[i][j] to 0
▪ For k = 0 to c1 - 1:
▪ Multiply A[i][k] * B[k][j] and add to Result[i][j]
8. Print the resulting matrix using pointer arithmetic.
9. End

PSEUDOCODE:

• FUNCTION readMatrix(mat, rows, cols)


• FOR i FROM 0 TO rows - 1 DO
• FOR j FROM 0 TO cols - 1 DO
o READ *(mat + i * cols + j)
• ENDFOR
• ENDFOR
• END FUNCTION
• FUNCTION printMatrix(mat, rows, cols)
• FOR i FROM 0 TO rows - 1 DO
• FOR j FROM 0 TO cols - 1 DO
o PRINT *(mat + i * cols + j)
• ENDFOR
• PRINT newline
• ENDFOR
• END FUNCTION
• FUNCTION multiplyMatrices(a, b, result, r1, c1, c2)
• FOR i FROM 0 TO r1 - 1 DO
• FOR j FROM 0 TO c2 - 1 DO
o *(result + i * c2 + j) ← 0
o FOR k FROM 0 TO c1 - 1 DO
o *(result + i * c2 + j) ← *(result + i * c2 + j) +
• (*(a + i * c1 + k)) * (*(b + k * c2 + j))
o ENDFOR
CS25C02-PROGRAMMING IN C LABORATORY

• ENDFOR
• ENDFOR
• END FUNCTION
• BEGIN MAIN
• DECLARE matrices a, b, result
• READ r1, c1, r2, c2
• IF c1 ≠ r2 THEN
• PRINT "Matrix multiplication not possible"
• EXIT
• ENDIF
• PRINT "Enter matrix A"
• CALL readMatrix(a, r1, c1)
• PRINT "Enter matrix B"
• CALL readMatrix(b, r2, c2)
• CALL multiplyMatrices(a, b, result, r1, c1, c2)
• PRINT "Result matrix:"
• CALL printMatrix(result, r1, c2)
• END MAIN

PROGRAM:

#include <stdio.h>

#define MAX 10

void multiplyMatrices(int *a, int *b, int *result, int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
*(result + i * c2 + j) = 0;
for (int k = 0; k < c1; k++) {
*(result + i * c2 + j) += (*(a + i * c1 + k)) * (*(b + k * c2 + j));
}
}
}
}

void readMatrix(int *mat, int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", (mat + i * cols + j));
}
}
}

void printMatrix(int *mat, int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", *(mat + i * cols + j));
CS25C02-PROGRAMMING IN C LABORATORY

}
printf("\n");
}
}

int main() {
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];
int r1, c1, r2, c2;

printf("Enter rows and columns for Matrix A: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and columns for Matrix B: ");


scanf("%d %d", &r2, &c2);

if (c1 != r2) {
printf("Matrix multiplication not possible. Columns of A must equal rows of B.\n");
return 1;
}

printf("\nEnter elements of Matrix A:\n");


readMatrix((int *)a, r1, c1);

printf("\nEnter elements of Matrix B:\n");


readMatrix((int *)b, r2, c2);

multiplyMatrices((int *)a, (int *)b, (int *)result, r1, c1, c2);

printf("\nResultant Matrix after multiplication:\n");


printMatrix((int *)result, r1, c2);

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT
CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:9
Date Structures and Union

A. Write a program to create structure for an account holder in a bank with following
Fields: name, account number, address, balance and display the details of ‘n’ account
holders.

AIM:

To write a C program to create structure for an account holder in a bank with following
Fields: name, account number, address, balance and display the details of ‘n’ account
holders.

ALGORITHM:

1. Start
2. Declare a structure AccountHolder with fields:
o name (string)
o accountNumber (integer)
o address (string)
o balance (float)
3. Prompt the user to enter the number of account holders n.
4. Create an array of AccountHolder structures of size n.
5. For each account holder i from 0 to n-1:
o Prompt and read name, accountNumber, address, and balance.
o Use fgets() to read strings (to allow spaces).
6. After input, display the details for each account holder:
o Print name, accountNumber, address, balance for each i.
7. End

PSEUDOCODE:

• DEFINE STRUCTURE AccountHolder


• name: STRING
• accountNumber: INTEGER
• address: STRING
• balance: FLOAT
• END STRUCTURE
• BEGIN MAIN
• DECLARE n AS INTEGER
• PRINT "Enter number of account holders:"
• READ n
• DECLARE holders[n] AS ARRAY OF AccountHolder
• FOR i FROM 0 TO n - 1 DO
• PRINT "Enter details for Account Holder (i + 1):"
• PRINT "Name:"
• READ holders[i].name
CS25C02-PROGRAMMING IN C LABORATORY

• PRINT "Account Number:"


• READ holders[i].accountNumber
• PRINT "Address:"
• READ holders[i].address
• PRINT "Balance:"
• READ holders[i].balance
• ENDFOR
• PRINT "Display Account Holder Details:"
• FOR i FROM 0 TO n - 1 DO
• PRINT "Account Holder (i + 1):"
• PRINT holders[i].name
• PRINT holders[i].accountNumber
• PRINT holders[i].address
• PRINT holders[i].balance
• ENDFOR
• END MAIN

PROGRAM:

#include <stdio.h>

// Structure definition
struct AccountHolder {
char name[50];
int accountNumber;
char address[100];
float balance;
};

int main() {
int n;

printf("Enter the number of account holders: ");


scanf("%d", &n);

struct AccountHolder holders[n]; // Array of structures

// Input account holder details


for (int i = 0; i < n; i++) {
printf("\n--- Enter details for Account Holder %d ---\n", i + 1);

printf("Name: ");
getchar(); // To consume the leftover newline
fgets(holders[i].name, sizeof(holders[i].name), stdin);

printf("Account Number: ");


scanf("%d", &holders[i].accountNumber);
CS25C02-PROGRAMMING IN C LABORATORY

printf("Address: ");
getchar(); // Again consume newline
fgets(holders[i].address, sizeof(holders[i].address), stdin);

printf("Balance: ");
scanf("%f", &holders[i].balance);
}

// Display account holder details


printf("\n================= Account Holder Details =================\n");
for (int i = 0; i < n; i++) {
printf("\nAccount Holder %d:\n", i + 1);
printf("Name: %s", holders[i].name);
printf("Account Number: %d\n", holders[i].accountNumber);
printf("Address: %s", holders[i].address);
printf("Balance: %.2f\n", holders[i].balance);
}

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

Output:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
CS25C02-PROGRAMMING IN C LABORATORY

B. Write a program to find total marks of individual student and average marks for
‘n’ students using structures.

AIM:

To write a C program to find total marks of individual student and average marks for ‘n’
students using structures.

ALGORITHM:

1. Start
2. Define a structure Student with the following fields:
o name (string)
o rollNumber (integer)
o marks (array of integers for 5 subjects)
o total (integer)
o average (float)
3. Read the number of students n.
4. Declare an array of Student structures of size n.
5. For each student i from 0 to n-1:
o Read name and rollNumber
o Initialize total to 0
o For each subject j from 0 to 4:
▪ Read marks[j]
▪ Add marks[j] to total
o Calculate average = total / 5.0
6. For each student i from 0 to n-1, display:
o name, rollNumber, total, average
7. End

PSEUDOCODE:

• DEFINE STRUCTURE Student


• name: STRING
• rollNumber: INTEGER
• marks[5]: ARRAY OF INTEGER
• total: INTEGER
• average: FLOAT
• END STRUCTURE
• BEGIN MAIN
• DECLARE n AS INTEGER
• PRINT "Enter number of students:"
• READ n
• DECLARE students[n] AS ARRAY OF Student
• FOR i FROM 0 TO n - 1 DO
o PRINT "Enter name of student i+1:"
o READ students[i].name
CS25C02-PROGRAMMING IN C LABORATORY

o PRINT "Enter roll number:"


o READ students[i].rollNumber

o SET students[i].total = 0

o FOR j FROM 0 TO 4 DO
▪ PRINT "Enter marks for subject j+1:"
▪ READ students[i].marks[j]
▪ students[i].total = students[i].total + students[i].marks[j]
o END FOR

o students[i].average = students[i].total / 5.0


• END FOR
• PRINT "Student Report"
• FOR i FROM 0 TO n - 1 DO
o PRINT "Name: ", students[i].name
o PRINT "Roll Number: ", students[i].rollNumber
o PRINT "Total Marks: ", students[i].total
o PRINT "Average Marks: ", students[i].average
• END FOR
• END MAIN

PROGRAM:

#include <stdio.h>

#define MAX_SUBJECTS 5

// Structure definition for a student


struct Student {
char name[50];
int rollNumber;
int marks[MAX_SUBJECTS];
int total;
float average;
};

int main() {
int n;

printf("Enter the number of students: ");


scanf("%d", &n);

struct Student students[n];

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


printf("\n--- Enter details for Student %d ---\n", i + 1);

printf("Name: ");
getchar(); // Clear newline buffer
CS25C02-PROGRAMMING IN C LABORATORY

fgets(students[i].name, sizeof(students[i].name), stdin);

printf("Roll Number: ");


scanf("%d", &students[i].rollNumber);

students[i].total = 0;

printf("Enter marks for %d subjects:\n", MAX_SUBJECTS);


for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
students[i].total += students[i].marks[j];
}

students[i].average = students[i].total / (float)MAX_SUBJECTS;


}

// Display student-wise total and average


printf("\n================= Student Report =================\n");
for (int i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name: %s", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Total Marks: %d\n", students[i].total);
printf("Average Marks: %.2f\n", students[i].average);
}

return 0;
}

Output:
CS25C02-PROGRAMMING IN C LABORATORY

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:

Thus the program successfully reads marks of n students, calculates the total marks for
each, and computes the average marks of all students using structures.
CS25C02-PROGRAMMING IN C LABORATORY

c. Write a program to illustrate the functions of union

AIM:

To write a C program to illustrate the functions of union.

ALGORITHM:

1. Start
2. Define a union called Data with:
o an integer i
o a float f
o a string str (array of characters)
3. Declare a variable data of type union Data.
4. Assign a value to data.i and print it.
5. Assign a value to data.f and:
o Print data.f
o Print data.i (observe corruption due to memory sharing)
6. Assign a value to data.str and:
o Print data.str
o Print data.f (observe corruption again)
7. End

PSEUDOCODE:

• DEFINE union Data


• INTEGER i
• FLOAT f
• CHARACTER ARRAY str[20]
• END UNION
• BEGIN
• DECLARE data AS union Data
• // Assign integer value
• SET data.i = 10
• PRINT "data.i = ", data.i
• // Assign float value (overwrites previous)
• SET data.f = 220.5
• PRINT "data.f = ", data.f
• PRINT "data.i (may be corrupted) = ", data.i
• // Assign string (overwrites again)
• COPY "Hello" TO data.str
• PRINT "data.str = ", data.str
• PRINT "data.f (may be corrupted) = ", data.f
• END
CS25C02-PROGRAMMING IN C LABORATORY

PROGRAM:

#include <stdio.h>
#include <string.h>

// Define a union
union Data {
int i;
float f;
char str[20];
};

int main() {
union Data data;

// Store integer
data.i = 10;
printf("After assigning integer:\n");
printf("data.i = %d\n", data.i);

// Store float (overwrites previous value)


data.f = 220.5;
printf("\nAfter assigning float:\n");
printf("data.f = %.2f\n", data.f);
printf("data.i (may be corrupted) = %d\n", data.i); // value of i is overwritten

// Store string (overwrites again)


strcpy(data.str, "Hello");
printf("\nAfter assigning string:\n");
printf("data.str = %s\n", data.str);
printf("data.f (may be corrupted) = %.2f\n", data.f); // value of f is overwritten

return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY

OUTPUT:

Rubrics

RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20

RESULT:
Thus the program successfully demonstrates the behavior of a union by storing and accessing
different members, showing that all members share the same memory location.
CS25C02-PROGRAMMING IN C LABORATORY

Ex:No:10 Mini Project


Date

Basic-Level Projects

1. Student Marks Record System


2. Simple Calculator using Switch Case
3. Electricity Bill Calculator
4. Library Management System
5. Bank Management System
6. Phone Book Application
7. Voting System
8. Temperature Converter
9. Unit Converter (length, weight, etc.)
10. Calendar Generator

Game-Based Projects

1 Tic-Tac-Toe Game
2 Snake Game (using graphics.h)
3 Number Guessing Game
4 Quiz Game
5 Hangman Game

File Handling Projects

1 Employee Record System using Files


2 Inventory Management System
3 Student Database using File Handling
4 Payroll System
5 Contact Management System

Data Structure-Based Projects

1 Stack Implementation for Expression Evaluation


2 Queue Simulation (Banking System)
3 Linked List-based Phonebook
4 Binary Search Tree Operations
5 Hospital Management using Linked Lists

Logical/Algorithmic Projects

1 Sudoku Solver
2 Maze Solver
3 Prime Number Analyzer
4 Password Generator
5 Sorting Algorithm Visualizer (text-based)
CS25C02-PROGRAMMING IN C LABORATORY

RUBRICS Marks
Problem Understanding & Objectives 3
Program Logic & Design 5
Implementation &Code Quality 7
Output & Functionality 5
Documentation & Report 3
Presentation &Viva 2
Total 25

You might also like