0% found this document useful (0 votes)
13 views65 pages

Problem Solving Techniques in C

The document is a lab manual for a course on problem-solving techniques using C programming. It includes various experiments that cover real-world applications, algorithms, flowcharts, pseudocode, and C programs for tasks such as calculating GST, finding factorials, generating Fibonacci series, and using loops. Each experiment provides a structured approach to problem-solving with detailed explanations and sample outputs.

Uploaded by

murali2007s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views65 pages

Problem Solving Techniques in C

The document is a lab manual for a course on problem-solving techniques using C programming. It includes various experiments that cover real-world applications, algorithms, flowcharts, pseudocode, and C programs for tasks such as calculating GST, finding factorials, generating Fibonacci series, and using loops. Each experiment provides a structured approach to problem-solving with detailed explanations and sample outputs.

Uploaded by

murali2007s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LAB MANUAL

PROBLEM SOLVING TECHNIQUES USING C


Course Code: S11BLH12

Submitted By

Student Name : ________________________________

Register Number: ________________________________

Class & Section : ________________________________


Table of Contents

Ex. No. Title

Real-World Applications using Algorithm, Flowchart, Pseudocode, and


1
Program

Implementation of Factorial and Fibonacci Series using Iteration and


2
Recursion in C

3 Application of Iterative Statements: For, While, and Do-While Loops

Decision-Making Programs in C Using If-Else, Nested If-Else, and If-


4
Else Ladder Statements

5 Implementation of Switch Case

6 Implementation of Arrays to Handle Group of Similar Data

7 Array Reversal

8 Array Separation into Odd and Even Numbers

9 Using Structures in C for Real-Time Data Management

10 Student Admission Eligibility Check using Structure


Ex. No: 1
Date :
Real-World Applications using Algorithm, Flowchart, Pseudocode, and
Program
1. Describe a simple real-world problem in your domain/department and describe it in the form
of problem statement, input, output and provide its solution in terms of algorithm, flowchart,
pseudo code and program.
Interpret problem statement, input, output for the following problem. And also Derive a
solution in terms of of algorithm, flowchart, pseudo code and program
a. To calculate Gst amount for a product (Based on the percentage given for gst)
b. To check the greatest of three numbers
c. To print n even numbers
Experiment 1(a): To calculate GST amount for a product

Aim
To calculate the GST (Goods and Services Tax) amount for a product based on the given
percentage and product base price, and to compute the final MRP.

Algorithm
1. Start
2. Read prod_base_price and tax_percent
3. Calculate Tax = prod_base_price * (tax_percent / 100)
4. Calculate prod_total_price = prod_base_price + Tax
5. Display Tax and prod_total_price
6. Stop

Pseudocode
BEGIN
INPUT prod_base_price
INPUT tax_percent
COMPUTE tax = prod_base_price * (tax_percent / 100)
COMPUTE prod_total_price = prod_base_price + tax
DISPLAY tax
DISPLAY prod_total_price
END

Flowchart

Program
#include <stdio.h>
#include <conio.h>

void main()
{
float base_price, tax_percent, tax, total_price;

clrscr(); // clear the screen (Turbo C specific)

printf("Enter the Product Base Price: ");


scanf("%f", &base_price);
printf("Enter the GST Percentage: ");
scanf("%f", &tax_percent);

// Calculate GST amount


tax = base_price * (tax_percent / 100);

// Calculate total price


total_price = base_price + tax;

printf("\nGST Amount = %.2f", tax);


printf("\nTotal Price (MRP) = %.2f", total_price);

getch(); // wait for a key press (Turbo C specific)


}

Sample Result
Input:
Product base price = 1000
GST percentage = 18
Output:
Tax = 1000 × (18/100) = 180
Product Total Price (MRP) = 1000 + 180 = 1180

Result
The program successfully calculates the GST amount and the final MRP of the product.

Experiment 1(b): To check greatest of three numbers


Aim
To write a program that reads three numbers and determines the greatest among them.
Algorithm
1. Start
2. Input three numbers: A, B, C
3. If A > B, then
o If A > C, print A is the greatest
o Else, print C is the greatest
4. Else (i.e., if B ≥ A), then
o If B > C, print B is the greatest
o Else, print C is the greatest
5. Stop

Pseudocode
BEGIN
READ a, b, c
IF (a > b) THEN
IF (a > c) THEN
DISPLAY "a is greatest"
ELSE
DISPLAY "c is greatest"
END IF
ELSE
IF (b > c) THEN
DISPLAY "b is greatest"
ELSE
DISPLAY "c is greatest"
END IF
END IF
END

Flowchart
Program
#include <stdio.h>
#include <conio.h>

void main()
{
int a, b, c;

clrscr(); // clear screen

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);

if (a > b)
{
if (a > c)
printf("Greatest number is: %d", a);
else
printf("Greatest number is: %d", c);
}
else
{
if (b > c)
printf("Greatest number is: %d", b);
else
printf("Greatest number is: %d", c);
}

getch(); // wait for key press


}

Sample output
Case 1:
Enter three numbers: 25 42 18
Greatest number is: 42
Case 2:
Enter three numbers: 10 5 20
Greatest number is: 20
Case 3 (all equal):
Enter three numbers: 15 15 15
Greatest number is: 15

Result :
The algorithm, pseudocode, and flowchart were implemented successfully. The program correctly
identifies and displays the greatest number among the three given inputs.

Experiment 1(c): Print n Even Numbers


Aim
To write a program that prints all even numbers up to a given limit n.

Algorithm
1. Start
2. Input value of n
3. Initialize i = 2
4. Check if i <= n
o If true, go to Step 5
o If false, go to Step 8
5. Print value of i
6. Increment i by 2
7. Go to Step 4
8. Stop

Pseudocode

BEGIN
GET n
INITIALIZE i = 2
WHILE (i <= n) DO
PRINT i
i=i+2
ENDWHILE
END

FLOWCHART
Program
#include <stdio.h>
#include <conio.h>

void main()
{
int n, i;

clrscr(); // clear screen

printf("Enter the limit: ");


scanf("%d", &n);

printf("Even numbers up to %d are:\n", n);

i = 2; // start with first even number


while (i <= n)
{
printf("%d ", i);
i = i + 2; // increment by 2
}

getch(); // wait for key press


}

Sample Output

Enter the limit: 10


Even numbers up to 10 are:
2 4 6 8 10

Result
The program was successfully executed. It accepts a limit n from the user and prints all even
numbers up to n. The output verified that the algorithm, pseudocode, and program logic are correct.
Ex. No : 2
Date :

Implementation of Factorial and Fibonacci Series using Iteration and Recursion


in C
2) Write an algorithm, flowchart, pseudo code followed by a simple C code to do find the
Factorial and Fibonacci series using both iteration and recursion.
Experiment 2(a): Factorial Function using Recursion
AIM
To write a C program to find the factorial of a number using recursion.

Algorithm
1. Start
2. Read a number n
3. Call the recursive function factorial(n)
4. Inside function:
o If n == 1, return 1
o Else return n * factorial(n-1)
5. Print the factorial result
6. Stop

Pseudocode
FUNCTION factorial(n)
IF n == 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n-1)
END FUNCTION

BEGIN
READ n
f = factorial(n)
PRINT f
END

FLOWCHART

Program
#include <stdio.h>

int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int n, f;
printf("Enter the number: ");
scanf("%d", &n);
f = factorial(n);
printf("Factorial of %d is %d", n, f);
return 0;
}

Sample Output :
Enter the number: 5
Factorial of 5 is 120

Result
The program was executed successfully. It calculates the factorial of a number using a recursive
function, and the output matches the expected result.

Experiment 2(b): Factorial Function using Iteration


AIM
To write a C program to find the factorial of a number using iteration.

Algorithm
1. Start
2. Read a number n
3. Initialize fact = 1
4. Repeat for i = 1 to n
o Multiply fact = fact * i
5. Print fact
6. Stop

Pseudocode
BEGIN
READ n
fact = 1
FOR i = 1 TO n DO
fact = fact * i
END FOR
PRINT fact
END
Flowchart

Program
#include <stdio.h>

int main() {
int n, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);

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


fact = fact * i;
}

printf("Factorial of %d is: %d", n, fact);


return 0;
}

Sample Output
Enter a number: 5
Factorial of 5 is: 120

Result
The program was successfully executed. It calculates the factorial of a number using an iterative
loop, and the result is correct.

Experiment 2(c): Fibonacci Series using Iteration


AIM
To write a C program to generate the Fibonacci series up to a given limit using iteration.

Algorithm
1. Start
2. Read the limit n
3. Initialize a = 0, b = 1
4. Print a and b
5. While (a + b <= n)
o t=a+b
o Print t
o Update a = b, b = t
6. Stop

Pseudocode
BEGIN
READ n
a=0
b=1
PRINT a, b
WHILE (a + b <= n) DO
t=a+b
PRINT t
a=b
b=t
END WHILE
END

Program
#include <stdio.h>

int main() {
int n, a = 0, b = 1, t;
printf("Enter the limit: ");
scanf("%d", &n);

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


if (n == 0)
printf("%d", a);
else
printf("%d %d", a, b);

while (a + b <= n) {
t = a + b;
printf(" %d", t);
a = b;
b = t;
}

return 0;
}

Flowchart
Sample Output
Enter the limit: 10
Fibonacci series up to 10:
0112358

Result
The program was executed successfully. It generates the Fibonacci sequence up to the given limit
using iteration.

Experiment 2(d): Fibonacci Series using Recursion


AIM
To write a C program to generate the Fibonacci series up to a given limit using recursion.

Algorithm
1. Start
2. Read the limit n
3. Define recursive function fibonacci(a, b, n)
o If a <= n
▪ Print a
▪ Call fibonacci(b, a+b, n)
o Else return
4. Call fibonacci(0, 1, n) from main
5. Stop

Pseudocode
FUNCTION fibonacci(a, b, n)
IF (a <= n) THEN
PRINT a
fibonacci(b, a+b, n)
END IF
END FUNCTION

BEGIN
READ n
PRINT "Fibonacci sequence up to n:"
CALL fibonacci(0, 1, n)
END

Program
#include <stdio.h>
void fibonacci(int a, int b, int n) {
if (a <= n) {
printf("%d ", a);
fibonacci(b, a + b, n);
}
}

int main() {
int n;
printf("Enter the limit: ");
scanf("%d", &n);

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


fibonacci(0, 1, n);

return 0;
}

Sample Output
Enter the limit: 10
Fibonacci series up to 10:
0112358

Result
The program was executed successfully.
It generates the Fibonacci series up to the given limit using recursion.
Ex. No 3
Date :
Application of Iterative Statements: For, While, and Do-While Loops

3. Describe a problem statement in your domain/department whose solution involves repetition


of same steps and provide code as solution involving for, while and do while loops
Experiment 3(a): Print the First n Natural Numbers using For Loop
AIM
To write a C program that prints the first n natural numbers using a for loop.
Algorithm
1. Start
2. Input the value of n (the number of natural numbers to be printed)
3. Initialize loop counter i = 1
4. Repeat for each i from 1 to n:
o Print i
o Increment i by 1
5. Stop
Pseudocode
BEGIN
INPUT n
FOR i = 1 TO n DO
PRINT i
ENDFOR
END

Program
#include <stdio.h>
#include <conio.h>

void main() {
int n, i;
clrscr(); // clear screen
printf("Enter the value of n: ");
scanf("%d", &n);
printf("First %d natural numbers are:\n", n);
for (i = 1; i <= n; i++) {
printf("%d ", i);
}
getch(); // wait for key press
}

Flowchart

Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345

Result
The program successfully prints the first n natural numbers using a for loop. The loop iterates from 1
to n and displays the numbers sequentially

Experiment 3(b): Print the First n Natural Numbers using While Loop
AIM
To write a C program that prints the first n natural numbers using a while loop.

Algorithm
1. Start
2. Input the value of n (the number of natural numbers to be printed)
3. Initialize counter i = 1
4. While i <= n do
o Print i
o Increment i by 1
5. Stop

Pseudocode
BEGIN
INPUT n
INITIALIZE i = 1
WHILE i <= n DO
PRINT i
i=i+1
ENDWHILE
END

Program
#include <stdio.h>
#include <conio.h>
void main() {
int n, i = 1;
clrscr(); // clear screen

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


scanf("%d", &n);

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


while (i <= n) {
printf("%d ", i);
i++;
}
getch(); // wait for key press
}

Flowchart
Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345

Result
The program was successfully executed. It prints the first n natural numbers using a while loop.

Experiment 3(c): Print the First n Natural Numbers using Do-While Loop
AIM
To write a C program that prints the first n natural numbers using a do-while loop.
Algorithm
1. Start
2. Input the value of n (the number of natural numbers to be printed)
3. Initialize counter i = 1
4. Do the following:
o Print i
o Increment i by 1
5. While i <= n
6. Stop

Pseudocode
BEGIN
INPUT n
INITIALIZE i = 1
DO
PRINT i
i=i+1
WHILE i <= n
END

Program
#include <stdio.h>
#include <conio.h>

void main() {
int n, i = 1;

clrscr(); // clear the screen

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


scanf("%d", &n);

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

do {
printf("%d ", i);
i++;
} while (i <= n);

getch(); // wait for key press


}

Flowchart
Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345

Result
The program was successfully executed. It prints the first n natural numbers using a do-while loop,
ensuring the loop body executes at least once.
Ex. No: 4
Date :
Decision-Making Programs in C Using If-Else, Nested If-Else, and If-Else Ladder
Statements

4. Describe a problem statement in your domain/department whose solution involves decision


making and provide code as solution involving if-else, nested if-else and ladder if-else.
Aim
To solve three decision-making problems using different control structures in C, including if-else,
nested if-else, and if-else ladder statements.
a. Write a program to determine if a person is eligible to vote based on their age using the if-else statement.
b. Write a program to determine if a given number is positive, negative, or zero using nested if-else statements.

c. Write a program to determine the grade of a student based on their marks using an if-else ladder.

Problem 1: Voting Eligibility (If-Else Statement)


Write a program to determine if a person is eligible to vote based on their age using the if-else statement.
Aim:
To solve three decision-making problems using different control structures in C, including if-else, nested if-
else, and if-else ladder statements.

Algorithm for Problem 1: Voting Eligibility (If-Else)


1. Start.
2. Read the age from the user.
3. If age is greater than or equal to 18, print "You are eligible for voting".
4. Else, print "You are not eligible for voting".
5. Stop.

Pseudocode:
START
INPUT age
IF age >= 18 THEN
PRINT "You are eligible for voting"
ELSE
PRINT "You are not eligible for voting"
END IF
STOP

C Program :
#include <stdio.h>
int main() {
int age;
// Read the age from the user
printf("Enter your age: ");
scanf("%d", &age);
// Decision making using if-else
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Flow chart

Result:
The program successfully determines if a person is eligible to vote based on their age using the if-else
statement.
Problem 2: Number Classification (Nested If-Else)
Write a program to determine if a given number is positive, negative, or zero using nested if-else statements.

Algorithm for Problem 2: Number Classification (Nested If-Else)


1. Start.
2. Read a number from the user.
3. If the number is greater than 0, print "The number is positive".
4. Else if the number is less than 0, print "The number is negative".
5. Else, print "The number is zero".
6. Stop.
Pseudocode:
START
INPUT number
IF number > 0 THEN
PRINT "Number is positive"
ELSE
IF number < 0 THEN
PRINT "Number is negative"
ELSE
PRINT "Number is zero"
END IF
END IF
STOP

C Program :
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
} else {
if (num < 0) {
printf("%d is negative.\n", num);
} else {
printf("%d is zero.\n", num);
}
}
return 0;
}
Flow chart

Result :
The program successfully determines if a given number is positive, negative, or zero using nested if-else
statements.

Problem 3: Grading System (If-Else Ladder)


Write a program to determine the grade of a student based on their marks using an if-else ladder.
Algorithm for Problem 3: Grading System (If-Else Ladder)
1. Start.
2. Read the marks of the student.
3. If marks are greater than or equal to 90, print "Your grade: A".
4. Else if marks are greater than or equal to 70 but less than 90, print "Your grade: B".
5. Else if marks are greater than or equal to 50 but less than 70, print "Your grade: C".
6. Else, print "Your grade: Failed".
7. Stop.

Pseudocode:
START
INPUT marks
IF marks >= 90 THEN
PRINT "Your grade: A"
ELSE IF marks >= 70 THEN
PRINT "Your grade: B"
ELSE IF marks >= 50 THEN
PRINT "Your grade: C"
ELSE
PRINT "Your grade: Failed"
END IF
STOP

C program
#include<stdio.h>
int main() {
int marks;
// Input the marks from the user
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
// If-Else Ladder to determine the grade
if (marks >= 90) {
printf("Your Grade: A\n");
} else if (marks >= 70) {
printf("Your Grade: B\n");
} else if (marks >= 50) {
printf("Your Grade: C\n");
} else {
printf("Your Grade: Failed\n");
}
return 0;
}

Result:
The program successfully determines the grade of a student based on their marks using an if-else ladder.
Ex. No : 5
Date :
Implementation of Switch Case
Aim:
To develop a simple scientific calculator using a switch-case statement that performs basic arithmetic
operations such as addition, subtraction, multiplication, division, and modulus.

Algorithm:
1. Start.
2. Display a menu with options for different operations (addition, subtraction, multiplication, division, and
modulus).
3. Read the user's choice of operation.
4. Based on the user's choice, use the switch-case statement to perform the respective operation:
o Case 1: Addition.
o Case 2: Subtraction.
o Case 3: Multiplication.
o Case 4: Division.
o Case 5: Modulus.
5. For each operation, ask the user to input two numbers.
6. Perform the chosen operation and display the result.
7. If the user enters an invalid choice, display an error message.
8. Stop.

Pseudocode:
sql
Copy code
START
DISPLAY menu with options for addition, subtraction, multiplication, division, modulus
INPUT choice
SWITCH choice
CASE 1:
INPUT two numbers
ADD the numbers
PRINT result
BREAK
CASE 2:
INPUT two numbers
SUBTRACT the numbers
PRINT result
BREAK
CASE 3:
INPUT two numbers
MULTIPLY the numbers
PRINT result
BREAK
CASE 4:
INPUT two numbers
DIVIDE the numbers
IF second number is zero THEN
PRINT error message
ELSE
PRINT result
BREAK
CASE 5:
INPUT two numbers
FIND modulus
PRINT result
BREAK
DEFAULT:
PRINT invalid choice
STOP
Flowchart:
C Program:
c
Copy code
#include <stdio.h>

int main() {
int choice;
double num1, num2, result;

// Display the menu


printf("Simple Scientific Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);

// Switch-case for different operations


switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;

case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;

case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;

case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
result = num1 / num2;
printf("Result: %.2lf\n", result);
}
break;

case 5:
printf("Enter two integers: ");
int int1, int2;
scanf("%d %d", &int1, &int2);
if (int2 == 0) {
printf("Error: Modulus by zero is not allowed.\n");
} else {
result = int1 % int2;
printf("Result: %d\n", (int)result);
}
break;
default:
printf("Invalid choice.\n");
}

return 0;
}

Sample Output:
Example 1: Addition
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 1
Enter two numbers: 10 20
Result: 30.00
Example 2: Division with Error Handling
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 4
Enter two numbers: 10 0
Error: Division by zero is not allowed.
Example 3: Modulus
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 5
Enter two integers: 10 3
Result: 1

Result:
The program successfully implements a simple scientific calculator using the switch-case statement. It can
perform addition, subtraction, multiplication, division, and modulus operations based on user input. The
program also handles invalid choices and division/modulus by zero errors.
Ex. No: 6
Date :
Implementation of Arrays to Handle Group of Similar Data
6. Describe a problem statement in your domain/department where you need to work with group
of same type of data. Provide a solution in terms of C program to store and manage the data
effectively.
Aim
To write a C program that demonstrates how to store and manage a group of data of the same type
(integers in this case) using an array.

Algorithm
1. Start.
2. Declare an array of integers values with size 5.
3. Prompt the user to enter 5 integers.
4. Use a for loop to read 5 integers from the user and store them in the array.
5. Display the stored integers using another for loop.
6. End.

Pseudo Code
BEGIN
DECLARE array `values` of size 5
PRINT "Enter 5 integers: "
FOR i = 0 to 4
READ input and STORE it in values[i]
END FOR

PRINT "Displaying integers: "


FOR i = 0 to 4
PRINT values[i]
END FOR
END
Flow chart

Program
#include <stdio.h>
#include <conio.h>

void main() {
int values[5];
int i;

clrscr(); // clear screen

// Input 5 integers
printf("Enter 5 integers: ");
for (i = 0; i < 5; i++) {
scanf("%d", &values[i]);
}

// Display stored integers


printf("Displaying integers:\n");
for (i = 0; i < 5; i++) {
printf("%d\n", values[i]);
}

getch(); // wait for key press


}

Sample Output
Enter 5 integers: 10 20 30 40 50
Displaying integers:
10
20
30
40
50

Result
The program successfully stores 5 integers entered by the user in an array and displays them as output.
This demonstrates effective data management of similar types using arrays in C.
Ex. No: 7
Date :
Array Reversal
7. You're playing UNO cards, suddenly a person is getting rev card. Write a C program to
reverse the round by storing the number of players in array.
Aim
To write a C program to reverse the order of players in an UNO game when a reverse card is drawn,
using arrays.
Algorithm
1. Start.
2. Prompt the user to enter the number of players and store it in n.
3. Declare an integer array players of size n.
4. Input player numbers into the array.
5. Display the original order of players.
6. Call the function reverse Array to reverse the order of the array:
o Swap the first element with the last, the second with the second last, and so on.
7. Display the reversed order of players.
8. End.

Pseudo Code
BEGIN
INPUT number of players (n)
DECLARE array `players` of size n
PRINT "Enter the player numbers:"
FOR i = 0 to n-1
READ players[i]
END FOR
PRINT "Original order of players:"
FOR i = 0 to n-1
PRINT players[i]
END FOR
CALL reverseArray(players, n):
FOR i = 0 to n/2
SWAP players[i] and players[n-i-1]
END FOR
RETURN
PRINT "Reversed order of players:"
FOR i = 0 to n-1
PRINT players[i]
END FOR
END
Flow chart
Sub program
Main program

Program
#include <stdio.h>
#include <conio.h>

// Function to reverse the order of the array


void reverseArray(int arr[], int n) {
int temp, i;
for (i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}

void main() {
int n, i;

clrscr(); // clear screen

// Input the number of players


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

// Declare an array to store player numbers


int players[50]; // fixed size for Turbo C (no variable length arrays)

// Input player numbers


printf("Enter the player numbers: ");
for (i = 0; i < n; i++) {
scanf("%d", &players[i]);
}

// Display the original order


printf("Original order: ");
for (i = 0; i < n; i++) {
printf("%d ", players[i]);
}

// Reverse the array


reverseArray(players, n);

// Display the reversed order


printf("\nReversed order: ");
for (i = 0; i < n; i++) {
printf("%d ", players[i]);
}

getch(); // wait for key press


}

Sample Output
Enter the number of players: 5
Enter the player numbers: 1 2 3 4 5
Original order: 1 2 3 4 5
Reversed order: 5 4 3 2 1

Result
The program effectively reverses the order of players in an UNO game using arrays, mimicking the
effect of a reverse card.
Ex. No: 8
Date :
Array Separation into Odd and Even Numbers
8. Write a C program for Vehicle Regulation System where odd number ending vehicles can
use the road on odd days and even number ending vehicles can use the road on even days using
two separate arrays to store and display the odd and even numbers.

Aim
To develop a C program for a vehicle regulation system where vehicles ending with an odd number
can use the road on odd days, and vehicles ending with an even number can use the road on even
days, using two separate arrays to store and display odd and even vehicle numbers.
Algorithm
1. Start.
2. Prompt the user to input the number of vehicles (n).
3. Declare three arrays:
o arr1[] to store all vehicle numbers.
o arr2[] to store even-numbered vehicles.
o arr3[] to store odd-numbered vehicles.
4. Loop through to input n vehicle numbers into arr1[].
5. For each number in arr1[]:
o If the number is even, store it in arr2[].
o If the number is odd, store it in arr3[].
6. Display the contents of arr2[] (vehicles allowed on even days).
7. Display the contents of arr3[] (vehicles allowed on odd days).
8. End.
Pseudocode
BEGIN
INPUT number of vehicles (n)
DECLARE arr1[n], arr2[n], arr3[n], j = 0, k = 0
PRINT "Enter vehicle numbers:"
FOR i = 0 TO n-1
INPUT arr1[i]
END FOR
FOR i = 0 TO n-1
IF arr1[i] % 2 == 0 THEN
arr2[j] = arr1[i]
INCREMENT j
ELSE
arr3[k] = arr1[i]
INCREMENT k
END IF
END FOR
PRINT "Vehicles allowed on Even days:"
FOR i = 0 TO j-1
PRINT arr2[i]
END FOR
PRINT "Vehicles allowed on Odd days:"
FOR i = 0 TO k-1
PRINT arr3[i]
END FOR
END
Program
#include <stdio.h>
#include <conio.h>

void main() {
int arr1[10], arr2[10], arr3[10];
int i, j = 0, k = 0, n;

clrscr(); // clear screen

printf("\nVehicle regulation system allows vehicles to use the road on specific days:\n");
printf("-----------------------------------------------------------------\n");

// Input the number of vehicles


printf("Enter the number of vehicle numbers to be stored in the array: ");
scanf("%d", &n);

// Input vehicle numbers


printf("Enter %d vehicle numbers in the array:\n", n);
for (i = 0; i < n; i++) {
printf("Vehicle number-%d: ", i + 1);
scanf("%d", &arr1[i]);
}

// Separate odd and even vehicles


for (i = 0; i < n; i++) {
if (arr1[i] % 2 == 0) {
arr2[j] = arr1[i];
j++;
} else {
arr3[k] = arr1[i];
k++;
}
}

// Display vehicles allowed on even days


printf("\nThe vehicles that can use the road on Even days are:\n");
for (i = 0; i < j; i++) {
printf("%d ", arr2[i]);
}

// Display vehicles allowed on odd days


printf("\nThe vehicles that can use the road on Odd days are:\n");
for (i = 0; i < k; i++) {
printf("%d ", arr3[i]);
}

getch(); // wait for key press


}
Flow chart

Sample Output
Enter the number of vehicle numbers to be stored in the array: 6
Enter 6 vehicle numbers in the array:
Vehicle number-1: 1234
Vehicle number-2: 5679
Vehicle number-3: 2468
Vehicle number-4: 1357
Vehicle number-5: 1122
Vehicle number-6: 7789

The vehicles that can use the road on Even days are:
1234 2468 1122

The vehicles that can use the road on Odd days are:
5679 1357 7789

Result
The program successfully separates odd and even vehicle numbers into two arrays and displays
them. Vehicles ending in:
• Even numbers can use the road on even days.
• Odd numbers can use the road on odd days
Ex. No: 9
Date :
Using Structures in C for Real-Time Data Management

9. Describe a problem statement in your domain/department where you need to work with group
of different type of data. Provide a solution in terms of C program to store and manage the data
effectively
Aim
To write a C program using structures to store and manage student details (name, roll number, and
marks) effectively.

Algorithm
1. Start
2. Define a structure student with fields:
o Name (string)
o roll (integer)
o marks (float)
3. Declare an array of student structure to store details of multiple students.
4. Loop through each student and:
o Assign roll number automatically
o Input name
o Input marks
5. Display all stored student details in a formatted manner.
6. Stop

Pseudocode
BEGIN
DEFINE structure student (Name, roll, marks)
DECLARE array of students

FOR each student i = 1 to 3 DO


roll = i
INPUT Name
INPUT Marks
END FOR

PRINT "Displaying Information"


FOR each student i = 1 to 3 DO
PRINT roll
PRINT Name
PRINT Marks
END FOR
END

Flowchart
Program
#include <stdio.h>
#include <conio.h>

// Define structure for student


struct student {
char Name[50];
int roll;
float marks;
} s[3];

void main() {
int i;

clrscr(); // Clear screen


printf("Enter information of students:\n");

// Input details for each student


for (i = 0; i < 3; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number %d,\n", s[i].roll);

printf("Enter name: ");


scanf("%s", s[i].Name);

printf("Enter marks: ");


scanf("%f", &s[i].marks);
}

// Display the student details


printf("\nDisplaying Information:\n\n");
for (i = 0; i < 3; ++i) {
printf("Roll number: %d\n", s[i].roll);
printf("Name: %s\n", s[i].Name);
printf("Marks: %.1f\n\n", s[i].marks);
}

getch(); // Wait for key press


}

Sample Output
Enter information of students:

For roll number 1,


Enter name: Arjun
Enter marks: 85.5

For roll number 2,


Enter name: Divya
Enter marks: 90.0

For roll number 3,


Enter name: Kiran
Enter marks: 78.5

Displaying Information:

Roll number: 1
Name: Arjun
Marks: 85.5

Roll number: 2
Name: Divya
Marks: 90.0

Roll number: 3
Name: Kiran
Marks: 78.5

Result
The program was successfully executed. It demonstrates how to use structures in C to store and
manage a group of different data types (string, integer, float) together, effectively handling student
details.
Ex. No: 10
Date :
Student Admission Eligibility Check using Structure

10. Write a C program to get the details of the student (roll no, name, date of birth, state, 10th
percentage and 12th percentage) using Structure. Calculate the age of the student and display
the eligibility status for his admission. Eligibility criteria: more than 60 percent in 10th and 12th,
age>=17, state==TN
Aim
To create a C program to collect and display student details, calculate their age, and determine their
eligibility for admission based on specific criteria.

Algorithm
1. Start.
2. Define a structure Student to store student details:
o Roll No.
o Name.
o Date of Birth (DOB).
o State.
o 10th percentage.
o 12th percentage.
3. Write a function calculateAge to compute age from the given DOB.
4. In the main function:
o Declare a variable of type Student.
o Prompt the user to enter the student's details.
o Call calculateAge to calculate the student's age.
5. Check eligibility criteria:
o Age should be greater than or equal to 17.
o Both 10th and 12th percentages should be greater than or equal to 60.
o State should be "TN".
6. Display the eligibility status based on the conditions.
7. Print all the entered and calculated student details.
8. End.
Pseudo Code
BEGIN
DEFINE structure `Student` with fields: rollNo, name, dob, state, tenthPercentage,
twelfthPercentage.

FUNCTION calculateAge(dob):
PARSE dob into year, month, day.
GET current year.
RETURN current year - year of birth.

MAIN:
DECLARE student of type Student.
INPUT student details (rollNo, name, dob, state, percentages).

CALL calculateAge([Link]) to get age.

IF (age >= 17) AND ([Link] >= 60.0) AND ([Link] >=
60.0) AND (state == "TN"):
PRINT "Eligible for Admission".
ELSE:
PRINT "Not Eligible for Admission".

DISPLAY student details (rollNo, name, dob, state, percentages, age).


END

Program
#include <stdio.h>
#include <conio.h>
#include <string.h>
// Define structure for student
struct Student {
int rollNo;
char name[50];
int age;
char state[50];
float tenthPercentage;
float twelfthPercentage;
};

void main() {
struct Student student;

clrscr(); // clear screen

// Input student details


printf("Enter Roll No: ");
scanf("%d", &[Link]);
printf("Enter Name: ");
scanf("%s", [Link]);
printf("Enter Age: ");
scanf("%d", &[Link]);
printf("Enter State: ");
scanf("%s", [Link]);
printf("Enter 10th Percentage: ");
scanf("%f", &[Link]);
printf("Enter 12th Percentage: ");
scanf("%f", &[Link]);
// Check eligibility
printf("\nEligibility Status: ");
if ([Link] >= 17 &&
[Link] >= 60.0 &&
[Link] >= 60.0 &&
strcmp([Link], "TN") == 0) {
printf("Eligible for Admission\n");
} else {
printf("Not Eligible for Admission\n");
}

// Display student details


printf("\nStudent Details:\n");
printf("Roll No: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("State: %s\n", [Link]);
printf("10th Percentage: %.2f\n", [Link]);
printf("12th Percentage: %.2f\n", [Link]);

getch(); // wait for key press


}
Flow chart
Sub program

Sample Output
Enter Roll No: 101
Enter Name: Arjun
Enter Age: 18
Enter State: TN
Enter 10th Percentage: 85
Enter 12th Percentage: 82

Eligibility Status: Eligible for Admission

Student Details:
Roll No: 101
Name: Arjun
Age: 18
State: TN
10th Percentage: 85.00
12th Percentage: 82.00

Result
The program accurately calculates the age and determines the eligibility of the student for admission
based on the given criteria.

You might also like