LAB PSAP Problem Statements-1
LAB PSAP Problem Statements-1
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() {
if ((S[i] >= 'a' && S[i] <= 'z') || (S[i] >= 'A' && S[i] <= 'Z'))
alphabets++;
digits++;
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
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
int main() {
char s[] = "Hello";
char s1[100], s2[] = "World", result[200];
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
int main() {
char s[] = "Hello";
char s1[100];
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;
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
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.
Code
#include <stdio.h>
#include <stdlib.h>
int main() {
char s[20], temp;
int n = 0, i, k;
scanf("%s", s);
int arr[n];
char shuffled[n+1]; // +1 for the null terminator
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;
}
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
int main() {
char str[] = "This book is very good";
int freq[MAX_CHAR] = {0};
int i;
return 0;
}
E11
Write a C program to print Fibonacci series up to n
terms.
#include <stdio.h>
#include <stdlib.h>
int main() {
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];
scanf("%[^\n]", str);
vowel++;
space++;
dot++;
else {
consonant++;
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;
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>
int main() {
int n;
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.
}
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
#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;
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2
int main() {
int n;
scanf("%d", &n);
scanf("%s", students[i].name);
scanf("%s", students[i].mobile_number);
scanf("%d", &students[i].maths_marks);
scanf("%d", &students[i].english_marks);
scanf("%d", &students[i].psap_marks);
scanf("%d", &students[i].mad_marks);
scanf("%d", &students[i].bt_marks);
// Printing the top 5 students or all students if there are less than 5
free(students);
return 0;
E18
FY-PSAP LAB EXAM AY:2023-24 SEM-2
(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
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
#include <stdio.h>
*arr1 = *arr2;
*arr2 = temp;
int main() {
swapArrays(&ptr1, &ptr2);
printf("After swapping:\n");
printf("Array 1: ");
printf("\nArray 2: ");
printf("\n");
return 0;
E20
FY-PSAP LAB EXAM AY:2023-24 SEM-2
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
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
scanf("%d", &size);
exit(0);
scanf("%d", &arr1[i]);
scanf("%d", &arr2[i]);
printf("\n");
free(arr1);
free(arr2);
free(sum);
return 0;
E22
#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);
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.
// 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);
}
}
}
int main() {
Student students[5];
int size = sizeof(students) / sizeof(students[0]);
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
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];
if (file == NULL) {
FY-PSAP LAB EXAM AY:2023-24 SEM-2
return 1;
printf("%s", buffer);
} else {
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
#include <stdlib.h>
#include <string.h>
typedef struct {
int roll_no;
char name[50];
int age;
char address[100];
} Student;
if (file == NULL) {
exit(1);
Student students[5] = {
FY-PSAP LAB EXAM AY:2023-24 SEM-2
};
fclose(file);
if (file == NULL) {
exit(1);
Student student;
printf("Roll No: %d, Name: %s, Age: %d, Address: %s\n", student.roll_no,
student.name, student.age, student.address);
fclose(file);
if (file == NULL) {
exit(1);
Student student;
int found = 0;
if (student.roll_no == roll_no) {
found = 1;
break;
FY-PSAP LAB EXAM AY:2023-24 SEM-2
if (!found) {
fclose(file);
int main() {
writeRecords(filename);
readRecords(filename);
int roll_no_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
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
if (sourceFile == NULL) {
exit(EXIT_FAILURE);
FY-PSAP LAB EXAM AY:2023-24 SEM-2
if (targetFile == NULL) {
fclose(sourceFile);
exit(EXIT_FAILURE);
fputc(ch, targetFile);
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() {
// matrix 1
scanf("%d", &arr1[i][j]);
// matrix 2
scanf("%d", &arr2[i][j]);
// Transpose of matrix 1
}
FY-PSAP LAB EXAM AY:2023-24 SEM-2
printf("\n");
// Diagonal element
if(i== j){
// addition
else{
for(int i = 0;i<r1;i++){
for(int j = 0;j<c1;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
if (r <= 0 || c <= 0) {
printf("Rows and columns must be positive integers.\n");
return 1;
}
int matrix[r][c];
// 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
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char ch;
int count = 0;
if (file == NULL) {
return 1;
// Read characters from the file, count them, and print each on a new line
printf("%c\n", ch);
count++;
fclose(file);
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() {
int i, j;
while(str[i] == ch) {
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
#include <stdio.h>
#include <string.h>
int main() {
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];
scanf("%[^\n]", str);
int i;
FY-PSAP LAB EXAM AY:2023-24 SEM-2
return 0;
}
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];
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);
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]);
}
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;
scanf("%d",&a);
scanf("%d",&b);
hcf = i;
lcm = a*b/hcf;
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 matrix1[r1][c1];
int matrix2[r2][c2];
int store = 0;
FY-PSAP LAB EXAM AY:2023-24 SEM-2
scanf("%d", &matrix1[i][j]);
scanf("%d", &matrix2[i][j]);
if (matrix1[i][j] != matrix2[i][j]) {
store = 1;
if (store == 1) {
if (store == 0) {
} else {
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);
printf("\nNew array:\n");
for (int i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}
} else {
printf("\nInvalid position!\n");
}
return 0;
}
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);
return 0;
}
E39
FY-PSAP LAB EXAM AY:2023-24 SEM-2
#include <stdlib.h>
if (size1 != size2) {
return;
arr1[i] = arr2[i];
arr2[i] = temp;
if (arr1[i] != arr2[i]) {
int main() {
scanf("%d", &size1);
scanf("%d", &size2);
exit(0);
scanf("%d", &arr1[i]);
scanf("%d", &arr2[i]);
printf("\n");
} else {
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];
scanf("%99s", str1);
printf("Enter second string: ");
scanf("%99s", str2);
return 0;
} E41
Write a C program for sorting list of elements using bubble sort.
#include <stdio.h>
int swapped = 0;
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;
printf("\n");
int main() {
bubbleSort(data, size);
printArray(data, size);
E42
Write a C program for sorting list of elements using selection sort
#include <stdio.h>
min_idx = i;
array[min_idx] = array[step];
array[step] = temp;
printf("\n");
int main() {
selectionSort(data, size);
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;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
if(arr[i] == arr[j]) {
printf("\n");
return 0;
E45
Write a C program to insert element in an array on given specific
position.