Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

COMPUTER PROJECT

------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------

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.

Perform the following task on the matrix:

(a)Display the original matrix.

(b)Rotate the matrix 90 degree clockwise shown below:

AOriginal Matrix Rotated 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

public static void main(String args[])

throws InputMismatchException{

Scanner scan=new Scanner(System.in);

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 a[][]=new int[m][m];

int b[] = new int[m*m];

int i,j,ctr,c;

● System.out.println("Enter "+(m*m)+" numbers for the matrix: ");

for(i=0;i < m;i++){

for(j=0;j < m;j++){

a[i][j] = scan.nextInt();

}j

}i

ctr = 0;

System.out.println("ORIGINAL MATRIX");

for(i=0;i < m;i++){

for(j=0;j < m;j++){

System.out.print(a[i][j] + " ");

b[ctr++] = a[i][j];

System.out.println();

c = m-1;

ctr = 0;

do{

for(i=0; i < m; i++)

a[i][c] = b[ctr++];

c--;

}
while(c >= 0);

System.out.println("MATRIX AFTER ROTATION");

for(i=0;i < m;i++){

for(j=0;j < m;j++){

System.out.print(a[i][j] + " ");

}j

System.out.println();

System.out.println("Sum of the corner elements="+(a[0][0] + a[0][m-1] + a[m-1][0] + a[m-1][m-1]));

}}}

------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------

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.

Perform the following tasks:


(a)Convert the first letter of each word into uppercase.
(b)Find the number of vowels and consonants in each word and display them with proper message
along with the words.

Examples:

INPUT: Intelligence plus character is education.

OUTPUT: Intelligence Plus Character Is Education.

Words Vowels Consonants

Intelligence 5 7

Plus 1 3

Character 3 6

Is 1 1

Education 5 4
ANS:

import java.io.*;

class ISCPrac2015Q03

public static void main(String arg[])throws IOException

int i, j, vowels, cons, p, l;

String str,word, tmp;

char ch, ch1;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a paragraph : ");

str=br.readLine();

l = str.length();

ch = str.charAt(l-1);

System.out.println("\nOUTPUT:");

if(ch != '.' && ch != '?')

System.out.println("INVALID INPUT");

else{

p = vowels = cons = 0;

tmp = str+" ";

str = "";

for(i=0;i < tmp.length();i++){

ch = tmp.charAt(i);

if(ch == ' '){

word = tmp.substring(p,i);

ch1 = word.charAt(0);

word = Character.toUpperCase(ch1)+word.substring(1);

str+= word+" ";

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;

for(i=0;i < l;i++)

ch = str.charAt(i);

if( ch != ' ' && ch != '.' && ch != '?')

if((ch>=65 && ch <= 90) || (ch>=97 && ch<=122))

if("aeiouAEIOU".indexOf(ch) != -1)

vowels++;

else

cons++;

}}

Else {

word = str.substring(p,i);

System.out.print(word);

for(j= 15 - word.length(); j>=1;j--)

System.out.print(" ");

System.out.println("\t "+vowels+"\t "+cons);

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

Hence, 131 is a circular prime.

Ans :

import java.io.*;

class ISCprac2016Q01

public static void main(String arg[])throws IOException

int l,i,n;

boolean flag = true;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a number : ");

String num = br.readLine();

l = num.length();

for(i = 0; i < l; i++){

String str = num.substring(i,l) + num.substring(0,i);

n = Integer.parseInt(str);

if(!isPrime(n)){

flag = false;

break;

}}

if(flag)

System.out.println(num + " IS A CIRCULAR PRIME");


else

System.out.println(num + " IS NOT A CIRCULAR PRIME");

private static boolean isPrime(int p){

for(int i = 2; i <= p/2; i++){

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.

Perform the following tasks on 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:

INPUT: M=4 REARRANGED MATRIX DIAGONAL MATRIX

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.

Perform the following tasks:


(a)Find the number of words in the beginning and ending with a vowel.

(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:

INPUT: ANAMIKA AND SUSUAN ARE NEVER GOING TO QUARREL ANYMORE.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL= 3

ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL.

Ans:

import java.io.*;

class ISCprac2016Q03

public static void main(String arg[])throws IOException

int l,i,n, p, nowwv;

boolean flag = true;

char ch, fc, lc, lastChar;

String str, prefix = "", suffix = "";

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a sentence : ");

str = br.readLine();

l = str.length();

lastChar = str.charAt(l-1);

if(lastChar != '.' && lastChar != '?' && lastChar != '!')

System.out.println("INVALID INPUT");

else{

p = 0;

nowwv = 0;

1 for(i = 0; i < l; i++){

ch = str.charAt(i);
if(ch == ' ' || ch == '?' || ch == '!' || ch == '.'){

if(isVowel(str.charAt(p)) && isVowel(str.charAt(i-1))){

prefix += str.substring(p,i) + " ";

nowwv++;

}else{

suffix += str.substring(p,i) + " ";

p = i + 1;

}}

System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " +


nowwv);

System.out.println(prefix + suffix);

}}

private static boolean isVowel(char ch){

if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

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:

INPUT: M=100 ,N=11

OUTPUT: The required number 119

The total number of digits=3

Ans:1

import java.util.*;

class ISCprac2015q01

public static void main(String args[])

throws InputMismatchException{

Scanner scan=new Scanner(System.in);

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();

if(m < 100 || m > 10000){

System.out.println("INVALID INPUT");

else if(n >=100){

System.out.println("INVALID INPUT");

else{1

int p = m,t;

byte sum,nod;

do{
t = p++;

sum = 0;

nod = 0;

while(t > 0){

sum = (byte)(sum + t%10);

t = t/10;

nod++;

}}1while(sum != n);

System.out.println("The required number = "+(p-1));

System.out.println("Total number of digits = "+(nod));

}}}

------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------

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 4​th​ column, the elements of 1​st​ columns
into 2​nd​ 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;

static void interchangeFirstLast(int m[][])

int cols = n;

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

int t = m[i][0];
m[i][0] = m[i][n - 1];

m[i][n - 1] = t;

public static void main (String[] args) {

// input in the array

int m[][] = { { 8, 9, 7, 6 },

{ 4, 7, 6, 5 },

{ 3, 2, 1, 8 },

{ 9, 9, 7, 7 } };

interchangeFirstLast(m);

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

for (int j = 0; j < n; j++)

System.out.print(m[i][j] + " ");

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:

INPUT: OUTPUT: HSIHNTDAIM

S1=HINDI

S2=MATHS
Ans:

import java.io.*;

class GFG8 {

public static void solve(String s)

int l = s.length();

int x = l / 2;

int y = l;

String p = "";

while (x > 0 && y > l / 2) {char ch = s.charAt(x - 1);

p += ch;

x--;

ch = s.charAt(y - 1);

p += ch;

y--;

if (y > l / 2) {

char ch = s.charAt(x - 1);

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:

A shift of 7 means that A,B,C,D….will change to U,V,W,X….


1​ST​ an extra space is added in the end of the string to make things a little more difficult. The spaces in
original text are replaced with QQ, before the text is encrypted. ”QQ” was selected because no word
ends with “QQ”. Additionally, the coded is printed in blocks of 6 characters separated by spaces. The
last block might not contain 6 characters. Write a program to take a coded message less than 100
characters, the shift value and print the coded text. Your program must reject any invalid value of
shift and display an error message “Invalid Shift Value”. Assume all characters in uppercase.

Example:

INPUT: UHINBY HYLKK OUTPUT: ANOTHER WINNER

Shift Value=7

Ans:

class gfg9{

public static StringBuffer encrypt(String text, int s)

StringBuffer result= new StringBuffer();

for (int i=0; i<text.length(); i++)

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()

Scanner sc=new Scanner(System.in);


System.out.println("Enter the size of array(row and column same):");

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++)

System.out.print("Enter the value:");

arr[i][j]=sc.nextInt();

System.out.println("The matrix is");

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)

System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");

else

System.out.println("IT IS NOT A WONDROUS SQUARE.");

System.out.println("PRIME ROW COLUMN");

for(i=0;i< n;i++)

for(j=0;j< n;j++)

if(prime(arr[i][j]))

System.out.println(arr[i][j]+ " "+i+ " "+j);

boolean prime(int no)

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[])

wonderous10 ob=new wonderous10();

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()

Scanner sc=new Scanner(System.in);

int a[][]=new int[4][4];

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 Input: Rs.1288

Sample Output:

Number of notes of Rs.100=12

Number of notes of Rs.50=1

Number of notes of Rs.20=1

Number of notes of Rs.10=1

Number of notes of Rs.5=1

Number of notes of Rs.2=1

Number of notes of Rs.1=1

Ans:

import java.util.*;

class Denominations

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int den[]={1000,500,100,50,20,10,5,2,1};

System.out.print("Enter any Amount: ");

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("--------------------------------");

System.out.println("Total Number of Notes\t= "+totalNotes);

}}

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.

Sample Input: Enter a number 4972


Sample Output: 2,4,7,9.

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 1​st​ column into the
2​nd​ column and so on. Display the new matrix.

SAMPLE INPUT: SAMPLE OUTPUT:

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.

You might also like