Java Prog

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Q2. Ans.

Write a program to sort an array. class numbersorting { public static void main(String args[]) { int number[]={56,89,7,5,2,1}; int n=number.length; System.out.println("unsorted elements"); for(int i=0;i<n;i++) { System.out.println(" "+ number[i]); } System.out.println("\n"); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(number[i]>number[j]) { int temp=number[i]; number[i]=number[j]; number[j]=temp; } } } System.out.println("sorted list is"); for(int i=0;i<n;i++) { System.out.println(" "+number[i]); } System.out.println(" "); } }

Q4. WAP to find whether the given number is a prime number or not. Ans. class prime { public static void main(String args[]) { String a; a=args[0]; int j,i; j=Integer.valueOf(a).intValue(); for(i=2;i<j;i++) { if(j%i==0) break; } if(i==j) System.out.println("No is prime"+i); else System.out.println("No is not prime"+i); } } -------------------------------------------------WAP create a Fibonacci series. class feb { public static void main(String ap[]) { int a=1,b=0,c=0; while(c<=21) { System.out.println(c); c=a+b; a=b; b=c; }. }

Q18. Ans.

WAP Largest Number b/t Numbers. import java.util.Scanner;

WAP Compute the Length of given string Import java.io.*; class StringLength { public static void main(String[] args){ try { BufferedReader object= new BufferedReader (new InputStreamReader (System.in)); System.out.println("Eneter string value:"); String s=object.readLine(); int len=s.length(); System.out.println(len); } catch(Exception e){} } } ----------------------------------------------------------WAP 2D Array public class twoDimension { public static void main(String[] args) { int[][] a2 = new int[10][5]; for (int i=0; i<a2.length; i++) { for (int j=0; j<a2[i].length; j++) { a2[i][j] = i; System.out.print(" " + a2[i][j]); } System.out.println(""); } } }

class LargestOfThreeNumbers { public static void main(String args[]) { int x, y, z; System.out.println("Enter three integers "); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); z = in.nextInt(); if ( x > y && x > z ) System.out.println("First number is largest."); else if ( y > x && y > z ) System.out.println("Second number is largest."); else if ( z > x && z > y ) System.out.println("Third number is largest."); else System.out.println("Entered numbers are not distinct."); } }

You might also like