Skip to content

Instantly share code, notes, and snippets.

@WrichikBasu
Last active September 28, 2024 00:14
Show Gist options
  • Save WrichikBasu/c0fa999cff6d40560ebef61d5ce4befe to your computer and use it in GitHub Desktop.
Save WrichikBasu/c0fa999cff6d40560ebef61d5ce4befe to your computer and use it in GitHub Desktop.

148 Programming Questions

The following is a list of all the programming questions that I had solved through middle school and high school.

I programmed these questions in Java, but with slight modifications, these can also be done in Python or any other language. These are meant to improve your concepts and build a programming logic, and thereby strengthen your foundations.

These were lying in a .docx file on my system. I have compiled them into a more readable and widely accepted format. Some formatting issues might still be present, which I intend to fix as I find them. On the same note, if any question does not make sense, feel free to post a comment and I will cross-check with the .docx file.

License

This file is released under Creative Commons Attribution-Share Alike 4.0.

General Instructions

  • All questions asking to print a certain pattern should be solved using loops.
  • x ^ y in Java, read as "x raised to the power of y", is equivalent to x ** y in Python.
  • switch statements in Java are equivalent to match-case in Python. If such statements are not available in the language you are using, just use if-else statements.

Questions

  1. Write a program to input three numbers and print their sum.
  2. Write a program to input a number and find its square root.
  3. Write a program to calculate the taxable income on ₹3,10,000 if tax rate is fixed at 2.2%.
  4. Write a program that computes payment for a proof-reader who is paid at the rate of ₹60 per page. Take the number of pages as Input.

  1. Write a program that displays an amount in rupees in terms of different denominations. For example, if the entered amount is ₹1728, the output should be:
The entered amount is: ₹1728
The number of ₹1000 notes is: 1
The number of ₹500 notes is: 1
The number of ₹100 notes is: 2
The number of ₹50 notes is: 0
The number of ₹10 notes is: 2
The number of ₹5 notes is: 1
The number of ₹2 notes is: 1
The number of ₹1 notes is: 1

  1. Write a program to read time in minutes and split it into days, hours and minutes.
  2. Write a program to take three sides of a triangle as input, and find whether the triangle is possible or not. If possible, identify the triangle.
  3. Write a program to take a number as input and display the sum of the digits.
  4. Write a program to take a number as input and reverse it.

  1. Write a program to display the following:
1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

  1. Write a program to print the following pattern:
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

  1. A telephone bill is drawn up for each users on the basis of the following criteria:
CALLS RATE
First 100 calls No charge
Next 100 calls 50p/call
Next 200 calls 75p/call
Next 400 calls 97p/call

All users, in addition to the charge for calls, pay a minimum amount of ₹50. Write a program, which inputs the user’s name, the numbers of calls made, and calculate the amount due and print the bill.


  1. Write a program to generate the Fibonacci Series.
  2. Write a program which takes an integer as input and tests whether it is divisible by 2 and 3.
  3. Write a program to read two numbers and an operator, and perform arithmetic operations based on the operator.
  4. Write a program to read the length and breadth of a rectangle and then compute the area and perimeter. Use three methods.
  5. Write a program to read two numbers and find their sum using two classes.
  6. Write a program to take a string as input and find whether it is palindrome. If it is palindrome, print the reversed string.
  7. Write a program to print in descending order the square of the first 10 natural numbers.
  8. Write a program which reads the number of sides of a polygon and finds whether it is a triangle or rectangle or pentagon or hexagon or heptagon or octagon or nonagon or decagon.
  9. Write a program to generate the series: 0, 0, 2, 6… 90.
  10. Write a program to read the base and height of a triangle and calculate its area.

  1. Write a program to print the following pattern:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

  1. Write a program to input three unequal numbers and print the greatest number.
  2. Write a program to generate the multiplication table from 2 to 10.
  3. Write a program to print the factorial of a number.

  1. There are 300 workers. Write a program to input the name of the workers and his basic pay. Calculate his gross salary and net salary according to the given criteria:
BP DA HRA TAX
< 10,000 20% of BP 12% of BP 10% of BP
10,000 to 15,000 30% of BP 15% of BP 20% of BP
> 15,000 40% of BP 20% of BP 30% of BP

Display the name of the person, his gross and net salary. Given:

GROSS SALARY = BP + DA + HRA
NET AMOUNT = GROSS SALARY – TAX

  1. The standard form of the quadratic equation is given by $ax^2 + bx + c = 0$ where $d = b^2 - 4ac$ is known as the discriminant, which determines the nature of the roots as:
Condition Nature
d >= 0 Roots are real
d < 0 Roots are imaginary

Write a program to take a, b and c as input and display the nature of the roots.


  1. Write a program to accept a number and find whether it is a prime number.
  2. Write a program to take some numbers as input and find their GCD.
  3. Write a program to find the factors of a number.
  4. Write a program to read a string and count the number of words in it.
  5. Write a program to read the name of a student, roll number of the student, marks in English and Maths and display them.

  1. Write a program to input the name, class and marks in four subjects of a pupil. The program should calculate and print the total marks, and grade, according to the following criteria:
PERCENTAGE GRADE
>= 90 O
80 to 89 A
70 to 79 B
55 to 69 C
40 to 54 D
<= 40 E

  1. Write a program to read a string and find the number of times the letter “a” appears in it.

  1. Write a program to print the following pattern:
       A
   B      B
C      C      C
   D      D
       E

  1. Take a integer array of length 4 x 4 and display its contents in a 4 x 4 matrix format.

  1. The Pig Latin form of a word is obtained by framing a new word with the first vowel present in the word and placing the remaining letters present before the vowel, after the vowel, and then adding ay at the end.

For example, the Pig Latin form of the word trouble is oubletray.

Write a program to accept a word and display the same in Pig Latin form.


  1. Write a menu-driven program to display the pattern of a string entered by the user. If the user enters “F” as the choice, display the first letters of each word in the sentence; if the user enters “L” as the choice, display the last letters of each word. For example:

Sample Input: HONESTY IS THE BEST POLICY.

Choice: F

Sample Output:

H
I
T
P
B

Choice: L

Sample output:

Y
S
E
T
Y

  1. Write a program to accept a string and display:
    1. The number of lower case characters.
    2. The number of upper case characters.
    3. The number of special characters.
    4. The number of digits.

For example:

Sample Input: S.T.D. code of New Delhi – 011

Sample Output:

The number of lower case characters: 12
The number of upper case characters: 5
The number of special characters: 10
The number of digits: 3

  1. You can encode or decode a string in many ways. Replace each vowel by two characters ahead in ASCII table and each consonant by the next letter to wrap around (i.e. A becomes Z). Write a program to encrypt a word as follows:
Word Encrypted Form
Computer Dqnqwugs

  1. Write a program to accept a word and print the letters in alphabetical order.

  1. Write a program to accept the ages of 100 employees of an organization and count the number of employees in the following age groups:
AGE (YEARS)
25-35
36-45
46-55

The program should throw an error if age is > 55 years.


  1. Write a program to find the sum of the following series: $\dfrac{2}{x + 2} + \dfrac{4}{x + 4} + … + \dfrac{16}{x + 16}.$

  1. Write a program to display the following pattern:
B L U E J
L U E J B
U E J B L
E J B L U
J B L U E

  1. The International Standard Book Number (ISBN) is a unique numeric book identifier that is printed on every book. The ISBN is based upon a ten-digit code. The ISBN is legal if:

1 * digit1 + 2 * digit2 + 3 * digit3 + 4 * digit4 + 5 * digit5 + 6 * digit6 + 7 * digit7 + 8 * digit8 + 9 * digit9 + 10 * digit10 is divisible by 11.

For example:

For an ISBN 1401601499,

Sum = (1 * 1) + (2 * 4) + (3 * 0) + (4 * 1) + (5 * 6) + (6 * 0) + (7 * 1) + (8 * 4) + (9 * 9) + (10 * 9) = 253, which is divisible by 11.

Write a program to:

(a) Input the ISBN code as a ten-digit number.

(b) If the ISBN is not a ten-digit number, output the message “Illegal ISBN” and terminate the program.

(c) If the number has ten digits, extract the digits of the number and compute the sum as explained above. If the sum is divisible by 11, output the message “Legal ISBN”; if the sum is not divisible by 11, output the message “Illegal ISBN”.


  1. Write a program to enter a number and check whether it is a Neon number. A number is said to be Neon, if the sum of the digits of the square of the number is equal to the number itself.

