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);
}
}
5. Print the truth tables for &&, || operators using the same operators.
public class truthtable {
public static void main(String[] args) {
boolean r, s;
System.out.println( "R\t S\t AND\t OR\t XOR\t \tNOT");
r = true; s = true;
System.out.print(r + "\t" + s + "\t");
System.out.print((r&s) + "\t" + (r|s) + "\t");
System.out.println((r^s) + "\t" + (!r));
r = true; s = false;
System.out.print(r + "\t" + s + "\t");
System.out.print((r&s) + "\t" + (r|s) + "\t");
System.out.println((r^s) + "\t" + (!r));
r = false; s = true;
System.out.print(r + "\t" + s + "\t");
System.out.print((r&s) + "\t" + (r|s) + "\t");
System.out.println((r^s) + "\t" + (!r));
r = false; s = false;
System.out.print(r + "\t" + s + "\t");
System.out.print((r&s) + "\t" + (r|s) + "\t");
System.out.println((r^s) + "\t" + (!r));
}}
5. 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; i < 10; i += 2) {
for (int j = 0; j < 9 - i/2 ; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i/2 ; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}}
6. Write a program to print ASCII value in java.
class ascii
{
public static void main(String args[])
{
for(char c=1;c<256;c++)
{
System.out.println(c+" at position
"+(int) c);
}
}
}
8. 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, too low or, the right numbe
r. Loops until 7 attempts has been finished.
import java.io.*;
import java.math.*;
class guess
{
public static void main(String args[])throws Exception
{
int n = (int)(Math.random()*200);
int i;
System.out.println("Now Please enter the number");
BufferedReader br = new BufferedReader(new InputStreamRe
ader(System.in));
for( i = 0;i<=7;i++)
{
String s = br.readLine();
int k =Integer.parseInt(s);
if(k==n)
{
System.out.println("\n Congratul
ations!!!!You won!!!");
break;
}
if(k>n)
{
System.out.println("\n Sorry!!!t
he number is too high!!!Enter a lower number!!!");
}
if(k<n)
{
System.out.println("\n Sorry!!!t
he number is too low!!!Enter a higher number!!!");
}
}
if(i>7)
{
System.out.println("Sorr
y!!!Game Over!!!");
System.out.print
ln("\n The original number was"+n);
}
}
}
}
}
}
11.WAP to find max and min value in array of n elements.
import java.io.*;
class maxmin
{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int []k = new int[10];
String str ;
System.out.println( Enter array elements );
for( int i = 0;i<10;i++)
{
str = br.readLine();
k[i] = Integer.parseInt(str);
}
int max = k[0];
for( int i = 0;i<10;i++)
{
if (k[i]>max)
{
max = k[i];
}
}
System.out.println( Maximum of array : +max);
int min = k[0];
for( int i = 0;i<10;i++)
{
if (k[i]<min)
{
min = k[i];
}
}
System.out.println( Minimum of array: +min);
}
}
12. WAP to find transpose of 3 X 3 array.
import java.io.*;
class transpose{
public static void main(String[] args) throws Exception
{
int i,j,n,t;
int m[][]= new int[10][10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Order of Matrix : ");
String s = br.readLine();
n =Integer.parseInt(s);
System.out.println("Enter Elements of Matrix :\n\n");
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{String l = br.readLine();
m[i][j] =Integer.parseInt(l);}}
System.out.println("Transpose of the Matrix :\n\n");
for(i=0;i<n;i++)
{for(j=i+1;j<n;j++)
{t=m[i][j];
m[i][j]=m[j][i];
m[j][i]=t;}
}
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{System.out.print(" "+m
[i][j]);}
System.out.println("\n");}
}}
13. WAP to multiply 3 X 3 array into 3 X 3 array.
import java.util.Scanner;
class Matrix
{
public static void main(String args[]) throws Exception
{
Scanner s= new Scanner(System.in);
int i,j,k;
System.out.println("enter the value of n");
int n=s.nextInt();
int a[][]=new int[n][n];
int b[][]=new int[n][n];
int c[][]=new int[n][n];
System.out.println("enter the array elements of a:");
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{a[i][j]=s.nextInt();}}
System.out.println("enter the array elements of b:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ b[i][j]=s.nextInt(); }
}//end of b matrix
System.out.println("the result matrix is:");
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{for(k=0;k<n;k++)
{c[i][j]+=a[i][k]*b[k][j];}}}
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{System.out.print(" "+c[i][j]);}
System.out.println();}}}