CLab Manual Final B
CLab Manual Final B
(Autonomous)
LAB MANUAL
CS25C02-PROGRAMMING IN C LABORATORY
Prepared by Verified by
INDEX
C) Write a Program to print different data types in ‘C’ and their ranges.
2. Operators
A) Write a Program to demonstrate arithmetic, logical and relational
operators.
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.
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.
500-600 8.55
601-800 9.65
800-1000 10.70
>1000 11.80
C) Write a program to count all subsets of given array with sum equal to
given sum.
E)Write a program to sort the given elements using bubble sort algorithm.
CS25C02-PROGRAMMING IN C LABORATORY
6. Strings
A)Write a program to perform various string manipulations using built-in
functions.
B) Write a program to find nth Fibonacci 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
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:
FLOWCHART
Start
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:
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:
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:
ALGORITHM:
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
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:
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);
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:
ALGORITHM:
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';
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:
AIM:
ALGORITHM:
• Compute a + b (Addition)
• Compute a - b (Subtraction)
• Compute a * b (Multiplication)
• Compute a / b (Division)
• Compute a % b (Modulus)
• Print all arithmetic results
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:
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:
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;
// Calculate area
area = PI * radius * radius;
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.
AIM:
ALGORITHM:
1. Start
2. Declare variables:
• principal (P)
• rate (R)
• time (T)
• simpleInterest (SI)
PROGRAM:
#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest;
// Input
printf("Enter the principal amount: ");
scanf("%f", &principal);
// 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
AIM:
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;
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:
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:
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;
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);
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
AIM:
DESCRIPTION:
ax^2 + bx + c = 0
D = b^2 - 4ac
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;
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.
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
PROGRAM:
#include <stdio.h>
int main() {
int startReading, endReading, units;
float billAmount;
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
AIM:
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;
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;
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
AIM:
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;
if (num < 0)
num = -num;
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
AIM:
DESCRIPTION:
• 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
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);
// 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
AIM:
ALGORITHM:
1. Start
2. Read start and end (the range values from the user)
3. Loop from i = start to i = end
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
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
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
AIM:
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);
return 0;
}
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);
return 0;
}
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);
return 0;
}
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);
return 0;
}
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);
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);
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;
}
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);
return 0;
}
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);
return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY
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);
}
printf("\n");
}
CS25C02-PROGRAMMING IN C LABORATORY
return 0;
}
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;
}
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;
}
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);
return 0;
CS25C02-PROGRAMMING IN C LABORATORY
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);
printf("\n");
}
return 0;
}
OUTPUT:
*
* *
* * *
* * * *
* * * * *
Full Pyramid
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * *
* * * *
* * *
* *
*
* * * * *
* * * *
* * *
* *
*
CS25C02-PROGRAMMING IN C LABORATORY
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Rhombus Pattern (n = 5)
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Diamond Pattern (n = 5)
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Hourglass Pattern (n = 5)
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * *
* *
* *
* *
* * * * *
*
CS25C02-PROGRAMMING IN C LABORATORY
* *
* *
* *
* * * * * * * * *
* * * * * * * * *
* *
* *
* *
*
*
* *
* *
* *
* *
* *
* *
* *
*
* * * * * * * * *
* *
* *
* *
*
* *
* *
* *
* * * * * * * * *
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]);
}
// 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;
// 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:
PROGRAM:
#include <stdio.h>
int main() {
int arr[100], n, target, 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 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
AIM:
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;
// 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;
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
AIM:
ALGORITHM:
Input:
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
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;
// 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
AIM:
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
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[200];
int length;
// 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:
Rubrics
RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
RESULT:
CS25C02-PROGRAMMING IN C LABORATORY
AIM:
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
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int length, i, isPalindrome = 1;
length = strlen(str);
return 0;
}
OUTPUT:
Input:
Output:
Input:
Output:
Rubrics
RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
RESULT:
AIM:
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>
}
}
}
}
int main() {
char str1[100], str2[100];
return 0;
}
OUTPUT:
OUTPUT:
Strings are anagrams.
CS25C02-PROGRAMMING IN C LABORATORY
Rubrics
RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
RESULT:
AIM:
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
PROGRAM:
#include <stdio.h>
int main() {
char str1[100], str2[100], result[200];
int i = 0, j = 0;
return 0;
}
OUTPUT:
Input:
Output:
Rubrics
RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
RESULT:
AIM:
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;
// 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);
}
}
}
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
AIM:
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;
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;
}
// Function definitions
float add(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
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>
int main() {
int n;
return 0;
}
OUTPUT:
CS25C02-PROGRAMMING IN C LABORATORY
Rubrics
RUBRICS MARKS
Code 10
Output 5
Online Quiz/Viva 5
Total 20
RESULT:
AIM:
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)
PROGRAM:
#include <stdio.h>
int main() {
int 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>
int main() {
int 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
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:
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:
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
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
// 2. Pointer to String
char *str = "Hello, World!";
// 3. Pointer to Pointer
int x = 100;
int *ptr = &x;
int **pptr = &ptr;
// 4. Array of Pointers
const char *names[] = {"Alice", "Bob", "Charlie", "Diana"};
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
AIM:
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:
PROGRAM:
#include <stdio.h>
int main() {
int arr[100], n;
sortArray(arr, n);
return 0;
}
CS25C02-PROGRAMMING IN C LABORATORY
II method:
#include <stdio.h>
int main() {
int arr[100], n;
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
AIM:
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:
• 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));
}
}
}
}
}
printf("\n");
}
}
int main() {
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];
int r1, c1, r2, c2;
if (c1 != r2) {
printf("Matrix multiplication not possible. Columns of A must equal rows of B.\n");
return 1;
}
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:
PROGRAM:
#include <stdio.h>
// Structure definition
struct AccountHolder {
char name[50];
int accountNumber;
char address[100];
float balance;
};
int main() {
int n;
printf("Name: ");
getchar(); // To consume the leftover newline
fgets(holders[i].name, sizeof(holders[i].name), stdin);
printf("Address: ");
getchar(); // Again consume newline
fgets(holders[i].address, sizeof(holders[i].address), stdin);
printf("Balance: ");
scanf("%f", &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:
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
PROGRAM:
#include <stdio.h>
#define MAX_SUBJECTS 5
int main() {
int n;
printf("Name: ");
getchar(); // Clear newline buffer
CS25C02-PROGRAMMING IN C LABORATORY
students[i].total = 0;
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
AIM:
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:
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);
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
Basic-Level Projects
Game-Based Projects
1 Tic-Tac-Toe Game
2 Snake Game (using graphics.h)
3 Number Guessing Game
4 Quiz Game
5 Hangman Game
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