0% found this document useful (0 votes)
42 views

Java Prog

The document contains code snippets that demonstrate how to: 1) Sort an array of numbers from lowest to highest using a bubble sort algorithm. 2) Check if a given number is prime by testing for divisibility by numbers from 2 to the number. 3) Generate the Fibonacci series up to a given number by calculating each term as the sum of the previous two terms. 4) Find the length of a given string. 5) Print the values of a 2D array. 6) Find the largest of three numbers entered by the user.

Uploaded by

jacksonsnoop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Java Prog

The document contains code snippets that demonstrate how to: 1) Sort an array of numbers from lowest to highest using a bubble sort algorithm. 2) Check if a given number is prime by testing for divisibility by numbers from 2 to the number. 3) Generate the Fibonacci series up to a given number by calculating each term as the sum of the previous two terms. 4) Find the length of a given string. 5) Print the values of a 2D array. 6) Find the largest of three numbers entered by the user.

Uploaded by

jacksonsnoop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy