Professional Documents
Culture Documents
COMPUTER2k19 PDF
COMPUTER2k19 PDF
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
1. Write a program to declare a square matrix A[][] of order (M X M) where ‘M’ is the number of
rows and number of column, such that M must be greater than 2 and less than 10. Accept the
value of user input. Display an appropriate message for an invalid input. Allow the user to input
positive integer into the above matrix.
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3
(c)Find the sum of the elements of the four corners of the matrix.
ANS:
import java.util.*;
class ISCPrac2015Q02
throws InputMismatchException{
System.out.println("Enter the number of rows (greater than 2 and less than 10) for the square matrix :
");
int m=scan.nextInt();
if(m<=2 || m>=10)
System.out.println("SIZE OUT OF RANGE");
else{
int i,j,ctr,c;
a[i][j] = scan.nextInt();
}j
}i
ctr = 0;
System.out.println("ORIGINAL MATRIX");
b[ctr++] = a[i][j];
System.out.println();
c = m-1;
ctr = 0;
do{
a[i][c] = b[ctr++];
c--;
}
while(c >= 0);
}j
System.out.println();
}}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
2. Write a program to accept a sentence that may be terminated by either ‘.’or ‘?’ only. The word
to be separated by a single blank space. Print an error a message if the input does not
terminated by ‘.’ Or ’?’. You can assume that no word in the sentence exceeds 15 characters,
so that you get formatted output.
Examples:
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
ANS:
import java.io.*;
class ISCPrac2015Q03
str=br.readLine();
l = str.length();
ch = str.charAt(l-1);
System.out.println("\nOUTPUT:");
System.out.println("INVALID INPUT");
else{
p = vowels = cons = 0;
str = "";
ch = tmp.charAt(i);
word = tmp.substring(p,i);
ch1 = word.charAt(0);
word = Character.toUpperCase(ch1)+word.substring(1);
p = i + 1;
}}
System.out.println("\n"+str);
System.out.print("\nWord");
for(j= 15 - 4; j>=1;j--)
System.out.print(" ");
System.out.println("\tVowels\tConsonants");
p=0;
ch = str.charAt(i);
if("aeiouAEIOU".indexOf(ch) != -1)
vowels++;
else
cons++;
}}
Else {
word = str.substring(p,i);
System.out.print(word);
System.out.print(" ");
p = i + 1;
vowels = cons = 0;
}}}}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
3. A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When
the leftmost digit is removed and replaced at the end of the remaining string of digits, 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 I and itself.
Example: 131
311
113
Ans :
import java.io.*;
class ISCprac2016Q01
int l,i,n;
l = num.length();
n = Integer.parseInt(str);
if(!isPrime(n)){
flag = false;
break;
}}
if(flag)
if(p%i == 0)
return false;
return true;
}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
4. Write a program to declare a square matrix A[][] of order(M x M) where ‘M’ must be greater
than 2 and less than 10.Allow user to input positive integer into the matrix.
(A) Sort the non-boundary element in ascending order using a standered sorting technique
rearrange them in matrix.
(B) Calculate the sum of both the diagonals.
(C) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged
matrix with their sum.
Example:
9 2 1 5 9 2 1 5 9 5
8 13 8 4 8 3 6 4 3 6
15 6 3 11 15 8 13 11 8 13
7 12 23 8 7 12 23 8 7 8
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
5. Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The
word may be separated by more than one blank space and are in UPPERCASE.
(b)Place the words which begin and end with a vowel at the beginning follow by the remaining words as
they occur in the sentence.
Example:
Ans:
import java.io.*;
class ISCprac2016Q03
str = br.readLine();
l = str.length();
lastChar = str.charAt(l-1);
System.out.println("INVALID INPUT");
else{
p = 0;
nowwv = 0;
ch = str.charAt(i);
if(ch == ' ' || ch == '?' || ch == '!' || ch == '.'){
nowwv++;
}else{
p = i + 1;
}}
System.out.println(prefix + suffix);
}}
return true;
else
return false;
}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
6. Given tow positive number M and N, such that M is betwwen 100 and 10000 and n is less than
100. Find the smallest integer that is greater than M and whose digits add up to N.
1
For Example:
If M=100 and N =11, the smallest integer is greater than 100 whose digits add up to 11 to 119.
Write a program to accept number M and N from user and print the smallest required number whose
sum of all digits is equal to N. Also, print the total number of digits present in the required
number. The program should check the validity of the inputs and display an appropriate
message for invalid input.
Example:
Ans:1
import java.util.*;
class ISCprac2015q01
throws InputMismatchException{
System.out.println("Enter M and N where M is between 100 and 10000 and N is less than 100");
int m=scan.nextInt();
byte n = scan.nextByte();
System.out.println("INVALID INPUT");
System.out.println("INVALID INPUT");
else{1
int p = m,t;
byte sum,nod;
do{
t = p++;
sum = 0;
nod = 0;
t = t/10;
nod++;
}}1while(sum != n);
}}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
7. Write a program in Java to enter natural number in double dimensional array of m X n(where m is
the number of rows and column).Shift the element on 4th column, the elements of 1st columns
into 2nd column and so on. Display the new matrix.
Example:
INPUT: OUTPUT:
11 16 7 4 4 11 16 7
8 10 9 18 18 8 10 9
9 8 12 15 15 9 8 12
14 15 13 6 6 14 15 13
Ans:
class GFG {
static int n = 4;
int cols = n;
int t = m[i][0];
m[i][0] = m[i][n - 1];
m[i][n - 1] = t;
int m[][] = { { 8, 9, 7, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 9, 9, 7, 7 } };
interchangeFirstLast(m);
System.out.println();
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
8. Write a program in Java to accept two strings. Display the new string by taking each character of
the first string from left to right and of the second string from right to left. The letters should be
taken alternatively from each string. Assume that the length of both the string are same.
Example:
S1=HINDI
S2=MATHS
Ans:
import java.io.*;
class GFG8 {
int l = s.length();
int x = l / 2;
int y = l;
String p = "";
p += ch;
x--;
ch = s.charAt(y - 1);
p += ch;
y--;
if (y > l / 2) {
p += ch;
y--;
System.out.println(p);
}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
9. A simple encryption system uses a shifting process to hide message. The value of shift can be in
the range 1-26.
For example:
Example:
Shift Value=7
Ans:
class gfg9{
if (Character.isUpperCase(text.charAt(i)))
char ch = (char)(((int)text.charAt(i) +
s - 65) % 26 + 65);
result.append(ch);
else
char ch = (char)(((int)text.charAt(i) +
s - 97) % 26 + 97);
result.append(ch);
}}
return result;
}}
------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
10. Program of a wondrous square which fulfills the following condition:
(a) It contains integers from 1 to n2 where each integer appears only once.
(b) The sum of integers in any row or column must add up to 0.5 x n(n2+1)
Example:
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
The following grid is a wondrous square where sum of elements in each row and each column is 65 at
n=5.
Write a program to read n[2<=n<=10] & value of array. Output if the grid is wondrous square or not.
Also print all the prime numbers with row and column index. The prime numbers must be
displayed in ascending order.
Ans:
import java.util.Scanner;
class wonderous10
int arr[][],arr1[];;
int n,i,j,x=0,r,c;
int flag;
void perform()
n=sc.nextInt();
arr=new int[n][n];
arr1=new int[2*n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr[i][j]=sc.nextInt();
for(i=0;i<n;i++)
r=0;
c=0;
for(j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
r=r+arr[i][j];
c=c+arr[j][i];
System.out.println();
arr1[x]=r;
arr1[x+n-1]=c;
x++;
for(i=0;i< x;i++)
{
if(arr1[i]!= 0.5 * n * (n*n + 1))
break;
if(i==x)
else
for(i=0;i< n;i++)
for(j=0;j< n;j++)
if(prime(arr[i][j]))
int index;
for(index=2;index<no;index++)
if(no%index==0)
break;
if(index==no)
return true;
else
return false;
}
public static void main(String args[])
ob.perform();
}}
11. Write a program to store the number in 4x4 matrix in double dimensional array. Arrange the
numbers of each row in ascending order and display the result.
Example:
INPUT OUTPUT
22 14 23 25
81 26 31 10
68 54 17 12
55 33 26 14
Ans:
import java.util.*;
class cpw11{
void main()
int j,k,i,t;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}}for(k=0;k<4;k++)
for(i=0;i<4-1;i++)
for(j=0;j<4-1;j++)
if(a[k][j]>a[k][j+1])
t=a[k][j];
a[k][j]=a[k][j+1];
a[k][j+1]=t;
}}}}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
System.out.println(a[i][j]+"");
System.out.println();
}}}
12. The consecutive prime numbers are known as Prime Triplets. If they satisfy the following
condition(n,n+2,n+6) are all prime numbers, where n is an integer number>0 if n=5 then
5,7(5+2),11(5+6). Here 5,7 and 11 are all prime number so (5,7,11) are prime triplets. if n=7 then
7,9(7+2),13(7+6). Here 7,9 and 13 are all prime number so (7,9,13) are not prime triplets.
But,
if n=7 then 7,11(7+4),13(7+6). Here 7,11 and 13 are all prime number so (7,11,13) are prime triplets.
Ans:
13. Mr. Kamal Nath Agarwal is a businessman. He want to now the number of notes of each denomination
(Rs.100,Rs.50,Rs.20,Rs.10,Rs.5,Rs.2 and Rs.1 coins) that can be obtained from a given sum. Write a
program in java to accept an amount from the user and display the number of notes of different
denominations.
Sample Output:
Ans:
import java.util.*;
class Denominations
int den[]={1000,500,100,50,20,10,5,2,1};
int amount=sc.nextInt();
int copy=amount;
int totalNotes=0,count=0;
System.out.println("DENOMINATIONS: ");
for(int i=0;i<9;i++) {
count=amount/den[i];
if(count!=0) {
System.out.println(den[i]+"\tx\t"+count+"\t= "+den[i]*count);
}
totalNotes=totalNotes+count;
amount=amount%den[i];
System.out.println("--------------------------------");
System.out.println("TOTAL\t\t\t= "+copy);
System.out.println("--------------------------------");
}}
14. Write a program in java to enter a number containing three digit or more. Arrange the digits of
the entered number in ascending order and display the result.
Ans:
import java.util.*;
class arrangeDig{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Digits to be arranged");
int n=sc.nextInt();
int d,num=n;
for(int i=0;i<10;i++)
{
num=n;
while(num>0)
{
d=num%10;
if(d==i)
{
System.out.print(d+",");
}
num=num/10;
}
}
System.out.println();
}}
15. Write a program in java to enter a natural number in a double dimensional array m*n(where m is
the number of rows and n is the number of columns). Shift the elements of the 1st column into the
2nd column and so on. Display the new matrix.
Ans:
Write a program in java to accept two strings. Display the new strings by taking each character of the first string
from left to right and from the second string from right to left. The letters should be taken alternatively
from each string. Assume that the length of both the strings are same.