Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

U23CS351-Problem Solving Using C Laboratory

Uploaded by

Hrithick M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

U23CS351-Problem Solving Using C Laboratory

Uploaded by

Hrithick M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

LABORATORY RECORD

U23CS351 – PROBLEM SOLVING USING C LABORATORY

U23CS351-Problem Solving Using C Laboratory


BONAFIDE CERTIFICATE

Certified that this is the Bonafide record of work done by,

Name: Mr/Ms …………………………………………………………………...

Register Number: …………………………………………………………... of

I year B.E/B.Tech ………………………………………………………………

in U23CS351 – PROBLEM SOLVING USING C LABORATORY during I


Semester of the academic year 2023-2024 (Odd Semester)

Signature of Faculty In-Charge Head of the Department

Submitted for the practical examinations held on …………….

INTERNAL EXAMINER EXTERNAL EXAMINER

U23CS351-Problem Solving Using C Laboratory


INDEX
S.No Date Name of the Experiment Page No Mark Signature

1 Develop flow charts and solve simple real-life or


scientific or technical
problems (Minimum 3 problems)
a. Traffic signal control
b. Water level controller
c. Temperature control system
d. Automatic washing machine control system
e. Automatic Street light control system
f. Electricity Billing
g. Retail shop billing
h. Computing Electrical Current in Three Phase AC
circuits
2 Implementation of applications of input and output
statements.
a. Integer, char, Float, string input and output,
b. ASCII value of character
c. User details
3 Implementation operators and expressions
a. Centigrade to Fahrenheit
b. Quotient and Remainder
c. Kilometres per hour to miles per hour
d. Hour and Minutes
e. Profit Calculator
4 Implementation of real-time applications using
conditional statements.
a. Vowel or Consonant
b. Eligible for casting vote
c. Leap year or not
d. Display the description for the given grade
e. Display number of days in a month
f. Calculator
g. Triangle type
h. Roots of a quadratic equation
5 Implementation of technical applications using iterative
loops
a. Display first N natural numbers
b. Read N numbers and find their sum and average
c. Find cube of the number upto a given integer
d. Multiplication Table
e. Sum of N natural numbers
f. Sum of N natural odd numbers
g. Pattern printing
6 Implementation of one-dimensional array
a. Display the array elements
b. Elements in reverse order
c. Sum of array elements

U23CS351-Problem Solving Using C Laboratory


d. Make a copy of array elements
e. Maximum and minimum
f. odd sum and even sum
7 Implementation of two dimensional and multi-
dimensional array
a. Sum
b. Subtraction
c. Transpose
d. Multiplication
e. Frequency of even numbers
f. Print diagonals
g. Sum of diagonal elements
h. Compare
8 Implementation of Functions in the program
a. Factorial
b. Largest number
c. Area of shape
d. Sum of digits
e. Prime number or not
9 Implementation of real time applications using
recursion
a. Factorial
b. Fibonacci series
c. Count digits of number
d. Length of string
e. Prime or not
f. GCD
g. Sum of all digits
h. Palindrome
10 Implementation of pointer in applications
a. Swap two numbers
b. Print string
c. Read array elements
d. Double pointer
e. Find maximum number
f. Palindrome
g. Reverse array
h. Dynamic memory allocation
11 Implementation of strings handling functions with and
without library
functions
a. Compare two strings
b. Reverse
c. Concatenate,
d. Copy
e. Palindrome,
f. Count number of character
g. Number of words,
h. Find
i. Replace

U23CS351-Problem Solving Using C Laboratory


12 Implementation of file-handling operations
a. Read
b. Write
c. Append file
d. Compare two files
e. Read student details and store into files
13 Implementations of Structure in real time applications
a. Accept & display employee details
b. Calculate total payment of workers
c. Library operations
d. Menu driven program for employee structure
14 Implementations of Union in programs
a. Accept & display employee details
b. Calculate total payment of workers
c. Library operations
d. Menu driven program for employee unions
15 Mini Project: Develop an application for any real-world
problem

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1a
TRAFFIC SIGNAL CONTROL

AIM

To develop a flowchart for Traffic signal control.

ALGORITHM

STEP 1:Initialize signal state: Green

STEP 2. Set timers for Green, Yellow, and Red phases.

STEP 3. Loop:

a. Display Green signal for the predetermined Green time.

b. Switch to Yellow signal for the predetermined Yellow time.

c. Switch to Red signal for the predetermined Red time.

STEP 4.End the program.

FLOWCHART

RESULT

Thus the flow chart for range of datatypes has been displayed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1b
WATER LEVEL CONTROLLER

AIM

To develop a flowchart for water level controller.

ALGORITHM

STEP 1:Start the program.

STEP 2. Intialize all the components.

STEP 3. Check the water level in upper Tank whether it is maximum or minimum.

STEP 4:If it maximum SWITCH ON the PUMP else SWITCH OFF.

STEP 5: End the program

FLOWCHART

RESULT

Thus the flow chart for swapping of two numbers has been displayed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1c
TEMPERATURE CONTROL SYSTEM

AIM

To develop a flowchart for Temperature control system.

ALGORITHM

STEP 1: Start the Program.

STEP 2: Read 4 Room Temperature, Critical Temperature and smoke sensor.

STEP 3:Check the room temperature> critical temperature if yes set the Alarm as ON,Main power as
OFF.

STEP 4: End the program.

FLOWCHART:

RESULT

Thus the flow chart for grade has been displayed based on mark scored.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1d
AUTOMATIC WASHING MACHINE CONTROL SYSTEM

AIM

To develop the flowchart for automatic washing machine control system.

ALGORITHM

STEP 1: Start the program.

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.

STEP 5: End the program.

FLOWCHART:

RESULT :

Thus the flow chart for multiplication table has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1e
AUTOMATIC STREET LIGHT CONTROL SYSTEM

AIM:

To develop the flowchart for automatic street light control system.

ALGORITHM:

STEP 1:Start the Program.

STEP 2:Get the values for SSID and password.

STEP 3: Check whether sun light is less or not.

STEP 4:If yes receive the sense signal from ultrasonic.

STEP 5:Check that vehicle is detected or not.

STEP 6:If yes SWITCH ON the street lights.

STEP 7: End the program

FLOWCHART:

RESULT

Thus the flow chart for Sum of disgits has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1f
ELECTRICITY BILLING

AIM:

To develop a flowchart for Electricity billing.

ALGORITHM:

STEP 1: Start the Program.

STEP 2: Get the values for Units, Time of Day(TOD).

STEP 3: Calculate the Used units as

Used Units= Current Units-Previous Units

STEP 4:Calculate Electricity Bill=Used Units x 2 for if 10pm<TOD<7am.

STEP 5:Calculate Electricity Bill=Used Units x 3.5 for if 7am<TOD<9am & 5pm<TOD<8pm.

STEP 6: Calculate the bill.

STEP 7: End the program.

FLOWCHART:

RESULT

Thus the flow chart for perfect number program is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1g
RETAIL SHOP BILLING

AIM

To develop a flowchart for retail shop billing.

ALGORITHM

STEP 1: Start the Program.

STEP 2: Enter the customer details. Check the database whether customer details is there or not.

STEP 3: Get the values as input by calculating the bill.

STEP 4: Display the total number of items to calculate the bill and display the bill.

STEP 5: End the program.

FLOWCHART:

RESULT

Thus the flow chart for sum and avergae of array elements has been executed successfully

U23CS351-Problem Solving Using C Laboratory


Ex.No : 1h
COMPUTING ELECTRICAL CURRENT IN THREE PHASE AC
CIRCUITS

AIM

To develop a flowchart for computing electric current in 3 phase AC circuits.

ALGORITHM

STEP 1:Start the Program


STEP 2:Print the “powerfactor”,”voltage” and “current”.
STEP 3:Read the variables pf ,V ,I.
STEP 4:Calculate power=v3 * pf * V * I.
STEP 5:Print Power.
STEP 6:End the Program.

FLOWCHART

RESULT

Thus the flow chart for computing electric current in 3 phase AC circuits was executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 2a IMPLEMENTATION OF APPLICATIONS OF INPUT AND
OUTPUT STATEMENTS.
AIM

To Write a program to display Integer, char, Float, string input and output.

ALGORITHM

Step 1: Declare the variable int,char,float input and output.


Step 2 : Read the variable int,char,float input and output.
Step 3 : Get the values and display the output.
Step 4: End the Program
PROGRAM
#include <stdio.h>
int main()
{
int integerInput;
char charInput;
float floatInput;
char stringInput[100];
printf("Enter an integer: ");
scanf("%d", &integerInput);
printf("Enter a character: ");
scanf(" %c", &charInput);
printf("Enter a float: ");
scanf("%f", &floatInput);
printf("Enter a string: ");
scanf("%s", stringInput);
printf("\nOutput:\n");
printf("Integer: %d\n", integerInput);
printf("Character: %c\n", charInput);
printf("Float: %.2f\n", floatInput);
printf("String: %s\n", stringInput);
return 0;
}
OUTPUT

RESULT

Thus the C program to display Integer, char, Float, string input and output has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 2b
IMPLEMENTATION OF APPLICATIONS OF INPUT
AND OUTPUT STATEMENTS.
AIM

Write a program to find ASCII value of character

ALGORITHM

STEP 1: Declare a character variable:

STEP 2: Get input from the user:

STEP 3: Compute the ASCII value

STEP 4: End the program.

PROGRAM:

#include <stdio.h>

int main() {

char inputCharacter;

printf("Enter a character: ");

scanf("%c", &inputCharacter);

printf("ASCII value of %c is %d\n", inputCharacter, inputCharacter);

return 0;

OUTPUT

RESULT

Thus the C program to display ASCII value of character has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 2c IMPLEMENTATION OF APPLICATIONS OF INPUT AND
OUTPUT STATEMENTS

AIM

Write a program to display user details

ALGORITHM

STEP 1: Declare variables to store user details such as name, age, and address.

STEP 2: Get user input:

STEP 3: Display user details:

STEP 4: End the program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display user details was executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 3a
IMPLEMENTATION OPERATORS AND EXPRESSIONS

AIM

Write a program to convert Centigrade to Fahrenheit.

ALGORITHM

Step 1 : Declare the two variables

Step 2 : Read the values from user.

Step 3 : Declare the pointer variables

Step 4 : Check the condition **ipp1>**ipp2 if the condition is true

Print “ipp1 is largest”

Else

Print “ipp1 is largest”

STEP 5: End the program

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 3b
IMPLEMENTATION OPERATORS AND EXPRESSIONS

AIM

Write a program to find Quotient and Remainder.

ALGORITHM

STEP 1:Start the Program.


STEP 2: Declare the two variables.
STEP 3:Get the input from the user.
STEP 4:Calculate Quotient and remainder
quotient = dividend / divisor;
remainder = dividend % divisor;
STEP 5:Display the result

PROGRAM
#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter the dividend: ");
scanf("%d", &dividend);
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.

U23CS351-Problem Solving Using C Laboratory


Ex.No :3c
IMPLEMENTATION OPERATORS AND EXPRESSIONS

AIM

Write a program find Kilometres per hour to miles per hour .

ALGORITHM

STEP 1:Start the program.

STEP 2:Declare the variables.

STEP 3: Get speed in kilometers per hour from the user

STEP 4: Convert kilometers per hour to miles per hour

milesPerHour = kmPerHour / 1.60934;

STEP 5: Display the result and stop the program.

STEP 6: End the program

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 3d
IMPLEMENTATION OPERATORS AND EXPRESSIONS

AIM

Write a program to convert the given time in Hour and Minutes

ALGORITHM

Step 1 : Start the program.

Step 2 : Declare variables.

Step 3 : Get input from the user

Step 4: Convert hours and minutes to total minutes

totalMinutes = (hours * 60) + minutes;

Step 5:Display the result and stop the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 3e
IMPLEMENTATION OPERATORS AND EXPRESSIONS

AIM

Write a program to perform Profit Calculator.

ALGORITHM
Step 1 : Start the program.

Step 2 : Declare variables

Step 3: Get input from the user

Step 4 : Calculate profit

profit = sellingPrice - costPrice;

Step 5: Display the result and stop 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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4a IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS

AIM

Write a program to find given character is Vowel or Consonant.

ALGORITHM:

STEP 1: Declare a variable to store the character.

STEP 2: Get input from the user.

STEP 3: Check if the character is a vowel or consonant.

STEP 4:Display the result as vowel or consonant.

STEP 5:Stop the Program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT
Thus the C program to find given character is Vowel or Consonant has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4b IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS
AIM
Write a program to check the given age is eligible for casting vote.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4c IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS.
AIM

Write a Program to check whether the given year is leap year or not.

ALGORITHM

STEP 1:Start the Program.


STEP 2: Declare a variable to store the year.
STEP 3: Get input from the user
STEP 4: Check if the year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
STEP 5:If yes, printf(“ leap year);
Else
If no, printf(“ Not a leap year);

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4d IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS

AIM

Write a program to Display the description for the given grade.

ALGORITHM

STEP 1: Declare a variable to store the grade.

STEP 2: Get input from the user:

Prompt the user to enter their grade (A, B, C, D, or F).

STEP 3:Read and store the entered grade in the variable.

STEP 4:Display the description for the given grade:

STEP 5: Use a switch statement to check the value of the grade.

For each case (A, B, C, D, F), print a corresponding description.

Handle the default case for an invalid grade.

STEP 6: End the program.

PROGRAM

#include <stdio.h>

int main() {

char grade;

printf("Enter your grade (A, B, C, D, or F): ");

scanf(" %c", &grade);

switch (grade) {

case 'A':

printf("Excellent!\n");

break;

case 'B':

printf("Good!\n");

break;

case 'C':

U23CS351-Problem Solving Using C Laboratory


printf("Average.\n");

break;

case 'D':

printf("Below Average.\n");

break;

case 'F':

printf("Fail.\n");

break;

default:

printf("Invalid grade. Please enter A, B, C, D, or F.\n");

return 0;

OUTPUT

RESULT

Thus the C program to display the description for the given grade has been executed.

U23CS351-Problem Solving Using C Laboratory


Ex.No :4e IMPLEMENTATION OF REAL-TIME APPLICATIONS
USING CONDITIONAL STATEMENTS.

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");
}

U23CS351-Problem Solving Using C Laboratory


else if(month == 7)
{
printf("31 days");
}
else if(month == 8)
{
printf("31 days");
}
else if(month == 9)
{
printf("30 days");
}
else if(month == 10)
{
printf("31 days");
}
else if(month == 11)
{
printf("30 days");
}
else if(month == 12)
{
printf("31 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}
return 0;
}

OUTPUT

RESULT
Thus the C program to display number of days in a month has been executed..

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4f IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS.

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 '*':

U23CS351-Problem Solving Using C Laboratory


result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero is not allowed.\n");
return 1;
}
break;
default:
printf("Invalid operator. Please enter +, -, *, or /.\n");
return 1;
}
printf("Result: %.2f %c %.2f = %.2f\n", num1, operator, num2, result);
return 0;
}

OUTPUT

RESULT
Thus the C program to display Calculator using switch case was displayed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :4g IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS

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()

float side1, side2, side3;

printf("Enter the length of side 1: ");

scanf("%f", &side1);

printf("Enter the length of side 2: ");

scanf("%f", &side2);

printf("Enter the length of side 3: ");

scanf("%f", &side3);

U23CS351-Problem Solving Using C Laboratory


if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {

if (side1 == side2 && side2 == side3) {

printf("Triangle is Equilateral.\n");

} else if (side1 == side2 || side1 == side3 || side2 == side3) {

printf("Triangle is Isosceles.\n");

} else {

printf("Triangle is Scalene.\n");

} else {

printf("Triangle is not valid.\n");

return 0;

OUTPUT

RESULT

Thus the C program to display the Triangle type as Isosceles Triangle, Equilateral Triangle,
Scalene Triangle was executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 4h IMPLEMENTATION OF REAL-TIME APPLICATIONS USING
CONDITIONAL STATEMENTS.

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.

STEP 2:Get input from the user:

STEP 3:Calculate the discriminant:

Use the formula discriminant=b2−4ac.

STEP 4:Check the nature of the 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.

STEP 5:Display the result and print the roots

STEP 6:End the program.

PROGRAM
#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2;

printf("Enter the coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c = 0):\n");

printf("Enter a: ");

scanf("%f", &a);

printf("Enter b: ");

scanf("%f", &b);

U23CS351-Problem Solving Using C Laboratory


printf("Enter c: ");

scanf("%f", &c);

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

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different:\n");

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

} else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("Roots are real and equal:\n");

printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

float realPart = -b / (2 * a);

float imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex and imaginary:\n");

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);

}return 0;}

OUTPUT

RESULT

Thus the C program to find Roots of a quadratic equation has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 5a IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

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;

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

scanf("%d", &N);

printf("First %d natural numbers are: ", N);

for (i = 1; i <= N; i++)

printf("%d ", i);

}return 0;

OUTPUT

RESULT

Thus the C program to display first N natural numbers has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 5b IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to read N numbers and find their sum and average has been executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 5c IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No :5d IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

AIM
Write a program to display Multiplication table.

ALGORITHM
STEP 1:Declare a variable to store the input number.

STEP 2:Get input from the user.

STEP 3:Read and store the entered number in the variable.

STEP 4:Display the multiplication table:

