Pasap Important Questions
Pasap Important Questions
Pasap Important Questions
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;
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>
while (*s) {
if (*s == '(' || *s == '{' || *s == '[') {
countOpening++;
} else if (*s == ')' || *s == '}' || *s == ']') {
countClosing++;
s++;
}
// Check if the counts of opening and closing brackets are the same
return (countOpening == countClosing);
}
int main() {
char input[100];
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
int main() {
char name[100];
printf("Enter the name: ");
scanf("%s", name);
int count = 0;
for (int i = 0; name[i] != '\0'; i++) {
count++;
}
// int s=0;
// int e=count-1;
// while(s<e){
// swap(&name[s++],&name[e--]);
// }
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
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);
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.
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);
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
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);
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
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);
return 0;
}
E11
Write a C program to print Fibonacci series up to n terms.
}
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;
}
return 0;
}
E14
Write a C program to reverse an array using pointers.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
E15
Write a function to print all perfect numbers in a given interval in C programming.
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;
};
int main() {
int n;
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
int main() {
// Declare a structure variable to store student data
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;
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
printf("\n");
}
int main() {
int 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;
}
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;
};
int main() {
struct Employee employees[3];
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;
};
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;
}
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
return 0;
}
E33
Write a c program to insert a sub-string in to given main string.
int main() {
char s1[100];
char s2[100];
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
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++;
}
}
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;
}
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;
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>
int main() {
int 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;
// Declare matrices
int matrix1[rows][cols];
int matrix2[rows][cols];
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;
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
}
(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>
int main() {
int size;
// Declare arrays
int array1[size], array2[size];
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];
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
int main() {
int n;
int arr[n];
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
return maxElement;
}
int main() {
int n, key;
int arr[n];
if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
E44
Write a C program to find duplicate element in an array.
#include <stdio.h>
int main() {
int n;
int arr[n];
findDuplicates(arr, n);
return 0;
}
E45
Write a C program to insert element in an array on given specific position.