Ex. 4 Lab Programs
Ex. 4 Lab Programs
You're playing UNO cards, suddenly a person is getting a rev card. Write a C program to
reverse the round by storing the number of players in the array.
Algorithm
Start
Initialize an array to store player IDs.
Prompt the user to enter the number of players (numPlayers).
Create a loop to input player IDs and store them in the array.
a. For i from 1 to numPlayers:
i. Prompt the user to enter the player ID for Player i.
ii. Read and store the player ID in the array at index i-1.
Display the current order of players.
Reverse the order of players in the array.
a. For i from 0 to numPlayers/2:
i. Swap the elements in the array at indices i and numPlayers - i - 1.
Display the reversed order of players.
End
Pseudocode:
1. START
2. DECLARE AN ARRAY PLAYER IDS OF SIZE 100
3. DISPLAY "ENTER THE NUMBER OF PLAYERS: "
4. READ NUMPLAYERS
5. FOR I = 1 TO NUMPLAYERS
A. DISPLAY "ENTER THE PLAYER ID FOR PLAYER " + I + ": "
B. READ PLAYERID
C. SET PLAYERIDS[I - 1] = PLAYERID
6. DISPLAY "CURRENT ORDER OF PLAYERS:"
7. FOR I = 1 TO NUMPLAYERS
A. DISPLAY "PLAYER " + I + ": " + PLAYERIDS[I - 1]
8. FOR I = 0 TO NUMPLAYERS/2
A. SWAP PLAYERIDS[I] AND PLAYERIDS[NUMPLAYERS - I - 1]
9. DISPLAY "REVERSED ORDER OF PLAYERS:"
10. FOR I = 1 TO NUMPLAYERS
A. DISPLAY "PLAYER " + I + ": " + PLAYERIDS[I - 1]
11. END
Flowchart:
Program :
#include <stdio.h>
int main() {
int numPlayers;
if (numPlayers <= 0) {
printf("Invalid number of players. Please enter a positive integer.\n");
return 1; // Exit with an error code
}
int players[numPlayers];
reverseRound(players, numPlayers);
return 0;
}
Output:
Enter the number of players: 5
Enter player IDs:
Player 1: 101
Player 2: 102
Player 3: 103
Player 4: 104
Player 5: 105
Current order of players:
Player 1: 101
Player 2: 102
Player 3: 103
Player 4: 104
Player 5: 105
Reversed order of players:
Player 1: 105
Player 2: 104
Player 3: 103
Player 4: 102
Player 5: 101
Exercise 4b
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.
Algorithm:
Start
Initialize two empty arrays: oddVehicles[] and evenVehicles[].
Prompt the user to enter the total number of vehicles: totalVehicles.
Loop for i from 1 to totalVehicles:
a. Prompt the user to enter the vehicle number: vehicleNumber.
b. If vehicleNumber is odd, add it to the oddVehicles[] array.
c. If vehicleNumber is even, add it to the evenVehicles[] array.
Prompt the user to enter the current day: currentDay.
If currentDay is an odd day:
a. Display the list of odd-number-ending vehicles (oddVehicles[]) that can use the road.
If currentDay is an even day:
a. Display the list of even-number-ending vehicles (evenVehicles[]) that can use the road.
End
Pseudocode:
Initialize empty arrays oddVehicles[], evenVehicles[]
Prompt user for totalVehicles
For i from 1 to totalVehicles:
Prompt user for vehicleNumber
If vehicleNumber is odd:
Add vehicleNumber to oddVehicles[]
Else:
Add vehicleNumber to evenVehicles[]
Prompt user for currentDay
If currentDay is odd:
Display oddVehicles[] as vehicles allowed today
Else:
Display evenVehicles[] as vehicles allowed today
Flowchart
Program:
#include <stdio.h>
int main() {
int oddVehicles[100], evenVehicles[100];
int oddCount = 0, evenCount = 0;
int vehicleNumber, day;
while (1) {
printf("Enter the vehicle number (0 to quit): ");
scanf("%d", &vehicleNumber);
if (vehicleNumber == 0) {
break;
}
printf("Enter the day (1 for odd, 2 for even): ");
scanf("%d", &day);
if (vehicleNumber % 2 == 0 && day == 2) {
evenVehicles[evenCount] = vehicleNumber;
evenCount++;
} else if (vehicleNumber % 2 != 0 && day == 1) {
oddVehicles[oddCount] = vehicleNumber;
oddCount++;
} else {
printf("Invalid input. Vehicle cannot use the road on this day.\n");
}
}
printf("Odd Number Ending Vehicles:\n");
for (int i = 0; i < oddCount; i++) {
printf("%d\n", oddVehicles[i]);
}
printf("Even Number Ending Vehicles:\n");
for (int i = 0; i < evenCount; i++) {
printf("%d\n", evenVehicles[i]);
}
return 0;
}
Output:
Vehicle Regulation System
Enter the vehicle number (0 to quit): 526
Enter the day (1 for odd, 2 for even): 1
Invalid input. Vehicles cannot use the road on this day.
Enter the vehicle number (0 to quit): 54562
Enter the day (1 for odd, 2 for even): 2
Enter the vehicle number (0 to quit): 4587
Enter the day (1 for odd, 2 for even): 1
Enter the vehicle number (0 to quit): 448
Enter the day (1 for odd, 2 for even): 1
Invalid input. Vehicles cannot use the road on this day.
Enter the vehicle number (0 to quit): 0
Odd Number Ending Vehicles:
4587
Even Number Ending Vehicles:
54562
Exercise 4c
Write a C program to do the following applications in array:
(i). Get a set of positive and negative integers from the user, replace
positive integers by 0 in the array.
(ii). Reverse the floating point numbers stored in the array.
(iii). Return the smallest value and largest value position in the array.
(iv). Search the number '5' in array and replace it with '10'
#include <stdio.h>
int main() {
int n;
// Get the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Declare an array of size n
float arr[n];
// Get input from the user and replace positive integers with 0
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
if (arr[i] > 0) {
arr[i] = 0;
}
}
// Find the smallest and largest values and their positions in the array
float smallest = arr[0];
float largest = arr[0];
int smallestPos = 0;
int largestPos = 0;
return 0;
}
Output:
Enter the number of elements in the array: 6
Enter the elements of the array:
3.5
-2.0
7.2
-4.8
5.0
1.2
Modified array:
1.20 10.00 -4.80 7.20 -2.00 0.00
Smallest value: -4.80 at position 3
Largest value: 10.00 at position 1
Exercise 4d
Write C program to do the following string handling applications. a.
(i). Get favourite actor and actress name, concatenate it and display
b.
(ii). Display your name in uppercase, lowercase and as fname and
lname. c.
(iii). Count the frequency of "the" in any sentence and delete it from
the sentence.
(iv). Check whether the given string is a palindrome or not.
ALGORITHM
Top of Form
Pseudo code
START
REPEAT
DISPLAY Menu
READ choice
SWITCH choice
CASE 1: CONCATENATE actor and actress names
CASE 5: EXIT
END SWITCH
UNTIL choice is 5
END
Flow chart
Program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to concatenate actor and actress names
void concatenateNames() {
scanf("%s", actor);
scanf("%s", actress);
strcat(actor, actress);
void formatName() {
char name[50];
scanf("%s", name);
void countAndDeleteThe() {
char sentence[1000];
int count = 0;
count++;
return 1; // Palindrome
int main() {
int choice;
do {
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
concatenateNames();
break;
case 2:
formatName();
break;
case 3:
countAndDeleteThe();
break;
case 4: {
char str[50];
scanf("%s", str);
if (isPalindrome(str)) {
} else {
break;
case 5:
break;
default:
return 0;
Output
Enter a sentence: The quick brown fox jumps over the lazy dog.
Frequency of 'the': 2
radar is a palindrome.
Exercise 4e
Write a C program for counting the total number of duplicate elements in
an array, print all the unique elements in the same array as two different
functions.
Aim:
To write a C program for counting the total number of duplicate elements in an array,
print all the unique elements in the same array as two different functions.
Algorithm:
Step 1: Input the size of an array and store into the size variable.
Step 2: Use a for loop to read the elements of an array and store them.
Step 3: To get the duplicate elements from an array, use two for loops. Set the first loop
start from 0 to size.
Step 4: Set loop to select each element of the array and compare with the corresponding
element to get the duplicate elements.
Step 5: Set for (j = i + 1; j < size; j++) and the code to find the subsequent same element
is: if (arr[i] == arr[j]).
Step 6: If any duplicate element is encountered, delete the duplicate element from an
array and the size of array is decremented by 1 such that, size = size - 1.
Step 7: Print the unique elements of an array.
Step 8: End.
Flowchart:
Program:
#include <stdio.h>
int main()
int i,j,k,elementCount,count=0;
scanf("%d",&elementCount);
for(i=0;i<elementCount;i++)
scanf("%d",&inputArray[i]);
for(i=0;i<elementCount;i++)
for(j=i+1;j<elementCount;j++)
if(inputArray[i]==inputArray[j])
count++;
for(k=j;k<elementCount-1;k++)
inputArray[k]=inputArray[k+1];
elementCount--;
j--;
}
}
for(i= 0;i<elementCount;i++)
printf("%d\t",inputArray[i]);
return 0;
Output:
Enter 4 numbers:2 3 2 4
Exercise 4f
Write a C program to sort the elements in an array in both ascending
and descending order using two different functions.
AIM: To write a C program to sort the elements in an array in both
ascending and descending order using two different functions.
Algorithm:
Step1: Start.
Step 2: Enter the array size and read as n.
Step 3: Enter the array elements and read as [i].
Step 4: Print the array elements before sorting.
Step 5: Take the first element(index = 0), compare the current element with the next
element.
Step 6: Describe function for Ascending order and If the current element is greater than
the next element, swap them in temp.
Step 7: Else, If the current element is less than the next element, then move to the next
element.
Step 8: Repeat Step 5 to Step 7 until all elements are sorted.
Step 9: Describe function for Descending order: If the current element is smaller than
the next element, swap them in temp.
Step 10: Else, If the current element is greater than the next element, then move to the
next element.
Step 11: Repeat Step 9 to Step 10 until all elements are sorted.
Step 12: Stop
Flowchart:
Program:
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for ascending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last
value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing array data after sorting
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for descending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] < a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last
value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing array data after sorting
{
printf(" %d ", a[i]); //Printing data
}
SAMPLE OUTPUT:
Array size: 10
Elements : 3 4 7 6 5 1 2 8 10 9
Ascending : 1 2 3 4 5 6 7 8 9 10
Descending : 10 9 8 7 6 5 4 3 2 1
Exercise 4g
Write a C program to find the largest and smallest number in an
array using recursion and to convert the output into a binary number.
ALGORITHM
3. Repeat from i = 2 to n
5. max = arr[i]
7. min = arr[i]
#include<stdio.h>
int main()
{
int a[50],i,n,min,max;
scanf(“%d”,&n);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);
max=min=a[0];
for(i=1;i<n;++i)
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
return 0;
SAMPLE OUTPUT:
Exercise 4h
Write a C program to swap two numbers using two functions, one
using pointers and the other one without using pointers.
AIM:
To write a C program to swap two numbers using two functions, one
using pointers and the other one without using pointers.
Flowchart:
Program:
#include <stdio.h>
// Function to swap two numbers without using pointers
void swapWithoutPointers(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Before swapping without pointers: num1 = %d, num2 = %d\n", num1, num2);
swapWithoutPointers(&num1, &num2);
printf("After swapping without pointers: num1 = %d, num2 = %d\n", num1, num2);
printf("Before swapping with pointers: num1 = %d, num2 = %d\n", num1, num2);
swapWithPointers(&num1, &num2);
printf("After swapping with pointers: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Output: