Pasap Important Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

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

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
#include<stdio.h>

int main(){
char ch[100];
int alpha=0, digit=0;
int n;

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


scanf("%d", &n);

printf("Enter the string: ");


for (int j = 0; j < n; j++) {
scanf(" %c", &ch[j]); // Use %c to read a single character
}

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


if ((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
alpha++;
} else {
digit++;
}
}

printf("The number of alphabets is %d\n", alpha);


printf("The number of digits is %d\n", digit);

return 0;
}

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
FY-PSAP LAB EXAM AY:2023-24 SEM-I

#include <stdio.h>

// Function to check if the input string is valid


int isValid(char *s) {
int countOpening = 0;
int countClosing = 0;

while (*s) {
if (*s == '(' || *s == '{' || *s == '[') {
countOpening++;
} else if (*s == ')' || *s == '}' || *s == ']') {
countClosing++;

// Check if the closing bracket has a corresponding opening bracket


if ((countClosing > countOpening) ||
(*s == ')' && countClosing > countOpening) ||
(*s == '}' && countClosing > countOpening) ||
(*s == ']' && countClosing > countOpening)) {
return 0; // Invalid string
}
}

s++;
}

// Check if the counts of opening and closing brackets are the same
return (countOpening == countClosing);
}

int main() {
char input[100];

printf("Enter the string containing brackets: ");


scanf("%s", input);

if (isValid(input)) {
printf("The input string is valid.\n");
} else {
printf("The input string is not valid.\n");
}

return 0;
}

E03
Given a string s, perform following operations on a string using function (Without pointer):
Find a length of a string.
Print string in reverse order.
Copy string s into s1 and print it.
Accept another string, say s2. Concatenate s and s2 and print concatenated string.
#include<stdio.h>
#include<string.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-I

void swap(char *a, char *b) {


char temp = *a;
*a = *b;
*b = temp;
}

int main() {
char name[100];
printf("Enter the name: ");
scanf("%s", name);

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

printf("The length of the string is %d\n", count);

// int s=0;
// int e=count-1;
// while(s<e){
// swap(&name[s++],&name[e--]);
// }

// printf("the reversed string is %s",name);

char stirng1[10];
strcpy(stirng1,name);
printf("the new one is %s\n",stirng1);

char string2[20];
printf("enter stirng 2 ");
scanf("%s",&string2);
strcat(string2,stirng1);
printf("%s",string2);

return 0;
}

E04
Given a string s, perform following operations on a string using function (With pointer):
Find a length of a string.
Print string in reverse order.
Copy string s into s1 and print it.
E05
Given a string s, return true if it a Palindrome or false otherwise.
Test Cases:
Case 1:
FY-PSAP LAB EXAM AY:2023-24 SEM-I

Input: MADAM
Output: true
Case 2:
Input: CAR
Output: false
E06
Accept limit of array from user. As per the limit, read integer elements in array. Then print :
Minimum, Maximum number from array.
Search for a particular number from array.
#include<stdio.h>
int findmin(int arr[],int n){
int min = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i]<min)
{
min = arr[i];
}
}
return min ;

}
int findmax(int arr[],int n){
int max = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i]>max)
{
max = arr[i];
}
}
return max ;

}
int find(int arr[],int n,int target){
for (int i = 0; i < n; i++)
{
if (arr[i]==target)
{
return 0;
}

return -1;

int main(){
int n,target;
FY-PSAP LAB EXAM AY:2023-24 SEM-I

printf("enter the limit of the array ");


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

int c = findmin(arr,n);
printf(" the min value is %d\n",c);
int m = findmax(arr,n);
printf("the max value is %d\n",m);

printf("enter the targeted integer ");


scanf("%d",&target);
int result= find(arr,n,target);
if (result == 0)
{
printf("target found ");
}
else
printf("target not found");

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.
#include <stdio.h>

void restoreString(const char* s, int* indices, int size, char* result) {


for (int i = 0; i < size; ++i) {
result[indices[i]] = s[i];
}
result[size] = '\0'; // Null terminate the string
}
FY-PSAP LAB EXAM AY:2023-24 SEM-I

int main() {
char s1[] = "codeleet";
int indices1[] = {4, 5, 6, 7, 0, 2, 1, 3};
int size1 = sizeof(indices1) / sizeof(indices1[0]);
char result1[size1 + 1]; // +1 for null terminator
restoreString(s1, indices1, size1, result1);
printf("Test Case 1: %s\n", result1);

char s2[] = "abc";


int indices2[] = {0, 1, 2};
int size2 = sizeof(indices2) / sizeof(indices2[0]);
char result2[size2 + 1]; // +1 for null terminator
restoreString(s2, indices2, size2, result2);
printf("Test Case 2: %s\n", result2);

return 0;
}

E08
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
Write a program to print factorial of 1 to 10 numbers.
#include <stdio.h>
// long long factorial(int n){
// if (n==0||n==1)
// {
// return 1;
// }
// return n*factorial(n-1);

// }

int main() {
int n = 5; // Change this value to adjust the number of rows

// Print the upper part of the pattern


for (int i = 1; i <= n; i++) {
for (char ch = 'A'; ch <= 'A' + i - 1; ch++) {
printf("%c ", ch);
}
printf("\n");
}

// Print the lower part of the pattern


for (int i = n -1; i >= 1; i--) {
FY-PSAP LAB EXAM AY:2023-24 SEM-I

for (char ch = 'A'; ch <= 'A' + i - 1; ch++) {


printf("%c ", ch);
}
printf("\n");
}
// for (int i = 1; i <=10; i++)
// {
// printf("the fact of %d is %d\n",i,factorial(i));
// }

return 0;
}

E09
Write a Program to print the output like:
1
12
123
12345
E D C B A
E D C B
E D C
E D
E
Write a program to print prime numbers between two numbers given by user.
#include<stdio.h>

int main() {
int s, e;
printf("Enter the starting value and ending value: ");
scanf("%d %d", &s, &e);

printf("Prime numbers between %d and %d are:\n", s, e);

for (int i = s; i <= e; i++) {


int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
if (count > 2) {
break; // Exit the loop if more than two divisors are found
}
}
}
if (count == 2) {
printf("%d\n", i);
}
}

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

#include <stdio.h>

int main() {
// int n = 5; // Change this value to adjust the number of rows

// // Print the upper part of the pattern


// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= i; j++) {
// printf("%d ", j);
// }
// printf("\n");
// }

// // Print the lower part of the pattern


// for (int i = n; i >= 1; i--) {
// for (int j = 1; j <= i; j++) {
// printf("%c ", 'A' + i - j);
// }
// printf("\n");
// }

//write a programme to find prime no between the two no

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.
#include <stdio.h>
#include <string.h>

int main() {
char s[100];
printf("Enter the string: ");
fgets(s, sizeof(s), stdin);

for (char i = 'a'; i <= 'z'; i++) {


int count = 0;
for (int j = 0; j < strlen(s); j++) {
if (i == s[j]|| (i - 'a' + 'A') == s[j]) {
count++;
}
}
if (count > 0) {
FY-PSAP LAB EXAM AY:2023-24 SEM-I

printf("The char %c found %d times\n", i, count);


}
}

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
#include<stdio.h>
void fibonnacii(int n){
int f=0 , s=1 ,next;
for (int i = 0; i < n; i++)
{
printf("%d, ", f);
next=f+s;
f=s;
s=next;

}
int main(){
int n;
printf("enter the no of terms ");
scanf("%d",&n);
fibonnacii(n);

return 0;
}

E12
Write a C program to count the number of Vowels and Consonants.
#include<stdio.h>
#include<string.h>
int main(){
char s[100];
printf("enter the string ");
gets(s);
int count=0,cons=0;
for (int i = 0; i < strlen(s) ; i++)
{

if (s[i]==
'a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='
O'||s[i]=='U')
{
count++;
FY-PSAP LAB EXAM AY:2023-24 SEM-I

}
else if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
cons++;

}
printf("the no of the vowels are%d \n",count);
printf("the no of consonants are %d ",cons);

return 0;
}

Test Cases: String: C is a programming language.


Vowels: 9
Consonants: 14
E13
Write a program to convert decimal to binary number.
Test Cases:
Input : 5
Output: 101
#include<stdio.h>
int main(){
int n;
printf("enter the no ");
scanf("%d",&n);
int arr[10];
int i=0;
while (n)
{
int reminder=n%2;
n=n/2;
arr[i]=reminder;
i++;
}
for (int j = i-1; j >=0; j--)
{
printf("%d",arr[j]);
}

return 0;
}

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

void reverseArray(int *arr, int size) {


int *start = &arr[0];
FY-PSAP LAB EXAM AY:2023-24 SEM-I

int *end = &arr[size-1];

while (start < end) {


// Swap elements using pointers
int temp = *start;
*start = *end;
*end = temp;

// Move pointers towards each other


start++;
end--;
}
}

void printArray(int *arr, int size) {


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

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");


printArray(arr, size);

// Reverse the array


reverseArray(arr, size);

printf("Reversed Array: ");


printArray(arr, size);

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
Enter upper limit to print perfect numbers: 70
All perfect numbers between 15 to 70 are: 28
#include<stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-I

int main(){
int s,e,n;
printf("enter starting and ending values ");
scanf("%d %d",&s,&e);
for (int i = s; i <= e; i++)
{
int sum=0;
n=i;
for (int j = 1; j <= n/2; j++)
{
if (n%j==0)
{
sum+=j;
}

}
if(sum==n)
printf("%d,",n);
}

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 should be calculated based on the entered marks)
#include <stdio.h>

struct Student {
char name[50];
char mobileNumber[15];
float marks[5];
float totalMarks;
};

float calculateTotalMarks(struct Student *student) {


float total = 0;
for (int i = 0; i < 5; i++)
total += student->marks[i];
return total;
}

void swap(struct Student *a, struct Student *b) {


struct Student temp = *a;
*a = *b;
*b = temp;
}

void sortStudents(struct Student students[], int n) {


for (int i = 0; i < n - 1; i++) {
FY-PSAP LAB EXAM AY:2023-24 SEM-I

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


if (students[j].totalMarks < students[j + 1].totalMarks) {
swap(&students[j], &students[j + 1]);
}
}
}
}

int main() {
int n;

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


scanf("%d", &n);

struct Student students[n];

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


printf("\nEnter details for student %d:\nName: ", i + 1);
scanf("%s", students[i].name);
printf("Mobile Number: ");
scanf("%s", students[i].mobileNumber);
printf("Marks for 5 subjects:\n");
for (int j = 0; j < 5; j++) {
printf("Subject %d: ", j + 1);
scanf("%f", &students[i].marks[j]);
}
students[i].totalMarks = calculateTotalMarks(&students[i]);
}

sortStudents(students, n);

printf("\nTop 5 Students:\n");
for (int i = 0; i < 5 && i < n; i++)
printf("\nDetails for student %d:\nName: %s\nMobile Number: %s\nTotal Marks:
%.2f\n",
i + 1, students[i].name, students[i].mobileNumber,
students[i].totalMarks);

return 0;
}

E18
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)
#include <stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-I

// Define a structure for student details


struct Student {
int rollNumber;
char name[50];
float marks[5];
}student;

// Function to calculate the total marks for a student


float calculateTotalMarks(struct Student *student) {
float total = 0;
for (int i = 0; i < 5; i++) {
total += student->marks[i];
}
return (total/500)*100;
}

// Function to calculate the grade based on total marks


char calculateGrade(float totalMarks) {
if (totalMarks >= 76) {
return 'A'; // Distinction
} else if (totalMarks >= 60) {
return 'B'; // First Class
} else if (totalMarks >= 50) {
return 'C'; // Second Class
} else if (totalMarks >= 40) {
return 'D'; // Pass Class
} else {
return 'F'; // Fail
}
}

int main() {
// Declare a structure variable to store student data

// Input details for the student


printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);

printf("Enter Name: ");


scanf("%s", &student.name);

printf("Enter Marks for 5 subjects:\n");


for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &student.marks[i]);
}

// Calculate total marks for the student


float totalMarks = calculateTotalMarks(&student);

// Calculate the grade based on total marks


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

char grade = calculateGrade(totalMarks);

// Print student details and grade


printf("\nStudent Details:\n");
printf("Roll Number: %d\n", student.rollNumber);
printf("Name: %s\n", student.name);
printf("Total Marks: %f\n", totalMarks);
printf("Grade: %c\n", grade);

return 0;
}

E19

Write a c program for swapping of two arrays using call by value function.
#include<stdio.h>
void swapparr(int arr1[],int arr2[],int n){
for (int i = 0; i < n; i++)
{
int temp = arr1[i];
arr1 [i] = arr2 [i];
arr2[i]=temp;

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

}
int main(){
int arr1[5]={45,6,4,89,5};
int arr2[5]={45, 44, 75, 89,8 };
printf("before swapiing ");
printarr(arr1,5);
printarr(arr2,5);
swapparr(arr1,arr2,5);
printf("\nafter swapiing \n");
printarr(arr1,5);
printarr(arr2,5);

return 0;
}

E20
Write a C program to count the number of digits in an integer. Then print addition of all digits in the given number.
FY-PSAP LAB EXAM AY:2023-24 SEM-I

Test Cases:
input: 671041

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

int main() {
int num, originalNum, digitCount = 0, sum = 0;

// Input: Taking an integer from the user


printf("Enter an integer: ");
scanf("%d", &num);

// Store the original number for later use


originalNum = num;

// Counting the number of digits


while (num != 0) {
num /= 10;
digitCount++;
}

// Reset num to the original value for digit addition


num = originalNum;

// Adding all the digits


while (num != 0) {
int r = num % 10;
sum += r;
num /= 10;
}

// Output: Displaying the results


printf("Number of digits in %d: %d\n", originalNum, digitCount);
printf("Sum of digits in %d: %d\n", originalNum, sum);

return 0;
}

E21

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.

E22
FY-PSAP LAB EXAM AY:2023-24 SEM-I

Perform following operations on 2D Matrix:


Accept number of rows and columns of two matrices and read elements of both matrices.
Print Transpose of both matrices.
Print Diagonal elements of both matrices.
#include <stdio.h>

// Function to accept matrix elements


void acceptMatrix(int matrix[100][100], int rows, int cols) {
printf("Enter the elements of the matrix:\n");

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


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

// Function to print matrix elements


void printMatrix(int matrix[100][100], int rows, int cols) {
printf("Matrix:\n");

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


for (int j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}

// Function to print transpose of a matrix


void printTranspose(int matrix[100][100], int rows, int cols) {
printf("Transpose of the matrix:\n");

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


for (int i = 0; i < rows; ++i) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}

// Function to print diagonal elements of a matrix


void printDiagonal(int matrix[100][100], int rows, int cols) {
if (rows != cols) {
printf("Diagonal elements are not applicable for non-square matrices.\n");
return;
}

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

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


printf("%d\t", matrix[i][i]);
}
FY-PSAP LAB EXAM AY:2023-24 SEM-I

printf("\n");
}

int main() {
int rows, cols;

// Input number of rows and columns for matrices


printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

int matrix1[100][100], matrix2[100][100];

// Accept elements for the first matrix


printf("For the first matrix:\n");
acceptMatrix(matrix1, rows, cols);

// Print the first matrix


printMatrix(matrix1, rows, cols);

// Accept elements for the second matrix


printf("For the second matrix:\n");
acceptMatrix(matrix2, rows, cols);

// Print the second matrix


printMatrix(matrix2, rows, cols);

// Print transpose of both matrices


printTranspose(matrix1, rows, cols);
printTranspose(matrix2, rows, cols);

// Print diagonal elements of both matrices


printDiagonal(matrix1, rows, cols);
printDiagonal(matrix2, rows, cols);

return 0;
}
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)
#include<stdio.h>
struct Date
{
int day;
int month;
int year;
FY-PSAP LAB EXAM AY:2023-24 SEM-I

}date,currentdate;

//function to display the date


void diplay(struct Date date){
printf("%d/%d/%d\n",date.day,date.month,date.year);
}
struct Date adddays(struct Date currentdate,int days){
currentdate.day = currentdate.day +days;
if(currentdate.day>31){
currentdate.day=currentdate.day-31;
currentdate.month++;

}
if (currentdate.month>12)
{
currentdate.month=currentdate.month-12;
currentdate.year++;
}
return currentdate;
}

int main(){
currentdate.day=6;
currentdate.month=3;
currentdate.year=23;
diplay(currentdate);
currentdate= adddays(currentdate,45);
diplay(currentdate);
return 0;
}

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 student whose roll no is given (i.e. roll no. entered by the user).
#include<stdio.h>
struct Student{
int rollno;
char name[50];
int age;
char address[100];

};
void printname14(struct Student students[],int size){
for (int i = 0; i < size; i++)
{
if (students[i].age==14)
{
FY-PSAP LAB EXAM AY:2023-24 SEM-I

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

}
void printeven(struct Student students[],int n){
for (int i = 0; i < n; i++)
{
if (students[i].rollno %2==0)
{
printf("%s ",students[i].name);
}

int main(){
struct Student students[2];
printf("enter the details ");
for (int i = 0; i < 2; i++)
{
printf("Roll No. : ");
scanf("%d",&students[i].rollno);

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

printf("Age(11-14) : ");
scanf("%d",&students[i].age);

// while(students[i].age>14||students[i].age<11)
// printf("age is invalid pls enter the valid age");
// scanf("%d",&students[i].age);

printf("adsress ");
scanf("%s",&students[i].address);
printf("\n");

}
printname14(students,2);
printf("\n");
printeven(students,2);

return 0;
}

E25
FY-PSAP LAB EXAM AY:2023-24 SEM-I

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 name of all the
employees along with their final salaries. Assume:

Test
Hours of work per day 8 10 >=12 Cases:
Input
A
Increase in salary ₹4000 ₹8000 ₹12000 40000
8
B
10000 10
C 60000 14
Output:
A 44000 8
B 18000 10
C 72000 12
#include<stdio.h>

struct Employee {
char name[100];
int salary;
int hours;
};

void increasesalary(struct Employee employees[]) {


for (int i = 0; i < 3; i++) {
if (employees[i].hours == 8) {
employees[i].salary += 4000;
} else if (employees[i].hours == 10) {
employees[i].salary += 8000;
} else if (employees[i].hours >= 12) {
employees[i].salary += 12000;
}
}
}

void show(struct Employee employees[]) {


printf("The new details are:\n");
for (int i = 0; i < 3; i++) {
printf("Name: %s\n", employees[i].name);
printf("Salary: %d\n", employees[i].salary);
printf("Hours: %d\n", employees[i].hours);
printf("\n");
}
}

int main() {
struct Employee employees[3];

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


for (int i = 0; i < 3; i++) {
printf("Name: ");
FY-PSAP LAB EXAM AY:2023-24 SEM-I

scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%d", &employees[i].salary); // Initialize the salary
printf("Hours: ");
scanf("%d", &employees[i].hours);
}

increasesalary(employees);
show(employees);

return 0;
}

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.
#include<stdio.h>
int main(){
char line[100];
FILE*file=fopen("programme.txt","r");
if (fgets(line,100,file)!=NULL)
{
printf("output: %s \n",line);
}

fclose(file);
return 0;
}

E27
Write a C program to create a student database using file.
Perform following operations:
Open file
Write five records in file.
Read all five records from file.
Search for a particular student from file and print his/her details.
#include <stdio.h>
#include<string.h>

struct Student {
char name[50];
int rollNo;
float marks;
};

void writeRecords(FILE *file) {


struct Student student;

printf("Enter five student records:\n");


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

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


printf("Record %d:\n", i + 1);
printf("Name: ");
scanf("%s", student.name);
printf("Roll No: ");
scanf("%d", &student.rollNo);
printf("Marks: ");
scanf("%f", &student.marks);

fprintf(file, "%s %d %.2f\n", student.name, student.rollNo, student.marks);


}

printf("Records written to file successfully.\n");


}

void readRecords(FILE *file) {


struct Student student;

rewind(file); // Rewind file pointer to the beginning

printf("\nReading records from file:\n");

while (fscanf(file, "%s %d %f", student.name, &student.rollNo, &student.marks) == 3)


{
printf("Name: %s\n", student.name);
printf("Roll No: %d\n", student.rollNo);
printf("Marks: %.2f\n", student.marks);
printf("--------------------\n");
}
}

void searchRecord(FILE *file, char *searchName) {


struct Student student;
int found = 0;

rewind(file); // Rewind file pointer to the beginning

while (fscanf(file, "%s %d %f", student.name, &student.rollNo, &student.marks) == 3)


{
if (strcmp(student.name, searchName) == 0) {
printf("\nStudent Found:\n");
printf("Name: %s\n", student.name);
printf("Roll No: %d\n", student.rollNo);
printf("Marks: %.2f\n", student.marks);
found = 1;
break; // Stop searching once found
}
}

if (!found) {
printf("\nStudent with name %s not found.\n", searchName);
}
FY-PSAP LAB EXAM AY:2023-24 SEM-I

int main() {
FILE *file = fopen("student_database.txt", "w+");

if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}

// Write five records to the file


writeRecords(file);

// Read all five records from the file


readRecords(file);

// Search for a particular student by name


char searchName[50];
printf("\nEnter the name to search: ");
scanf("%s", searchName);
searchRecord(file, searchName);

fclose(file);

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.
#include<stdio.h>
int main(){
char c;
FILE *ptr1,*ptr2;
ptr1=fopen("abc.txt","r");
if (ptr1==NULL)
{
printf("error");

ptr2=fopen("destination.txt","w");
while ((c= fgetc(ptr1))!=EOF)
{
fputc(c,ptr2);
}
printf("successfully copied");
fclose(ptr1);
fclose(ptr2);

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

E29
Perform following operations on 2D Matrix:
Accept number of rows and columns of two matrices and read elements of both matrices.
Print Transpose of both matrices.
Print Addition of two matrices.
#include<stdio.h>
void read(int matrix[100][100],int rows,int colum){
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < colum; j++)
{
printf("Enter element at the postition(%d,%d)",i+1,j+1);
scanf("%d",&matrix[i][j]);
}

}
void printMatrix(int matrix[100][100], int rows, int cols) {
printf("Matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void transpose(int matrix[100][100], int rows, int cols) {
printf("Transposed Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d\t", matrix[j][i]);
}
printf("\n");
}
}
void add(int matrix1[100][100],int matrix2[100][100],int row,int col,int
result[100][100]){
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
result[i][j]=matrix1[i][j]+matrix2[i][j];
}

}
int main(){
int matrix1[100][100];
int matrix2[100][100];
FY-PSAP LAB EXAM AY:2023-24 SEM-I

int finalmatrix[100][100];
read(matrix1,2,2);
printMatrix(matrix1,2,2);
printf("enter the second matrix elements \n");
read(matrix2,2,2);
printMatrix(matrix2,2,2);
transpose(matrix1,2,2);
add(matrix1,matrix2,2,2,finalmatrix);
printMatrix(finalmatrix,2,2);

return 0;
}E30
Accept number of rows and columns and read elements of matrix.
Print matrix in row major format.
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:
2 3
4 5 6
Column Major
1 3 5
2 4 6
E31

Write a C Program to count number of characters in the file and print every character on new line on screen.
#include<stdio.h>
int main(){
FILE*file;
int count=0;
char c;
file = fopen("abc.txt","r");
if (file == NULL) {
printf("File not found or unable to open.\n");
return 1;
}
while ((c=fgetc(file))!=EOF)
{
count++;
printf("%c\n",c);
}
printf("\nthe total no of the chara are %d ",count);
fclose(file);

return 0;
}

E32
Write a c program to Delete all occurrences of Character from the String.
Test case: Computer_engineering
FY-PSAP LAB EXAM AY:2023-24 SEM-I

Enter character to delete: e


Output: Computr_nginring
#include <stdio.h>
#include<string.h>
int main(){
char ch[100];
printf("enter the string: ");
gets(ch);
char target;
printf("enter the target to be deleted: ");
scanf("%c",&target);
printf("the new stirng is: ");
for (int i = 0; i < strlen(ch); i++)
{
if (ch[i]==target)
{
continue;
}
printf("%c",ch[i]);
}

return 0;
}

E33
Write a c program to insert a sub-string in to given main string.

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
#include<stdio.h>
#include<string.h>

int main() {
char s1[100];
char s2[100];

printf("Enter the first string: ");


fgets(s1, sizeof(s1), stdin);

printf("Enter the second string: ");


fgets(s2, sizeof(s2), stdin);

int l1 = strlen(s1);
int l2 = strlen(s2);

int index;
printf("Enter the position to insert: ");
scanf("%d", &index);
FY-PSAP LAB EXAM AY:2023-24 SEM-I

// Remove newline characters from the input


if (s1[l1 - 1] == '\n') {
s1[l1 - 1] = '\0';
}

for (int i = l1; i >= index; i--) {


s1[i + l2] = s1[i];
}

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


s1[index + j] = s2[j];
}

printf("After inserting a string: %s\n", s1);

return 0;
}

E34
Write a C program to print a given string in upper case using C
#include<stdio.h>
#include<string.h>
void convertTOuppercase(char str[]){
int i;
while(str[i]!='\0'){
if (str[i]>='a'&&str[i]<='z')
{
str[i]= str[i]-'a'+'A';
}
i++;
}
}

void reversestring(char str[], int n){


int s=0;
int e=n-1;
char temp;

while(s<e)
{
temp=str[s];
str[s]=str[e];
str[e]=temp;
s++;
e--;

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

char str[100];
printf("enter the string ");
fgets(str,sizeof(str),stdin);
convertTOuppercase(str);
printf("after: %s",str);
reversestring(str,strlen(str));
printf("after reversing: %s",str);

return 0;
}

Write a C program to Reverse a string using pointers


E35
Write a C program to evaluate a^b using function.
Write a C program to find out the maximum number in an array using function.
#include<stdio.h>

float raiseto(float a, float b) {


// Base case
if (b == 0) {
return 1;
} else {
float ans = a * raiseto(a, b - 1);
return ans;
}
}

int main() {
float result = raiseto(2, 2);
printf("Result: %f\n", result);

return 0;
}

#include<stdio.h>
int maxno(int arr[],int n){
int max=arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i]>max)
{
max=arr[i];
}

}
return max;
}
int main(){
int n;

printf("enter no of elements ");


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

int arr[n];
printf("enter the elements of array: ");
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
int max=maxno(arr,n);
printf("the max no is: %d",max);

return 0;
}

E36
Write a C program to find HCF and LCM of two numbers given by user.
#include <stdio.h>

// Function to calculate HCF


int findHCF(int num1, int num2) {
while (num1 != num2) {
if (num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
return num1;
}

// Function to calculate LCM


int findLCM(int num1, int num2) {
return (num1 * num2) / findHCF(num1, num2);
}

int main() {
int num1, num2;

// Input two numbers from the user


printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

// Calculate and display HCF


printf("HCF of %d and %d is: %d\n", num1, num2, findHCF(num1, num2));

// Calculate and display LCM


printf("LCM of %d and %d is: %d\n", num1, num2, findLCM(num1, num2));

return 0;
}

E37
FY-PSAP LAB EXAM AY:2023-24 SEM-I

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.
#include<stdio.h>
void input(int row,int col,int matrix[10][10]){
for (int i = 0; i < row; i++)
{
for(int j=0;j<col;j++){
printf("enter the elements:(%d,%d)",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}

}
int check(int rows,int col,int matrix1[][10],int matrix2[][10]){
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < col; j++)
{
if(matrix1[i][j]!=matrix2[i][j])
return 0;

}
return 1;
}
int main(){
int rows, cols;

// Input the order of the first matrix


printf("Enter the number of rows for the matrices: ");
scanf("%d", &rows);
printf("Enter the number of columns for the matrices: ");
scanf("%d", &cols);

// Declare matrices
int matrix1[rows][cols];
int matrix2[rows][cols];

// Input the elements of the first matrix


printf("For the first matrix:\n");
input(rows, cols, matrix1);

// Input the elements of the second matrix


printf("\nFor the second matrix:\n");
input(rows, cols, matrix2);

// Check if matrices are equal


if (check(rows, cols, matrix1, matrix2)) {
printf("\nThe matrices are equal.\n");
} else {
printf("\nThe matrices are not equal.\n");
}
FY-PSAP LAB EXAM AY:2023-24 SEM-I

return 0;
}

E38
Write a C program to insert a given number in the array at given position.
Write a C program to remove a number in the array from a given position.
#include<stdio.h>
void insert(int arr[],int *n,int pos,int num){
for (int i = *n; i >=pos ; i--)
{
arr[i]=arr[i-1];
}
arr[pos-1]=num;
(*n)++;

}
int main(){
int n;

printf("enter the size of array: ");


scanf("%d",&n);
printf("enter the elements of array: ");
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
insert(arr,&n,2,50);
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}

return 0;
}
#include<stdio.h>

int main(){
int n;
int num;
printf("enter no to be deleted: ");
scanf("%d",&num);
printf("enter the size of array: ");
scanf("%d",&n);
printf("enter the elements of array: ");
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
FY-PSAP LAB EXAM AY:2023-24 SEM-I

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


{
if (arr[i]== num)
{
continue;
}

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

E39
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>

// Function to swap two arrays


void swapArrays(int arr1[], int arr2[], int size) {
for (int i = 0; i < size; i++) {
// Swap elements of the two arrays
int temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
}

// Function to check if two arrays are equal


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
}
}
return 1; // Arrays are equal
}

int main() {
int size;

// Input the size of arrays


printf("Enter the size of arrays: ");
scanf("%d", &size);

// Declare arrays
int array1[size], array2[size];

// Input elements for the first array


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

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


for (int i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &array1[i]);
}

// Input elements for the second array


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

// Call the swapArrays function


swapArrays(array1, array2, size);

// Check if arrays are equal after swapping


if (areArraysEqual(array1, array2, size)) {
printf("\nArrays are equal after swapping.\n");
} else {
printf("\nArrays are not equal after swapping.\n");
}

// Display the swapped arrays


printf("\nArray 1 after swapping:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array1[i]);
}

printf("\nArray 2 after swapping:\n");


for (int i = 0; i < size; i++) {
printf("%d ", array2[i]);
}

return 0;
}

E40
Write a C program for swapping of two string
#include<stdio.h>
#include<string.h>
void swapStrings(char str1[], char str2[]) {
char temp[100];

// Copy the content of str1 to temp


strcpy(temp, str1);

// Copy the content of str2 to str1


strcpy(str1, str2);
FY-PSAP LAB EXAM AY:2023-24 SEM-I

// Copy the content of temp to str2


strcpy(str2, temp);
}

int main(){
char str1[100];
char str2[100];
printf("enter string 1: ");
fgets(str1,sizeof(str1),stdin);
printf("enter string 1: ");
fgets(str2,sizeof(str2),stdin);
swapStrings(str1,str2);
printf("after swapping\n");
printf("%s",str1);
printf("%s",str2);
return 0;
}

E41
Write a C program for sorting list of elements using bubble sort.
#include<stdio.h>
void bubblesort(int arr[], int n){
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i; j++)
{
if (arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}

}
int main(){
int arr[5]={11,80,1,0,45};
bubblesort(arr,5);
for (int i = 0; i < 5; i++)
{
printf("%d ",arr[i]);
}

return 0;
}

E42
Write a C program for sorting list of elements using selection sort
#include <stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-I

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void selectionSort(int arr[], int n) {


int i, j, minIndex;

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


minIndex = i;

// Find the index of the minimum element in the unsorted part


for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// Swap the found minimum element with the first element


swap(&arr[minIndex], &arr[i]);
}
}

int main() {
int n;

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


scanf("%d", &n);

int arr[n];

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


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

// Perform selection sort


selectionSort(arr, n);

// Display the sorted array


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

return 0;
}

E43
Write a C program to search element in given list using linear search . also find largest element in given array.
#include <stdio.h>
FY-PSAP LAB EXAM AY:2023-24 SEM-I

// Function to perform linear search


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Element found, return index
}
}
return -1; // Element not found
}

// Function to find the largest element in the array


int findLargestElement(int arr[], int n) {
int maxElement = arr[0]; // Assume the first element is the largest

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


if (arr[i] > maxElement) {
maxElement = arr[i]; // Update maxElement if a larger element is found
}
}

return maxElement;
}

int main() {
int n, key;

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


scanf("%d", &n);

int arr[n];

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


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

printf("Enter the element to search: ");


scanf("%d", &key);

// Perform linear search


int result = linearSearch(arr, n, key);

if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}

// Find the largest element in the array


int largestElement = findLargestElement(arr, n);
printf("The largest element in the array is: %d\n", largestElement);
FY-PSAP LAB EXAM AY:2023-24 SEM-I

return 0;
}

E44
Write a C program to find duplicate element in an array.
#include <stdio.h>

void findDuplicates(int arr[], int n) {


printf("Duplicate elements in the array are: ");
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
printf("%d ", arr[i]);
break;
}
}
}
printf("\n");
}

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


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

findDuplicates(arr, n);

return 0;
}

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

You might also like