Programming in C – Module Bank
I [Link]., I Semester, CSE, Section-4
Faculty: N Bharath Kumar
1. Given an array of integers A.
a) Design an algorithm to construct a Sum_Array S (of the same size) such that S[i] is
equal to the sum of all the elements of A except A[i].
Input:
First line contains n – size of the array
Second line contains n space-separated integers (elements of array A)
Output:
n-space separated integers (elements of array S)
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
14 13 12 11 10
Test Case-2:
Input:
10
21 23 56 87 32 45 69 12 8 77
Output:
409 407 374 343 398 385 361 418 422 353
Explanation:
In test case-1, the array A contains 5 elements [1, 2, 3, 4, 5]. The sum array S is [2+3+4+5,
1+3+4+5, 1+2+4+5, 1+2+3+5, 1+2+3+4] = [14, 13, 12, 11, 10]
b) Design an algorithm to check the sum of prime numbers of the array, A, is also a prime
number or not.
Input:
First line contains n – size of the array
Second line contains n space-separated integers (elements of array A)
Output:
“Prime” if the sum of prime numbers of the array is a prime number else “Not Prime”
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
Not Prime
Test Case-2:
Input:
10
1 2 3 4 5 6 7 8 9 10
Output:
Prime
Explanation:
In test case-1, the array A contains 5 elements [1, 2, 3, 4, 5]. The sum of prime numbers
(2+3+5) is 10 which is not a prime number.
2. Given an array of integers A.
a) Design an algorithm to find the count of pairs (x, y) in A such that x > y.
Input:
First line contains n – size of the array
Second line contains n space-separated integers (elements of array A)
Output:
Count of pairs (x, y) in A such that x > y.
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
0
Test Case-2:
Input:
5
2 1 0 3 4
Output:
3
Explanation:
In test case-1, the array A contains 5 elements [1, 2, 3, 4, 5]. There is no such pair (x, y) in
A where x > y.
In test case-2, the array A contains 5 elements [2, 1, 0, 3, 4]. There are 3 pair (2, 1), (2, 0),
(1, 0) in A where x > y.
b) Design an algorithm to find the sum of the elements of the array, A, without using
loops.
Input:
First line contains n – size of the array
Second line contains n space-separated integers (elements of array A)
Output:
Sum of the elements of the array.
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
15
Test Case-2:
Input:
5
1 2 3 4 5 6 7 8 9 10
Output:
55
Explanation:
In test case-1, the array A contains 5 elements [1, 2, 3, 4, 5]. The sum of the elements of the
array is 15.
3. Given a range of n-digit (n>=2) numbers between x and y.
a) Design an algorithm to find all the prime numbers in the given range after swapping
the first and last digits of each number.
Input:
First line contains two numbers x and y (range of numbers between x and y)
Output:
All prime numbers (swap the first digit and last digit of each no. b/w x and y and check for
prime, if prime print that number).
Example:
Test Case-1:
Input:
11
15
Output:
11
31
41
Test Case-2:
Input:
1154
1160
Output:
6151
7151
9151
Explanation:
In test case-1, x = 11 and y = 15. So, the range of numbers is 11, 12, 13, 14, and 15. After
swapping the first and last digits, the numbers are 11, 21, 31, 41, and 51. The prime numbers
among them are 11, 31, and 41.
In test case-2, x = 1154 and y = 1160. So, the range of numbers is 1154, 1155, 1156, 1157,
1158, 1159, and 1160. After swapping the first and last digits, the numbers are 4151, 5151,
6151, 7151, 8151, 9151, and 161. The prime numbers among them are 6151, 7151, and
9151.
b) Design an algorithm to find the palindromic primes in the given range after reversing
each number (A palindromic prime is a prime number that is also a palindromic
number).
Input:
First line contains two numbers x and y (range of numbers between x and y)
Output:
All palindromic prime numbers.
Example:
Test Case-1:
Input:
1 20
Output:
2 3 5 7 11
Test Case-2:
Input:
100 1000
Output:
101 131 151 181 191 313 353 373 383 727 757 787
797 919 929
Explanation:
In test case-1, x = 1 and y = 20. After reversing, the numbers are 1, 2, 3, …, 9, 1, 11, 21, 3,
…, 91, and 2. The palindromic prime numbers among them are 2, 3, 5, 7, and 11.
In test case-2, x = 100 and y = 1000. So, the range of numbers is 100, 101, 102, …, 998,
999, and 1000. After reversing, the numbers are 1, 101, 201, 301, …., 899, 999, and 1. The
palindromic prime numbers among them are 101, 131, 151, 181, 191, 313, 353, 373, 383,
727, 757, 787, 797, 919, and 929.
4. Given a range of n-digit (n > 2) numbers between x and y. Design an algorithm to find
the absolute difference between the sum of even indexed digits and odd indexed digits
of each number between x and y.
Input:
First line contains two numbers x and y (range of numbers between x and y)
Output:
Absolute difference between the sum of even indexed and odd indexed digits.
Example:
Test Case-1:
Input:
5678 5682
Output:
2 3 7 6 5
Test Case-2:
Input:
45678 45687
Output:
6 7 3 2 1 0 1 2 3 4
Explanation:
In test case-1, x = 5678 and y = 5682. In the first number 5678, the sum of odd indexed
digits is 5+7 = 12 (5 is 1st and 7is 3rd digit) and sum of even indexed digits are 6+8 = 14 (6
is 2nd and 8 is 4th digit). The absolute difference between even sum and odd sum is |12 – 14|
= 2.
5. There are some students standing in a row, each has a number written on their back.
The students are about to divide into two teams: those standing at the even positions
will go to team A if the number written on their back is a prime, and those standing at
the odd position will join team B if the number written on their back is a prime, the
remaining students join team C.
a) Design an algorithm that calculates the absolute difference between the sum of
numbers written on the back of the students that will join i) team A, team C and ii)
team B, team C.
Input:
First line contains n, no. of students
Second line contains n integers (the numbers that are written on the back)
Output:
First line contains absolute difference between the sum of numbers written on the back of
the students that will join team A and team C.
Second line contains absolute difference between the sum of numbers written on the back
of the students that will join team B and team C.
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
3
3
Test Case-2:
Input:
6
45 67 81 34 91 12
Output:
263
196
Explanation:
In test case-1, there are 5 students standing in a row with 1, 2, 3, 4, 5 numbers written on their
back. The students standing at even positions join team A if the numbers written on their back
are prime else team C (i.e., the student with number 2 joins team A and with number 4 join
team C, as 2 is prime and 4 is not a prime) and while standing at odd positions will join team
B if the numbers written on their back are prime else team C (i.e., the students with numbers 3
and 5 written on their back will join team B and with number 1 will join team C). The sum of
the numbers of team A = 2, team B = 8, and team C = 5. The absolute differences are |2-5| = 3
and |8-5| = 3.
6. You are given an array of integers A. Design an algorithm to form another array C
where each element is the count of trailing zeros of the factorial of each integer in the
given array A.
Input:
First line contains n, size of the array
Second line contains n integers (the elements of the array)
Output:
First line contains n integers where each represents the factorial of each integer of given
array.
Second line contains n integers where each element is the count of trailing zeros of the
factorial of each integer in the given array.
Example:
Test Case-1:
Input:
5
1 2 3 4 5
Output:
1 2 6 24 120
0 0 0 0 1
Test Case-2:
Input:
6
10 9 6 3 3 7
Output:
3628800 362880 720 6 6 5040
2 1 1 0 0 1
Explanation:
In test case-1, there are 5 elements 1, 2, 3, 4, 5 numbers. The factorials of these numbers are
1 2 6 24 120. 1, 2, 6, 24 contains 0 zeros at the end while 120 contains 1 zero at the end.
Hence, the second line of the output is 0 0 0 0 1.
7. Given an integer. First, you have to convert the integer to binary, then extract k bits
from left to right from the position pthbit position of the binary number and display its
equivalent decimal number. (Note: The position p is from right to left. Also, don’t use
arrays). Design an algorithm to display the decimal number of the extracted bits.
Input:
First line contains n. (An integer)
The second line contains p. (the pth bit position from where the bits need to be extracted)
Third line contains value k. (no. of bits need to be extracted)
Output:
Decimal equivalent of extracted binary bits.
Example:
Test Case-1:
Input:
25
4
3
Output:
4
Test Case-2:
Input:
678
6
5
Output:
19
Explanation:
In test case-1, the given integer is 25. Its binary equivalent is 11001. We have to extract k
(3 bits towards left) from position p (4th bit from right).
8. You are given a sentence.
a) Design an algorithm to find the largest and smallest word from the given sentence.
Consider the first word if there are two or more words of the same length. A single
letter in a sentence is also considered a word.
Input:
First line contains the sentence taken as input from user.
Output:
Largest and smallest words in the sentence
Example:
Test Case-1:
Input:
Problem Solving through Programming in C
Output:
Largest word: Programming
Smallest word: C
Test Case-2:
Input:
Hello all welcome to Vignan University
Output:
Largest word: University
Smallest word: to
b) Design an algorithm to print the sentence after reversing each word.
Input:
First line contains the sentence taken as input from user.
Output:
The sentence with each word to be printed in reverse
Example:
Test Case-1:
Input:
Programming in C
Output:
gnimmargorP ni C
Test Case-2:
Input:
Hello World!
Output:
olleH !dlroW
9. You are given a sentence. Design an algorithm to find the beautiful words in the given
sentence. A word is said to be beautiful if it doesn’t contain duplicates (i.e., no
character is repeated more than once).
Input:
First line contains the sentence taken as input from user.
Output:
Beautiful / Not Beautiful
Example:
Test Case-1:
Input:
Welcome to Programming Class
Output:
Not Beautiful
Beautiful
Not Beautiful
Not Beautiful
Test Case-2:
Input:
Hello World
Output:
Not Beautiful
Beautiful
Explanation:
In test case-1, the given sentence is Welcome to Programming Class. It contains 4 strings
(words) – Welcome, to, Programming, and Class. The first word Welcome is Not Beautiful
word because it has repeated characters (e). Similarly, to is a Beautiful word, Programming
is Not Beautiful word and Class is also Not Beautiful word.
10. You are given a sentence. Design an algorithm to find the attractive words in the
given sentence. A word is said to be attractive if the no. of vowels and consonants in it
are equal.
Input:
First line contains the sentence taken as input from user.
Output:
Attractive / Not Attractive
Example:
Test Case-1:
Input:
Hi good morning welcome to class
Output:
Attractive
Attractive
Not Attractive
Not Attractive
Attractive
Not Attractive
Test Case-2:
Input:
Logic is needed to be expert in programming
Output:
Not Attractive
Attractive
Attractive
Attractive
Attractive
Not Attractive
Attractive
Not Attractive
Explanation:
In test case-1, the given sentence is Hi good morning welcome to class. It contains 6 strings
(words) – Hi, good, morning, welcome, to, and class. The first word Hi is an Attractive
word because it has an equal no of vowels and consonants (1-vowel i, 1-consonant
H). Similarly, good is an Attractive word, morning is Not Attractive word (because it
contains 2 vowels [o, i] and 5 consonants [m, r, n, n, g]), welcome is Not Attractive word,
to is an Attractive word and Class is Not Attractive word.