Programming Question
Previous Year Written Question
Write a program in any language to find the prime numbers between 1....... n, where n is taken as user input.
Sample input:
Enter value of n: 20
Sample Output:
Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19Combined Bank (SO(it)), 2024
Sample input:
Enter value of n: 20
Sample Output:
Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19Combined Bank (SO(it)), 2024
#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
void findPrimes(int n) {
cout << "Prime Numbers: ";
for (int i = 1; i <= n; i++) {
if (isPrime(i))
cout << i << " ";
}
cout << endl;
}
int main() {
int n;
cout << "Enter value of n: ";
cin >> n;
findPrimes(n);
return 0;
}
Sample I/O:Enter value of n: 20
Prime Numbers: 2 3 5 7 11 13 17 19
Write a program in any language to find the sum of rows and columns of a m X n matrix, where m and n is taken input from the user. Give the output in the following format:
Combined Bank (SO(it)), 2024
Combined Bank (SO(it)), 2024 #include <stdio.h>
int main() {
int m, n, i, j;
// Input matrix dimensions
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int matrix[m][n];
// Input matrix elements
printf("Enter the elements of the matrix row-wise:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate and print row sums
printf("\nMatrix with Row Sums:\n");
for (i = 0; i < m; i++) {
int rowSum = 0;
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
rowSum += matrix[i][j];
}
printf("%d\n", rowSum); // Row sum
}
// Calculate and print column sums
printf("\nColumn Sums:\n");
for (j = 0; j < n; j++) {
int colSum = 0;
for (i = 0; i < m; i++) {
colSum += matrix[i][j];
}
printf("%d ", colSum);
}
return 0;
}
Sample I/O:Enter the number of rows (m): 3 Enter the number of columns (n): 4 Enter the elements of the matrix row-wise: 1 3 4 2 2 4 5 3 3 2 2 5 Output: Matrix with Row Sums: 1 3 4 2 10 2 4 5 3 14 3 2 2 5 12 Column Sums: 6 9 11 10
Write a Program to print Prime numbers from 1 to n. Combined Bank (AP), 2024
#include <iostream>
using namespace std;
int main() {
int i, num, n, count;
cout << "Enter the range: ";
cin >> n;
cout << "The prime numbers in between the range 1 to " << n << " are: ";
for (num = 1; num <= n; num++) {
count = 0;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
count++;
break;
}
}
if (count == 0 && num != 1)
cout << num << " ";
}
return 0;
}Sample I/O:
Enter the range: 50
The prime numbers in between the range 1 to 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
=== Code Execution Successful ===Write the pseudocode/ program (in any language) to print the common element between two arrays and the total number of such elements found. [assume each element in an array distinct].Bangladesh Bank (AD(ict)), 2025
#include <iostream>
using namespace std;
int main() {
int size1, size2;
// Taking input size of first array
cout << "Enter size of first array: ";
cin >> size1;
int arr1[size1];
cout << "Enter elements of first array:\n";
for (int i = 0; i < size1; i++) {
cin >> arr1[i];
}
// Taking input size of second array
cout << "Enter size of second array: ";
cin >> size2;
int arr2[size2];
cout << "Enter elements of second array:\n";
for (int i = 0; i < size2; i++) {
cin >> arr2[i];
}
// To store common elements
int commonElements[size1 < size2 ? size1 : size2];
int count = 0;
// Check each element in arr1 against arr2
for (int i = 0; i < size1; i++) {
bool found = false;
for (int j = 0; j < size2; j++) {
if (arr1[i] == arr2[j]) {
found = true;
break;
}
}
if (found) {
// Check for duplicates
bool alreadyExists = false;
for (int k = 0; k < count; k++) {
if (commonElements[k] == arr1[i]) {
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
commonElements[count] = arr1[i];
count++;
}
}
}
// Print common elements
cout << "Common elements: ";
for (int i = 0; i < count; i++) {
cout << commonElements[i] << " ";
}
cout << endl;
// Print total count
cout << "Total number of common elements: " << count << endl;
return 0;
}
Sample I/O:
Enter size of first array: 5
Enter elements of first array: 0 20 30 40 50
Enter size of second array: 6
Enter elements of second array: 10 2 20 30 60 90
Common elements: 20 30
Total number of common elements: 2
=== Code Execution Successful ===Write a Program to print Floyd's Triangle for n = 5. Combined Bank (AP), 2024
Expected Output:
1
01
101
0101
10101
Expected Output:
1
01
101
0101
10101
#include <iostream>
using namespace std;
int main() {
int i, j, n, p, q;
cout << "Input number of rows: ";
cin >> n;
for (i = 1; i <= n; i++) {
if (i % 2 == 0) {
p = 1;
q = 0;
} else {
p = 0;
q = 1;
}
for (j = 1; j <= i; j++) {
if (j % 2 == 0)
cout << p;
else
cout << q;
}
cout << endl;
}
return 0;
}Sample I/O:
Input number of rows: 6
1
01
101
0101
10101
010101
=== Code Execution Successful ===Write a C++ program to find the sum of the series: 1 + 2 + 4 + 7 + 11 + ... + N. Combined Bank (AP), 2024
#include <iostream>
using namespace std;
int main() {
int n, i, sum = 0, term = 1, nextTerm;
cout << "Enter the value of n: ";
cin >> n;
cout << "The series is: ";
for (i = 1; i <= n; i++) {
cout << term << " ";
sum += term;
nextTerm = term + i;
term = nextTerm;
}
cout << "\nThe sum of the series is: " << sum << endl;
return 0;
}Sample I/O:
Enter the value of n: 10
The series is: 1 2 4 7 11 16 22 29 37 46
The sum of the series is: 175
=== Code Execution Successful ===Consider the following code snippet in a C like Programming language -
int a = 5, b= 18, c = 27,
a=b++ + ++c;
b = a++ - c --;
c= a++ + b --;
C++ Code Execution Explanation
Step-by-Step Execution
Initial values:
a = 5, b = 18, c = 27
Step 1: a = b++ + ++c;
• b++ → use 18, then b = 19
• ++c → increment first → c = 28, use 28
⇒ a = 18 + 28 = 46
Now: a = 46, b = 19, c = 28
Step 2: b = a++ - c--;
• a++ → use 46, then a = 47
• c-- → use 28, then c = 27
⇒ b = 46 – 28 = 18
Now: a = 47, b = 18, c = 27
Step 3: c = a++ + b--;
• a++ → use 47, then a = 48
• b-- → use 18, then b = 17
⇒ c = 47 + 18 = 65
Now: a = 48, b = 17, c = 65
Final Output
a = 48 b = 17 c = 65
Write a function which receives an array of integers as parameter and print the numbers divisible by 3 in the array. CB, Officer (it), 2025
#include <stdio.h>
void printDivisibleBy3(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] % 3 == 0) {
printf("%d\n", arr[i]);
}
}
}
int main() {
int size;
printf("Enter the number of elements: ");
scanf("%d", &size);
int numbers[size]; // Variable Length Array (VLA)
printf("Enter %d numbers:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
printf("Numbers divisible by 3:\n");
printDivisibleBy3(numbers, size);
return 0;
}Sample I/O:
Enter the number of elements: 6
Enter 6 numbers:
5 6 9 12 8 11
Numbers divisible by 3:
6
9
12
=== Code Execution Successful === একটি C program লিখুন যা একটি 5-digit সংখ্যার সকল digit-এর যোগফল নিরূপণ করতে পারে। 45 BCS,Computer Science(971)
#include <stdio.h> int main() { int num, sum = 0, digit; printf("Enter a 5-digit number: "); scanf("%d", &num); while (num != 0) { digit = num % 10; // get last digit sum = sum + digit; // add digit to sum num = num / 10;// remove last digit } printf("Sum of digits = %d\n", sum); return 0; }
Suppose you are working with an array of size 10. It contains all the numbers from 1 to 10 exactly once in a random order. But accidentally, one of the numbers in the array got replaced by a zero (0). Write a C/C++ programme using functions, to restore the lost number. [Ministry of food, network/website Manager(ICT),2025]
#include <stdio.h>
// Function to find the missing number
int findMissing(int arr[], int size) {
int total = (size * (size + 1)) / 2; // Sum of numbers from 1 to size
int sum = 0;
for(int i = 0; i < size; i++) {
sum += arr[i];
}
return total - sum; // Missing number
}
int main() {
int arr[10] = {1, 2, 3, 4, 5, 0, 7, 8, 9, 10};
int missing;
missing = findMissing(arr, 10);
// Replace zero with missing number
for(int i = 0; i < 10; i++) {
if(arr[i] == 0) {
arr[i] = missing;
}
}
printf("Restored Array:\n");
for(int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Using recursion, develop a computer program to find the n-th Fibonacci number using this rule.[ BPSC AME)-24]
#include <stdio.h>
// Recursive function to find nth Fibonacci number
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter value of n: ");
scanf("%d", &n);
printf("The %dth Fibonacci number is: %d\n", n, fibonacci(n));
return 0;
}
Previous Year MCQ Question
Find the output:
int main()
{
int x=3; int y=2;
if (x==2)
y=3;
else
y=2;
printf("%d %d\n", x, y);
}
Bangladesh Bank (AD(ict)), 2025 {
int x=3; int y=2;
if (x==2)
y=3;
else
y=2;
printf("%d %d\n", x, y);
}
2,3
More Practice programming
Coming SOON
Coming Soon
