PSCP Question Bank Answer Key
PSCP Question Bank Answer Key
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; ++i) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int *ptr = numbers;
printf("Array elements using pointer arithmetic: ");
for (int i = 0; i < 5; ++i) {
printf("%d ", *ptr);
}
printf("\n");
return 0;
return 0;
}
6. Write a C program for swapping of two numbers by passing an address to the function.
#include <stdio.h>
void swapNumbers(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Original numbers: %d, %d\n", num1, num2);
swapNumbers(&num1, &num2);
printf("Swapped numbers: %d, %d\n", num1, num2);
return 0;
}
7. Develop a C program to get 5 students information like Rollno, Name and marks using structure
and display the details.
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
const int numStudents = 2;
struct Student students[numStudents];
for (int i = 0; i < numStudents; ++i) {
printf("Enter details for Student %d:\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollNo);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
printf("\n");
}
printf("Student details:\n");
for (int i = 0; i < numStudents; ++i) {
printf("Student %d:\n", i + 1);
printf("Roll No: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
}
return 0;
}
9. Develop a C program which reads name, register number, physics mark, chemistry mark and
mathematics mark using a structure. Calculate the Engineering cut-off using structures.
#include <stdio.h>
struct Student {
char name[50];
int regNumber;
float physicsMark;
float chemistryMark;
float mathMark;
};
float calculateCutOff(struct Student student) {
return ((student.physicsMark + student.chemistryMark) / 2.0)+student.mathMark ;
}
int main() {
struct Student student;
printf("Enter student details:\n");
printf("Name: ");
scanf("%s", student.name);
printf("Register Number: ");
scanf("%d", &student.regNumber);
printf("Physics Mark: ");
scanf("%f", &student.physicsMark);
printf("Chemistry Mark: ");
scanf("%f", &student.chemistryMark);
printf("Mathematics Mark: ");
scanf("%f", &student.mathMark);
float cutOff = calculateCutOff(student);
printf("\nEngineering Cut-off for %s (Reg No: %d): %.2f\n", student.name, student.regNumber,
cutOff);
return 0;
}
10. Develop a C program to create struct employee that contains members employee name and
address. Store the address of the employee into a separate structure and nest the struct address
into the structure employee.
#include <stdio.h>
struct Address {
char street[50];
char city[50];
char state[50];
int zipCode[15];
};
struct Employee {
char name[50];
struct Address address;
};
int main() {
struct Employee employee;
printf("Enter employee details:\n");
printf("Name: ");
scanf("%s", employee.name);
printf("Address details:\n");
printf("Street: ");
scanf("%s", employee.address.street);
printf("City: ");
scanf("%s", employee.address.city);
printf("State: ");
scanf("%s", employee.address.state);
printf("Zip Code: ");
scanf("%s", employee.address.zipCode);
printf("\nEmployee Details:\n");
printf("Name: %s\n", employee.name);
printf("Address: %s, %s, %s, %s\n", employee.address.street, employee.address.city,
employee.address.state, employee.address.zipCode);
return 0;
}
11. Write a C program which prints only the odd positioned alphabets from the given string
“ELEPHANT” using array of characters
#include <stdio.h>
int main() {
char str[] = "ELEPHANT";
int length = 0;
while (str[length] != '\0') {
length++;
}
printf("Odd-positioned Alphabets: ");
for (int i = 0; i < length; i += 2) {
printf("%c ", str[i]);
}
printf("\n");
return 0;
}
12. Identify the element 8 and its index from the given array {10, 2, 8, 5, 17} using linear search.
#include <stdio.h>
int linearSearch(int arr[], int size, int target, int *index) {
for (int i = 0; i < size; ++i) {
if (arr[i] == target) {
*index = i;
return 1;
}
}
return 0;
}
int main() {
int arr[] = {10, 2, 8, 5, 17};
int target = 8;
int index;
int found = linearSearch(arr, sizeof(arr) / sizeof(arr[0]), target, &index);
if (found) {
printf("Element %d found at index %d.\n", target, index);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
13. Write a C program which accepts two strings of equal length and clubs them together using the
following pattern. String1: abcd, String2: 1234, output: a1b2c3d4
#include <stdio.h>
#include <string.h>
int main() {
char string1[50], string2[50], result[100];
printf("Enter the first string: ");
scanf("%s", string1);
printf("Enter the second string: ");
scanf("%s", string2);
if (strlen(string1) != strlen(string2)) {
printf("Error: Strings must have equal length.\n");
return 1;
}
int index = 0;
for (int i = 0; i < strlen(string1); ++i) {
result[index++] = string1[i];
result[index++] = string2[i];
}
result[index] = '\0';
printf("Clubbed String: %s\n", result);
return 0;
}
14. Write a C program to calculate area of the square using user- defined function.
#include <stdio.h>
float calculateSquareArea(float side) {
return side * side;
}
int main() {
float side, area;
printf("Enter the side length of the square: ");
scanf("%f", &side);
area = calculateSquareArea(side);
printf("Area of the square with side %.2f is: %.2f\n", side, area);
return 0;
}
15. Given different prices of a vegetable which is varying through the day (from morning to
evening), find out the best buy price and sell price for the maximum profit. Eg. For the prices [33,
35, 28, 36, 39, 25, 22, 31], best buy is at 28 and best sell is at 39
#include <stdio.h>
void findBestBuySellPrices(int prices[], int n, int *bestBuy, int *bestSell) {
if (n < 2) {
printf("Insufficient prices to calculate profit.\n");
return;
}
*bestBuy = prices[0];
*bestSell = prices[1];
int maxProfit = *bestSell - *bestBuy;
for (int i = 1; i < n; ++i) {
int currentProfit = prices[i] - *bestBuy;
if (currentProfit > maxProfit) {
*bestSell = prices[i];
maxProfit = currentProfit;
}
if (prices[i] < *bestBuy) {
*bestBuy = prices[i];
}}}
int main() {
int prices[] = {33, 35, 28, 36, 39, 25, 22, 31};
int n = sizeof(prices) / sizeof(prices[0]);
int bestBuy, bestSell;
findBestBuySellPrices(prices, n, &bestBuy, &bestSell);
if (bestBuy < bestSell) {
printf("Best Buy Price: %d\n", bestBuy);
printf("Best Sell Price: %d\n", bestSell);
printf("Maximum Profit: %d\n", bestSell - bestBuy);
} else {
printf("No profitable buying and selling opportunities.\n");
}
return 0;
}
16. Write a C program to sort the given array ‘a’ using insertion sort. a[5]={4,3,5,6,7}
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int a[] = {4, 3, 5, 6, 7};
int n = sizeof(a) / sizeof(a[0]);
printf("Original array: ");
printArray(a, n);
insertionSort(a, n);
printf("Sorted array: ");
printArray(a, n);
return 0;
}
17. Write a C program to find the factorial of a number “8” using recursion.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}}
int main() {
int number = 8;
printf("Factorial of %d is: %d\n", number, factorial(number));
return 0;
}
18. Write a C program that takes input for a base number - 8 and an exponent 2, and calculate the
power of the base number using recursion.
#include <stdio.h>
double power(int base, int exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1); // Recursive case
}}
int main() {
int base, exponent;
printf("Enter the base number: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
printf("%d^%d is: %.0f\n", base, exponent, power(base, exponent));
return 0;
}
19. Create a C program that prompt the user to get input, calculates the sum of n numbers stored
in a file using sequentially access method.
#include <stdio.h>
int main() {
FILE *file;
int n, number, sum = 0;
printf("Enter the number of elements (n): ");
scanf("%d", &n);
file = fopen("numbers.txt", "r");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
for (int i = 0; i < n; ++i) {
// Read each number from the file
if (fscanf(file, "%d", &number) == 1) {
sum += number;
} else {
printf("Error reading number from the file.\n");
fclose(file);
return 1;
}}
fclose(file);
printf("Sum of the first %d numbers in the file: %d\n", n, sum);
return 0;
}
20. Develop a C program that performs the transaction processing using random access files.
(notes)
21. Develop a C program that writes the data 'I Love India' to a file using random access file
operations. After writing the data, display the position of the file pointer in the file.
#include <stdio.h>
int main() {
FILE *file;
char data[] = "I Love India";
file = fopen("random_access_file.txt", "wb+");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
fwrite(data, sizeof(char), sizeof(data) - 1, file);
long currentPosition = ftell(file);
printf("Position of the file pointer after writing: %ld\n", currentPosition);
fclose(file);
return 0;
}