0% found this document useful (0 votes)
77 views2 pages

Java Programming Basics and Examples

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)
77 views2 pages

Java Programming Basics and Examples

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

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=[Link]; [Link]("unsorted elements"); for(int i=0;i<n;i++) { [Link](" "+ number[i]); } [Link]("\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; } } } [Link]("sorted list is"); for(int i=0;i<n;i++) { [Link](" "+number[i]); } [Link](" "); } }

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=[Link](a).intValue(); for(i=2;i<j;i++) { if(j%i==0) break; } if(i==j) [Link]("No is prime"+i); else [Link]("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) { [Link](c); c=a+b; a=b; b=c; }. }

Q18. Ans.

WAP Largest Number b/t Numbers. import [Link];

WAP Compute the Length of given string Import [Link].*; class StringLength { public static void main(String[] args){ try { BufferedReader object= new BufferedReader (new InputStreamReader ([Link])); [Link]("Eneter string value:"); String s=[Link](); int len=[Link](); [Link](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<[Link]; i++) { for (int j=0; j<a2[i].length; j++) { a2[i][j] = i; [Link](" " + a2[i][j]); } [Link](""); } } }

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

You might also like