• No results found

Check whether a matrix (B) is equal to the transpose of another matrix (C)

In document ISC COMPUTER SCIENCE PROJECT (Page 55-89)

import java.io.*;

class Trans_Array {

int a[][]=new int[3][3];

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

void input() throws IOException {

int i,j;

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

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

a[i][j]=Integer.parseInt(br.readLine());

} } }

void check(Trans_Array A) {

int i,j,flag=0;

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

{

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

if(i==j)

{//CHECKING IF IT IS A UNIT MATRIX if(A.a[i][j]!=1)

{

flag=1;

break;

} } else {

if(A.a[i][j]!=0) {

flag=1;

break;

} } } }

if(flag==1)

System.out.println("\nARRAY 'A' IS NOT A UNIT MATRIX");

else

System.out.println("\nARRAY 'A' IS A UNIT MATRIX");

}

void trans(Trans_Array A)

{

int i,j,flag=0;

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

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

{//CHECKING IF THE MATRIX IS A TRANSPOSE OF THE OTHER if(A.a[i][j]!=A.a[j][i])

{

flag=1;

break;

} else flag=0;

} }

if(flag==1)

System.out.println("\nArray 'A' is not equal to the transpose of 'A'");

else

System.out.println("\nArray 'A' is equal to the transpose of 'A'");

}

void transCheck(Trans_Array A) {

int i,j,flag=0;

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

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

{

if(A.a[i][j]!=this.a[j][i]) {

flag=1;

break;

} else flag=0;

} }

if(flag==1)

System.out.println("\nArray 'B' is not equal to the transpose of Array 'C'");

else

System.out.println("\nArray 'B' is equal to the transpose of Array 'C'");

}

public static void main() throws IOException {

Trans_Array A=new Trans_Array();

Trans_Array B=new Trans_Array();

Trans_Array C=new Trans_Array();

System.out.println("Enter the elements of array A");

A.input();

System.out.println("Enter the elements of array B");

B.input();

System.out.println("Enter the elements of array C");

C.input();

A.check(A);

A.trans(A);

B.transCheck(C);

} }

QUESTION 21

Write a program to simulate the functioning of a ticket vending machine and print the amount collected and number of people visiting the fair

(Price for one ticket = Rs. 2.50)

import java.io.*;

class Ticket_Booth {

int noofpeople;

double amount;

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

void initial() {

noofpeople=0;

amount=0.0;

}

void notSold() {

noofpeople+=1;

}

void sold() {

noofpeople+=1;

amount+=2.50;

}

void displayTotal() {

System.out.println("\nTotal no. of people who have visited the booth : "+noofpeople);

}

void displayTicket() {

int no=(int)(amount/2.50);

System.out.println("Total no. of tickets sold : "+no);

System.out.println("Total amount collected : Rs. "+amount);

}

public static void main() throws IOException {

Ticket_Booth tb=new Ticket_Booth();

int ch=0,c=0;

while(c!=1) {

System.out.println("Do you want to purchase ticket ?");

System.out.println("Enter 1 - Yes , 0 - No");

ch=Integer.parseInt(br.readLine());

if(ch==1) {

System.out.println("Ticket Purchased!");

tb.sold();

} else {

System.out.println("Ticket not Purchased");

tb.notSold();

}

System.out.println("Do you want to continue ?");

System.out.println("Enter 1 - Yes, 0 - No");

ch=Integer.parseInt(br.readLine());

if(ch==0) c=1;

}

tb.displayTotal();

tb.displayTicket();

} }

QUESTION 22 :

Write a program to print the rank list along with relevant details of the students.

import java.io.*;

class Record {

String name[];

int rnk[],pos[],n;

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

Record() throws IOException {

System.out.println(" Enter the number of students");

n = Integer.parseInt(br.readLine());

name = new String[n];

rnk = new int[n];

pos = new int[n];

}

void readValues() throws IOException {

System.out.println("Enter the name & rank of " +n+" students");

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

name [i] = br.readLine();

rnk [i] = Integer.parseInt(br.readLine());

} }

void display()

{

System.out.println("Sl.no."+"\t"+"NAME"+"\t"+"RANK");

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

System.out.println((i+1)+"\t"+name[i]+"\t"+rnk[i]);

} } }

class Rank extends Record {

int index;

Rank()throws IOException {

index=0;

}

void highest() throws IOException {

super.readValues();

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

if(rnk[i+1]<rnk[i]) index=i+1;

} }

void arrange() throws IOException {

super.readValues();

int low,tmp;

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

low = i;

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

if (rnk[j] < rnk[low]) low = j;

}

pos[i] = low;

tmp = rnk[low];

rnk[low] = rnk[i];

rnk[i] = rnk[low];

} }

void display() {

System.out.println("\nHIGHEST RANK :"+name[index]+" ,Rank: "+rnk[index]);

System.out.println();

System.out.println("RANK LIST :");

System.out.println();

super.display();

} }

public class Student_Record {

public static void main() throws IOException

{

Rank rk=new Rank();

rk.highest();

rk.arrange();

rk.display();

} }

QUESTION 23 :

Program to print twin primes and calculate Brun's constant within an inputted limit.

import java.io.*;

class Brun {

int n;

double sum;

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

Brun(int limit) {

n=limit;

sum=0.0;

}

boolean isPrime(int no) {

int i,f=0;

boolean b=false;

for(i=1;i<=no;i++) {

if(no%i==0) f=f+1;

} if(f==2) b=true;

return b;

}

void twinPrime()

{ int i;

System.out.println("TWIN PRIMES :");

for(i=3;i<=(n-2);i++) {

if((isPrime(i)==true)&&(isPrime(i+2)==true)) System.out.println(i+" "+(i+2));

} }

void compute() {

int i;

for(i=3;i<=(n-2);i++)

{//CHECKING FOR TWIN PRIME

if((isPrime(i)==true)&&(isPrime(i+2)==true)) sum=sum+(double)((1.0/i)+(1.0/(i+2)));

}

System.out.println("BRUN'S CONSTANT : "+sum);

}

public static void main() throws IOException {

System.out.println("Enter the limit");

Brun b=new Brun(Integer.parseInt(br.readLine()));

b.twinPrime();

b.compute();

} }

QUESTION 24 :

Program to accept date in string format dd/mm/yyyy and accept name of the day on the 1st of January that year and calculates the name of the day of the inputted date.

import java.io.*;

class Date_Calculate {

int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};

BufferedReader br;

String str1,day,day1;

int x,i,dayno,mon,yr,leap1;

public static void main() throws IOException {

Date_Calculate dc=new Date_Calculate();

dc.take();

dc.show();

}

Date_Calculate() {

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

}

public void take() throws IOException {

System.out.println("Enter the date in dd/mm/yyyy format");

day=br.readLine().trim();

day1=day;

i=day.indexOf("/");

dayno=Integer.parseInt(day.substring(0,i));

day=day.substring(i+1);

i=day.indexOf("/");

mon=Integer.parseInt(day.substring(0,i));

day=day.substring(i+1);

yr=Integer.parseInt(day);

leap1=0;

if(mon>2) leap1=leap(yr);

System.out.println("Enter the name of the day on 1st January of that year");

str1=br.readLine().trim();

}

int leap(int p) {

if(p%100==0 && p%400==0) return 1;

else if(p%100!=0 && p%4==0) return 1;

else return 0;

}

void show() {

if(str1.equalsIgnoreCase("Sunday")) x=1;

else if(str1.equalsIgnoreCase("Monday")) x=2;

else if(str1.equalsIgnoreCase("Tuesday")) x=3;

else if(str1.equalsIgnoreCase("Wednesday")) x=4;

else if(str1.equalsIgnoreCase("Thursday")) x=5;

else if(str1.equalsIgnoreCase("Friday")) x=6;

else if(str1.equalsIgnoreCase("Saturday")) x=7;

else

System.out.println("INVALID DAY NAME!");

for(i=0;i<mon-1;i++) dayno=dayno+arr[i];

dayno=dayno+leap1;

for(i=0;i<dayno-1;i++) {

x++;

if(x==8) x=1;

}

System.out.print(day1+" : ");

switch(x) {

case 1:

System.out.print("Sunday");

break;

case 2:

System.out.print("Monday");

break;

case 3:

System.out.print("Tuesday");

break;

case 4:

System.out.print("Wednesday");

break;

case 5:

System.out.print("Thursday");

break;

case 6:

System.out.print("Friday");

break;

case 7:

System.out.print("Saturday");

break;

} } }

QUESTION 25 :

Program to perform binary search for an item on an array both inputted from the user using Recursion technique.

import java.io.*;

class BinarySearch1 {

int A[],n,l,u,x;

public BinarySearch1(int nn) {

n=nn;

A=new int[n];

l=0;

u=n-1;

}

void readData()throws IOException {

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("Enter the "+n"+ elements of the array");

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

A[i]=Integer.parseInt(br.readLine());

}

System.out.println("Enter the element to be searched");

x-Integer.parseInt(br.readLine());

}

int binarysearch(int v,int low,int high) {

l=low;

u=high;

if(l>u) {

return(-1);

}

int m=(l+u)/2;

if(v>A[m]) {

return binarysearch(v,m+l,u);

}

else if(v<A[m]) {

return binarysearch(v,m-l,u);

} else {

return (m);

}

}

public static void main() throws IOException {

System.out.println("Enter the size of array");

size=Integer.parseInt(br.readLine());

BinarySearch1 b=new BinarySearch1(size);

b.readData();

int p=b.binarysearch(x,l,u,);

if(p==-1)

System.out.println("Not Found");

else

System.out.println("Found At Position : " +(p+1));

} }

QUESTION 26 :

Program to multiply two matrices provided number of rows in first match number of columns in the second. Limit the maximum number of rows and columns to 10 in each case.

import java.io.*;

class Arr {

int m1[][]=new int[10][10];

int m2[][]=new int[10][10];

int mult[][]=new int[10][10];

int i,j,k,r1,c1,r2,c2;

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

public void show() throws IOException {

System.out.println("Enter number of rows of first matrix (less than 10)\n");

r1=Integer.parseInt(br.readLine());

System.out.println("Enter number of columns of first matrix (less than 10)\n");

c1=Integer.parseInt(br.readLine());

System.out.println("Enter number of rows of second matrix (less than 10)\n");

r2=Integer.parseInt(br.readLine());

System.out.println("Enter number of columns of second matrix (less than 10)\n");

c2=Integer.parseInt(br.readLine());

System.out.println("Value for 1st matrix:");

if(r2==c1) {

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

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

{

m1[i][j]=Integer.parseInt(br.readLine());

} }

System.out.println("Value for 2nd matrix:");

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

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

m2[i][j]=Integer.parseInt(br.readLine());

} }

System.out.println("\n1st Matrix\n");

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

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

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

System.out.println();

}

System.out.println("\n2nd Matrix\n");

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

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

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

System.out.println();

}

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

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

mult[i][j]=0;

for(k=0;k< r1;k++)

{//MULTIPLYING BOTH MATRICES mult[i][j]+=m1[i][k]*m2[k][j];

} } }

System.out.println("\nFinal Matrix\n");

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

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

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

System.out.println();

} } } }

QUESTION 27 :

Program to remove duplicate characters from a string and print final string

import java.io.*;

public class Remove_Duplicate_Characters {

public static void main() throws IOException {

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

System.out.println("Enter string");

String s=br.readLine();

char ch;

String s1,sf="";

int l=s.length();

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

for(int j=i+1;j<l;j++) {

String s2=s.substring(0,j);

String s3=s.substring(j);

if(s.charAt(j)==s.charAt(i))

// replacing duplicate characters with '*'

s3=s3.replace(s.charAt(j),'*');

s=s2+s3;

}

}

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

// REMOVING '*' AND JOINING THE REST OF THE CHARACTERS if(s.charAt(i)=='*')

sf=sf+"";

else

sf=sf+s.charAt(i);

}

System.out.println("The new string is - "+sf);

} }

QUESTION 28 :

Program to accept a sentence and print the number of words and sentence arranged according to alphabetical order of the first letter of each word.

