1) wap in java to check whether no is prime or...

36
//1) WAP in JAVA to check whether no is prime or not. package finalpracs; import java.util.Scanner; public class Prime { public static void main(String args[]) { int a, i, flag=0; System.out.println("Enter number"); Scanner s = new Scanner(System.in); a = s.nextInt(); for(i=2;i<a;i++) { if(a%i==0) { flag = 1; break; } else flag = 0; } if(flag == 0) System.out.println("Prime Number"); else System.out.println("Not a Prime Number"); } } cebdiv.weebly.com

Upload: lecong

Post on 04-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

//1) WAP in JAVA to check whether no is prime or not.package finalpracs;import java.util.Scanner;public class Prime{

public static void main(String args[]){

int a, i, flag=0;System.out.println("Enter number");Scanner s = new Scanner(System.in);a = s.nextInt();for(i=2;i<a;i++){

if(a%i==0){ flag = 1;

break;}else

flag = 0;}if(flag == 0)

System.out.println("Prime Number");else

System.out.println("Not a PrimeNumber");

}

}

cebdiv.weebly.com

//2) WAP in JAVA to find factorial of a number usingrecursive method.package finalpracs;import java.util.Scanner;public class Fact{

public static void main(String[] args){

int a, f;System.out.println("Enter Number to calculate

Factorial");Scanner s = new Scanner(System.in);a = s.nextInt();f = fact(a);System.out.println("Factorial is "+f);

}

static int fact(int n){

if(n==1)return 1;

elsereturn (n*fact(n-1));

}}

cebdiv.weebly.com

//3) WAP in JAVA to generate first 20 terms inFibonacci series.package finalpracs;public class Fibo{

public static void main(String[] args){

int a=0, b=1, c, n;System.out.print(" "+a+" "+b);for(n=0;n<18;n++){

c = a + b;System.out.print(" "+c);a = b;b = c;

}}

}

cebdiv.weebly.com

//4) WAP in JAVA to sort an array of 10 elements indescending order.package finalpracs;import java.util.Scanner;public class Arrdesc{

public static void main(String[] args){

int a[] = new int[10];int i, temp;Scanner s = new Scanner(System.in);for(i=0; i<10; i++){

System.out.println("Enter Element"+(i+1));

a[i] = s.nextInt();}

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

for(int j=i; j<10-i; j++){

if(a[i]>a[i+1]){ temp = a[i];

a[i] = a[i+1];a[i+1] = a[i];

}}

}for(i=0; i<10; i++)

System.out.println(a[i]);}

}

cebdiv.weebly.com

//5) WAP in JAVA to multiply two 3*3 matrices.package finalpracs;import java.util.*;public class matrixmult{

public static void main(String[] args){

int a[][] = new int [3][3];int b[][] = new int [3][3];int c[][] = new int [3][3];int i,j,k;Scanner s = new Scanner(System.in);System.out.println("Matrix 1");for(i=0;i<3;i++){

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

System.out.println("Enter Element"+(i+1)+" "+(j+1));

a[i][j] = s.nextInt();}

}

System.out.println("Matrix 2");for(i=0;i<3;i++){

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

System.out.println("Enter Element"+(i+1)+" "+(j+1));

b[i][j] = s.nextInt();}

}

cebdiv.weebly.com

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

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

c[i][j] = 0;}

}

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

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

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

c[i][j] += a[i][k]*b[k][j];}

}}

System.out.println("Multiplied Matrix");for(i=0;i<3;i++){

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

System.out.println("Element"+(i+1)+","+(j+1)+"="+c[i][j]);

}}

}

}

cebdiv.weebly.com

//6) WAP in JAVA to create multiplication table infollowing format// 1 2 3// 2 4 6// 3 6 9package finalpracs;public class tables{

public static void main(String[] args){

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

System.out.println();for(j=1;j<=10;j++){

System.out.print("\t"+i*j);}

}}

}

cebdiv.weebly.com

//7) WAP in JAVA to overload “max()” method to findmaximum between 2 integer number and 2 float number.package finalpracs;import java.util.Scanner;public class Overload{ static void max(int a, int b)

{if(a>b)System.out.println("Larger Integer is : "+a);

elseSystem.out.println("Larger Integer is : "+b);

}static void max(float a, float b){if(a>b)

System.out.println("Larger Float is : "+a);else

System.out.println("Larger Float is : "+b);}public static void main(String[] args){ int x,y;

float p,q;Scanner s = new Scanner(System.in);System.out.print("Enter Integer 1 : ");x = s.nextInt();System.out.print("Enter Integer 2 : ");y = s.nextInt();System.out.print("Enter Float 1 : ");p = s.nextFloat();System.out.print("Enter Float 2 : ");q = s.nextFloat();max(x,y);max(p,q);

}

}