Use a for loop to iterate from 1 to 10 (or any desired range).

STEP 5:Print each entry in the format "number x loop variable = product".

STEP 6:End the program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display Multiplication table has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 5e IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

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 2:Get input from the user:

Initialize the sum to zero.

STEP 3:Calculate the sum of the first N natural numbers using a for loop:

STEP 4:Use a loop that iterates from 1 to N.

STEP 5:SDisplay the result:

STEP 6:Print the calculated sum.

STEP 7:End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 5f IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS

AIM
Write a program to find Sum of N natural odd numbers.

ALGORITHM

STEP 1: Start the Program.


STEP 2: Declare the variable.
STEP 3: Get the value for variable.
STEP 4: Calculate the sum of N natural odd numbers by using the condition
if (i % 2 != 0) {
sum += i;
}
STEP 5:Display the result and stop the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No :5g IMPLEMENTATION OF TECHNICAL APPLICATIONS
USING ITERATIVE LOOPS
AIM
Write a program to display the following pattern:
*
**
***
****
*****
ALGORITHM
STEP 1:Ask the user for input values if needed (e.g., the number of rows, symbols to be used, etc.).
STEP 2:Use an outer loop to iterate over each row of the pattern.
STEP 3:Use an inner loop inside the outer loop to iterate over the columns within each row.
STEP 4:Use a print statement inside the inner loop to output the symbols for the current position in the
pattern.
STEP 5:End the program.

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

Thus the C program to display pattern has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6a
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6b IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display the elements in reverse order has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6c
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display the Sum of array elements has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6d
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to make a copy of array elements has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6e
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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];
}

U23CS351-Problem Solving Using C Laboratory


}
printf("\nMaximum element in the array: %d\n", maxElement);
printf("Minimum element in the array: %d\n", minElement);
return 0;
}

OUTPUT

RESULT

Thus the C program to find maximum and minimum element in an array has been executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :6f
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

AIM
Write a program to calculate odd sum and even sum.

ALGORITHM
STEP 1: Start the Program.

STEP 2: Declare an array of the given size.

STEP 3:Calculate Odd and Even Sums:

• Use a loop to iterate over the array elements.


• Check each element if it is odd or even.
• If it's even, add it to evenSum.
• If it's odd, add it to oddSum.
STEP 4: Display Odd and Even Sums:

• Print the values of oddSum and evenSum.


STEP 5: 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 oddSum = 0;
int evenSum = 0;
for (int i = 0; i < size; ++i) {
if (arr[i] % 2 == 0) {
evenSum += arr[i];
} else {
oddSum += arr[i];

U23CS351-Problem Solving Using C Laboratory


}
}
printf("\nSum of odd elements: %d\n", oddSum);
printf("Sum of even elements: %d\n", evenSum);

return 0;
}

OUTPUT

RESULT

Thus the C program to calculate odd sum and even sum has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :7a
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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];

U23CS351-Problem Solving Using C Laboratory


}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

OUTPUT

RESULT

Thus the C program to matrix addition has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No :7b
IMPLEMENTATION OF ONE-DIMENSIONAL ARRAY

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++)

U23CS351-Problem Solving Using C Laboratory


{
for(j=0; j<3; j++)
printf("%d ", matSub[i][j]);
printf("\n");
}
getch();
return 0;
}

OUTPUT

RESULT

Thus the C program to display matrix subtraction has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7c IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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");

U23CS351-Problem Solving Using C Laboratory


for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

OUTPUT

RESULT

Thus the C program to display transpose of the matrix has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7d
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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;

U23CS351-Problem Solving Using C Laboratory


for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMultiplication result of the two given Matrix is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}
getch();
return 0;
}

OUTPUT

RESULT

Thus the C program to display mmultiplication of the matrix has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7e
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display ffrequency of odd numbers in a given matrix has been executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7f
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY
AIM
Write a program to print diagonals of a matrix.

ALGORITHM

STEP 1: Start the program.


STEP 2:Initialize a loop variable i from 0 to rows-1 (inclusive).
STEP 3:Initialize a loop variable i from 0 to rows-1 (inclusive).
STEP 4:Print the element at position (i, i) as it belongs to the main diagonal.Print the element at
position (i, cols-1-i) as it belongs to the secondary diagonal.
STEP 5:Stop the Program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to find Diagonals of a matrix has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7g
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus the C program to display Sum of diagonal elements in the matrix has been executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 7h
IMPLEMENTATION OF TWO DIMENSIONAL AND MULTI-
DIMENSIONAL ARRAY

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}
};

U23CS351-Problem Solving Using C Laboratory


if (areMatricesEqual(rows, cols, matrix1, matrix2)) {
printf("Matrices are equal.\n");
} else {
printf("Matrices are not equal.\n");
}
return 0;
}

OUTPUT

RESULT

Thus the C program to display ccomparison of the matrix has been executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 8a
IMPLEMENTATION OF FUNCTIONS IN THE PROGRAM

AIM

To write a program to implement factorial for the given input using function.

ALGORITHM

STEP 1: Start the program.


STEP 2: Prompt the user to enter a number and read and store the user-input number in a variable,
STEP 3: Check if num is less than 0 and If true, print an error and exit the program.
STEP 4: Define a function factorial that takes an integer parameter n. If n is 0 or 1, return 1
Otherwise, return n multiplied by the result of factorial(n - 1).
STEP 5: Call the factorial function with the input num and store the result in a variable,
STEP 6: Print the calculated factorial
STEP 7: End the program.

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

Thus, the C program to implement Functions using factorial is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 8b
IMPLEMENTATION OF FUNCTIONS IN THE PROGRAM

AIM

To write a program to find largest number from the given input using function.

ALGORITHM

STEP 1: Start the program execution.


STEP 2: Enter the number of elements. Read and store the number in 'n'.
STEP 3: Check if 'n' is less than 2. If true, print an error message and exit the program.
STEP 4: Initialize a variable 'largest' with a default value (e.g., 0).
STEP 5: Enter a loop to get 'n' numbers from the user. Prompt the user to enter a number.
STEP 6 : Read and store the number in 'currentNum'.
STEP 7: Update 'largest' using the findLargest function with parameters 'largest' and 'currentNum'.
STEP 8: Print the final value of 'largest'.
STEP 9: End the program.

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", &currentNum);
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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 8c
IMPLEMENTATION OF FUNCTIONS IN THE PROGRAM

AIM

To write a program to find area of shape from the given input using function.

ALGORITHM

STEP 1: Start the Program


STEP 2: Define the local and global variables
STEP 3: Declare the required functions for the various shapes
STEP 4: Uses a switch statement to handle different choices made by the user. for each shape, it
prompts the user for the necessary input and calculates the area using the appropriate
function.
STEP 5: Print the valid output based on the choice. Else print the default case.
STEP 6: End the program.

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);

U23CS351-Problem Solving Using C Laboratory


printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = calculateRectangleArea(length, width);
printf("The area of the rectangle is: %.2f\n", area);
break;
case 3:
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = calculateTriangleArea(base, height);
printf("The area of the triangle is: %.2f\n", area);
break;
default:
printf("Invalid choice. Please enter a valid number.\n");
break;
}

return 0;
}

OUTPUT

RESULT

Thus, the C program to find area of shape using functions is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 8d
IMPLEMENTATION OF FUNCTIONS IN THE PROGRAM

AIM

To write a program to find sum of digits from the given input using function.

ALGORITHM

STEP 1: Start the program.


STEP 2: The sumofDigits function is defined to calculate the sum of digits of a given number. It uses a
while loop to extract each digit from the number and adds them to the sum.
STEP 3: A variable num is declared to store the user-input number.
STEP 4: The sumofDigits function is called with the user-input number, and the result is stored in the
variable result.
STEP 5: Print the sum of digits using variable result.
STEP 6: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 8e
IMPLEMENTATION OF FUNCTIONS IN THE PROGRAM

AIM

To write a program to find the given input as prime number or not using function.

ALGORITHM

STEP 1: Start the program


STEP 2: Include the necessary stdio.h and stdbool.h headers for input/output functions and boolean
types.
STEP 3: The isPrime function is defined to check whether a given number is prime or not. It uses a loop
to check for divisors from 2 to the square root of the number.
STEP 4: A variable num is declared to store the user-input number.
STEP 5: The isPrime function is called with the user-input number, and the result is stored in the
variable result.
STEP 6: End the program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT:

RESULT

Thus, the C program to find the given input as prime number or not using function is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9a
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the factorial for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: The factorial function is defined to calculate the factorial of a given number using recursion.
STEP 3: a variable num is declared to store the user-input number and the user is prompted to enter a
non-negative integer
STEP 4: The program checks if the input is non-negative. If it is negative, it prints a message that the
factorial is not defined for negative numbers.
STEP 5: If the input is non-negative, the factorial function is called with the user-input number, and
the result is stored in the variable result.
STEP 6: End the program

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus, the C program to find the factorial for the given input using recursion is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9b
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the fibonacci series for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Fibonacci function is defined to calculate the nth Fibonacci number using recursion.
STEP 3: The program prompts the user to enter the length of the Fibonacci series
STEP 4: If the user enters a non-positive number, the program prints an error message.

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.

STEP 6: End the program

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus, the C program to find the fibonacci series for the given input using recursion is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9c
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to count digits of number for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input in main function and pass it to user define function.
STEP 3: Define countDigits user function, takes an integer n as input and returns the count of digits in
that number.
STEP 4: The above function uses the fact that dividing an integer by 10 removes the last digit.
STEP 5: Print the result in main function
STEP 6: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9d
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the length of string for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the length of the string and pass it to user define function.
STEP 3: Define stringLength user Function. It takes a pointer to a character array (string) as input and
returns the length of the string.
STEP 4: Print the result in main function
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9e
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the given number is prime or not using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define isPrime user Function. It consists two parameters - num and divisor. It returns 1 if the
number is prime and 0 if it is not.
STEP 4: Print the result in main function
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9f
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the GCD for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define gcd user Function. It takes two integers a and b as input and returns their greatest
common divisor.
STEP 4: Print the result in main function
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9g
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the Sum of all digits for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define sumOfDigits user Function. It takes an integer as input and returns the sum of its
digits.
STEP 4: Print the result in main function
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 9h
IMPLEMENTATION OF REAL TIME APPLICATIONS USING
RECURSION

AIM

To write a program to find the palindrome for the given input using recursion.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define isPalindrome user function. that checks whether a given string is a palindrome or not
using recursion.
STEP 4: Print the result in main function
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10a
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to perform swapping of two numbers for the given input using pointer.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define swap user function. Where the swapping of two numbers are performed.
STEP 4: Print the result in main function after swapping.
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10b
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to print string using pointer.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define printString user function.
STEP 4: Check if it not equal to null, if the condition is true print the string value.
STEP 5: End the program.

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

Thus, the C program to print string using pointer is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10c IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to read array elements using pointer for the given input.

ALGORITHM

STEP 1: Start the program


STEP 2: Define the array variable and get the elements
STEP 3: Store the elements using pointers.
STEP 4: Print the array elements using pointer.
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10d
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to perform double pointer for the given input.

ALGORITHM

STEP 1: Start the program


STEP 2: Define the two dimensional array variable and get the elements using malloc.
STEP 3: Store the elements using double pointers.
STEP 4: Print the array elements using double pointer and free the memory of the 2D-array.
STEP 5: End the program.

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT

Thus, the C program to perform double pointer for the given input is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10e
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to find maximum number using pointer from the given input.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define findMax user function. Where it checks array is empty or not. If not an empty array
compare the input value max with array values.
STEP 4: Print the maximum value in main function.
STEP 5: End the program.

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10f
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to verify the given input is palindrome using pointer.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input and pass it to user define function.
STEP 3: Define isPalindrome user function. that checks whether a given string is a palindrome or not
using recursion.
STEP 4: Print the result in main function
STEP 5: End the program

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");
}

U23CS351-Problem Solving Using C Laboratory


free(input);

return 0;
}

OUTPUT

RESULT

Thus, the C program to verify palindrome using pointer is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10g
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to perform reverse array using pointer.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input array with valid size.
STEP 3: Define reverseArray user function that where the reverse operation performed.
STEP 4: Print the result in main function
STEP 5: End the program

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]);
}

U23CS351-Problem Solving Using C Laboratory


printf("\n");

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No: 10h
IMPLEMENTATION OF POINTER IN APPLICATIONS

AIM

To write a program to perform dynamic memory allocation using pointer.

ALGORITHM

STEP 1: Start the program


STEP 2: Get the input array with valid size.
STEP 3: Define the size of the array using malloc.
STEP 4: Enter the value of the array within allocated memory and print the result.
STEP 5: End the program

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.

U23CS351-Problem Solving Using C Laboratory


Ex.No : 11a
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS

AIM
To Write a program to compare two strings.

ALGORITHM

STEP 1:Start the Program.


STEP 2: Declare the variables.
STEP 3:Check the strings are equal or not using strcmp().
STEP 4:Display the result.
STEP 5:Stop the Program.

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

STEP 1 : Start the Program.


STEP 2:Initialize Variables:
STEP 3:Declare two indices, start initialized to 0 and end initialized to the length of the string minus 1.
STEP 4:Swap Characters:Use a loop to swap characters at indices start and end.
STEP 5:Stop the Program.

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

Thus the C program to reverse of a string has been executed successfully.


Ex.No :11c
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS

AIM
To Write a program to find the concatenate of a string.

ALGORITHM

STEP 1 : Start the Program.


STEP 2:Initialize Variables:
STEP 3:Declare two strings for concatenation as str1 and str2.
STEP 4:Perform strcat() for concatenating of two strings.
STEP 5:End the Program.

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

Thus, the C program to concatenate of a string has been executed successfully.


Ex.No :11d
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS

AIM
To Write a program to copy the content of a string.

ALGORITHM

STEP 1 : Start the Program.


STEP 2:Initialize Variables:
STEP 3:Declare two strings for concatenation as source and destination.
STEP 4:Perform strcpy() for copying of a strings.
STEP 5:End the Program.
PROGRAM
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[50];
strcpy(destination, source);
printf("Source: %s\n", source);
printf("Copied: %s\n", destination);
return 0;
}

OUTPUT

RESULT

Thus, the C program to Copy of a string has been executed successfully.


Ex.No :11e
IMPLEMENTATION OF STRINGS HANDLING FUNCTIONS WITH AND
WITHOUT LIBRARY FUNCTIONS

AIM
To Write a program to find palindrome of a string.

ALGORITHM

STEP 1:Start the Program.

STEP 2:Take a string as input.

STEP 3:Initialize Variables:

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.

STEP 5:End the Program.

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

STEP 1: Start the Program.


STEP 2:Input as Take two strings as input: the main string and the substring to be found.
STEP 3:Initialize Variables
STEP 4:Iterate Through Main String to find the string.
STEP 5:Check for Substring Match.
STEP 6:Stop the Program.

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

STEP 1: Start the Program.


STEP 2:Input as Take two strings as input: the main string and the substring to be found.
STEP 3:Initialize Variables:
STEP 4:Iterate Through Main String
STEP 5:Check for Substring Match
STEP 6:Stop the Program

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

STEP 1: Start the Program.


STEP 2:Create a file called file.txt and type the content called “hello world”.
STEP 3:check the condition “if (file == NULL)”.
STEP 4:Perform the read operation operation by checking the condition
while ((ch = fgetc(file)) != EOF)
STEP 5:Display the result.
STEP 6:Stop the Program

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

STEP 1: Start the Program.


STEP 2:Create a file called file.txt .
STEP 3:check the condition “if (file == NULL)”.
STEP 4:Perform the read operation operation by checking the condition
while ((ch = fgetc(file)) != EOF)
STEP 5:Display the result.
STEP 6:Stop the Program

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 1:Start the program

STEP 2:Declare variables for file pointers and filenames.

STEP 3:Open file1 with fopen using inputFile1 in read mode.

STEP 4:Open file2 with fopen using inputFile2 in read mode.

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 7:Open resultFile with fopen using outputFile in append mode.

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.

STEP 10:End the Program.

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

STEP 1:Start the program.


STEP 2:Declare a structure to hold student details (struct Student with fields for name, roll number, and
marks).
STEP 3:Declare variables for file pointer (file), filename (filename), number of students (numStudents),
and a temporary student variable (student).
STEP 4:Get the filename from the user and store it in filename.
STEP 5:Open the file in write mode using fopen and assign the file pointer to file.
STEP 6:If the file pointer is NULL, display an error message and exit the program.
STEP 7:Get the number of students from the user and store it in numStudents.
STEP 8:Close the file using fclose.
STEP 9:Display a message indicating that student details have been successfully stored in the file.
STEP 10:End the Program.

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);

printf("Student details successfully stored in %s.\n", filename);

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

To write a program to Accept & display employee details using structures.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the structure employee and declare the data members name and code.
STEP 3: Read the employee name , ID , salary from user.
STEP 4: Display the employee details.
STEP 5: End the program

PROGRAM

#include <stdio.h>

/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};

int main()
{
/*declare structure variable*/
struct employee emp;

/*read employee details*/


printf("\nEnter details :\n");
printf("Name ?:"); gets(emp.name);
printf("ID ?:"); scanf("%d",&emp.empId);
printf("Salary ?:"); scanf("%f",&emp.salary);

/*print employee details*/


printf("\nEntered detail is:");
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
return 0;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT
Thus, the C program to accept & display employee details using structures is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 13b IMPLEMENTATIONS OF STRUCTURE IN REAL TIME
APPLICATIONS

AIM

To write a program to calculate total payment of workers using structures.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the structure worker and declare the data members name, wage and wdays.
STEP 3: Read the worker name , wage , wdays from user.
STEP 4: Calculate the payment using the formula wage *wdays.
STEP 5: Display the payment of worker.
STEP 6: End the program

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");

U23CS351-Problem Solving Using C Laboratory


int p2=b.wage*b.wdays;
printf("Name of Second Worker : %s\nPayment of Second Worker : %d\n",b.name,p2);
return 0;
}

OUTPUT

RESULT
Thus, the C program to calculate total payment of workers using structures is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 13c
IMPLEMENTATIONS OF STRUCTURE IN REAL TIME APPLICATIONS

AIM

To write a program to perform library operations using structures.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the structure book and declare the data members name and author_name and no of
pages.
STEP 3: Create a menu to add book, display books, display books by author_name, count the number
of books and exit choice.
STEP 4: Read the book details from user in choice 1.
STEP 5: Display the book details using choice 2 and display the book detail by author name using
choice 3.
STEP 6: Display the count of books using choice 4.
STEP 7: End the program

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");

U23CS351-Problem Solving Using C Laboratory


printf("Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow Many Records You Want to Add: ");
scanf("%d",&n);
printf("-------------------------------------\n");
printf("Add Details of %d Book\n",n);
printf("-------------------------------------\n");
for(i = 0 ; i < n ; i++)
{
printf("Enter Book No. : ");
scanf("%d",&b[i].b_no);
printf("Book Name : ");
scanf("%s",b[i].b_name);
printf("Enter Author Name : ");
scanf("%s",b[i].b_author);
printf("Enter No. of Pages : ");
scanf("%d",&b[i].no_pages);
printf("-------------------------------------\n");
}
break;
case 2:
printf("\n\t\tDetails of All Book");
printf("\n-----------------------------------------------------------\n");
printf("Book No. Book Name\t Author Name\tNo. of Pages");
printf("\n------------------------------------------------------------");
for( i = 0 ; i < n ; i++)
{
printf("\n %d\t %s\t %s\t %d",b[i].b_no,b[i].b_name,b[i].b_author,b[i].no_pages);
}
printf("\n\n");
break;
case 3:
printf("\nEnter Author Name: ");
scanf("%s",temp);
printf("--------------------------------------");
for( i = 0 ; i < n ; i++)
{
if(strcmp(b[i].b_author,temp) == 0)
{
printf("\n%s\n",b[i].b_name);
}
}
break;
case 4 :
for( i = 0 ; i < n ; i++)
{
count++;
}
printf("\nTotal Number of Books in Library : %d\n",count);
printf("-----------------------------------------\n");
break;
case 5 :

U23CS351-Problem Solving Using C Laboratory


exit(0);
}
}while(ch != 5);
return 0;
}

OUTPUT

RESULT
Thus, the C program to perform library operations using structures is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 13d
IMPLEMENTATIONS OF STRUCTURE IN REAL TIME APPLICATIONS

AIM

To write a program to perform menu driven program for employee structure.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the structure employee and declare the data members name. ID and salary.
STEP 3: Read the employee name , ID , salary from user.
STEP 4: Create a menu to search an employee by emp_id as choice 1.
STEP 5: Display the employee details as choice 2.
STEP 6: Display the employee details whose salary is greater than 10000 as choice 3.
STEP 7: End the program

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");

U23CS351-Problem Solving Using C Laboratory


printf("--------------------------------\n");
for(i=0;i<r;i++)
{
printf("%d\t%s\t %d\n",emp[i].eid,emp[i].name,emp[i].salary);
}
}
void greater(int r)
{
int i;
printf("\nDetails of Employee Whose Salary > 10000\n");
printf("------------------------------------------------");
for(i=0;i<r;i++)
{
if(emp[i].salary>10000)
{
printf("\n Employee Name : %s",emp[i].name);
printf("\n Employee-Id : %d",emp[i].eid);
printf("\n Salary : %d\n",emp[i].salary);
}
}
}
int main()
{
int n,i,ch;
printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");
scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("-----------------------------------------");
printf("\n\tEnter Details of Employee-%d",i+1);
printf("\n-----------------------------------------");
printf("\nName of Employee : ");
scanf("%s",emp[i].name);
printf("Employee-Id : ");
scanf("%d",&emp[i].eid);
printf("Salary : ");
scanf("%d",&emp[i].salary);
}
while(1)
{
printf("-----------------------------------------\n");
printf("\t\tMenu\n");
printf("-----------------------------------------");
printf("\n 1:Search Employee by E-ID");
printf("\n 2:List of All Employee");
printf("\n 3:Display Employee Name whose Salary > 10000 ");
printf("\n 4:Exit");
printf("\n----------------------------------------\n");
printf("Enter Your Choice : ");
scanf("\n %d",&ch);
switch(ch)
{
case 1: emp_search(n);
break;
case 2: display(n);

U23CS351-Problem Solving Using C Laboratory


break;
case 3: greater(n);
break;
case 4: exit(0);
}
}
return 0;
}

OUTPUT

RESULT
Thus, the C program to perform menu driven program for employee structure is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 14a
IMPLEMENTATIONS OF UNION IN PROGRAMS

AIM

To write a program to accept & display employee details using union.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the union employee and declare the data members dept_no, name and salary.
STEP 3: Read the employee dept_no, name , salary from user.
STEP 4: Display the employee details.
STEP 5: End the program

PROGRAM

#include <stdio.h>
struct emp_basic_details
{
int dept_no;
char name[30];
float salary;
};

union employee_information {
struct emp_basic_details employee;
};

typedef union employee_information emp;