import java.io.*;

import java.util.*;

class Sentence_Operations {

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

String str;

String words[];

StringTokenizer stk;

int i,j,c,x;

public void take()throws Exception {

int flag;

while(true) {

flag=0;

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

str=br.readLine();

for(i=0;i< str.length();i++) {//ARRANGING IN ORDER

if(Character.isLowerCase(str.charAt(i))) {

flag=1;

break;

} }

if (flag==0) break;

}

stk=new StringTokenizer(str," .,?!");

c=stk.countTokens();

x=0;

words=new String[c];

while(stk.hasMoreElements()) {

words[x++]=stk.nextToken();

}

System.out.println("Length="+c);

for(i=0;i< x-1;i++) {

for(j=i+1;j< x;j++) {

if(words[i].compareTo(words[j])>0) {

str=words[i];

words[i]=words[j];

words[j]=str;

} } }

System.out.println("\nRearranged Sentence:\n");

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

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

}

public static void main(String args[]) throws Exception {

Sentence_Operations ob=new Sentence_Operations();

ob.take();

} }

QUESTION 29 :

Program to print the Tribonacci series. Tribonacci series is like Fibonacci series with slight difference. In this series the latest three numbers are added and the result is displayed.

import java.io.*;

class Tribonacci {

int i,p,m,c,next,n;

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

public void disp()throws Exception {

p=0;

m=1;

c=1;

System.out.println("Enter the number of elements in the series:");

n=Integer.parseInt(br.readLine());

System.out.print("The series is="+ p+" "+m+" "+c+ " ");

for(i=0;i< n-3;i++) {

next=p+m+c;

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

p=m;

m=c;

c=next;

} }

public static void main()throws IOException

{

Tribonacci t1=new Tribonacci ();

t1.disp();

} }

QUESTION 30:

Write a program to print the inputted number in words with proper conjunctions.

The Limit is 9999.

import java.io.*;

class NumToWords_till_9999 {

static int r,d1=0,d2=0,d3=0,d4=0;

static int i,j;

static int n=0;

static String ones[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

static String

tens[]={"","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};

static String hundreds[]={"","One Hundred","Two Hundred","Three Hundred","Four Hundred","Five Hundred","Six Hundred ","Seven Hundred","Eight Hundred","Nine Hundred"};

static String thousands[]={"","One Thousand","Two Thousand","Three Thousand","Four

Thousand","Five Thousand","Six Thousand","Seven Thousand","Eight Thousand","Nine Thousand"};

public static void main()throws IOException {

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

System.out.println("Enter Number to Convert to words");

n=Integer.parseInt(br.readLine());

digit();

if(n==0)

System.out.print("Zero");

else if(n<=9) ones();

else if(n>=10&&n<=99) tens();

else if(n>=100&&n<=999) hundreds();

else if(n>=1000&&n<=9999) thousands()

}

public static void digit() { //EXTRACTING DIGITS d1=n%10;

d2=(n%100)/10;

d3=(n%1000)/100;

d4=(n%10000)/1000;

}

public static void ones() {

System.out.print(" "+ones[d1]+" ");

}

public static void tens() {

if(d1!=0&&d4!=0&&d3!=0) System.out.print(" And ");

switch(d1+d2*10) {

case 10 : System.out.print("Ten");

break;

case 11 : System.out.print("Eleven");

break;

case 12 : System.out.print("Twelve");

break;

case 13 : System.out.print("Thirteen");

break;

case 14 : System.out.print("Fourteen");

break;

case 15 : System.out.print("Fifteen");

break;

case 16 : System.out.print("Sixteen");

break;

case 17 : System.out.print("Seventeen");

break;

case 18 : System.out.print("Eightteen");

break;

case 19 : System.out.print("Nineteen");

break;

default : System.out.print(tens[d2]);

ones();

break;

}

}

public static void hundreds() {

System.out.print(" "+hundreds[d3]+" ");

tens();

}

public static void thousands() {

System.out.print(thousands[d4]);

hundreds();

} }

In document ISC COMPUTER SCIENCE PROJECT (Page 55-89)

Related documents