cebdiv.weebly.com

//8) WAP in JAVA to implement following string methodsi)concat() ii) indexOf() iii) toUpperCase() iv) length() v)

charAt()import java.util.Scanner;

public class str{

public static void main(String[] args){String s1;Scanner in = new Scanner(System.in);

System.out.print("Enter the String ");s1 = in.next();System.out.println("This Program is to perform

STRING FUNCTIONS");System.out.println("1.String Length");int len = s1.length();System.out.println("Length is "+len);

System.out.println("2.Lower Case");String l = s1.toLowerCase();

System.out.println("Lower Case is "+l);

System.out.println("3.Upper Case");String u = s1.toUpperCase();

System.out.println("Upper Case is "+u);

System.out.println("4.Substring Nth character tolast");

cebdiv.weebly.com

System.out.println("Enter the beginning Index");int b = in.nextInt();String a = s1.substring(b);System.out.println("The new string is "+a);

System.out.println("5.Substring Nth character toMth character");

System.out.println("Enter the beginning Index");int beg = in.nextInt();

System.out.println("Enter the Ending Index");int e = in.nextInt();String s3 = s1.substring(beg,e);System.out.println("The new string is "+s3);

System.out.println("6.Character at specificposition");

System.out.println("Enter Position number");int position = in.nextInt();

System.out.println("Character is"+s1.charAt(position));

System.out.println("7.First occurence Position ofParticular Element");

System.out.println("Enter the element");char cr = in.next().charAt(0);int pos = s1.indexOf(cr);

cebdiv.weebly.com

System.out.println("First occurence is at "+pos);

System.out.println("8.Last occurence Position ofParticular Element");

System.out.println("Enter the element");char cha = in.next().charAt(0);int post = s1.lastIndexOf(cha);System.out.println("Last occurence is at "+post);

}

}

cebdiv.weebly.com

//10) WAP in JAVA to handle ArrayIndexOutOfBoundsExceptionand ArithmeticException.package finalpracs;

public class except{public static void main(String[] args){int a[] = {10,20,30};try{

int b = a[4]/(10 - a[0]);}catch(ArrayIndexOutOfBoundsException e1){

System.out.println("Exception is : "+e1);}catch(ArithmeticException e2){

System.out.println("Exception is : "+e2);}finally{

System.out.println("Caught Exception E1");}

try{

int b = a[2]/(20 - a[1]);}catch(ArrayIndexOutOfBoundsException e1){

System.out.println("Exception is : "+e1);}

cebdiv.weebly.com

catch(ArithmeticException e2){

System.out.println("Exception is : "+e2);}finally{

System.out.println("Caught Exception E2");}}

}

cebdiv.weebly.com

//11) WAP in JAVA to create one package (e. g. MyPackage ) andadd any two classes in that package. Import that package in someother program.package package2;

import package1.expt11a;import package1.expt11b;

public class expt11{

public static void main(String args[]){

expt11a x = new expt11a();expt11b y = new expt11b();x.disp();y.disp();System.out.println("Hello This is Package 2

Class expt11");}

}

cebdiv.weebly.com

//12)WAP in JAVA to implement the followinghierarchical inheritance.

package finalpracs;import java.util.Scanner;

import java.util.Scanner;

public class Twelve{

public static int b,h;public static void main(String args[]){Twelve obj=new Twelve();System.out.println("Enter Base and Height : ");obj.getdata();rectangle r = new rectangle();r.displayarea();triangle t=new triangle();t.displayarea();}

void getdata(){

Scanner sc=new Scanner(System.in);System.out.println("Enter Base");b = sc.nextInt();System.out.println("Enter Height");h = sc.nextInt();sc.close();

}

}class rectangle extends Twelve

cebdiv.weebly.com

{int a;void displayarea(){

a=b*h;System.out.println("Area of Rectangle is : "+a);}

}class triangle extends Twelve{

int a;Twelve tr=new Twelve();void displayarea(){a = (int) (0.5*b*h);System.out.print("Area of Triangle is : "+a);}

}

cebdiv.weebly.com

//13) Create an applet to print followingoutput on screen//FACEpackage finalpracs;import java.applet.*;import java.awt.*;/*<applet CODE = "appl.class" WIDTH=6000 HEIGHT=4000></applet>*/

public class appl extends Applet{

public void paint(Graphics g){

g.drawString("Hello World", 10, 20);g.drawOval(50, 100, 150, 200);//for face

outlineg.drawOval(85, 150, 20, 20);//for left eyeg.drawOval(150, 150, 20, 20);//for right eyeg.drawOval(120, 180, 10, 40);//for noseg.drawOval(80, 240, 100, 10);//for mouthg.drawOval(20, 160, 30, 50);//for left earg.drawOval(200, 160, 30, 50);//for right ear

}}OUTPUT:

cebdiv.weebly.com

cebdiv.weebly.com

//14) Create an applet to print following output onscreen//HOUSEpackage finalpracs;import java.applet.*;import java.awt.*;

/*<applet CODE = "appl.class" WIDTH=6000 HEIGHT=4000>* </applet>*/

public class house extends Applet{

public void paint(Graphics g){

int x[]= {60,240,270,20};int y[] = {10,10,50,50};g.drawRect(50, 50, 200, 100);//for house

outlineg.fillRect(90, 70, 30, 30);//for windowg.drawRect(150, 70, 50, 80);//for doorg.drawPolygon(x, y, 4);

}

}

OUTPUT

cebdiv.weebly.com

cebdiv.weebly.com

//15) WAP in JAVA to add two 3*3 matrices and printsum of the elements of both the matrices//individually.package finalpracs;import java.util.Scanner;public class matrixadd {

public static void main(String[] args){int a[][] = new int [3][3];int b[][] = new int [3][3];int c[][] = new int [3][3];int i,j;Scanner s = new Scanner(System.in);System.out.println("Matrix 1");for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.println("Enter Element "+(i+1)+""+(j+1));a[i][j] = s.nextInt();}}

System.out.println("Matrix 2");for(i=0;i<3;i++){

for(j=0;j<3;j++){System.out.println("Enter Element "+(i+1)+"

"+(j+1));b[i][j] = s.nextInt();}

cebdiv.weebly.com

}

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

c[i][j] = 0;}}

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

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

c[i][j] = a[i][j]+b[i][j];}

}

System.out.println("Added Matrix");for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.println("Element "+(i+1)+","

+(j+1)+"="+c[i][j]);}}

}

}

cebdiv.weebly.com

//Q.16,17. WAP to print 1A2B3C4D5E6F7G8H9I10J.package finalpracs;

class alpha extends Thread{

public void run(){

for(char i = 65; i<=74; i++){

System.out.println(i);try{

Thread.sleep(1000);//Change this Value if u don't get desired output

} catch(InterruptedException e){}}

}}

class digit extends Thread{

public void run(){

for(int i=1; i<=10; i++){

System.out.println(i);try{

Thread.sleep(1000);//Change this Value if u don't get desired output

} catch(InterruptedException e){}}

}}

public class multithreadpattern

cebdiv.weebly.com

{

public static void main(String[] args){

digit a = new digit();alpha b = new alpha();a.start();b.start();try{

a.join();b.join();

}catch(InterruptedException e){}

}

}

cebdiv.weebly.com

//18) WAP in JAVA to implement the followingMultilevel Inheritance

package finalpracs;import java.util.*;

public class multilevelinherit{

public static void main(String agrs[]){

stu s = new stu();test t = new test();result r = new result();s.getdata();t.getmarks();r.display();

}

}

class stu{

Scanner s = new Scanner(System.in);String name;int roll;void getdata(){

System.out.print("Enter Name :");name = s.next();System.out.print("Enter Roll Number :");roll = s.nextInt();

}

cebdiv.weebly.com

}

class test extends stu{

Scanner s = new Scanner(System.in);int t1;int t2;void getmarks(){

System.out.print("Enter Marks of Test1 :");t1 = s.nextInt();System.out.print("Enter Marks of Test2 :");t2 = s.nextInt();

}

}

class result extends test{

float avg;void display(){

System.out.println("Test 1 "+t1);avg = (t1+t2)/2;System.out.println("Result is :"+avg);

}

}

cebdiv.weebly.com

//19) Create an applet to print following output onscreen//Starpackage finalpracs;import java.applet.*;import java.awt.*;

/*<applet CODE = "appl.class" WIDTH=6000 HEIGHT=4000>* </applet>*/

public class star extends Applet{

public void paint(Graphics g){

int x[] = {30,130,80};int y[] = {50,50,100};int p[] = {30,130,80};int q[] = {70,70,20};g.drawPolygon(x, y, 3);//for downward facing

triangle.g.drawPolygon(p,q,3);

}

}

__________________________________________________________________________________________________________

cebdiv.weebly.com

//20) WAP in JAVA to print sum of digits of given number usingcommand-line argument

class Main20{public static void main(String args[]){int x=Integer.parseInt(args[0]);int n=x,sum=0,d;while(n!=0){

d=n%10;n=n/10;sum=sum+d;

}System.out.print("Sum of the digits is"+sum);

}}

cebdiv.weebly.com

//21) WAP in JAVA for addition of two complex numbers usingconstructor overloading.import java.util.Scanner;

class complex{

int x;int y;

complex(){

x=0; y=0;}

void add(complex a, complex b){

complex sum = new complex();sum.x = a.x + b.x;sum.y = a.y + b.y;

System.out.println("Added Complex Number is :"+sum.x+"+i"+sum.y);

}}

public class Main21{

public static void main(String args[]){complex c1 = new complex();complex c2 = new complex();complex s = new complex();

Scanner in = new Scanner(System.in);System.out.print("Enter the first complex Number :\n

Real1 = ");c1.x = in.nextInt();System.out.print("Imaginary1 = ");c1.y = in.nextInt();

cebdiv.weebly.com

Maurya
Underline

System.out.print("Enter the second complex Number :\nReal2 = ");

c2.x = in.nextInt();System.out.print("Imaginary2 =");c2.y = in.nextInt();s.add(c1,c2);}

}

/*OUPUTEnter the first complex Number :Real1 = 4Imaginary1 = 3Enter the second complex Number :Real2 = 5Imaginary2 = 7Added Complex Number is : 9 + i10*/

cebdiv.weebly.com

//22) WAP in Java to find maximum and minimum between 3numbers using decision makingstatements (input with command-line argument)class Main22{public static void main(String args[]){int x=Integer.parseInt(args[0]);int y=Integer.parseInt(args[1]);int z=Integer.parseInt(args[2]);//Maximumif(x>y && x>z)System.out.println("Maximum is"+x);else if(y>x && y>z)System.out.println("Maximum is"+y);elseSystem.out.println("Maximum is"+z);//Minimumif(x<y && x<z)System.out.println("Minimum is"+x);else if(y<x && y<z)System.out.println("Minimum is"+y);elseSystem.out.println("Minimum is"+z);}

}

cebdiv.weebly.com

23) WAP in Java to find smallest of n numbers taken from userusing array.import java.util.*;class Main23{public static void main(String args[]){int n,i;Scanner s=new Scanner(System.in);System.out.print("Enter the number of elements");n=s.nextInt();int []a=new int[n];for(i=0;i<n;i++){System.out.print("Enter element"+(i+1));a[i]=s.nextInt();

}int min=a[0];for(i=0;i<n;i++){if (min>a[i])min=a[i];

}System.out.println("Smallest no. is"+min);}

}

cebdiv.weebly.com

24) WAP in Java to create a Rectangle class ,objects,and implementmethod to calculatearea of rectangle.import java.util.*;

class Rectangle{ int l,b;Scanner s=new Scanner(System.in);void getdata(){

System.out.print("Enter the length");l=s.nextInt();System.out.print("Enter the Breadth");b=s.nextInt();

}void area(){System.out.println("The area is"+(l*b));

}}

class Main24{public static void main(String arg[]){Rectangle r=new Rectangle();r.getdata();r.area();

}}

cebdiv.weebly.com

25) WAP in Java to implement method overloading to calculate areaof rectangle.import java.util.*;

class Rectangle{ int l,b;void area(int x,int y){l=x;b=y;System.out.println("The area is"+(l*b));

}void area(float x,float y){System.out.println("The area is"+(x*y));

}}

class Main25{public static void main(String arg[]){ float l,b;

int x,y;Rectangle r=new Rectangle();Scanner s=new Scanner(System.in);System.out.print("Enter the length in decimals");l=s.nextFloat();System.out.print("Enter the Breadth in decimals");b=s.nextFloat();r.area(l,b);System.out.print("Enter the length");x=s.nextInt();System.out.print("Enter the Breadth");y=s.nextInt();r.area(x,y);

}}

cebdiv.weebly.com

26) WAP in Java to demonstrate interfaces in java.

interface operation{int a=5;void add();

}

class ABC implements operation{int b;public void add(){System.out.println(a+b);

}}

class Main26{public static void main(String args[]){ABC x=new ABC();x.b=20;x.add();

}}

cebdiv.weebly.com

27) WAP in Java to print follwing pattern11 2 A1 2 3 B A1 2 3 4 C B A1 2 3 4 5 D C B A

class Main27{public static void main(String args[]){int i,j,k,l,c;for(i=0;i<5;i++){

for(j=4-i;j>0;j--){System.out.print(" ");

}for(k=1;k<=i+1;k++){

System.out.print(k);}

c=i;for(l=0;l<i;l++){System.out.print((char)(c+64));c--;

}System.out.println();}}}

cebdiv.weebly.com