void main()
{
emp employee_details;

printf("Enter the details of employee:\n");


printf("Employee Department ID:\t");
scanf("%d", &employee_details.employee.dept_no);
printf("Employee Name:\t");
scanf(" %s", employee_details.employee.name);
printf("Employee Salary:\t");
scanf(" %c", &employee_details.employee.sex);

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);
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT
Thus, the C program to accept & display employee details using unions is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 14b
IMPLEMENTATIONS OF UNION IN PROGRAMS

AIM

To write a program to calculate total payment of workers using union.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the unione worker and declare the data members name, wage and wdays.
STEP 3: Read the worker name , wage , wdays from user.
STEP 4: Calculate the payment using the formula wage *wdays.
STEP 5: Display the payment of worker.
STEP 6: End the program

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;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT
Thus, the C program to calculate total payment of workers using union is executed
successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 14c
IMPLEMENTATIONS OF UNION IN PROGRAMS

AIM

To write a program to perform library operations using union.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the union book and declare the data members as a sub-structure with object ‘b’ and
data members of structure are name and author_name and no of pages.
STEP 3: Create a menu to add book, display books, display books by author_name, count the number
of books and exit choice.
STEP 4: Read the book details from user in choice 1.
STEP 5: Display the book details using choice 2 and display the book detail by author name using
choice 3.
STEP 6: Display the count of books using choice 4.
STEP 7: End the program

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: ");

U23CS351-Problem Solving Using C Laboratory


scanf("%d",&ch);
switch(ch){
case 1:
printf("\nHow Many Records You Want to Add: ");
scanf("%d",&n);
printf("-------------------------------------\n");
printf("Add Details of %d Book\n",n);
printf("-------------------------------------\n");
for(i = 0 ; i < n ; i++) {
printf("Enter Book No. : ");
scanf("%d",&b[i].b.b_no);
printf("Book Name : ");
scanf("%s",b[i].b.b_name);
printf("Enter Author Name : ");
scanf("%s",b[i].b.b_author);
printf("Enter No. of Pages : ");
scanf("%d",&b[i].b.no_pages);
printf("-------------------------------------\n");
}
break;
case 2:
printf("\n\t\tDetails of All Book");
printf("\n-----------------------------------------------------------\n");
printf("Book No. Book Name\t Author Name\tNo. of Pages");
printf("\n------------------------------------------------------------");
for( i = 0 ; i < n ; i++){
printf("\n %d\t %s\t %s\t
%d",b[i].b.b_no,b[i].b.b_name,b[i].b.b_author,b[i].b.no_pages);
}
printf("\n\n");
break;
case 3:
printf("\nEnter Author Name: ");
scanf("%s",temp);
printf("--------------------------------------");
for( i = 0 ; i < n ; i++) {
if(strcmp(b[i].b.b_author,temp) == 0){
printf("\n%s\n",b[i].b.b_name);
}
}
break;
case 4 :
for( i = 0 ; i < n ; i++){
count++; }
printf("\nTotal Number of Books in Library : %d\n",count);
printf("-----------------------------------------\n");
break;
case 5 :
exit(0);
}
}while(ch != 5);
return 0;
}

U23CS351-Problem Solving Using C Laboratory


OUTPUT

RESULT
Thus, the C program to perform library operations using union is executed successfully.

U23CS351-Problem Solving Using C Laboratory


Ex. No: 14d
IMPLEMENTATIONS OF UNION IN PROGRAMS

AIM

To write a program to perform menu driven program for employee using union.

ALGORITHM

STEP 1: Start the program


STEP 2: Declare the union employee and declare the data members as a sub structure with object ‘e’
and with data members name. ID and salary.
STEP 3: Read the employee name , ID , salary from user.
STEP 4: Create a menu to search an employee by emp_id as choice 1.
STEP 5: Display the employee details as choice 2.
STEP 6: Display the employee details whose salary is greater than 10000 as choice 3.
STEP 7: End the program

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");

U23CS351-Problem Solving Using C Laboratory


printf("Emp-Id\tEmp-Name Salary\n");
printf("--------------------------------\n");
for(i=0;i<r;i++)
{
printf("%d\t%s\t %d\n",emp[i].e.eid,emp[i].e.name,emp[i].e.salary);
}
}
void greater(int r){
int i;
printf("\nDetails of Employee Whose Salary > 10000\n");
printf("------------------------------------------------");
for(i=0;i<r;i++)
{
if(emp[i].e.salary>10000)
{
printf("\n Employee Name : %s",emp[i].e.name);
printf("\n Employee-Id : %d",emp[i].e.eid);
printf("\n Salary : %d\n",emp[i].e.salary);
}
}
}
int main(){
int n,i,ch;
printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");
scanf("\n %d",&n);
for(i=0;i<n;i++){
printf("-----------------------------------------");
printf("\n\tEnter Details of Employee-%d",i+1);
printf("\n-----------------------------------------");
printf("\nName of Employee : ");
scanf("%s",emp[i].e.name);
printf("Employee-Id : ");
scanf("%d",&emp[i].e.eid);
printf("Salary : ");
scanf("%d",&emp[i].e.salary);
}
while(1) {
printf("-----------------------------------------\n");
printf("\t\tMenu\n");
printf("-----------------------------------------");
printf("\n 1:Search Employee by E-ID");
printf("\n 2:List of All Employee");
printf("\n 3:Display Employee Name whose Salary > 10000 ");
printf("\n 4:Exit");
printf("\n----------------------------------------\n");
printf("Enter Your Choice : ");
scanf("\n %d",&ch);
switch(ch) {
case 1: emp_search(n);
break;
case 2: display(n);
break;
case 3: greater(n);
break;
case 4: exit(0);

U23CS351-Problem Solving Using C Laboratory


}
}
return 0;
}

OUTPUT

RESULT
Thus, the C program to perform menu driven program for employee using union is executed
successfully.

U23CS351-Problem Solving Using C Laboratory

You might also like