Combined Bank
Post: Assistant Programmer,
Exam Date: 09.02.2024, Exam Taker: BIBM
1. Write a Program to print Prime numbers from 1 to n.
#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 ===2. Write a Program to print Floyd's Triangle for n = 5.
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 ===3. Write a C++ program to find the sum of the series: 1 + 2 + 4 + 7 + 11 + ... + N.
#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 ===4. What is Polymorphism? Discuss different types of Polymorphism with examples.
Polymorphism means “having many forms.” It allows the same method or function to perform different tasks based on the context. In simpler terms, it enables a single interface to represent different functionalities.
Polymorphism can be broadly categorized into two types:
- Compile-time Polymorphism (Static Polymorphism)
- Runtime Polymorphism (Dynamic Polymorphism)
1. Compile-time Polymorphism (Static Polymorphism)
Compile-time polymorphism is achieved through method overloading and operator overloading. The method to be executed is determined at compile time based on the method signature.
2. Runtime Polymorphism (Dynamic Polymorphism)
Runtime polymorphism is achieved through method overriding. The method to be executed is determined at runtime based on the object being referred to.