Sample Input: 9

Logic: 9 * 9 = 81; 8 + 1 = 9.

Sample Output:

9 is a Neon number.

  1. Write a program to print 50 prime numbers starting from a number entered by the user.

  1. Write a program to print the following pattern:
B   L   U   E   J
    B   L   U   E
        B   L   U
            B   L
                B

  1. Write a program to assign the full path name and file name as given below. Using library functions, extract and output the file path, file name and file extension as shown.

Sample Input:

C:\Users\admin\pictures\flower.jpg

Sample Output:

Path: C:\Users\admin\pictures
File Name: flower
File Extension: jpg

  1. Write a program to take numbers in a 4 x 4 matrix in Double Dimensional Array. Transpose the matrix and print the transposed matrix.
  2. Write a program to store numbers in a 4 by 4 matrix, and find the highest and lowest numbers in the matrix.
  3. Write a program to input elements in a single-dimensional array and delete an element according to the user’s choice.
  4. Write a program to take elements as input in a single-dimensional array, and then add an element in the position defined by the user.
  5. Write a program to take two single-dimensional arrays as input and the merge them.
  6. Write a program to input 10 different numbers in a single-dimensional array and display the sum of all the numbers which are divisible by either 3 or 5.
  7. Write a program to accept 10 different numbers in a single-dimensional array and search for a number entered by the user using Linear Search technique.
  8. Write a program to take elements in a single-dimensional array as input. Now, enter a number, and by Binary Search technique, check whether the number is present in the list of array elements. If the number is present, then show the message “Search Successful” and if not present, then show the message “Search Unsuccessful”.
  9. Write a program to accept 10 different numbers in a single-dimensional array. Arrange the numbers in ascending order by using selection-sort technique and display them.
  10. Write a program to accept a set of 10 integers in a single-dimensional array. Sort the numbers in ascending order by using Bubble Sort technique and display the sorted array.
  11. Write a program to display Pascal’s triangle.

  1. Write a program to accept a word in a single-dimensional array. Arrange all the letters in ascending order and display the new word.

Sample Input: BLUEJ

Sample Output: BEJLU


  1. Write a program to accept a string in a single-dimensional array. Display the string in a reversed order.

Sample Input: Computer is Fun

Sample Output: Fun is Computer


  1. Write a program to enter a natural number. Display all the possible combinations of consecutive natural numbers which add up to give the sum equal to the original number.

Sample Input: 15

Sample Output:

1, 2, 3, 4, 5 (as 1 + 2 + 3 + 4 + 5 = 15)
4, 5, 6 (as 4 + 5 + 6 = 15)
7, 8 (as 7 + 8 = 15)

  1. Write a program to accept a number and arrange the digits of the number in ascending order.

Sample Input: 4925

Sample Output: 2, 4, 5, 9


  1. Write a program to enter a number and check whether a number is Automorphic or not. A number is said to be an Automorphic number if it is present in the last digits of its square.

Sample Input: 25

Sample Output: 25 is an Automorphic number.

Reason: 252 = 625. As 25 is present in the last two digits of its square 625, it is an Automorphic number.


  1. Write a program to enter two numbers and check whether they are twin prime. Twin prime numbers are those consecutive prime numbers which differ by 2. For example, 11 and 13 are twin prime numbers, 17 and 19 are twin prime numbers and so on.

  1. “Atul Transport” charges for carrying parcels as per the city and weight. The tariff is given below:
City Weight Tariff
Kolkata Upto 100kg ₹45 per kg
^ More than 100kg ₹75 per kg
Mumbai Upto 100kg ₹65 per kg
^ More than 100kg ₹95 per kg
Chennai Upto 100kg ₹75 per kg
^ More than 100kg ₹115 per kg
Delhi Upto 100kg ₹90 per kg
^ More than 100kg ₹125 per kg

Write a program to input the city name: ‘K’ for Kolkata, ‘M’ for Mumbai, ‘C’ for Chennai and ‘D’ for Delhi, and the weight of the parcel. Calculate and display the charges to be paid by the customer for booking.


  1. There are 200 members in a library. Write a program to input the name of a member and the number of days delayed in returning a book. Calculate the amount of fine the member has to pay according to the following criteria:
NUMBER OF DAYS FINE
First 7 days 50p / day
Next 7 days ₹1 / day
Next 10 days ₹1.5/ day
> 24 ₹2 / day

  1. A company announces revised Dearness Allowances and Special Allowances as per the following tariff:
Basic Dearness Allowances (DA) Special Allowances (SA)
Upto ₹10,000 10% 5%
₹10,001 to ₹20,000 12% 8%
₹20,001 to ₹30,000 15% 10%
> ₹30,000 20% 12%

Write a program to accept the Basic Salary (BS) of an employee. Calculate and display the gross salary.

Gross Salary = Basic + Dearness Allowance + Special Allowance

Print the information in the given format:

Basic    DA     SA     Gross Salary
*        *      *      *

  1. Write a program to print the following series:
0, 3, 8, 15 … 99.

  1. Write a program to print the following pattern:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

  1. Write a program to find the sum of the following series: $\dfrac{1}{2} - \dfrac{2}{3} + \dfrac{3}{4} - \dfrac{4}{5} + ...$ for n terms.

  1. Write a program to generate the following series:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5

  1. Write a program to find the sum of the following series:
9 + 99 + 8 + 89 + 7 + 79 + ... + 0 + 9

  1. Write a program to find the sum of the following series:
1 + (1 * 2) + 2 + (1 * 2 * 3) + 3 + ... + 9 + (1 * 2 * 3 * ... * 10)

  1. Write a program to display the following series up to 10 terms:
1, -3, 5, -7, 9, ...

  1. Write a program to print the following pattern:
1   2   3   4   5
    2   3   4   5
        3   4   5
            4   5
                5
            4   5
        3   4   5
    2   3   4   5
1   2   3   4   5

  1. Write a program to print the following pattern:
1   2   3   4   5   5   4   3   2   1
1   2   3   4           4   3   2   1
1   2   3                   3   2   1
1   2                           2   1
1                                   1

  1. Write a program to print the following pattern:
            1
        3   3   3
     5  5   5   5   5
   7  7  7  7   7  7  7
9  9  9  9  9   9  9  9  9
   7  7  7  7   7  7  7
     5  5   5   5  5
        3   3   3
            1

  1. Using switch statement (or match-case in Python), write a menu-driven program to calculate the maturity amount of a bank deposit. The user is given the following options:

(a) Term Deposit

(b) Recurring Deposit

For option (a), accept the principal ($p$), the rate of interest ($r$), and time period in years ($n$). Calculate and display the maturity amount ($A$) by the formula:

$A = p \left(1 + \dfrac{r}{100} \right) ^ n$

For option (b), accept the monthly instalment ($p$), rate of interest ($r$) and time period ($n$) in months. Calculate and display the maturity amount using the formula:

$A = (p \times n) + p \times ((n \times (n + 1)))$

For an incorrect option, display the appropriate error message.


  1. Write a program to accept 10 different city names in a single-dimensional array. Arrange the names in ascending order by using Bubble Sort technique and display them.

  2. Write a program to store numbers in a 3 by 4 matrix. Find the sum of all the numbers in the matrix and display them.

  3. Write a program to store numbers in a 4 by 4 matrix. Find the sum of the numbers in the left diagonal as well as that in the right diagonal and display them.

  4. Write a program to store the day, the maximum and the minimum temperature during a week in a 7 by 3 matrix. The program accepts a day of the week and displays the maximum and the minimum temperature of that very day.

  5. A metropolitan hotel has 10 floors numbered from 0 to 9, each floor having 50 rooms. Using a single subscripted variable for the name of the customers and a double subscripted variable R[F,I], where F and I represent the floor and room numbers respectively, write a program for the room allocation work. The program should automatically record the name of the customer and the room number.


  1. Write a program to create a 3 by 3 square matrix and store numbers in it, and check whether it is symmetric or not. A square matrix is said to be symmetric if the element of the i-th row and the j-th column is equal to the element of the j-th row and the i-th column. For example, the following matrix is a symmetric matrix:
1 2 3
2 4 5
3 5 6

  1. Write a program to accept the year of graduation as an integer value from the user. Using the Binary Search technique on the array of integers given below, display the message “Record Exists” if the value input is present in the array, else display the message “Record does not Exist”.
1987, 1982, 2010, 1993, 1996, 2006, 1999, 2003, 2009, 2007.

  1. Write a program to store 20 numbers in a single-dimensional array and display the numbers which are perfect squares.
  2. Write a program to store 5 words in a single-dimensional array. Frame a new word by taking out the first letters of each of the 5 words in the array. Arrange the letters of the new word in alphabetical order and display the new word.

  1. Write a program, using arrays, to:
  1. Store the roll number, name and marks in 6 subjects for 100 students.
  2. Calculate the percentage of marks obtained by each candidate. The maximum marks in each subject are 100.
  3. Calculate the grade as per the given criteria:
Percentage Marks Grade
80 to 100 A
60 to 79 B
40 to 59 C
< 40 D

  1. Write a program to accept a string and change the case of each letter of the string.
  2. Write a program to enter a string and enter a word. Find the frequency of the entered word in the string.
  3. Write a program to accept a string and print the word containing the maximum number of vowels.
  4. Write a program to accept a string and find the longest word.
  5. Write a program to accept a string and replace all the vowels with asterisk (*) mark.

  1. Write a program to input an upper limit and a lower limit from the user and print all the Disarium numbers between the limits (both inclusive).

A number will be called Disarium if the sum of its digits powered to their respective positions is equal to the original number.

Exapmle: 135 is a Disarium number, as 11 + 32 + 53 = 135


  1. Write a menu-driven program to print each of the following patterns as per the user’s choice:

(a)

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

(b)

A 1 1 1 1
B B 2 2 2
C C C 3 3
D D D D 4
E E E E E
  1. A natural number is said to be a Smith number if the sum of all the digits in the number and the sum of all the digits in the prime factors of the number are equal.

Sample Input: 666

Sum of Digits: 6 + 6 + 6 = 18

Prime factors: 2, 3, 3, 37

Sum of digits of prime factors: 2 + 3 + 3 + (3 + 7) = 18

666 is a Smith number.

Write a program to take a number as input from the user and find whether it is a Smith number.


  1. Write a program to enter any number from 2 to 5 and print all the combinations of numbers starting from 1 to that number. There should be one blank space between each digit and each new combination should appear on a new line. Also display the total number of combinations formed for the given input.

  1. The computer department of the agency International Espionage is trying to decode intercepted messages. The agency’s spies have determined that the enemy encodes messages by converting all characters to their ASCII values and then reversing the string.

For example, consider A_z (the underscore is just to highlight the space). The ASCII values of A, <space> and z are 65, 32 and 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.

Write a program which reads a coded message and decodes it. The coded message will not exceed 200 characters. It will contain only letters of the English alphabet and spaces.

Test your program for the following data and some random data.

Input: 2312179862310199501872379231018117927

Output:

Have A Nice Day
*    * *    *

Input: 23511011501782351112179911801562340161171141148

Output:

Truth Always Wins
*     *      *

(* implies the letters should be in capital.)


  1. Write a program to check whether an integer is a magic number. An integer is said to be a magic number if the sum of its digits, kept on adding, is 1.

Consider 289.

289 → 2 + 8 + 9 = 19 → 1 + 9 = 10 → 1 + 0 = 1


  1. Write a program to take some cities as input in a single-dimensional array and sort the array in alphabetical order using Bubble Sort technique.
  2. Write a program to take a string as input and find the character with the highest frequency in it.

  1. Hamming Distance is defined as comparison of binary digits of decimal numbers from the same location. If both the digits are same, then Hamming Distance = 0, otherwise 1. Write a program to enter binary equivalent of two different decimal numbers in two different single-dimensional arrays and count the Hamming distance. Assume that both the binary digits have the same length.

Sample Input:

1 1 0 0 1 0 0 1 1
1 0 1 0 0 1 0 0 1

Sample Output:

Hamming distance = 5

  1. Write a program to fill numbers in a square matrix in a circular fashion (clockwise) with natural numbers from 1 to n2, taking n as input.

Example:

If n = 4, then n2 = 16. The array is filled as:

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

  1. Write a program to accept any three letter word and print all the probable combinations of the letters.
  2. Write a program to print the factorial of a number using recursive function.

  1. Write a program to enter a 4-digit number and display its Kaprekar routine.

6174 is known as Kaprekar's constant after the Indian mathematician D. R. Kaprekar.

