P. 1
Java Programs

Java Programs

|Views: 148|Likes:
Published by Manoj Mahajan

More info:

Published by: Manoj Mahajan on Sep 19, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

01/21/2011

pdf

text

original

1. WAP to understand java basic syntax ( Printing a String on console ) ?

class hello { class hello { public static void main(String args[]) { System.out.print("Hello WORLD"); } } 2. Take a number from user and check wheather it is prime or not?

import java.io.*; class prime { public static void main(String args[]) throws Exception { int k=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); String s = br.readLine(); int n = Integer.parseInt(s); for(int i =2;i<n;i++) { if((n%i)==0) k=1; break; } if (k==0) System.out.println("The number is prime"); else System.out.println("The number is not prime"); } } 3. Take a upper limit from user and print the sum of square of all the numb er comes in series ? ( Hint : 12 + 22 +32 + = sum ) import java.io.*; class Series { public static void main(String args[]) throws Exception { int sum =0; BufferedReader br = new BufferedReader(newInputStreamReader(System.in)); System.out.println("Enter the limit of the series:"); String s = br.readLine(); int n = Integer.parseInt(s); for(int i = 0;i<=n;i++) { sum = sum + (i*i); } System.out.println("The sum of the series is"+sum); } }

r = false.println((r^s) + "\t" + (!r)). .println((r^s) + "\t" + (!r)). s.out. s = false.out.print("*"). System. System.print(r + "\t" + s + "\t"). r = true.print((r&s) + "\t" + (r s) + "\t"). j < 9 . operators using the same operators.out. j++) System. Print the truth tables for &&. System. for (int j = 0.out. j++) System. i < 10. System.out.out.out. s = true. i -= 2) { for (int j = 0. for (int j = 0.println((r^s) + "\t" + (!r)). System.out.print(r + "\t" + s + "\t").out. System. i > 0. j < 9 . j < i. r = true.out. s = true. }} 5.i/2 .out. public class truthtable { public static void main(String[] args) { boolean r. System.out.out.println((r^s) + "\t" + (!r)).out. System.5.print(r + "\t" + s + "\t").out.i/2 . System. j++) System. j++) System.print(r + "\t" + s + "\t").print(" "). System.out. } } for (int i = 7. Write a program to show following design on the screen with the help of * : class Diamond { public static void main(String[] args) { for (int i = 1.print((r&s) + "\t" + (r s) + "\t"). System. i += 2) { for (int j = 0. System. r = false.print("*"). j < i. System.out.println( "R\t S\t AND\t OR\t XOR\t \tNOT").print((r&s) + "\t" + (r s) + "\t"). s = false.print(" ").out.print((r&s) + "\t" + (r s) + "\t").print("\n"). System.

} } } 8. } } if(i>7) { System. too low or.print("\n").println("\n Congratul ations!!!!You won!!!").out. GAME ? Creates a random number between 1 and 100 and then asks the user to g uess the number and tells if the guess was too high. System.*. import java.out. } if(k>n) { System.out. at position . Write a program to print ASCII value in java. Loops until 7 attempts has been finished.println(c+" "+(int) c).out. break.out.out.random()*200).*.i++) { String s = br.System.c<256. }} 6. class guess { public static void main(String args[])throws Exception { int n = (int)(Math. int k =Integer. class ascii { public static void main(String args[]) { for(char c=1. if(k==n) { System. the right numbe r.println("\n Sorry!!!t he number is too high!!!Enter a lower number!!!").c++) { System.parseInt(s).println("\n Sorry!!!t he number is too low!!!Enter a higher number!!!").out.i<=7. int i.math. for( i = 0.println("Now Please enter the number").io. } if(k<n) { System. BufferedReader br = new BufferedReader(new InputStreamRe ader(System.println("Sorr y!!!Game Over!!!").readLine().in)). import java.

println( Maximum of array : +max).parseInt(str). System. String str .io. } } System. import java.println( Minimum of array: +min).*.io. } } } } } } 11. for( int i = 0.t. k[i] = Integer. class transpose{ public static void main(String[] args) throws Exception { int i.out. WAP to find transpose of 3 X 3 array.i<10.out.i++) { str = br.i<10. } } System.out.println("Enter Order of Matrix : "). BufferedReader br = new BufferedReader(new InputStreamReader(System.*. int min = k[0]. } int max = k[0].i++) { if (k[i]<min) { min = k[i].in)). class maxmin { public static void main(String args[])throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.n.System.println( Enter array elements ).i++) { if (k[i]>max) { max = k[i]. } } 12.out.readLine().j. import java. int []k = new int[10]. . for( int i = 0.in)). int m[][]= new int[10][10]. for( int i = 0. System.print ln("\n The original number was"+n).i<10.WAP to find max and min value in array of n elements.out.

}}} . int a[][]=new int[n][n].i<n.j<n.println().parseInt(l).out.} } for(i=0.i++) {for(j=0.j<n.print(" "+c[i][j]).out.readLine().i++) {for(j=0. m[j][i]=t.} System.println("Enter Elements of Matrix :\n\n").j<n.nextInt(). int n=s.Scanner.k++) {c[i][j]+=a[i][k]*b[k][j].println("enter the array elements of a:"). for(i=0.}} System.out.j++) { b[i][j]=s.j++) {a[i][j]=s.println("Transpose of the Matrix :\n\n").} }} 13.nextInt().nextInt().out.util.j<n.parseInt(s).j++) {String l = br.i<n.out.i++) {for(j=i+1.i<n.println("enter the array elements of b:"). for(i=0.} System. class Matrix { public static void main(String args[]) throws Exception { Scanner s= new Scanner(System.i++) {for(j=0. int b[][]=new int[n][n].i<n.j.in).i<n. for(i=0.j<n. for(i=0. int c[][]=new int[n][n]. m[i][j] =Integer. n =Integer.out. int i. import java.j<n.k.String s = br.j++) {for(k=0.println("the result matrix is:"). System.println("enter the value of n").k<n.i<n.i<n.out.println("\n"). m[i][j]=m[j][i].j++) {System. System.print(" "+m [i][j]).out.j<n. for(i=0. System.i++) {for(j=0.i++) { for(j=0.out. WAP to multiply 3 X 3 array into 3 X 3 array. } }//end of b matrix System.out.}}} for(i=0.}} System.i++) {for(j=0.readLine().j++) {t=m[i][j].j++) {System.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->