U23CS351-Problem Solving Using C Laboratory
U23CS351-Problem Solving Using C Laboratory
AIM
ALGORITHM
STEP 3. Loop:
FLOWCHART
RESULT
Thus the flow chart for range of datatypes has been displayed.
AIM
ALGORITHM
STEP 3. Check the water level in upper Tank whether it is maximum or minimum.
FLOWCHART
RESULT
Thus the flow chart for swapping of two numbers has been displayed.
AIM
ALGORITHM
STEP 3:Check the room temperature> critical temperature if yes set the Alarm as ON,Main power as
OFF.
FLOWCHART:
RESULT
Thus the flow chart for grade has been displayed based on mark scored.
AIM
ALGORITHM
STEP 2: check whether water and detergent is available and also door is closed in the machine.
STEP 3:Check the time for opening the water valve,and start the washing machine.
STEP 4:Drain water if the process is completed and close the tap.
FLOWCHART:
RESULT :
Thus the flow chart for multiplication table has been executed.
AIM:
ALGORITHM:
FLOWCHART:
RESULT
Thus the flow chart for Sum of disgits has been executed.
AIM:
ALGORITHM:
STEP 5:Calculate Electricity Bill=Used Units x 3.5 for if 7am<TOD<9am & 5pm<TOD<8pm.
FLOWCHART:
RESULT
Thus the flow chart for perfect number program is executed successfully.
AIM
ALGORITHM
STEP 2: Enter the customer details. Check the database whether customer details is there or not.
STEP 4: Display the total number of items to calculate the bill and display the bill.
FLOWCHART:
RESULT
Thus the flow chart for sum and avergae of array elements has been executed successfully
AIM
ALGORITHM
FLOWCHART
RESULT
Thus the flow chart for computing electric current in 3 phase AC circuits was executed successfully.
To Write a program to display Integer, char, Float, string input and output.
ALGORITHM
RESULT
Thus the C program to display Integer, char, Float, string input and output has been executed.
ALGORITHM
PROGRAM:
#include <stdio.h>
int main() {
char inputCharacter;
scanf("%c", &inputCharacter);
return 0;
OUTPUT
RESULT
Thus the C program to display ASCII value of character has been executed.
AIM
ALGORITHM
STEP 1: Declare variables to store user details such as name, age, and address.
PROGRAM:
#include <stdio.h>
int main()
{
char name[50];
int age;
char address[100];
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
while (getchar() != '\n');
printf("Enter your address: ");
fgets(address, sizeof(address), stdin);
printf("\nUser Details:\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Address: %s\n", address);
return 0;
}
RESULT
AIM
ALGORITHM
Else
PROGRAM
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
OUTPUT
RESULT
Thus the C program to Convert Centigrade to Fahrenheit has been executed sucuessfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter the dividend: ");
scanf("%d", ÷nd);
printf("Enter the divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
OUTPUT
RESULT
Thus the C program to find Quotient and Remainder of a given number is executed.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
int main()
{
float kmPerHour, milesPerHour;
printf("Enter speed in kilometers per hour: ");
scanf("%f", &kmPerHour);
milesPerHour = kmPerHour / 1.60934;
printf("%.2f kilometers per hour is equal to %.2f miles per hour\n", kmPerHour, milesPerHour);
return 0;
}
OUTPUT
RESULT
Thus the C program to finding Kilometres per hour to miles per hour was executed.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
int hours, minutes, totalMinutes;
printf("Enter hours: ");
scanf("%d", &hours);
printf("Enter minutes: ");
scanf("%d", &minutes);
totalMinutes = (hours * 60) + minutes;
printf("%d hours and %d minutes is equal to %d minutes\n", hours, minutes, totalMinutes);
return 0;
}
OUTPUT
RESULT
Thus the C program to convert the given time in Hour and Minutes has been displayed.
AIM
ALGORITHM
Step 1 : Start the program.
PROGRAM
#include <stdio.h>
int main()
{
float costPrice, sellingPrice, profit;
printf("Enter the cost price: ");
scanf("%f", &costPrice);
printf("Enter the selling price: ");
scanf("%f", &sellingPrice);
profit = sellingPrice - costPrice;
if (profit > 0) {
printf("Profit: %.2f\n", profit);
} else if (profit < 0) {
printf("Loss: %.2f\n", -profit);
} else {
printf("No Profit, No Loss\n");
}
return 0;
}
OUTPUT
RESULT
Thus the C program to display profit calculator has been executed successfully.
AIM
ALGORITHM:
PROGRAM
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("%c is a Vowel.\n", ch);
break;
default:
printf("%c is a Consonant.\n", ch);
}
} else {
printf("Invalid input. Please enter an alphabet.\n");
}
return 0;
}
RESULT
Thus the C program to find given character is Vowel or Consonant has been executed.
ALGORITHM
STEP 1:Start the program.
STEP 2: Declare the variable.
STEP 3:Get the value for the variable.
STEP 4: check if the age is greater or equal to 18.
if (age >= 18)
STEP 5: If yes, printf("You are eligible for casting your vote.\n");
Else
If no, printf("You are not eligible for casting your vote.\n");
STEP 6: Display the result and stop the program.
PROGRAM
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible for casting your vote.\n");
}
else
{
printf("You are not eligible for casting your vote.\n");
}
return 0;
}
OUTPUT
RESULT
Thus the C program to predict the age eligible for casting vote was executed.
Write a Program to check whether the given year is leap year or not.
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
OUTPUT
RESULT
Thus the C program to find the given number is leap year or not is executed.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
char grade;
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
break;
case 'D':
printf("Below Average.\n");
break;
case 'F':
printf("Fail.\n");
break;
default:
return 0;
OUTPUT
RESULT
Thus the C program to display the description for the given grade has been executed.
AIM
Write a c program to Display number of days in a month.
ALGORITHM
STEP 1:Get the input month as a number N.
STEP 2:If N is one of these value 1, 3, 5, 7, 8, 10, 12, then print “31 Days.”.
STEP 3:If N is one of these value 4, 6, 9, 11, then print “30 Days.”.
STEP 4:If N is 2, then print “28/29 Days.”.
STEP 5:Else print “Invalid Month”.
PROGRAM
#include <stdio.h>
int main()
{
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
if(month == 1)
{
printf("31 days");
}
else if(month == 2)
{
printf("28 or 29 days");
}
else if(month == 3)
{
printf("31 days");
}
else if(month == 4)
{
printf("30 days");
}
else if(month == 5)
{
printf("31 days");
}
else if(month == 6)
{
printf("30 days");
}
OUTPUT
RESULT
Thus the C program to display number of days in a month has been executed..
AIM
To Write a program to implement calculator using switch case.
ALGORITHM
STEP 1: Start the Program.
STEP 2: Declare variables to store two numbers, an operator, and the result.
STEP 3: Get input from the user:
Prompt the user to enter the first number.
STEP 4: Read and store the entered number in the variable.
Prompt the user to enter an operator (+, -, *, or /).
STEP 5: Read and store the entered operator in the variable.
Prompt the user to enter the second number.
Read and store the entered number in the variable.
Perform calculations based on the operator:
Use a switch statement to check the value of the operator.
STEP 6: Display the result:
STEP 7: Print the entered expression and the result.
STEP 8: End the program.
PROGRAM
#include <stdio.h>
int main()
{
float num1, num2, result;
char operator;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator);
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
OUTPUT
RESULT
Thus the C program to display Calculator using switch case was displayed successfully.
AIM
Write a program to display the Triangle type as Isosceles Triangle, Equilateral Triangle, Scalene
Triangle.
ALGORITHM
STEP 1: Start the Program
STEP 2:Declare variables to store the lengths of three sides of the triangle.
STEP 3:Get input from the user:
Prompt the user to enter the length of side 1.
Prompt the user to enter the length of side 2.
Prompt the user to enter the length of side 3.
STEP 4:Read and store the entered length in the variable.
STEP 5:Check if the entered side lengths form a valid triangle:
Determine the type of the triangle:
If all three sides are equal, it is an equilateral triangle.
If at least two sides are equal, it is an isosceles triangle.
Otherwise, it is a scalene triangle.
STEP 6:If the triangle is not valid, display an error message and end the program.
STEP 7:Display the result:
STEP 8:Print the type of the triangle.
STEP 9:End the program.
PROGRAM
#include <stdio.h>
int main()
scanf("%f", &side1);
scanf("%f", &side2);
scanf("%f", &side3);
printf("Triangle is Equilateral.\n");
printf("Triangle is Isosceles.\n");
} else {
printf("Triangle is Scalene.\n");
} else {
return 0;
OUTPUT
RESULT
Thus the C program to display the Triangle type as Isosceles Triangle, Equilateral Triangle,
Scalene Triangle was executed successfully.
AIM
Write a program to find Roots of a quadratic equation.
ALGORITHM
STEP1:Declare variables to store the coefficients a, b, c, discriminant, and roots.
If the discriminant is greater than 0, calculate and print two real and different roots.
If the discriminant is equal to 0, calculate and print two real and equal roots.
If the discriminant is less than 0, calculate and print two complex and imaginary roots.
PROGRAM
#include <stdio.h>
#include <math.h>
int main() {
printf("Enter a: ");
scanf("%f", &a);
printf("Enter b: ");
scanf("%f", &b);
scanf("%f", &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
} else if (discriminant == 0) {
} else {
}return 0;}
OUTPUT
RESULT
Thus the C program to find Roots of a quadratic equation has been executed successfully.
AIM
Write a program to Display first N natural numbers.
ALGORITHM
STEP 1 : Start the Program.
STEP 2 : Declare variables
STEP 3 : Get input from the user.
STEP 4 : Display the first N natural numbers using for loop.
STEP 5 : Close the file
PROGRAM
#include <stdio.h>
int main() {
int N, i;
scanf("%d", &N);
}return 0;
OUTPUT
RESULT
Thus the C program to display first N natural numbers has been executed successfully.
AIM
Write a program to Read N numbers and find their sum and average.
ALGORITHM
STEP 1:Start the Program.
STEP 2: Declare variables.
STEP 3: Get input from the user
STEP 4: Read N numbers and calculate sum.
STEP 5: Calculate average
average = sum / N;
STEP 6: Display the sum and average and stop the program.
PROGRAM
#include <stdio.h>
int main() {
int N, i;
float num, sum = 0, average;
printf("Enter the value of N: ");
scanf("%d", &N);
for (i = 1; i <= N; i++) {
printf("Enter number %d: ", i);
scanf("%f", &num);
sum += num;
}
average = sum / N;
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
RESULT
Thus the C program to read N numbers and find their sum and average has been executed
successfully.
AIM
Write a program to find cube of the number upto a given integer.
ALGORITHM
Step 1 : Start the Program.
Step 2 : Declare variables.
Step 3 : Get input from the user.
Step 4 : Display the cubes of numbers up to N.
Step 5 : Stop the Program.
PROGRAM
#include <stdio.h>
int main()
{
int N, i;
printf("Enter the value of N: ");
scanf("%d", &N);
printf("Cubes of numbers up to %d are:\n", N);
for (i = 1; i <= N; i++)
{
printf("Cube of %d = %d\n", i, i * i * i);
}
return 0;
}
OUTPUT
RESULT
Thus the C program to find cube of the number upto a given integer has been executed successfully.
AIM
Write a program to display Multiplication table.
ALGORITHM
STEP 1:Declare a variable to store the input number.
STEP 5:Print each entry in the format "number x loop variable = product".
PROGRAM
#include <stdio.h>
int main()
{
int num, i;
printf("Enter the number for multiplication table: ");
scanf("%d", &num);
printf("Multiplication table for %d:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
RESULT
Thus the C program to display Multiplication table has been executed successfully.
AIM
Write a program to Sum of N natural numbers.
ALGORITHM
STEP 1:Declare variables to store the value of N, loop variable, and the sum.
STEP 3:Calculate the sum of the first N natural numbers using a for loop:
PROGRAM
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer N: ");
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum of the first %d natural numbers is: %d\n", n, sum);
return 0;
}
OUTPUT
RESULT
Thus the C program to sum of N natural numbers has been executed successfully.
AIM
Write a program to find Sum of N natural odd numbers.
ALGORITHM
PROGRAM
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter a positive integer N: ");
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
if (i % 2 != 0) {
sum += i;
}
}
printf("Sum of the first %d natural odd numbers is: %d\n", n, sum);
return 0;}
OUTPUT
RESULT
Thus the C program to sum of N natural odd numbers has been executed successfully.
PROGRAM
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows for the pattern: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
OUTPUT
RESULT
AIM
Write a program to Display the array elements.
ALGORITHM
Step 1 : Declare the file pointer fp and open a new file file.txt in write mode
Step 2 : Declare the variable str[100]
Step 3 : Read the string from user
Step 4 : Write the content into a file using fprint()
Step 5 : Display the current file position pointer
Step 6 : use fseek() and over write the file content
PROGRAM
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Array elements are: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT
RESULT:
Thus the C program to display the array elements has been executed successfully.
AIM
Write a program to Display the Elements in reverse order.
ALGORITHM
STEP 1:Start the Program.
STEP 2:Ask the user for the size of the array and Declare an array of the given size.
STEP 3:Ask the user to input elements into the array.
STEP 4:Use a loop to display the elements of the array in the original order.
STEP 5:Use another loop to display the elements of the array in reverse order.
STEP 6:Print each element in the loop.
STEP 7:End the program.
PROGRAM
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Array elements are: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
printf("\nArray elements in reverse order are: ");
for (int i = size - 1; i >= 0; --i) {
printf("%d ", arr[i]);
}
return 0;
}
RESULT
Thus the C program to display the elements in reverse order has been executed successfully.
AIM
Write a program to display the Sum of array elements.
ALGORITHM
STEP 1:Declare an array of the given size.
STEP 2:Ask the user to input elements into the array.
STEP 3:Declare a variable (sum) and initialize it to zero.
STEP 4:Calculate Sum:
Use a loop to iterate over the array elements.
Add each element to the sum variable.
STEP 5:Print the calculated sum.
STEP 6:End the program.
PROGRAM
#include <stdio.h>
int main()
{
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
printf("\nSum of array elements is: %d\n", sum);
return 0;
}
RESULT
Thus the C program to display the Sum of array elements has been executed successfully.
AIM
Write a program to Make a copy of array elements.
ALGORITHM
STEP 1: Declare an array of the given size.
STEP 2: Ask the user to input elements into the array.
STEP 3: Declare another array (copiedArray) with the same size as the original array.
STEP 4: Copy each element from the original array to the corresponding position in the copied array.
STEP 5: Print the elements of the original array.
STEP 6: Print the elements of the copied array.
STEP 7: End the program.
PROGRAM
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int originalArray[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &originalArray[i]);
}
int copiedArray[size];
for (int i = 0; i < size; ++i) {
copiedArray[i] = originalArray[i];
}
printf("\nOriginal Array elements are: ");
for (int i = 0; i < size; ++i) {
printf("%d ", originalArray[i]);
}
printf("\nCopied Array elements are: ");
for (int i = 0; i < size; ++i) {
printf("%d ", copiedArray[i]);
}
return 0;
}
RESULT
Thus the C program to make a copy of array elements has been executed successfully.
AIM
Write a program to find Maximum and minimum element in an array.
ALGORITHM
BEGIN
STEP 1: Start the program and Declare an array of the given size.
STEP 2: Ask the user to input elements into the array.
STEP 3: Declare variables to store the maximum (maxElement) and minimum (minElement) elements.
STEP 4:Find Maximum and Minimum:
• Use a loop to iterate over the array elements.
• Compare each element with the current maximum and minimum.
• Update maxElement if the current element is greater.
• Update minElement if the current element is smaller.
STEP 5: Print the values of maxElement and minElement.
STEP 6: End the program.
END
PROGRAM
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int maxElement = arr[0];
int minElement = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
if (arr[i] < minElement) {
minElement = arr[i];
}
OUTPUT
RESULT
Thus the C program to find maximum and minimum element in an array has been executed
successfully.
AIM
Write a program to calculate odd sum and even sum.
ALGORITHM
STEP 1: Start the Program.
PROGRAM
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements into the array:\n", size);
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < size; ++i) {
if (arr[i] % 2 == 0) {
evenSum += arr[i];
} else {
oddSum += arr[i];
return 0;
}
OUTPUT
RESULT
Thus the C program to calculate odd sum and even sum has been executed successfully.
AIM
Write a program to implement matrix addition.
ALGORITHM
STEP 1: Start the program.
STEP 2: Declare matrix a[100][100];
and matrix b[100][100];
and matrix sum[100][100]; row= no. of rows, col= no. of columns
STEP 3: Read row, col, a[100][100] and b[100][100]
STEP 4: Declare variable i=0, j=0
STEP 5: Repeat until i < row
5.1: Repeat until j < col
sum[i][j]=a[i][j] + b[i][j]
STEP 6: sum is the required matrix after addition
STEP 7: Stop the program
PROGRAM
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
return 0;
}
OUTPUT
RESULT
AIM
Write a program to implement matrix subtraction.
ALGORITHM
STEP 1: Start the program.
STEP 2: Declare matrix mat1[3][3] and matrix mat2[3][3] and matSub[i][j]; row= no. of rows, col= no.
of columns.
STEP 3: Read row, col, a[100][100] and b[100][100].
STEP 4: Declare variable i=0, j=0.
STEP 5: Repeat until i < row
5.1: Repeat until j < col
matSub[i][j]=mat1[i][j] - mat2[i][j]
STEP 6: Matrix subtraction is displayed.
STEP 7: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], matSub[3][3], i, j;
printf("Enter First 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter Second 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = mat1[i][j] - mat2[i][j];
}
printf("\nThe Subtraction Result is:\n");
for(i=0; i<3; i++)
OUTPUT
RESULT
Thus the C program to display matrix subtraction has been executed successfully.
AIM
Write a program to display transpose of a matrix.
ALGORITHM
STEP 1: Declare and initialize a two-dimensional array a.
STEP 2: Calculate the number of rows and columns present in the matrix and store it variables rows
and cols respectively.
STEP 3: Declare another array t with reversed dimensions i.e t[cols][rows]. Array t will be used to store
the elements of the transposed matrix.
STEP 4: Loop through the array a and convert its rows into columns of matrix t using
transpose[ j ][ i ] = a[ i ][ j ];
STEP 5: Finally, display the elements of matrix t.
STEP 6: Stop the Program.
PROGRAM
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
OUTPUT
RESULT
Thus the C program to display transpose of the matrix has been executed successfully.
AIM
Write a program to display multiplication of a matrix.
ALGORITHM
STEP 1: Start the program.
STEP 2: Declare matrix mat1[3][3] and matrix mat2[3][3] row= no. of rows, col= no. of columns.
STEP 3: Read row, col, a[100][100] and b[100][100].
STEP 4: Declare variable i=0, j=0, sum=0.
STEP 5: Repeat until i < row
5.1: Repeat until j < col
sum = sum + mat1[i][k] * mat2[k][j];
STEP 6: Matrix Multiplication is displayed.
STEP 7: Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
printf("Enter first 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter second 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
printf("\nMultiplying two matrices...");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
OUTPUT
RESULT
Thus the C program to display mmultiplication of the matrix has been executed successfully.
AIM
Write a program to find frequency of even numbers.
ALGORITHM
STEP 1: Initialize a variable even_count to 0 to keep track of the count of even numbers.
STEP 2: Use nested loops to iterate through each element in the matrix.
STEP 3: For each element, check if it is an even number.
STEP 4: If the element is even, increment the even_count by 1.
STEP 5: After iterating through all elements, even_count will contain the frequency of even numbers
in the matrix.
PROGRAM
#include <stdio.h>
int findEvenFrequency(int rows, int cols, int matrix[rows][cols])
{
int even_count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] % 2 == 0) {
even_count++;
}
}
}
return even_count;
}
int main() {
int rows = 3;
int cols = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int frequency = findEvenFrequency(rows, cols, matrix);
printf("Frequency of even numbers in the matrix: %d\n", frequency);
return 0;
}
RESULT
Thus the C program to display ffrequency of odd numbers in a given matrix has been executed
successfully.
ALGORITHM
PROGRAM
#include <stdio.h>
void printDiagonals(int rows, int cols, int matrix[rows][cols])
{
printf("Main Diagonal: ");
for (int i = 0; i < rows && i < cols; i++) {
printf("%d ", matrix[i][i]);
}
printf("\n");
printf("Secondary Diagonal: ");
for (int i = 0; i < rows && i < cols; i++) {
printf("%d ", matrix[i][cols - 1 - i]);
}
printf("\n");
}
int main() {
int rows = 3;
int cols = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printDiagonals(rows, cols, matrix);
return 0;
}
RESULT
Thus the C program to find Diagonals of a matrix has been executed successfully.
AIM
Write a program to display sum of diagonal elements in the matrix.
ALGORITHM
STEP 1: Start the Program.
STEP 2:Initialize a variable sum to 0 to keep track of the sum of diagonal elements.
STEP 3:Iterate through the matrix using a loop variable i from 0 to rows-1 (inclusive).
STEP 4:Add the element at position (i, i) to the sum. This element belongs to the main diagonal.
STEP 5:The variable sum now contains the sum of diagonal elements.
STEP 6: End the Program.
PROGRAM
#include <stdio.h>
int sumDiagonal(int rows, int cols, int matrix[rows][cols]) {
int sum = 0;
for (int i = 0; i < rows && i < cols; i++)
{
sum += matrix[i][i];
}
return sum;
}
int main() {
int rows = 3;
int cols = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int diagonalSum = sumDiagonal(rows, cols, matrix);
printf("Sum of diagonal elements: %d\n", diagonalSum);
return 0;
}
RESULT
Thus the C program to display Sum of diagonal elements in the matrix has been executed
successfully.
AIM
Write a program to display transpose of a matrix.
ALGORITHM
STEP 1:Start with two matrices, matrix1 and matrix2, each with the same number of rows and columns.
STEP 2:Initialize a variable areEqual to 1, assuming initially that the matrices are equal.
STEP 3:Use nested loops to iterate through each element of the matrices.
a. For each element at position (i, j), compare matrix1[i][j] with matrix2[i][j].
b. If any corresponding elements are not equal, set areEqual to 0 and break out of the loop.
STEP 4:After the loops, if areEqual is still 1, then the matrices are equal. Otherwise, they are not equal.
STEP 5:End the Program.
PROGRAM
#include <stdio.h>
int areMatricesEqual(int rows, int cols, int matrix1[rows][cols], int matrix2[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix1[i][j] != matrix2[i][j]) {
return 0;
}
}
}
return 1;
}
int main() {
int rows = 3;
int cols = 3;
int matrix1[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
OUTPUT
RESULT
Thus the C program to display ccomparison of the matrix has been executed successfully.
AIM
To write a program to implement factorial for the given input using function.
ALGORITHM
PROGRAM
#include <stdio.h>
int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else {
return n * factorial(n - 1); }
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else
{
int result = factorial(num);
printf("The factorial of %d is: %d\n", num, result);
}
return 0;
}
OUTPUT
RESULT
AIM
To write a program to find largest number from the given input using function.
ALGORITHM
PROGRAM
#include <stdio.h>
int findLargest(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n < 2) {
printf("Please enter at least two numbers for comparison.\n");
return 1;
}
int largest = 0;
printf("Enter the numbers:\n");
for (int i = 0; i < n; i++) {
int currentNum;
printf("Number %d: ", i + 1);
scanf("%d", ¤tNum);
largest = findLargest(largest, currentNum);
}
printf("The largest number is: %d\n", largest);
return 0;
}
OUTPUT
RESULT
Thus, the C program to find the largest number using functions is executed successfully.
AIM
To write a program to find area of shape from the given input using function.
ALGORITHM
PROGRAM:
#include <stdio.h>
#include <math.h>
#define PI 3.14
float calculateCircleArea(float radius) {
return PI * radius * radius;
}
float calculateRectangleArea(float length, float width) {
return length * width;
}
float calculateTriangleArea(float base, float height) {
return 0.5 * base * height;
}
int main() {
int choice;
float radius, length, width, base, height, area;
printf("Choose a shape:\n");
printf("1. Circle\n");
printf("2. Rectangle\n");
printf("3. Triangle\n");
printf("Enter the number corresponding to the shape: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = calculateCircleArea(radius);
printf("The area of the circle is: %.2f\n", area);
break;
case 2:
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
return 0;
}
OUTPUT
RESULT
Thus, the C program to find area of shape using functions is executed successfully.
AIM
To write a program to find sum of digits from the given input using function.
ALGORITHM
PROGRAM
#include <stdio.h>
int sumofDigits (int number) {
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
int main ()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
int result = sumofDigits(num);
printf("The sum of digits is: %d\n", result);
return 0;
}
OUTPUT:
RESULT
Thus, the C program to find sum of digits using functions is executed successfully.
AIM
To write a program to find the given input as prime number or not using function.
ALGORITHM
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int number)
{
if (number <= 1)
{
return false; // 0 and 1 are not prime numbers
}
for (int i = 2; i * i <= number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
int main ()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
bool result = isPrime(num);
if (result) {
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
return 0;
}
RESULT
Thus, the C program to find the given input as prime number or not using function is executed
successfully.
AIM
To write a program to find the factorial for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
unsigned long factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int main ()
{
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else
{
unsigned long result = factorial(num);
printf("The factorial of %d is: %lu\n", num, result);
}
return 0;
}
RESULT
Thus, the C program to find the factorial for the given input using recursion is executed
successfully.
AIM
To write a program to find the fibonacci series for the given input using recursion.
ALGORITHM
STEP 5: Otherwise, it prints the Fibonacci series up to the specified length using a loop. It iterates
from 1 to n, calling the fibonacci function for each index.
PROGRAM
#include <stdio.h>
int fibonacci (int n) {
if (n <= 0) {
return -1; // Invalid input
} else if (n == 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
printf("Enter the length of the Fibonacci series (greater than 0): ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
} else {
printf("Fibonacci series: ");
for (int i = 1; i <= n; ++i) {
printf("%d ", fibonacci(i));
}
printf("\n");
}
return 0;
}
RESULT
Thus, the C program to find the fibonacci series for the given input using recursion is executed
successfully.
AIM
To write a program to count digits of number for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
int countDigits(int n) {
if (n == 0) {
return 0; // Base case: no digits in 0
} else {
return 1 + countDigits(n / 10);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
int result = countDigits(num);
printf("Number of digits: %d\n", result);
return 0;
}
OUTPUT
RESULT
Thus, the C program to count digits of number for the given input using recursion is executed
successfully.
AIM
To write a program to find the length of string for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
int stringLength(const char *str) {
if (*str == '\0') {
return 0;
} else {
// Recursive case: move to the next character in the string
return 1 + stringLength(str + 1);
}
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
int result = stringLength(inputString);
printf("Length of the string: %d\n", result);
return 0;
}
OUTPUT
RESULT
Thus, the C program to find the length of string for the given input using recursion is executed
successfully.
AIM
To write a program to find the given number is prime or not using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
int isPrime(int num, int divisor) {
if (num <= 1) {
return 0; // Not a prime number
} else if (divisor == 1) {
return 1; // Prime number
} else {
// Recursive case
if (num % divisor == 0) {
return 0; // Not a prime number
} else {
return isPrime(num, divisor - 1);
}
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num, num / 2)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to find the given number is prime or not using recursion is executed
successfully.
AIM
To write a program to find the GCD for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int result = gcd(num1, num2);
printf("GCD of %d and %d is: %d\n", num1, num2, result);
return 0;
}
OUTPUT:
RESULT
Thus, the C program to find the GCD for the given input using recursion is executed
successfully.
AIM
To write a program to find the Sum of all digits for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
int sumOfDigits(int num) {
if (num == 0) {
return 0;
} else {
return (num % 10) + sumOfDigits(num / 10);
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Sum of digits: %d\n", sumOfDigits(number));
return 0;
}
OUTPUT
RESULT
Thus, the C program to find the Sum of all digits for the given input using recursion is executed
successfully.
AIM
To write a program to find the palindrome for the given input using recursion.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[], int start, int end) {
if (start >= end) {
return 1; // It is a palindrome
}
if (str[start] == str[end]) {
return isPalindrome(str, start + 1, end - 1);
} else {
return 0;
}}
int main() {
char input[100];
printf("Enter a string: ");
scanf("%s", input);
if (isPalindrome(input, 0, strlen(input) - 1)) {
printf("%s is a palindrome.\n", input);
} else {
printf("%s is not a palindrome.\n", input);
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to find the palindrome for the given input using recursion is executed
successfully.
AIM
To write a program to perform swapping of two numbers for the given input using pointer.
ALGORITHM
PROGRAM
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("\nBefore swapping: \n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
swap(&num1, &num2);
printf("\nAfter swapping: \n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
OUTPUT
RESULT
Thus, the C program to perform swapping of two numbers for the given input using pointer is
executed successfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
void printString(char *str) {
while (*str != '\0') {
printf("%c", *str);
str++;
}
printf("\n");
}
int main() {
char myString[] = "Hello, World!";
char *ptr = myString;
printString(ptr);
return 0;
}
OUTPUT
RESULT
AIM
To write a program to read array elements using pointer for the given input.
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &(*(arr + i)));
}
printf("\nArray elements are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", *(arr + i));
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to read array elements using pointer is executed successfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
int main()
{
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int **matrix = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
printf("Enter the elements of the 2D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\n2D Array elements are:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
RESULT
Thus, the C program to perform double pointer for the given input is executed successfully.
AIM
To write a program to find maximum number using pointer from the given input.
ALGORITHM
PROGRAM
#include <stdio.h>
int findMax(int *arr, int size) {
if (size <= 0) {
printf("Error: Empty array\n");
return -1;
}
int max = *arr;
for (int i = 1; i < size; ++i) {
if (*(arr + i) > max) {
max = *(arr + i);
}
}
return max;
}
int main() {
int arr[] = {5, 8, 2, 10, 3};
int size = sizeof(arr) / sizeof(arr[0]);
int max = findMax(arr, size);
if (max != -1) {
printf("The maximum number is: %d\n", max);
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to find maximum number using pointer for the given input is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPalindrome(char *str) {
char *start = str;
char *end = str + strlen(str) - 1;
while (start < end) {
while (!isalnum(*start) && start < end) {
start++;
}
while (!isalnum(*end) && start < end) {
end--;
}
if (tolower(*start) != tolower(*end)) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1;
}
int main()
{
const int max_length = 100;
char *input = (char *)malloc(max_length * sizeof(char));
printf("Enter a string: ");
fgets(input, max_length, stdin);
input[strcspn(input, "\n")] = '\0';
if (isPalindrome(input)) {
printf("The entered string is a palindrome.\n");
}
else
{
printf("The entered string is not a palindrome.\n");
}
return 0;
}
OUTPUT
RESULT
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
void reverseArray(int *arr, int size) {
if (size <= 1) {
return;
}
int *start = arr;
int *end = arr + size - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
const int max_size = 100;
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size <= 0 || size > max_size) {
printf("Error: Invalid array size\n");
return 1;
}
int arr[max_size];
printf("Enter the array elements:\n");
for (int i = 0; i < size; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("\nOriginal array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
reverseArray(arr, size);
printf("Reversed array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT
RESULT
Thus, the C program to perform reverse array using pointer is executed successfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size <= 0) {
printf("Error: Invalid array size\n");
return 1;
}
int *arr = (int *)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Error: Memory allocation failed\n");
return 1;
}
printf("Enter %d array elements separated by spaces: ", size);
for (int i = 0; i < size; ++i) {
scanf("%d", &arr[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
OUTPUT
RESULT
Thus, the C program to perform dynamic memory allocation using pointer is executed successfully.
AIM
To Write a program to compare two strings.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("str1 comes before str2.\n");
} else {
printf("str1 comes after str2.\n");
}
return 0;
}
OUTPUT
RESULT
Thus the C program to compare the two strings has been executed successfully.
Ex.No :11b
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS
AIM
To Write a program to find the reverse of a string.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
reverseString(inputString);
printf("Reversed string: %s\n", inputString);
return 0;
}
OUTPUT
RESULT
AIM
To Write a program to find the concatenate of a string.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
OUTPUT
RESULT
AIM
To Write a program to copy the content of a string.
ALGORITHM
OUTPUT
RESULT
AIM
To Write a program to find palindrome of a string.
ALGORITHM
STEP 4:Check Palindrome:Use a loop to compare characters at indices start and end. If they are different,
the string is not a palindrome. Increment start and decrement end in each iteration.
PROGRAM
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
if (isPalindrome(inputString)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to find Palindrome of a string has been executed successfully.
Ex.No :11f IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS
WITH AND WITHOUT LIBRARY FUNCTIONS
AIM
To Write a program to count number of characters.
ALGORITHM
STEP 1: Start the Program.
STEP 2:Initialize Variables.
STEP 3:Declare the strings as inputstring.
STEP 4:Check the condition while (inputString[count] != '\0') if yes count++.
STEP 5:End the Program.
PROGRAM
#include <stdio.h>
int main()
{
char inputString[100];
printf("Enter a string: ");
scanf("%[^\n]", inputString);
int count = 0;
while (inputString[count] != '\0') {
count++;
}
printf("Number of characters in the string: %d\n", count);
return 0;
}
OUTPUT
RESULT
Thus the C program to Count the character of a string has been executed successfully.
Ex.No :11g
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS
AIM
To Write a program to Count number of words in a given text.
ALGORITHM
STEP 1 : Start the program.
STEP 2:Take a text string as input.
STEP 3:Declare a variable wordCount and set it to 0.
STEP 4:Iterate Through Characters
STEP 5:Check for Word Start
STEP 6:Output the count.
STEP 7:Stop the Program.
PROGRAM
#include <stdio.h>
int countWords(char str[]) {
int count = 0;
int isWord = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ' && (i == 0 || str[i - 1] == ' ')) {
isWord = 1;
count++;
}
if (str[i] == ' ') {
isWord = 0;
}}
return count;}
int main() {
char inputString[1000];
printf("Enter a string: ");
scanf("%[^\n]", inputString);
int wordCount = countWords(inputString);
printf("Number of words in the string: %d\n", wordCount);
return 0;
}
OUTPUT
RESULT
Thus the C program to Count number of words in a text has been executed successfully.
Ex.No :11h IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS
WITH AND WITHOUT LIBRARY FUNCTIONS
AIM
To Write a program to find the substring of a given string.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
int main()
{
char mainString[1000];
char subString[100];
printf("Enter the main string: ");
scanf("%[^\n]", mainString);
printf("Enter the substring to find: ");
scanf("%s", subString);
char *result = strstr(mainString, subString);
if (result != NULL) {
printf("'%s' is found in '%s' at position %ld.\n", subString, mainString, result - mainString);
} else {
printf("'%s' is not found in '%s'.\n", subString, mainString);
}
return 0;
}
OUTPUT
RESULT
Thus, the C program to Find the substring of a given string has been executed successfully.
Ex.No :11i IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS
WITH AND WITHOUT LIBRARY FUNCTIONS
AIM
To Write a program to replace the substring of a given string.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <string.h>
void replaceSubstring(char original[], char find[], char replace[]) {
char result[1000]; // Assuming the result won't exceed 1000 characters
int i, j, k, findLen, replaceLen;
int len = strlen(original);
int isMatch;
findLen = strlen(find);
replaceLen = strlen(replace);
for (i = 0; i < len;) {
isMatch = 1;
for (j = 0; j < findLen; j++) {
if (original[i + j] != find[j]) {
isMatch = 0;
break;
}
}
if (isMatch) {
for (k = 0; k < replaceLen; k++) {
result[i + k] = replace[k];
}
i += replaceLen;
} else {
result[i] = original[i];
i++;
}
}
result[i] = '\0';
printf("Original String: %s\n", original);
printf("String after replacement: %s\n", result);
}
int main() {
char original[1000];
char find[100];
char replace[100];
printf("Enter the original string: ");
scanf("%[^\n]", original);
printf("Enter the substring to find: ");
scanf("%s", find);
printf("Enter the replacement string: ");
scanf("%s", replace);
replaceSubstring(original, find, replace);
return 0;
}
OUTPUT
RESULT
Thus the C program to Replace the substring of a given string has been executed successfully.
Ex.No :12a
IMPLEMENTATION OF FILE-HANDLING OPERATIONS
AIM
To Write a program to read a content from file.
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
char ch;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("File not found or could not be opened.\n");
return 1;
}
printf("File content:\n");
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
OUTPUT
RESULT
Thus the C program to read the content of a file has been executed successfully.
Ex.No :12b
IMPLEMENTATION OF FILE-HANDLING OPERATIONS
AIM
To Write a program to write a content from file.
ALGORITHM
PROGRAM
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
char content[1000];
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "w");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
printf("Enter the content (max 999 characters):\n");
scanf(" %[^\n]", content);
fprintf(file, "%s", content);
fclose(file);
printf("Content successfully written to the file.\n");
return 0;
}
OUTPUT
RESULT
Thus the C program to program to read the content of a file has been executed successfully.
Ex.No :12c
IMPLEMENTATION OF FILE-HANDLING OPERATIONS
AIM
To Write a program to append two files in an another file.
ALGORITHM
STEP 5:Check if file1 and file2 are successfully opened. If not, display an error message and exit the
program.
STEP 6:Get the output filename from the user and store it in outputFile.
STEP 8:Check if resultFile is successfully opened. If not, display an error message and exit the program.
STEP 9:Display a message indicating that the content from inputFile1 and inputFile2 has been
successfully appended to outputFile.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
OUTPUT
RESULT
Thus the C program to program to Append of a file has been executed successfully.
Ex.No :12d
IMPLEMENTATION OF FILE-HANDLING OPERATIONS
AIM
To Write a program to compare the two files.
ALGORITHM
STEP 1:Start the program.
STEP 2:Declare variables for file pointers and filenames (file1, file2, filename1, filename2).
STEP 3:Get the first filename from the user and store it in filename1.
STEP 4:Get the second filename from the user and store it in filename2.
STEP 5:Open file1 with fopen using filename1 in read mode.
STEP 6:Open file2 with fopen using filename2 in read mode.
STEP 7:Check if file1 and file2 are successfully opened. If not, display an error message and exit
the program.
STEP 8:Read characters from file1 and file2 using fgetc and compare each character until EOF is
reached in either file.
STEP 9:If characters differ, display that the files are different and exit.
STEP 10:End the program.
PROGRAM
#include <stdio.h>
int compareFiles(FILE *file1, FILE *file2) {
char ch1, ch2;
do {
ch1 = fgetc(file1);
ch2 = fgetc(file2);
if (ch1 != ch2) {
return 0;
}
} while (ch1 != EOF && ch2 != EOF);
if (ch1 == EOF && ch2 == EOF) {
return 1;
} else {
return 0;
}
}
int main() {
FILE *file1, *file2;
char filename1[100], filename2[100];
printf("Enter the first filename: ");
scanf("%s", filename1);
printf("Enter the second filename: ");
scanf("%s", filename2);
file1 = fopen(filename1, "r");
file2 = fopen(filename2, "r");
if (file1 == NULL || file2 == NULL) {
printf("Error: One or both files could not be opened.\n");
return 1;
}
if (compareFiles(file1, file2)) {
printf("Files are identical.\n");
} else {
printf("Files are different.\n");
}
fclose(file1);
fclose(file2);
return 0;
}
OUTPUT
RESULT
Thus the C program to Compare the file has been executed successfully.
Ex.No :12e
IMPLEMENTATION OF FILE-HANDLING OPERATIONS
AIM
To Write a program to read student details and store into files.
ALGORITHM
PROGRAM
#include <stdio.h>
struct Student {
char name[50];
int rollNumber;
float marks;
};
int main() {
FILE *file;
struct Student student;
char filename[100];
int numStudents;
printf("Enter the filename to store student details: ");
scanf("%s", filename);
file = fopen(filename, "w");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
printf("Enter the number of students: ");
scanf("%d", &numStudents);
for (int i = 0; i < numStudents; ++i) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", student.name);
printf("Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Marks: ");
scanf("%f", &student.marks);
fprintf(file, "Name: %s\nRoll Number: %d\nMarks: %.2f\n\n", student.name, student.rollNumber,
student.marks);
}
fclose(file);
return 0;
}
OUTPUT
RESULT
Thus, the C program to read student details and store into files has been executed successfully.
Ex. No: 13a
IMPLEMENTATIONS OF STRUCTURE IN REAL TIME
APPLICATIONS
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare structure variable*/
struct employee emp;
RESULT
Thus, the C program to accept & display employee details using structures is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
struct worker
{
char name[20];
int wage;
int wdays;
};
int main()
{
struct worker a,b;
printf("Enter Details of First Worker\n");
printf("-------------------------------\n");
printf("Enter Worker Name : ");
scanf("%s",a.name);
printf("Enter Wage : ");
scanf("%d",&a.wage);
printf("Enter wdays : ");
scanf("%d",&a.wdays);
printf("-------------------------------\n");
printf("Enter Details of Second Worker\n");
printf("-------------------------------\n");
printf("Enter Worker Name : ");
scanf("%s",b.name);
printf("Enter Wage : ");
scanf("%d",&b.wage);
printf("Enter wdays : ");
scanf("%d",&b.wdays);
printf("-------------------------------\n");
int p1=a.wage*a.wdays;
printf("Name of First Worker : %s\nPayment of First Worker : %d\n",a.name,p1);
printf("-------------------------------\n");
OUTPUT
RESULT
Thus, the C program to calculate total payment of workers using structures is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int b_no;
char b_name[40];
char b_author[40];
int no_pages;
};
int main()
{
struct book b[20];
int ch,n,i,count = 0;
char temp[40];
do
{
printf("\t\tMENU");
printf("\n-------------------------------------\n");
printf("PRESS 1.TO ADD BOOK DETAILS.");
printf("\nPRESS 2.TO DISPLAY BOOK DETAILS.");
printf("\nPRESS 3.TO DISPLAY BOOK OF GIVEN AUTHOR.");
printf("\nPRESS 4.TO COUNT NUMBER OF BOOKS.");
printf("\nPRESS 5.TO EXIT.");
printf("\n-------------------------------------\n");
OUTPUT
RESULT
Thus, the C program to perform library operations using structures is executed successfully.
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct details
{
char name[30];
int eid;
int salary;
}emp[5];
void emp_search(int r)
{
int id,i;
printf("\nEnter Employee-Id to be Searched : ");
scanf("%d",&id);
printf("----------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].eid==id)
{
printf("Employee Id : %d",emp[i].eid);
printf("\nName : %s",emp[i].name);
printf("\nSalary : %d\n",emp[i].salary);
}
}
}
void display(int r)
{
int i;
printf("\nList of All Employees:\n");
printf("-------------------------------\n");
printf("Emp-Id\tEmp-Name Salary\n");
OUTPUT
RESULT
Thus, the C program to perform menu driven program for employee structure is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
struct emp_basic_details
{
int dept_no;
char name[30];
float salary;
};
union employee_information {
struct emp_basic_details employee;
};
void main()
{
emp employee_details;
printf("*********************************\n");
printf("Entered employee details are:\n");
printf("Department ID:\t%d\n", employee_details.employee.dept_no);
printf("Employee Name:\t%s\n", employee_details.employee.name);
printf("Employee Salary:\t%.2f\n", employee_details.employee.salary);
}
RESULT
Thus, the C program to accept & display employee details using unions is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<string.h>
union worker
{
char name[20];
int wage;
int wdays;
};
int main()
{
union worker a,b;
char name[20];
int wage, wdays;
printf("Enter Details of First Worker\n");
printf("-------------------------------\n");
printf("Enter Worker Name : ");
scanf("%s",a.name);
strcpy(name,a.name);
printf("Enter Wage : ");
scanf("%d",&a.wage);
wage = a.wage;
printf("Enter wdays : ");
scanf("%d",&a.wdays);
wdays = a.wdays;
printf("-------------------------------\n");
int p1=wage*wdays;
printf("Name of First Worker : %s\nPayment of First Worker : %d\n",name,p1);
return 0;
}
RESULT
Thus, the C program to calculate total payment of workers using union is executed
successfully.
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
union book{
struct{
int b_no;
char b_name[40];
char b_author[40];
int no_pages;
}b;
};
int main(){
union book b[20];
int ch,n,i,count = 0;
char temp[40];
do {
printf("\t\tMENU");
printf("\n-------------------------------------\n");
printf("PRESS 1.TO ADD BOOK DETAILS.");
printf("\nPRESS 2.TO DISPLAY BOOK DETAILS.");
printf("\nPRESS 3.TO DISPLAY BOOK OF GIVEN AUTHOR.");
printf("\nPRESS 4.TO COUNT NUMBER OF BOOKS.");
printf("\nPRESS 5.TO EXIT.");
printf("\n-------------------------------------\n");
printf("Enter Your Choice: ");
RESULT
Thus, the C program to perform library operations using union is executed successfully.
AIM
To write a program to perform menu driven program for employee using union.
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
union details{
struct{
char name[30];
int eid;
int salary;
}e;
}emp[5];
void emp_search(int r){
int id,i;
printf("\nEnter Employee-Id to be Searched : ");
scanf("%d",&id);
printf("----------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].e.eid==id)
{
printf("Employee Id : %d",emp[i].e.eid);
printf("\nName : %s",emp[i].e.name);
printf("\nSalary : %d\n",emp[i].e.salary);
}
}
}
void display(int r){
int i;
printf("\nList of All Employees:\n");
printf("-------------------------------\n");
OUTPUT
RESULT
Thus, the C program to perform menu driven program for employee using union is executed
successfully.