0% found this document useful (0 votes)
27 views92 pages

LAB PSAP Problem Statements-1

The document contains a series of programming exercises for a lab exam, focusing on string manipulation, array operations, and basic algorithms in C. Each exercise includes a description, sample input/output, and corresponding code implementations. Topics range from counting characters in a string to validating parentheses, reversing strings, and generating Fibonacci series.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views92 pages

LAB PSAP Problem Statements-1

The document contains a series of programming exercises for a lab exam, focusing on string manipulation, array operations, and basic algorithms in C. Each exercise includes a description, sample input/output, and corresponding code implementations. Topics range from counting characters in a string to validating parentheses, reversing strings, and generating Fibonacci series.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 92

FY-PSAP LAB EXAM AY:2023-24 SEM-2

E01
Given a string, S , consisting of alphabets and digits, find the number of
alphabets and digits in the given string.
Test Case 1:
Sample Input: a11472o5t6

Number of digits: 7

Number of alphabets:3

Code

#include <stdio.h>

int main() {

char S[] = "a11472o5t6"; // Sample input

int alphabets = 0, digits = 0;


FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (int i = 0; S[i] != '\0'; i++) {

if ((S[i] >= 'a' && S[i] <= 'z') || (S[i] >= 'A' && S[i] <= 'Z'))

alphabets++;

else if (S[i] >= '0' && S[i] <= '9')

digits++;

printf("Number of digits: %d\n", digits);

printf("Number of alphabets: %d\n", alphabets);

return 0;

}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E02
Given a string s containing just the characters like:
'(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if number of opening and closing brackets is same
(irrespective of the sequence of opening and closing brackets)
Test Case 1:
Sample Input: ( )
Sample Output: Valid

Test Case 2:
Sample Input: ( )[ ]{ }
Sample Output: Valid

Test Case 3:
Sample Input: ( [ { }
Sample Output: Invalid

Code
#include <stdio.h>

int main() {
char s[] = "( )"; // Sample input
int count = 0; // Counter for brackets

// Iterate through the string


for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

count++; // Increment for opening bracket


} else if (s[i] == ')' || s[i] == '}' || s[i] == ']') {
count--; // Decrement for closing bracket
}
}

// Check if count is zero


if (count == 0) {
printf("Sample Output: Valid\n");
} else {
printf("Sample Output: Invalid\n");
}

return 0;
}
E03
Given a string s, perform following operations on a string using function
(Without pointer):
1. Find a length of a string.
2. Print string in reverse order.
3. Copy string s into s1 and print it.
4. Accept another string, say s2. Concatenate s and s2 and print
concatenated string.
Code
#include <stdio.h>
#include <string.h> // Include the string.h library for string functions
FY-PSAP LAB EXAM AY:2023-24 SEM-2

// Function to find the length of a string


int stringLength(char s[]) {
return strlen(s); // Use the strlen function from string.h
}

// Function to print a string in reverse order


void printReverse(char s[]) {
int length = stringLength(s);
for (int i = length - 1; i >= 0; i--) {
printf("%c", s[i]);
}
printf("\n");
}

// Function to copy string s into s1


void copyString(char s[], char s1[]) {
strcpy(s1, s); // Use the strcpy function from string.h
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

// Function to concatenate two strings


void concatenateStrings(char s[], char s2[], char result[]) {
strcpy(result, s); // Copy the first string
strcat(result, s2); // Concatenate the second string
}

int main() {
char s[] = "Hello";
char s1[100], s2[] = "World", result[200];

// Find the length of the string


printf("Length of the string: %d\n", stringLength(s));

// Print the string in reverse order


printf("String in reverse order: ");
printReverse(s);

// Copy string s into s1 and print it


copyString(s, s1);
printf("Copied string: %s\n", s1);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

// Concatenate s and s2 and print the concatenated string


concatenateStrings(s, s2, result);
printf("Concatenated string: %s\n", result);

return 0;
}

E04
Given a string s, perform following operations on a string using function
(With pointer):
1. Find a length of a string.
2. Print string in reverse order.
3. Copy string s into s1 and print it.
Code
#include <stdio.h>
#include <string.h> // Include the string.h library for string functions

// Function to find the length of a string using a pointer


int stringLength(const char *s) {
const char *ptr = s;
while (*ptr) ptr++; // Increment pointer until we reach the null
terminator
FY-PSAP LAB EXAM AY:2023-24 SEM-2

return ptr - s; // The length is the difference between the pointers


}

// Function to print a string in reverse order using a pointer


void printReverse(const char *s) {
int len = stringLength(s);
const char *ptr = s + len - 1; // Set pointer to the last character of the
string
while (ptr >= s) { // Continue until the pointer reaches the start of the
string
printf("%c", *ptr);
ptr--; // Decrement the pointer
}
printf("\n");
}

// Function to copy string s into s1 using pointers


void copyString(const char *source, char *destination) {
while (*source) { // While the character source points to is not a null
terminator
*destination = *source; // Copy the character
source++; // Move to the next character in the source
destination++; // Move to the next position in the destination
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

*destination = '\0'; // Null-terminate the destination string


}

int main() {
char s[] = "Hello";
char s1[100];

// Find the length of the string


printf("Length of the string: %d\n", stringLength(s));

// Print the string in reverse order


printf("String in reverse order: ");
printReverse(s);

// Copy string s into s1 and print it


copyString(s, s1);
printf("Copied string: %s\n", s1);

return 0;

}E05
Given a string s, return true if it a Palindrome or false otherwise.
Test Cases:
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Case 1:
Input: MADAM
Output: true
Case 2:
Input: CAR
Output: false

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
char s[20];
printf("Enter the string: ");
scanf("%s", s);
int count = 0;

// Count the length of the string


for(int a = 0; s[a] != '\0'; a++){
count++;
}

int i = 0, j = count - 1, flag = 1;


while(i < j){
if(s[i] != s[j]){
flag = 0;
break;
}
i++;
j--;
}
if(flag == 0){
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("The string is not a palindrome");


}
else{
printf("The string is a palindrome");
}
return 0;
}
E06
Accept limit of array from user. As per the limit, read integer elements in
array. Then print :
1. Minimum, Maximum number from array.
2. Search for a particular number from array.
Code
#include <stdio.h>

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

int arr[n];
printf("Enter %d integers:\n", n);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

// Loop to accept each element of the array


for(int i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
// max element
int max = arr[0];
for( int j = 1;j<n;j++){
if(max < arr[j] ){
max = arr[j];
}
}
printf("\nThe max value is %d",max);
// min value
int min = arr[0];
for(int k = 1; k<n;k++){
if( min > arr[k]){
min = arr[k];
}
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("\nThe min value is %d",min);

// Search for a particular number from array.


int value,flag = 1;
printf("\nEnter the value to search : ");
scanf("%d",&value);
for(int a = 0;a<n;a++){
if(value == arr[a]){
flag = 0;
break;
}
}
if(flag == 1){
printf("\n not present");
}
else{
printf("\n present");
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;
}

E07
You are given a string s and an integer array index of the same length. The
string s will be shuffled such that the character at the ith position moves
to indices[i] in the shuffled string.
Return the shuffled string.

Test Cases:
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

Input: s = "abc", indices = [0,1,2]


Output: "abc"
Explanation: After shuffling, each character remains in its position.

Code
#include <stdio.h>
#include <stdlib.h>

int main() {
char s[20], temp;
int n = 0, i, k;

printf("Enter the string: ");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

scanf("%s", s);

// Count the length of the string


for(i = 0; s[i] != '\0'; i++) {
n++;
}

int arr[n];
char shuffled[n+1]; // +1 for the null terminator

// Loop to accept each element of the array


for(i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}

// Creating the shuffled string


for(k = 0; k < n; k++) {
if(arr[k] < n) {
shuffled[k] = s[arr[k]];
} else {
printf("\nIndex out of bounds.\n");
return 1; // Exit if an index is out of bounds
}
}
shuffled[n] = '\0'; // Null-terminate the shuffled string

printf("\nThe shuffled string: %s\n", shuffled);

return 0;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E08
1. Write a Program to print the output like:
A
A B
A B C
A B C D
A B C D E
A B C D
A B C
A B
A

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
for(int i = 65;i<=69;i++){
for(int j = 65 ; j<=i;j++ ){
printf("%c",j);
}
printf("\n");
}
for(int a = 65;a<=69;a++){
for(int b = 1 ; b<=69-a;b++){
printf("%c",64+b);
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("\n");
}
}
2. Write a program to print factorial of 1 to 10 numbers.
Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n ,fact =1;
printf("Enter the value :");
scanf("%d",&n);
for(int i = 1;i<=n;i++){
fact = fact*i;
}
printf("\n factorial of %d is : %d",n,fact);
return 0;
}

E09
1. Write a Program to print the output like:
FY-PSAP LAB EXAM AY:2023-24 SEM-2

1
12
123
12345
E D C B A
E D C B
E D C
E D
E
Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
for(int i = 1;i<6;i++){
for(int j = 1;j<=i;j++){
printf("\%d",j);
}
printf("\n");
}
for(int k = 69; k >= 65; k--){
for(int l = 65; l <= k; l++){
printf("%c ", k);
}
printf("\n");
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;
}

2. Write a program to print prime numbers between two numbers given


by user.
Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b;
printf("Enter lower limit :");
scanf("%d",&a);
printf("Enter upper limit :");
scanf("%d",&b);
printf("\nPrime numbers between %d and %d are ",a,b);
for(int i = a;i<=b;i++){
int store = 1;
for(int x=2;x<i;x++){
if(i%x==0){
FY-PSAP LAB EXAM AY:2023-24 SEM-2

store = 0;
break;
}
}
if(store){
printf("\n%d",i);
}
}

return 0;
}

E10
Write a C program to find the frequency of each character in a string.
Test Cases: String: This book is very good
Frequency of T:1
Frequency of h:0
Frequency of o:4 and so on for every distinct character.
Code
#include <stdio.h>
#include <ctype.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-2

#define MAX_CHAR 256

int main() {
char str[] = "This book is very good";
int freq[MAX_CHAR] = {0};
int i;

// Convert string to uppercase for case-insensitivity


for(i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}

// Count frequency of each character


for(i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
freq[str[i]]++;
}
}

// Display the frequency of each character


printf("String: %s\n", str);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (i = 0; i < MAX_CHAR; i++) {


if (freq[i] != 0) {
printf("Frequency of %c: %d\n", i, freq[i]);
}
}

return 0;
}

E11
Write a C program to print Fibonacci series up to n
terms.

Test Cases: No. of terms 10


Output: 0,1,1,2,3,5,8,13,21,34
Code

#include <stdio.h>

#include <stdlib.h>

int main() {

int n,a = 0,b = 1,c;

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


FY-PSAP LAB EXAM AY:2023-24 SEM-2

scanf("%d",&n);

printf("%d %d ",a,b);

for(int i = 3;i<=n;i++){

c=a+b;

printf("%d ",c);

a=b;

b=c;

return 0;

Empty Page
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E12
Write a C program to count the number of Vowels and
Consonants.
Test Cases: String: C is a programming language.

Vowels: 9

Consonants: 14

Code

#include <stdio.h>

#include <stdlib.h>

int main() {

char str[50];

printf("Enter the string: ");

scanf("%[^\n]", str);

while ((getchar()) != '\n');

int consonant = 0, vowel = 0, space = 0,dot = 0;


FY-PSAP LAB EXAM AY:2023-24 SEM-2

for(int i = 0; str[i] != '\0'; i++) {

if(str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' ||

str[i] == 'i' || str[i] == 'I' || str[i] == 'o' || str[i] == 'O' ||

str[i] == 'u' || str[i] == 'U') {

vowel++;

} else if (str[i] == ' ') {

space++;

else if (str[i] == '.') {

dot++;

else {

consonant++;

printf("Vowels = %d\n", vowel);

printf("Consonants = %d\n", consonant);


FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;

E13
Write a program to convert decimal to binary
number.
Test Cases:
Input : 5
Output: 101
Code

#include <stdio.h>

#include <stdlib.h>

int main() {

int n,b;

printf("Enter the number : ");

scanf("%d",&n);

while(n>0){

b = n%2;

printf("%d",b);

n= n/2;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;

E14
Write a C program to reverse an array using
pointers.
#include <stdio.h>

void reverseArray(int *arr, int size) {


int *left = arr; // Pointer to the start of the array
int *right = arr + size - 1; // Pointer to the end of the array
while (left < right) {
// Swap the values pointed by left and right
int temp = *left;
*left = *right;
*right = temp;

// Move the pointers towards the center


left++;
right--;
}
}

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);
int arr[n];

printf("Enter the elements of the array:\n");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (int i = 0; i < n; i++) {


scanf("%d", &arr[i]);
}

reverseArray(arr, n); // Call the function to reverse the array

printf("Reversed array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

E15
Write a function to print all perfect numbers in a
given interval in C programming.

Test Cases:1) Enter lower limit to print perfect numbers:1


Enter Upper limit to print perfect numbers:10000
All perfect numbers between 1 to 10000 are:
6,28,496,8128
2) Enter lower limit to print perfect numbers: 23
Enter upper limit to print perfect numbers: 450
All perfect numbers between 23 to 450 are:
28
3) Enter lower limit to print perfect numbers: 15
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Enter upper limit to print perfect numbers: 70


All perfect numbers between 15 to 70 are: 28
Code
#include <stdio.h>
#include <stdlib.h>
int isPerfect(int num) {
int sum = 0;
for(int i = 1; i < num; i++) {
if(num % i == 0) {
sum += i;
}
}
return sum == num;
}
void printPerfectNumbers(int lower, int upper) {
printf("\nAll perfect numbers between %d and %d are: ", lower, upper);
for(int i = lower; i <= upper; i++) {
if(isPerfect(i)) {
printf("%d ", i);
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

}
printf("\n");
}

int main() {
int upper, lower;
printf("Enter lower bound: ");
scanf("%d", &lower);
printf("Enter upper bound: ");
scanf("%d", &upper);
printPerfectNumbers(lower, upper);

return 0;
}

E16
Write a C Program to read and print name and
other details like mobile number, marks of 5
subjects of n number of students using
Structure. Print data of top 5 students ( top 5
FY-PSAP LAB EXAM AY:2023-24 SEM-2

should be calculated based on the entered


marks)
Code

#include <stdio.h>

#include <stdlib.h>

typedef struct {

char name[50];

char mobile_number[15];

int maths_marks;

int english_marks;

int psap_marks;

int mad_marks;

int bt_marks;

int total_marks;

} student;

int compare(const void *s1, const void *s2) {

student *stu1 = (student *)s1;

student *stu2 = (student *)s2;

return stu2->total_marks - stu1->total_marks;

}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

int main() {

int n;

printf("Enter the number of students: ");

scanf("%d", &n);

student *students = malloc(n * sizeof(student));

for (int i = 0; i < n; i++) {

printf("Enter details for student %d\n", i + 1);

printf("Enter name: ");

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

printf("Enter mobile number: ");

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

printf("Enter maths marks: ");

scanf("%d", &students[i].maths_marks);

printf("Enter English marks: ");

scanf("%d", &students[i].english_marks);

printf("Enter PSAP marks: ");

scanf("%d", &students[i].psap_marks);

printf("Enter MAD marks: ");

scanf("%d", &students[i].mad_marks);

printf("Enter BT marks: ");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

scanf("%d", &students[i].bt_marks);

students[i].total_marks = students[i].maths_marks + students[i].english_marks +


students[i].psap_marks + students[i].mad_marks + students[i].bt_marks;

// Sorting the students by total marks in descending order

qsort(students, n, sizeof(student), compare);

// Printing the top 5 students or all students if there are less than 5

int top_students = n < 5 ? n : 5;

printf("\nTop %d Students:\n", top_students);

for (int i = 0; i < top_students; i++) {

printf("Name: %s, Mobile: %s, Total Marks: %d\n", students[i].name,


students[i].mobile_number, students[i].total_marks);

free(students);

return 0;

E18
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Write a C program to store student data(roll no, name,


marks of 5 subjects) in structure. Then calculate the
grade of a student using a logical operator.

(76-99 Distinction
60-75 First Class
50-59 Second Class
40-49 Pass Class
Below 40 Fail)
Code
#include <stdio.h>

typedef struct {
int roll_no;
char name[50];
int marks[5];
} Student;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

const char* calculateGrade(int average) {


if (average >= 76 && average <= 99) {
return "Distinction";
} else if (average >= 60 && average < 76) {
return "First Class";
} else if (average >= 50 && average < 60) {
return "Second Class";
} else if (average >= 40 && average < 50) {
return "Pass Class";
} else {
return "Fail";
}
}

int main() {
Student s;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

int total = 0;
printf("Enter roll number: ");
scanf("%d", &s.roll_no);
printf("Enter name: ");
scanf("%s", s.name);
for (int i = 0; i < 5; i++) {
printf("Enter marks for subject %d: ", i + 1);
scanf("%d", &s.marks[i]);
total += s.marks[i];
}
int average = total / 5;
printf("Student Roll No: %d\n", s.roll_no);
printf("Name: %s\n", s.name);
printf("Average Marks: %d\n", average);
printf("Grade: %s\n", calculateGrade(average));
return 0;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E19

Write a c program for swapping of two arrays using


call by value function.
Code

#include <stdio.h>

void swapArrays(int **arr1, int **arr2) {

int *temp = *arr1;

*arr1 = *arr2;

*arr2 = temp;

int main() {

int arr1[] = {1, 2, 3, 4, 5};

int arr2[] = {6, 7, 8, 9, 10};

int n1 = sizeof(arr1) / sizeof(arr1[0]);

int n2 = sizeof(arr2) / sizeof(arr2[0]);

// Pointers to the arrays

int *ptr1 = arr1;


FY-PSAP LAB EXAM AY:2023-24 SEM-2

int *ptr2 = arr2;

// Swap the arrays

swapArrays(&ptr1, &ptr2);

// Print the swapped arrays

printf("After swapping:\n");

printf("Array 1: ");

for (int i = 0; i < n1; i++) {

printf("%d ", ptr1[i]);

printf("\nArray 2: ");

for (int i = 0; i < n2; i++) {

printf("%d ", ptr2[i]);

printf("\n");

return 0;

E20
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Write a C program to count the number of digits in


an integer. Then print addition of all digits in the
given number.

Test Cases:
1) input: 671041

output:
Number of digits:6
Addition of digits:19
Code
#include <stdio.h>
#include <stdlib.h>

int main() {
int n,store, sum =0,count = 0;
printf("Enter the number :");
scanf("%d",&n);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

while(n>0){
count++;
store = n%10;
sum = sum + store;
n = n/10;
}
printf("\nNumber of digits: %d",count);
printf("\nAddition of digits: %d",sum);

return 0;
}

E21
FY-PSAP LAB EXAM AY:2023-24 SEM-2

1. Find the sum of two one-dimensional arrays


using Dynamic Memory Allocation and functions.
Array should be passed as a function argument and
in function should perform addition of passed
arrays.
Code

#include <stdio.h>

#include <stdlib.h>

// Function to add two arrays

void addArrays(int *arr1, int *arr2, int *sum, int size) {

for (int i = 0; i < size; i++) {

sum[i] = arr1[i] + arr2[i];

int main() {

int *arr1, *arr2, *sum;

int size;

printf("Enter the size of the arrays: ");

scanf("%d", &size);

// Dynamically allocate memory for the arrays


FY-PSAP LAB EXAM AY:2023-24 SEM-2

arr1 = (int *)malloc(size * sizeof(int));

arr2 = (int *)malloc(size * sizeof(int));

sum = (int *)malloc(size * sizeof(int));

if (arr1 == NULL || arr2 == NULL || sum == NULL) {

printf("Memory not allocated.\n");

exit(0);

printf("Enter elements of first array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr1[i]);

printf("Enter elements of second array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr2[i]);

// Call the function to add the arrays

addArrays(arr1, arr2, sum, size);

printf("Sum of the two arrays:\n");

for (int i = 0; i < size; i++) {


FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("%d ", sum[i]);

printf("\n");

// Free the dynamically allocated memory

free(arr1);

free(arr2);

free(sum);

return 0;

E22

Perform following operations on 2D Matrix:


2. Accept number of rows and columns of two
matrices and read elements of both matrices.
3. Print Transpose of both matrices.
4. Print Diagonal elements of both matrices.
Code

#include <stdio.h>
#include <stdlib.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-2

int main() {
int c1, r1, c2, r2;
printf("Enter rows and column of first matrix:\
n");
scanf("%d %d", &r1, &c1);
printf("Enter the rows and column of second
matrix:\n");
scanf("%d %d", &r2, &c2);

int arr1[r1][c1], arr2[r2][c2];


// matrix 1
printf("Enter the elements of matrix 1:\n");
for(int i = 0; i < r1; i++) {
for(int j = 0; j < c1; j++) {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

scanf("%d", &arr1[i][j]);
}
}

// matrix 2
printf("Enter the elements of matrix 2:\n");
for(int i = 0; i < r2; i++) {
for(int j = 0; j < c2; j++) {
scanf("%d", &arr2[i][j]);
}
}
// Transpose of matrix 1
printf("Transpose of first matrix:\n");
for(int i = 0; i < c1; i++) {
for(int j = 0; j < r1; j++) {
printf("%d ", arr1[j][i]);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

}
printf("\n");
}
// Diagonal element
printf("diagonal element of first matrix:\n");
for(int i = 0; i < r2; i++) {
for(int j = 0; j < c2; j++) {
if(i== j){
printf("%d ", arr1[i][j]);
}
}
}
return 0;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E23
Create a structure named Date having day, month
and year as its elements.
Store the current date in the structure. Now add 45
days to the current date and display the final date.

Test Cases:
Input : dd mm yy (e.g 6 /3/23)
Output: dd/mm/yy (20/4/23)

E24
Write a structure to store the roll no., name, age
(between 11 to 14) and address of students (5
students). Store the information of the students.

1 - Write a function to print the names of all the


students having age 14.
2 - Write a function to print the names of all the
students having even roll no.
3 - Write a function to display the details of the
FY-PSAP LAB EXAM AY:2023-24 SEM-2

student whose roll no is given (i.e. roll no. entered


by the user).
Code
#include <stdio.h>
#include <string.h>

// Define the structure for student


typedef struct {
int roll_no;
char name[50];
int age;
char address[100];
} Student;

// Function to print the names of all students with age 14


void printAge14(Student students[], int size) {
printf("Students with age 14:\n");
for (int i = 0; i < size; i++) {
if (students[i].age == 14) {
printf("%s\n", students[i].name);
}
}
}

// Function to print the names of all students with even roll number
void printEvenRollNo(Student students[], int size) {
printf("Students with even roll number:\n");
for (int i = 0; i < size; i++) {
if (students[i].roll_no % 2 == 0) {
printf("%s\n", students[i].name);
}
}
}

// Function to display the details of a student by roll number


void printStudentDetails(Student students[], int size, int roll_no) {
for (int i = 0; i < size; i++) {
if (students[i].roll_no == roll_no) {
printf("Details of roll number %d:\n", roll_no);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("Address: %s\n", students[i].address);


return;
}
}
printf("No student found with roll number %d\n", roll_no);
}

int main() {
Student students[5];
int size = sizeof(students) / sizeof(students[0]);

// Assuming we're storing the information for 5 students


// This part can be replaced with actual data input
strcpy(students[0].name, "John Doe");
students[0].roll_no = 1;
students[0].age = 14;
strcpy(students[0].address, "123 Main St");

// ... Initialize other students similarly

// Call functions to perform the required operations


printAge14(students, size);
printEvenRollNo(students, size);

int roll_no_to_search;
printf("Enter roll number to search: ");
scanf("%d", &roll_no_to_search);
printStudentDetails(students, size, roll_no_to_search);

return 0;
}

E25
Write a structure to store the names, salary, and hours
of work per day of 10 employees in a company. Write a
program to increase the salary depending on the number
of hours of work per day as follows and then print the
FY-PSAP LAB EXAM AY:2023-24 SEM-2

name of all the employees along with their final


salaries. Assume:

Hours of work per day 8 10 >=12


Increase in salary ₹4000 ₹8000 ₹12000
Test Cases: Input
A 40000 8
B 10000 10
C 60000 14
Output:
A 44000 8
B 18000 10
C 72000 12
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E26
Write a C program to read the first line from a file.
Test Cases: Suppose the program.txt file contains
the following text in the current directory.
Java is Object Oriented Programming.
How are you?
Welcome to VIT
Output: Java is Object Oriented Programming.
Code

#include <stdio.h>

int main() {

FILE *file;

char buffer[1000];

// Open the file in read mode

file = fopen("program.txt", "r");

if (file == NULL) {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

perror("Error opening file");

return 1;

// Read the first line from the file

if (fgets(buffer, 1000, file) != NULL) {

printf("%s", buffer);

} else {

printf("Error reading from file\n");

// Close the file

fclose(file);

return 0;

E27
Write a C program to create a student database
using file.
Perform following operations:
1. Open file
2. Write five records in file.
FY-PSAP LAB EXAM AY:2023-24 SEM-2

3. Read all five records from file.


4. Search for a particular student from file and
print his/her details.
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

int roll_no;

char name[50];

int age;

char address[100];

} Student;

void writeRecords(const char *filename) {

FILE *file = fopen(filename, "w");

if (file == NULL) {

perror("Error opening file");

exit(1);

// Example student records

Student students[5] = {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

{1, "John Doe", 14, "123 Main St"},

{2, "Jane Smith", 13, "456 North St"},

{3, "Alice Jones", 11, "789 South St"},

{4, "Bob Brown", 14, "101 East St"},

{5, "Charlie Black", 12, "202 West St"}

};

for (int i = 0; i < 5; i++) {

fprintf(file, "%d,%s,%d,%s\n", students[i].roll_no, students[i].name,


students[i].age, students[i].address);

fclose(file);

void readRecords(const char *filename) {

FILE *file = fopen(filename, "r");

if (file == NULL) {

perror("Error opening file");

exit(1);

Student student;

while (fscanf(file, "%d,%49[^,],%d,%99[^\n]\n", &student.roll_no, student.name,


&student.age, student.address) == 4) {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("Roll No: %d, Name: %s, Age: %d, Address: %s\n", student.roll_no,
student.name, student.age, student.address);

fclose(file);

void searchStudent(const char *filename, int roll_no) {

FILE *file = fopen(filename, "r");

if (file == NULL) {

perror("Error opening file");

exit(1);

Student student;

int found = 0;

while (fscanf(file, "%d,%49[^,],%d,%99[^\n]\n", &student.roll_no, student.name,


&student.age, student.address) == 4) {

if (student.roll_no == roll_no) {

printf("Details of roll number %d:\n", roll_no);

printf("Name: %s\n", student.name);

printf("Age: %d\n", student.age);

printf("Address: %s\n", student.address);

found = 1;

break;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

if (!found) {

printf("No student found with roll number %d\n", roll_no);

fclose(file);

int main() {

const char *filename = "student_database.txt";

// Write records to file

writeRecords(filename);

// Read all records from file

printf("Reading all records from file:\n");

readRecords(filename);

// Search for a particular student

int roll_no_to_search;

printf("\nEnter roll number to search: ");

scanf("%d", &roll_no_to_search);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

searchStudent(filename, roll_no_to_search);

return 0;

E28

Write a C program to copy one file contents to


another file using character by character. Consider
a small source file of 4-5 lines only.
Code

#include <stdio.h>

#include <stdlib.h>

int main() {

FILE *sourceFile, *targetFile;

char ch;

// Open the source file in read mode

sourceFile = fopen("source.txt", "r");

if (sourceFile == NULL) {

printf("Cannot open source file.\n");

exit(EXIT_FAILURE);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

// Open the target file in write mode

targetFile = fopen("target.txt", "w");

if (targetFile == NULL) {

fclose(sourceFile);

printf("Cannot open target file.\n");

exit(EXIT_FAILURE);

// Copy contents character by character

while ((ch = fgetc(sourceFile)) != EOF) {

fputc(ch, targetFile);

printf("File copied successfully.\n");

// Close the files

fclose(sourceFile);

fclose(targetFile);

return 0;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E29
Perform following operations on 2D Matrix:
5. Accept number of rows and columns of two
matrices and read elements of both matrices.
6. Print Transpose of both matrices.
7. Print Addition of two matrices.

Code

#include <stdio.h>

#include <stdlib.h>

int main() {

int c1, r1, c2, r2;

printf("Enter rows and column of first matrix:\n");

scanf("%d %d", &r1, &c1);

printf("Enter the rows and column of second matrix:\n");

scanf("%d %d", &r2, &c2);

int arr1[r1][c1], arr2[r2][c2],sum[r1][c2];


FY-PSAP LAB EXAM AY:2023-24 SEM-2

// matrix 1

printf("Enter the elements of matrix 1:\n");

for(int i = 0; i < r1; i++) {

for(int j = 0; j < c1; j++) {

scanf("%d", &arr1[i][j]);

// matrix 2

printf("Enter the elements of matrix 2:\n");

for(int i = 0; i < r2; i++) {

for(int j = 0; j < c2; j++) {

scanf("%d", &arr2[i][j]);

// Transpose of matrix 1

printf("Transpose of first matrix:\n");

for(int i = 0; i < c1; i++) {

for(int j = 0; j < r1; j++) {

printf("%d ", arr1[j][i]);

}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("\n");

// Diagonal element

printf("diagonal element of first matrix:\n");

for(int i = 0; i < r2; i++) {

for(int j = 0; j < c2; j++) {

if(i== j){

printf("%d ", arr1[i][j]);

// addition

if( r1!=r2 || c1!=c2){

printf("Addition cannot be perform\n");

else{

printf("\nAddition of matrix are\n :");

for(int i = 0;i<r1;i++){

for(int j = 0;j<c1;j++){

sum[i][j] = arr1[i][j] + arr2[i][j];

printf("%d ",sum[i][j]);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("\n");

return 0;

}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E30
Accept number of rows and columns and read elements of matrix.
1. Print matrix in row major format.
2. Print matrix in column major format.
Test Case 1:
Number of rows:2
Number of columns: 3
Matrix Elements: 1 2 3 4 5 6
Row Major:
1 2 3
4 5 6
Column Major
1 3 5
2 4 6
Code
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("Enter the number of columns: ");


scanf("%d", &c);

if (r <= 0 || c <= 0) {
printf("Rows and columns must be positive integers.\n");
return 1;
}

int matrix[r][c];

printf("\nEnter matrix elements:\n");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Row major
printf("Row major:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// column major
printf("Column major:\n");
for (int j = 0; j < r; j++) {
for (int i = 0; i < c; i++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E31

Write a C Program to count number of characters in the file and


print every character on new line on screen.
Code

#include <stdio.h>

#include <stdlib.h>

int main() {

FILE *file;

char ch;

int count = 0;

// Open the file in read mode

file = fopen("input.txt", "r");

if (file == NULL) {

perror("Error opening file");

return 1;

// Read characters from the file, count them, and print each on a new line

while ((ch = fgetc(file)) != EOF) {


FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("%c\n", ch);

count++;

// Close the file

fclose(file);

// Print the character count

printf("Total number of characters: %d\n", count);

return 0;

E32
Write a c program to Delete all occurrences of Character from the
String.
Test case: Computer_engineering
Enter character to delete: e
Output: Computr_nginring
Code

#include <stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-2

int main() {

char str[] = "Computer_engineering"; // String to process

char ch = 'e'; // Character to delete

int i, j;

for(i = 0; str[i] != '\0'; ++i) {

while(str[i] == ch) {

for(j = i; str[j] != '\0'; ++j) {

// Shift all characters to the left

str[j] = str[j + 1];

str[j] = '\0'; // Null-terminate the string

printf("Output: %s\n", str);

return 0;

E33
Write a c program to insert a sub-string in to given main string.
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Enter First String: Life is beautiful


Enter Second String: very
Enter the position to insert second string in first: 9
Output: Life is very beautiful
Code

#include <stdio.h>

#include <string.h>

void insertSubstring(char *mainStr, const char *subStr, int position)


{

char buffer[200]; // Buffer to hold the final string

strncpy(buffer, mainStr, position); // Copy the first part of the


main string

buffer[position] = '\0'; // Null-terminate the buffer

strcat(buffer, subStr); // Concatenate the substring

strcat(buffer, mainStr + position); // Concatenate the remaining


part of the main string

strcpy(mainStr, buffer); // Copy the buffer back to the main string

int main() {

char mainStr[200] = "Life is beautiful";

char subStr[] = "very ";


FY-PSAP LAB EXAM AY:2023-24 SEM-2

int position = 9; // Position to insert the substring

insertSubstring(mainStr, subStr, position);

printf("Output: %s\n", mainStr);

return 0;

E34
1. Write a C program to print a given string in upper case using
C
Code

#include <stdio.h>

int main() {

char str[50];

printf("Enter the string: ");

scanf("%[^\n]", str);

while ((getchar()) != '\n');

int i;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (i = 0; str[i] != '\0'; i++) {

if (str[i] >= 'a' && str[i] <= 'z') {

// Convert lowercase letters to uppercase

str[i] = str[i] - 32;

printf("String in uppercase: %s\n", str);

return 0;

2. Write a C program to Reverse a string using pointers.


Code
#include <stdio.h>

void reverseString(char* str) {


char *left = str;
char *right = str;
while (*right != '\0') {
right++;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

}
right--; // Set right to the last valid character of the string

// Swap the characters from the start and the end of the string
while (left < right) {
char temp = *left;
*left++ = *right;
*right-- = temp;
}
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

reverseString(str); // Call the function to reverse the string


FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("Reversed string: %s\n", str); // Print the reversed string

return 0;
}
E35
1. Write a C program to evaluate a^b using function.
Code
#include <stdio.h>
int power(int a,int b){
int ans = 1;
for(int i = 1;i<=b;i++){
ans = ans * a;
}
return ans;
}

int main() {
FY-PSAP LAB EXAM AY:2023-24 SEM-2

int a,b;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("\nEnter the value of b : ");
scanf("%d",&b);

printf("\n %d^%d = %d ",a,b,power(a,b));


return 0;

2. Write a C program to find out the maximum number in an


array using function.
Code
#include <stdio.h>

// Function to find the maximum number in an array


int findMaximum(int arr[], int size) {
int max = arr[0];
FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (int i = 1; i < size; i++) {


if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);

int numbers[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}

int maxNumber = findMaximum(numbers, size);


printf("The maximum number in the array is: %d\n",
maxNumber);
return 0;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2

E36
Write a C program to find HCF and LCM of two numbers given by
user.
Code

#include <stdio.h>

#include <stdio.h>

#include <stdlib.h>

int main() {

int a,b,hcf,lcm;

printf("Enter first number : ");

scanf("%d",&a);

printf("Enter second number : ");

scanf("%d",&b);

for(int i = 1; i<= a || i<= b;i++){

if(a%i == 0 && b%i == 0){

hcf = i;

lcm = a*b/hcf;

printf("\n hcf = %d",hcf);

printf("\n lcm = %d",lcm);


FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 0;

E37
Write a C program to accept two matrices and check if they are
equal or not. Order of both matrices must be accepted from user at
run time.
Code
#include <stdio.h>

int main() {

int r1, c1;

int r2, c2;

printf("Enter the rows and column for matrix 1: ");

scanf("%d %d", &r1, &c1);

printf("\nEnter the rows and column for matrix 2: ");

scanf("%d %d", &r2, &c2);

// Matrices should be declared after checking if the dimensions match

if (r1 == r2 && c1 == c2) {

int matrix1[r1][c1];

int matrix2[r2][c2];

int store = 0;
FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("\nEnter the elements of matrix 1:\n");

for (int i = 0; i < r1; i++) {

for (int j = 0; j < c1; j++) {

scanf("%d", &matrix1[i][j]);

printf("\nEnter the elements of matrix 2:\n");

for (int i = 0; i < r2; i++) {

for (int j = 0; j < c2; j++) {

scanf("%d", &matrix2[i][j]);

// Check if matrices are equal

for (int i = 0; i < r1; i++) {

for (int j = 0; j < c1; j++) {

if (matrix1[i][j] != matrix2[i][j]) {

store = 1;

break; // Break out of the inner loop if a mismatch is found

if (store == 1) {

printf("\nMatrices are not equal\n");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

break; // Break out of the outer loop if a mismatch is found

if (store == 0) {

printf("\nMatrices are equal\n");

} else {

printf("\nRows and columns are not the same\n");

return 0;

E38
1. Write a C program to insert a given number in the array at
given position.
Code
#include <stdio.h>

int main() {
int n, v, p;
printf("Enter the size of array: ");
scanf("%d", &n);
int arr[n + 1]; // Increase the size of the array by 1 to accommodate the new element
printf("\nEnter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]); // Removed the space after %d in scanf
FY-PSAP LAB EXAM AY:2023-24 SEM-2

}
printf("\nEnter the value to insert: ");
scanf("%d", &v);
printf("\nEnter the position to insert [0-%d]: ", n - 1);
scanf("%d", &p);

if (p >= 0 && p < n) { // Check if 'p' is within the valid range


// Shift elements to the right to make space for the new element
for (int i = n; i > p; i--) {
arr[i] = arr[i - 1];
}
arr[p] = v; // Use '=' to assign the value 'v' to the position 'p'

printf("\nNew array:\n");
for (int i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}
} else {
printf("\nInvalid position!\n");
}

return 0;
}

2. Write a C program to remove a number in the array from a


given position.
Code
#include <stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-2

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

int arr[n];
printf("\nEnter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int pos;
printf("\nEnter the position of the element to delete [0-%d]: ", n - 1);
scanf("%d", &pos);

if (pos >= 0 && pos < n) {


// Shift elements to the left to overwrite the element at 'pos'
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--; // Decrease the size of the array

printf("\nArray after deleting element at position %d:\n", pos);


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("\nInvalid position!\n");
}

return 0;
}

E39
FY-PSAP LAB EXAM AY:2023-24 SEM-2

Write a c program for swapping of two arrays using function and


check if both arrays are equal or not. Limits and numbers of both
arrays must be accepted from user at run time.
#include <stdio.h>

#include <stdlib.h>

void swapArrays(int *arr1, int *arr2, int size1, int size2) {

if (size1 != size2) {

printf("Swap not possible, array sizes differ.\n");

return;

for (int i = 0; i < size1; i++) {

int temp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = temp;

int areArraysEqual(int *arr1, int *arr2, int size) {

for (int i = 0; i < size; i++) {

if (arr1[i] != arr2[i]) {

return 0; // Arrays are not equal


FY-PSAP LAB EXAM AY:2023-24 SEM-2

return 1; // Arrays are equal

int main() {

int size1, size2;

printf("Enter the size of the first array: ");

scanf("%d", &size1);

int *arr1 = (int *)malloc(size1 * sizeof(int));

printf("Enter the size of the second array: ");

scanf("%d", &size2);

int *arr2 = (int *)malloc(size2 * sizeof(int));

if (arr1 == NULL || arr2 == NULL) {

printf("Memory not allocated.\n");

exit(0);

printf("Enter elements of the first array:\n");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

for (int i = 0; i < size1; i++) {

scanf("%d", &arr1[i]);

printf("Enter elements of the second array:\n");

for (int i = 0; i < size2; i++) {

scanf("%d", &arr2[i]);

swapArrays(arr1, arr2, size1, size2);

printf("After swapping:\nFirst array: ");

for (int i = 0; i < size1; i++) {

printf("%d ", arr1[i]);

printf("\nSecond array: ");

for (int i = 0; i < size2; i++) {

printf("%d ", arr2[i]);

printf("\n");

if (areArraysEqual(arr1, arr2, size1)) {


FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("The arrays are equal.\n");

} else {

printf("The arrays are not equal.\n");

free(arr1);

free(arr2);

return 0;

E40
Write a C program for swapping of two string
Code
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100], temp[100];

// Input two strings


printf("Enter first string: ");
FY-PSAP LAB EXAM AY:2023-24 SEM-2

scanf("%99s", str1);
printf("Enter second string: ");
scanf("%99s", str2);

// Swapping the contents of str1 and str2


strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);

// Output the swapped strings


printf("After swapping:\n");
printf("First string: %s\n", str1);
printf("Second string: %s\n", str2);

return 0;

} E41
Write a C program for sorting list of elements using bubble sort.
#include <stdio.h>

void bubbleSort(int array[], int size) {

for (int step = 0; step < size - 1; ++step) {


FY-PSAP LAB EXAM AY:2023-24 SEM-2

// The "swapped" variable is used to optimize the algorithm.

int swapped = 0;

for (int i = 0; i < size - step - 1; ++i) {

// To sort in descending order, change ">" to "<".

if (array[i] > array[i + 1]) {

// Swap if the element found is greater than the next element

int temp = array[i];

array[i] = array[i + 1];

array[i + 1] = temp;

swapped = 1;

// If there's no swapping in the last swap, then the array is already sorted.

if (swapped == 0) {

break;

// Function to print an array

void printArray(int array[], int size) {

for (int i = 0; i < size; ++i) {


FY-PSAP LAB EXAM AY:2023-24 SEM-2

printf("%d ", array[i]);

printf("\n");

// Main function to run the program

int main() {

int data[] = {-2, 45, 0, 11, -9};

int size = sizeof(data) / sizeof(data[0]);

bubbleSort(data, size);

printf("Sorted Array in Ascending Order:\n");

printArray(data, size);

E42
Write a C program for sorting list of elements using selection sort
#include <stdio.h>

void selectionSort(int array[], int size) {

for (int step = 0; step < size - 1; step++) {

int min_idx = step;

for (int i = step + 1; i < size; i++) {

// Select the minimum element in each loop.


FY-PSAP LAB EXAM AY:2023-24 SEM-2

if (array[i] < array[min_idx])

min_idx = i;

// Put min at the correct position

int temp = array[min_idx];

array[min_idx] = array[step];

array[step] = temp;

// Function to print an array

void printArray(int array[], int size) {

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

// Main function to run the program

int main() {

int data[] = {20, 12, 10, 15, 2};


FY-PSAP LAB EXAM AY:2023-24 SEM-2

int size = sizeof(data) / sizeof(data[0]);

selectionSort(data, size);

printf("Sorted Array in Ascending Order:\n");

printArray(data, size);

return 0;

E43
Write a C program to search element in given list using linear
search . also find largest element in given array.
E44
Write a C program to find duplicate element in an array.
Code

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");


FY-PSAP LAB EXAM AY:2023-24 SEM-2

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

scanf("%d", &arr[i]);

printf("Duplicate elements found in the array are: ");

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

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

if(arr[i] == arr[j]) {

printf("%d ", arr[i]);

break; // Stop checking further once a duplicate is found

printf("\n");

return 0;

E45
Write a C program to insert element in an array on given specific
position.

You might also like