This number is notable for the following property:

  1. Take any four-digit number, using at least two different digits. (Leading zeros are allowed.)
  2. Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary.
  3. Subtract the smaller number from the bigger number.
  4. Go back to step 2.

The above process, known as Kaprekar's routine, will always reach its fixed point, 6174, in at most 7 iterations. Once 6174 is reached, the process will continue yielding 7641 – 1467 = 6174.

For example, choose 3524:

5432 – 2345 = 3087
8730 – 0378 = 8352
8532 – 2358 = 6174
7641 – 1467 = 6174
              ^^^^

  1. Given two positive numbers M and N, such that M is between 100 and 10,000, and N is less than 100. Find the smallest number that is greater than M and whose digits add up to N.

Write a program that accepts M and N from the user and displays the required number, and also displays the number of digits in the number. The program should check for validity of inputs and display appropriate error message.

For example,

If M = 100 and N = 11, then output is:

The required number is: 119
The number of digits is: 3

  1. Write a program to print the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

  1. Write a program to print the following pattern:
1   2   3   4   5   6   7
    2   3   4   5   6
        3   4   5
            4
        3   4   5
    2   3   4   5   6
1   2   3   4   5   6   7

  1. Write a program to take a m × n array as input, and sort it, in ascending order,:
  1. Row-wise
  2. Column-wise
  3. The full array

  1. Write a program to declare a square matrix of order < 20. Allow the user to input positive integers into the matrix. Perform the following tasks on the matrix: (a) Output the original matrix. (b) Find the Saddle Point for the matrix.

A saddle point is an element in the matrix such that it is the minimum element of the row to which it belongs, and the maximum of the column to which it belongs. Saddle Point for a matrix is always unique. If the matrix has no Saddle Point, show the message “NO SADDLE POINT”.


  1. Write a program to take a square matrix as input, and print the sum of the left and the right diagonals.

  2. Write a program to enter natural numbers in an array m × n. Shift the elements of the nth column to the 1st, that of the 1st to the 2nd and so on.

  3. Write a program to take two matrices as input. Check if the matrices are compatible for multiplication. If yes, then multiply the matrices and display the result. If not, then display appropriate error message.


  1. A wondrous square is an n × n grid which fulfils the following conditions:
(i) It contains integers from 1 to n<sup>2</sup>, where each integer appears only once.

(ii) The sum of integers in any row or column must add up to 0.5 * n * (n2 + 1).

For example the following grid is a wondrous square where the sum of each row or column is 65 when n = 5:

17  24   1   8  15
23   5   7  14  16
 4   6  13  20  22
10  12  19  21   3
11  18  25   2   9

Write a program to read n (2 <= n <= 10) and the values stored in these n × n cells and output if the grid represents a wondrous square or not.

Also output all the prime numbers in the grid along with their row index and column index as shown in the output.

Test your program for the following data and some random data.

Sample Data:

Input:

n = 4

16   15    1    2
 6    4   10   14
 9    8   12    5
 3    7   11   13

Output:

Yes it represents a wondrous square.

PRIME    ROW INDEX     COLUMN INDEX
  2          0                3
  3          3                0
  5          2                3
  7          3                1
 11          3                2
 13          3                3

  1. Write a program to take a square matrix as input and print the following patterns:

Input:

 5   6   8
 2   1   3
 4   9   7

Output:

Pattern 1:

5
2   1
4   9   7

Pattern 2:

5  6  8
2  1
4

Pattern 3:

      8
   1  3
4  9  7

Pattern 4:

5  6  8
   1  3
      7

  1. A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and added to the end of the remaining string of digits on the right, the generated number is still prime. The process is repeated until the original number is reached again.

A number is said to be prime if it has only two factors 1 and itself.

Example:

131
311
113

Hence, 131 is a circular prime.

Test your program with the sample data and some random data:

INPUT:

N = 197

OUTPUT:

197
971
719

197 IS A CIRCULAR PRIME

INPUT:

N = 1193

OUTPUT:

1193
1931
9311
3119

1193 IS A CIRCULAR PRIME

INPUT:

N = 29

OUTPUT:

29
92
29 IS NOT A CIRCULAR PRIME

  1. Write a program to take an array as input and print its mirror array.
