0% found this document useful (0 votes)
6 views

computer set3

The document contains a series of programming problems and their solutions, primarily in Java. It covers topics such as Goldbach numbers, pangrams, symmetric matrices, unique-digit integers, and anagrams, with examples and code implementations for each problem. The document serves as a sample paper for programming exercises, emphasizing user input validation and algorithmic logic.

Uploaded by

amalambikaschool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

computer set3

The document contains a series of programming problems and their solutions, primarily in Java. It covers topics such as Goldbach numbers, pangrams, symmetric matrices, unique-digit integers, and anagrams, with examples and code implementations for each problem. The document serves as a sample paper for programming exercises, emphasizing user input validation and algorithmic logic.

Uploaded by

amalambikaschool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

SAMPLE PAPER 1

Solve any one of the following Problems.

Question 1
A number is said to be a Goldbach number, if the number can be expressed as the
addition of two odd prime number pairs. If we follow the above condition, then we can find
that every even number larger than 4 is a Goldbach number because it must have any pair
of odd prime number pairs.

Example:
6 = 3, 3 (One pair of odd prime)
10 = 3, 7 and 5, 5 (Two pairs of odd prime)
Write a program to enter any positive even natural number ‘N’ where 1 ≤ N ≤ 50 and
generate odd prime twin of ‘N’.
Test your program for the following data and some random data.
Example 1
INPUT: N = 14
OUTPUT:
ODD PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT: N = 20
OUTPUT:
ODD PRIME PAIRS ARE:
17, 3
13, 7

Answer:

import java.util.Scanner;
class Goldbach
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
if(n > 50 || n % 2 != 0)
{
System.out.println("INVALID INPUT");
return;
}
int i = 3;
while(i <= (n - i))
{
if(isPrime(i) && isPrime(n - i))
System.out.println(i + ", " + (n - i));
i++;
}
}
public static boolean isPrime(int n)
{
int f = 0;
for(int i = 1; i <= n; i++)
{
if(n % i == 0)
f++;
}
return f == 2;
}
}

Question 3

Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only.
The words may be separated by a single blank space and should be case-sensitive.

Perform the following tasks:


(a) Determine if the accepted sentence is a Pangram or not. A Pangram is a sentence
that contains every letter of the alphabet at least once. Example: “The quick brown fox
jumps over the lazy dog.”
(b) Display the first occurring longest and shortest word in the accepted sentence.

Example 1
INPUT: Pack my box with five dozen liquor jugs.
OUTPUT:
IT IS A PANGRAM
LONGEST WORD: liquor
SHORTEST WORD: my

import java.util.Scanner;
class Pangram
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = in.nextLine();
if(s.length() == 0)
{
System.out.println("INVALID INPUT");
return;
}
char last = s.charAt(s.length() - 1);
if(".?!".indexOf(last) == -1)
{
System.out.println("INVALID INPUT");
return;
}
String shortest = new String(s);
String longest = new String();
boolean isPangram = true;
for(char ch = 'a'; ch <= 'z'; ch++)
{
boolean match = false;
for(int i = 0; i < s.length(); i++)
{
if(ch == Character.toLowerCase(s.charAt(i))){
match = true;
break;
}
}
if(!match)
{
isPangram = false;
break;
}
}
if(isPangram)
System.out.println("IT IS A PANGRAM");
else
System.out.println("IT IS NOT A PANGRAM");
String word = new String();
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
if(word.length() > longest.length())
longest = new String(word);
if(word.length() < shortest.length())
shortest = new String(word);
word = new String();
}
}
System.out.println("LONGEST WORD: " + longest);
System.out.println("SHORTEST WORD: " + shortest);
}
}

SAMPLE PAPER 3
Write a program to declare a square matrix A[][] of order M × M where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10.

Accept the value of M as user input. Display an appropriate message for an invalid input.
Allow the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is said to be symmetric or not. A square matrix is said to be
symmetric, if the element of the i row and j column is equal to the element of the j row
th th th

and i column.
th

(c) Find the sum of the elements of left diagonal and the sum of the elements of right
diagonal of the matrix and display them.
Example 1
INPUT:
M=3
Enter elements of the matrix:
123
245
356
OUTPUT:
ORIGINAL MATRIX
123
245
356
THE GIVEN MATRIX IS SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL = 11
THE SUM OF THE RIGHT DIAGONAL = 10

import java.util.Scanner;
class Symmetric
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("M = ");
int m = sc..nextInt();
if(m < 3 || m > 9)
{
System.out.println("SIZE IS OUT OF RANGE");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter elements of the matrix:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
a[i][j] = Integer.parseInt(in.nextLine());
}
}
int left = 0;
int right = 0;
boolean isSymmetric = true;
System.out.println("ORIGINAL MATRIX");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j] + "\t");
if(a[i][j] != a[j][i])
isSymmetric = false;
if(i == j)
left += a[i][j];
if(i + j == m - 1)
right += a[i][j];
}
System.out.println();
}
if(isSymmetric)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC.");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC.");
System.out.println("THE SUM OF THE LEFT DIAGONAL = " + left);
System.out.println("THE SUM OF THE RIGHT DIAGONAL = " + right);
}
}
Question 1

A unique-digit integer is a positive integer (without leading zeros) with no duplicate digits. For
example, 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not. Given two
positive integers m and n, where m < n, write a program to determine how many unique-digit
integers are there in the range between m and n (both inclusive) and output them. The input
contains two positive integers m and n. Assume m < 30000 and n < 30000. You are to output the
number of unique-digit integers in the specified range along with their values in the format
specified below: Test your program for the following data and some random data.

Example 1
INPUT: m = 100 n = 120
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE: 102, 103, 104, 105, 106, 107, 108, 109, 120
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9
Example 2
INPUT: m = 2505 n = 2525
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE: 2506, 2507, 2508, 2509, 2510, 2513, 2514,
2516, 2517, 2518, 2519
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 11
Example 3 INPUT: m = 2520 n = 2529
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE: NIL FREQUENCY OF UNIQUE-DIGIT
INTEGERS IS: 0.

import java.util.Scanner;

public class UniqueIntegers

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter m: ");

int m = in.nextInt();

if (m < 1 || m > 30000) {

System.out.println("Value of m should be between 1 and 30000");


return;

System.out.print("Enter n: ");

int n = in.nextInt();

if (n < 1 || n > 30000) {

System.out.println("Value of n should be between 1 and 30000");

return;

if (m > n) {

System.out.println("Value of m should be less than n");

return;

System.out.println("The Unique-Digit integers are:");

int count = 0;

for (int i = m; i <= n; i++) {

int num = i;

boolean visited[] = new boolean[10];

boolean isUnique = true;

while (num != 0) {

int d = num % 10;

if (visited[d]) {

isUnique = false;
break;

visited[d] = true;

num /= 10;

if (isUnique) {

count++;

System.out.print(i + " ");

System.out.println();

System.out.println("Frequency of unique-digit integers is: " + count);

Question 2

Write a program to declare a matrix A [ ] [ ] of order (M x N) where ‘M’ is the number of

rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and

less than 20. Allow the user to input integers into this matrix. Perform the following tasks on

the matrix:

Perform the following tasks on the matrix


(a) Display the input matrix
(b) Find the maximum and minimum value in the matrix and display them along with their
Position
(c) Sort the elements of the matrix in descending order using any standard sorting technique
and rearrange them in the matrix.
(c) Output the rearranged matrix.
import java.util.Scanner;
class MaxMin{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
if(m < 3 || n < 3 || m > 19 || n > 19){
System.out.println("SIZE OUT OF RANGE");
return;
}
int a[][] = new int[m][n];
int b[] = new int[m * n];
int index = 0;
System.out.println("ENTER ELEMENTS OF THE MATRIX:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = Integer.parseInt(in.nextLine());
b[index++] = a[i][j];
}
}
System.out.println("ORIGINAL MATRIX");
display(a);
int max = a[0][0];
int i1 = 0;
int j1 = 0;
int min = a[0][0];
int i2 = 0;
int j2 = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(a[i][j] > max){
max = a[i][j];
i1 = i;
j1 = j;
}
if(a[i][j] < min){
min = a[i][j];
i2 = i;
j2 = j;
}
}
}
System.out.println("LARGEST NUMBER: " + max);
System.out.println("ROW = " + i1);
System.out.println("COLUMN = " + j1);
System.out.println("SMALLEST NUMBER: " + min);
System.out.println("ROW = " + i2);
System.out.println("COLUMN = " + j2);
for(int i = 0; i < b.length; i++){
for(int j = 0; j < b.length - 1 - i; j++){
if(b[j] > b[j + 1]){
int temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
index = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = b[index++];
}
}
System.out.println("REARRANGED MATRIX");
display(a);
}
public static void display(int mat[][]){
for(int i = 0; i < mat.length; i++){
for(int j = 0; j < mat[0].length; j++){
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}
}
}

Question 3

Write a program to check if a given string is an Anagram of another string. Two strings are

anagrams if they can be rearranged to form the same string. For example, "listen" and "silent"

are anagrams.

Accept two strings from the user and check if they are anagrams of each other. Ensure that

the comparison is case-insensitive and ignores spaces. Display an appropriate message based
on whether they are anagrams or not. If any of the strings contain invalid characters (e.g.,

numbers or special characters), generate an error message.

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

Example 1

INPUT: Enter first string: Listen

Enter second string: Silent

OUTPUT: STRINGS ARE ANAGRAMS

Example 2

INPUT: Enter first string: Dormitory

Enter second string: Dirty room

OUTPUT: STRINGS ARE ANAGRAMS

Algorithm

1 Take string input from user and store it in the variable called “str” .

2 Call isAnagram() method by passing two string as parameter .

3 Write if condition if(s1.length()!=s2.length()) set statue = false;

4 else convert both array to character arrya using toCharArray()


5 After that sort both the array using Arrays class sort() after that return status

import java.util.Arrays;

import java.util.Scanner;

public class CheckIfTwoStringsAreAnagramAreNot {

static boolean isAnagram(String str1 , String str2) {

String s1 = str1.replaceAll("[\\s]", "");

String s2 = str2.replaceAll("[\\s]", "");

boolean status=true;

if(s1.length()!=s2.length())

status = false;

else {

char[] a1 = s1.toLowerCase().toCharArray();

char[] a2 = s2.toLowerCase().toCharArray();

Arrays.sort(a1);

Arrays.sort(a2);

status = Arrays.equals(a1, a2);

return status;

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter two String :");


String s1 = sc.next();

String s2 = sc.next();

boolean status = isAnagram(s1,s2);

if(status)

System.out.println(s1+" and "+s2+" are Anagram");

else

System.out.println(s1+" and "+s2+" are not Anagram");

Most (not all) cell phone keypads look like the following arrangement (the letters are above
the respective number):

For sending text/SMS, the common problem is the number of keystrokes to type a particular
text.

For example, in the word “STOP”, there are a total of 9 keystrokes needed to type the word.
You need to press the key 7 four times, the key 8 once, the key 6 three times and the key 7
once to get it.

Develop a program code to find the number of keystrokes needed to type the text.
For this problem, accept just one word without any punctuation marks, numbers or
whitespaces and the text message would consist of just 1 word.

Example 1:
INPUT: DEAR
OUTPUT: Number of keystrokes = 7
Example 2:
INPUT: Thanks
OUTPUT: Number of keystrokes = 12

import java.util.Scanner;
class Keypad{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
String word = in.next().toUpperCase();
int count = 0;
for(int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
if(!Character.isLetter(ch)){
System.out.println("INVALID ENTRY");
return;
}
if("ADGJMPTW".indexOf(ch) >= 0)
count++;
else if("BEHKNQUX".indexOf(ch) >= 0)
count += 2;
else if("CFILORVY".indexOf(ch) >= 0)
count += 3;
else
count += 4;
}
System.out.println("Number of keystrokes = " + count);
}
}

Write a program in Java to accept day number (between 1 and 366) and year (yyyy) from
the user and display the corresponding date. Also accept ‘N’ from the user where (1 ≤ N ≤
100) to compute and display the future date ‘N’ days after the given date.

Display error message if the value of the day number or ‘N’ are not within the limit. Day
number is calculated taking 1 January of the given year as 1.
st

Test your program with given set of data and some random data.

Example 1
INPUT:
DAY NUMBER: 50
YEAR: 2024
N = 25
OUTPUT:
ENTERED DATE: FEBRUARY 19, 2024
25 DAYS LATER: MARCH 15, 2024
Example 2
INPUT:
DAY NUMBER: 321
YEAR: 2024
N: 77
OUTPUT:
ENTERED DATE: NOVEMBER 16, 2024
77 DAYS LATER: FEBRUARY 1, 2025
Example 3
INPUT:
DAY NUMBER: 400
YEAR: 2024
N: 125
OUTPUT:
INCORRECT DAY NUMBER
INCORRECT VALUE OF ‘N’
import java.util.Scanner;
class LaterDate{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean validDayNum = true;
boolean validN = true;
System.out.print("DAY NUMBER: ");
int dayNum = Integer.parseInt(in.nextLine());
System.out.print("YEAR: ");
int year = Integer.parseInt(in.nextLine());
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
if(dayNum < 1 || (dayNum > 365 && !isLeap(year))){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
else if(dayNum > 366 && isLeap(year)){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
if(n < 1 || n > 100){
validN = false;
System.out.println("INCORRECT VALUE OF 'N'");
}
if(!validDayNum || !validN)
return;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String month[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
if(isLeap(year))
day[1] = 29;
int index = 0;
while(dayNum > day[index]){
dayNum -= day[index++];
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
System.out.println("ENTERED DATE: " + month[index] + " " + dayNum + ", " + year);
System.out.print(n + " DAYS LATER: ");
while(n > 0){
dayNum++;
n--;
if(dayNum > day[index]){
dayNum = 1;
index++;
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
}
System.out.println(month[index] + " " + dayNum + ", " + year);
}
public static boolean isLeap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
}

You might also like