1  2  3       3  2  1
4  5  9   →   6  5  4
7  8  9       9  8  7

  1. Write a program to carry out the following conversions:
  1. Decimal to Binary.
  2. Binary to Decimal.
  3. Decimal to Octal.
  4. Octal to Decimal.
  5. Decimal to Hexadecimal.
  6. Hexadecimal to Decimal.
  7. Octal to Binary.
  8. Binary to Octal.
  9. Hexadecimal to Binary.
  10. Binary to Hexadecimal.

  1. Input a paragraph containing ‘n’ number of sentences where 1 <= n < 4. The words should be in capital letters separated with a single blank space in between. A sentence may be terminated either with a full stop ‘.’ or a question mark ‘?’. Any other character may be ignored. Perform the following operations:
(a) Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate error message must be displayed.

(b) Find the number of words in the whole paragraph.

(c) Display in an ascending order the number of times a word appears in the text. Words with the same frequency may appear in any order.

Test your program for the sample data and some random data:

Example 1:

INPUT:

Enter number of sentences. 1

Enter sentences: TO BE OR NOT TO BE.

OUTPUT:

Total number of words: 6

WORD FREQUENCY
or 1
not 1
to 2
be 2

Example 2

INPUT:

Enter number of sentences: 3

Enter sentences THIS IS A STRING PROGRAM.IS THIS EASY?YES, IT IS.

OUTPUT:

Total number of words: 11

WORD FREQUENCY
a 1
string 1
program  1
easy 1
yes 1
it 1
this 2
is   3

Example 3

INPUT :

Enter number of sentences 5

OUTPUT:

Invalid entry

  1. A unique-digit integer is a positive integer without leading zeros and with no repetition of digits. For example 17, 235, 421 are all unique-digit integers.

Write a program to input two positive integers m and n (m < n, m < 30000 and n < 30000) and print all the unique-digit integers between m and n (inclusive of both). Also print only those unique-digit integers, the digits of which are in an ascending order.

Test your program for the sample data and some random data.

Example 1:

Input:

m = 100, n = 125

Output:

The unique-digit integers are: 102, 103, 104, 105, 106, 107, 108, 109, 120. 123, 124, 125

Integers with digits in an ascending order are: 123, 124, 125

Example 2:

Input:

m = 2560 n = 2570

Output:

The unique-digit integers are: 2560, 2561, 2563, 2564, 2567, 2568, 2569, 2570

Integers with digits in an ascending order are: 2567, 2568, 2569

  1. Write a program to declare a matrix of order r x c, where r is an even number more than 2 and c is an odd number more than 2. Accept values of r and c from the user and display a valid message in case of invalid input. Perform the following tasks:
  1. Create and print a new matrix in which the last column is the first row of the original matrix, the penultimate column is the second row of the original matrix, and so on.
  2. Sort the original matrix row-wise such that the smallest element is placed in the middle, the next smallest element to its right, the next smallest to its left, and so on. Display the sorted matrix.

Test your program for the following data and some random data:

Input:

r = 4
c = 5

1  2  3  4  5
5  6  7  8  9
9  1  5  6  2
4  8  3  7  9

Output:

New Matrix:

4  9  5  1
8  1  6  2
3  5  7  3
7  6  8  4
9  2  9  5


Sorted Matrix:

5  3  1  2  4
9  7  5  6  8
9  5  1  2  6
9  7  3  4  8

  1. Write a program to take a square matrix as input and rotate it by 90° counter-clockwise.
  2. Write a program to print the Fibonacci series by recursion.
  3. Write a program to make a preliminary stack data structure using arrays.
  4. Write a program to make a preliminary queue data structure using arrays.
  5. Write a program to make a preliminary dequeue data structure using arrays.
  6. Write a program to take a number as input, and raise it to a certain power, using recursion.
  7. Write a program to check whether a number is prime or not by using recursion.
  8. Write a program to print the binary equivalent of a decimal number using recursion.
  9. Write a program to print the decimal equivalent of a binary number using recursion.
  10. Write a program to print the prime factors of a number using recursion.
  11. Write a program to take two numbers and print their HCF and LCM using recursion.
  12. Write a program to input an array and perform selection sort on the array using recursion.
  13. Preliminary program on SLL and input numbers to it such that the SLL remains sorted.
  14. Write a program to implement stack using SLL.
  15. Write a program to implement dequeue using SLL.
  16. Write a program to implement queue using SLL.
  17. Write a program to take a string and reverse it using recursion.
  18. Write a program to take a two-dimensional array as input, sort the boundary elements, and print only the boundary elements in array format.

  1. Write a program to declare a square matrix of order m x m, where m must be greater than 3 and less than 10. Allow the user to input positive integers to this matrix. Perform the following tasks on the matrix: 1. Sort the non-boundary elements in an ascending order using any standard sorting technique and rearrange them in the matrix. 2. Calculate the sum of both the diagonal elements of the rearranged matrix. 3. Display the rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.

Test your program for the following data and some random data,

Input:

m = 4

 9     2     1     5
 8    13     8     4
15     6     3    11
 7    12     23    8

Output:

Rearranged Matrix:

 9    2    1    5
 8    3    6    4
15    8    3   11
 7   12   23    8

Diagonal elements of rearranged matrix:

9            5
    3    6
    8   13
7            8

  1. Write a program to accept sentences which are terminated by . only. Consider each sentence as a row. Encrypt the sentences of odd rows by replacing each letter with the third preceding letter. Also change the sentences of even rows by reversing the order of the words. Display the encrypted sentences as per the sample data given below.

Test your program for the sample data and some random data.

Example:

Input:

PAST IS HISTORY.FUTURE IS A MYSTERY.TODAY IS A GIFT.DO YOUR BEST TODAY.

Output:

PAST IS HISTORY.
MXPQ FP EFPQLOV.

FUTURE IS A MYSTERY.
MYSTERY A IS FUTURE.

TODAY IS A GIFT.
QLAXV FP X DFCQ.

DO YOUR BEST TODAY.
TODAY BEST YOUR DO.

  1. Write a program to take a date and the day as on the 1st of January of that year. Validate the date, and thereby find the day of the date.

  1. A Goldbach number is a positive integer that can be expressed as the sum of two odd primes.

All even integers greater than 4 are Goldbach numbers.

Example:

6 = 3 + 3
10 = 3 + 7
10 = 5 + 5

Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7, 5 and 5.

Write a program to accept an even integer N where N > 9 and N < 50. Find all the odd prime pairs whose sum is equal to the number N.

Test your program with the following data and some random data:

Example 1:

Input:

N = 14

Output:

Prime pairs are:
3, 11
7, 7

Example 2:

Input:

N = 30

Output:

Prime pairs are:
7, 23
11, 19
13, 17

Example 3:

Input:

N = 17

Output:

Invalid input. Number is odd.

Example 4:

Input:

N = 126

Output:

Invalid input. Number out of range.

  1. The Computer Department decodes the intercepted messages. The message has been encoded by first converting all characters to their ASCII values and then reversing the string.

For example, consider A_z (the underscore is just to highlight the space). The ASCII values of A <space>, z are 65, 32, 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the code message.

Write a program which reads an encoded message and decodes it. The encoded message should not exceed 100 characters and should contain only letters (A … Z, and a …z) and spaces.

ASCII values of A … Z are 65 ... 90 and those of a … z are 97…122.

Test your program for the following data and some random data.

Example 1:

Input:

Encoded message:
2312179862310199501872379231018117927

Output:

The decoded message:

Have a nice day

Example 2:

Input:

Encode message:
23511011501782351112179911801562340161171141148

Output:

The decoded message:

Truth always wins

  1. A square matrix M is of order n. The maximum value possible for n is 10. Accept three different characters from the keyboard and fill the array according to the instructions given below:

(a) Fill the upper and lower elements formed by the intersection of the diagonals by the first character.

(b) Fill the left and right elements formed by the intersection of the diagonals by the second character.

(c) Fill both the diagonals by the third character.

Output the result in the format given below:

Example 1:

Input:

Enter size: 4
Enter the first character: *
Enter the second character: ?
Enter the third character: #

Output:

#  *  *  #
?  #  #  ?
?  #  #  ?
#  *  *  #

Example 2:

Input:

Enter size: 5
Enter the first character: $
Enter the second character: !
Enter the third character: @

Output:

@  $  $  $  @
!  @  $  @  !
!  !  @  !  !
!  @  $  @  !
@  $  $  $  @

Example 3:

Input:

Enter size: 65

Output:

Size out of range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment