Java break statement

  1. /*

  2.   Java break statement example.

  3.   This example shows how to use java break statement to terminate the loop.

  4. */

  5. public class JavaBreakExample {

  6.  

  7. public static void main(String[] args) {

  8. /*

  9.   * break statement is used to terminate the loop in java. The following example

  10.   * breaks the loop if the array element is equal to true.

  11.   *

  12.   * After break statement is executed, the control goes to the statement

  13.   * immediately after the loop containing break statement.

  14.   */

  15.  

  16. int intArray[] = new int[]{1,2,3,4,5};

  17.  

  18. System.out.println("Elements less than 3 are : ");

  19. for(int i=0; i < intArray.length ; i ++)

  20. {

  21. if(intArray[i] == 3)

  22. break;

  23. else

  24. System.out.println(intArray[i]);

  25. }

  26.  

  27. }

  28. }

  29.  

  30. /*

  31. Output would be

  32. Elements less than 3 are :

  33. 1

  34. 2

  35. */

Java break statement with label

  1. /*

  2.   Java break statement with label example.

  3.   This example shows how to use java break statement to terminate the labeled loop.

  4.   The following example uses break to terminate the labeled loop while searching two

  5.   dimensional int array.

  6. */

  7.  

  8. public class JavaBreakWithLableExample {

  9.  

  10. public static void main(String[] args) {

  11.  

  12.  

  13. int[][] intArray = new int[][]{{1,2,3,4,5},{10,20,30,40,50}};

  14. boolean blnFound = false;

  15.  

  16. System.out.println("Searching 30 in two dimensional int array..");

  17.  

  18. Outer:

  19. for(int intOuter=0; intOuter < intArray.length ; intOuter++)

  20. {

  21. Inner:

  22. for(int intInner=0; intInner < intArray[intOuter].length; intInner++)

  23. {

  24. if(intArray[intOuter][intInner] == 30)

  25. {

  26. blnFound = true;

  27. break Outer;

  28. }

  29.  

  30. }

  31. }

  32.  

  33. if(blnFound == true)

  34. System.out.println("30 found in the array");

  35. else

  36. System.out.println("30 not found in the array");

  37. }

  38. }

  39.  

  40. /*

  41. Output would be

  42. Searching 30 in two dimensional int array..

  43. 30 found in the array

  44. */

Swap Numbers Without Using Third Variable

  1. /*

  2. Swap Numbers Without Using Third Variable Java Example

  3. This Swap Numbers Java Example shows how to

  4. swap value of two numbers without using third variable using java.

  5. */

  6.  

  7. public class SwapElementsWithoutThirdVariableExample {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. int num1 = 10;

  12. int num2 = 20;

  13.  

  14. System.out.println("Before Swapping");

  15. System.out.println("Value of num1 is :" + num1);

  16. System.out.println("Value of num2 is :" +num2);

  17.  

  18. //add both the numbers and assign it to first

  19. num1 = num1 + num2;

  20. num2 = num1 - num2;

  21. num1 = num1 - num2;

  22.  

  23. System.out.println("Before Swapping");

  24. System.out.println("Value of num1 is :" + num1);

  25. System.out.println("Value of num2 is :" +num2);

  26. }

  27.  

  28.  

  29. }

  30.  

  31. /*

  32. Output of Swap Numbers Without Using Third Variable example would be

  33. Before Swapping

  34. Value of num1 is :10

  35. Value of num2 is :20

  36. Before Swapping

  37. Value of num1 is :20

  38. Value of num2 is :10

  39. */

Swap Numbers Java Example

  1. /*

  2. Swap Numbers Java Example

  3. This Swap Numbers Java Example shows how to

  4. swap value of two numbers using java.

  5. */

  6.  

  7. public class SwapElementsExample {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. int num1 = 10;

  12. int num2 = 20;

  13.  

  14. System.out.println("Before Swapping");

  15. System.out.println("Value of num1 is :" + num1);

  16. System.out.println("Value of num2 is :" +num2);

  17.  

  18. //swap the value

  19. swap(num1, num2);

  20. }

  21.  

  22. private static void swap(int num1, int num2) {

  23.  

  24. int temp = num1;

  25. num1 = num2;

  26. num2 = temp;

  27.  

  28. System.out.println("After Swapping");

  29. System.out.println("Value of num1 is :" + num1);

  30. System.out.println("Value of num2 is :" +num2);

  31.  

  32. }

  33. }

  34.  

  35. /*

  36. Output of Swap Numbers example would be

  37. Before Swapping

  38. Value of num1 is :10

  39. Value of num2 is :20

  40. After Swapping

  41. Value of num1 is :20

  42. Value of num2 is :10

  43. */

Reverse Number using Java

  1. /*

  2.   Reverse Number using Java

  3.   This Java Reverse Number Example shows how to reverse a given number.

  4. */

  5.  

  6. public class ReverseNumber {

  7.  

  8. public static void main(String[] args) {

  9.  

  10. //original number

  11. int number = 1234;

  12. int reversedNumber = 0;

  13. int temp = 0;

  14.  

  15. while(number > 0){

  16.  

  17. //use modulus operator to strip off the last digit

  18. temp = number%10;

  19.  

  20. //create the reversed number

  21. reversedNumber = reversedNumber * 10 + temp;

  22. number = number/10;

  23.  

  24. }

  25.  

  26. //output the reversed number

  27. System.out.println("Reversed Number is: " + reversedNumber);

  28. }

  29. }

  30.  

  31. /*

  32. Output of this Number Reverse program would be

  33. Reversed Number is: 4321

  34. */

Java Interface example

  1. /*

  2. Java Interface example.

  3. This Java Interface example describes how interface is defined and

  4. being used in Java language.

  5.  

  6. Syntax of defining java interface is,

  7. <modifier> interface <interface-name>{

  8.   //members and methods()

  9. }

  10. */

  11.  

  12. //declare an interface

  13. interface IntExample{

  14.  

  15. /*

  16.   Syntax to declare method in java interface is,

  17.   <modifier> <return-type> methodName(<optional-parameters>);

  18.   IMPORTANT : Methods declared in the interface are implicitly public and abstract.

  19.   */

  20.  

  21. public void sayHello();

  22. }

  23. }

  24. /*

  25. Classes are extended while interfaces are implemented.

  26. To implement an interface use implements keyword.

  27. IMPORTANT : A class can extend only one other class, while it

  28. can implement n number of interfaces.

  29. */

  30.  

  31. public class JavaInterfaceExample implements IntExample{

  32. /*

  33.   We have to define the method declared in implemented interface,

  34.   or else we have to declare the implementing class as abstract class.

  35.   */

  36.  

  37. public void sayHello(){

  38. System.out.println("Hello Visitor !");

  39. }

  40.  

  41. public static void main(String args[]){

  42. //create object of the class

  43. JavaInterfaceExample javaInterfaceExample = new JavaInterfaceExample();

  44. //invoke sayHello(), declared in IntExample interface.

  45. javaInterfaceExample.sayHello();

  46. }

  47. }

  48.  

  49. /*

  50. OUTPUT of the above given Java Interface example would be :

  51. Hello Visitor !

  52. */

Java Factorial Using Recursion

  1. /*

  2. Java Factorial Using Recursion Example

  3. This Java example shows how to generate factorial of a given number

  4. using recursive function.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10.  

  11. public class JavaFactorialUsingRecursion {

  12.  

  13. public static void main(String args[]) throws NumberFormatException, IOException{

  14.  

  15. System.out.println("Enter the number: ");

  16.  

  17. //get input from the user

  18. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

  19. int a = Integer.parseInt(br.readLine());

  20.  

  21. //call the recursive function to generate factorial

  22. int result= fact(a);

  23.  

  24.  

  25. System.out.println("Factorial of the number is: " + result);

  26. }

  27.  

  28. static int fact(int b)

  29. {

  30. if(b <= 1)

  31. //if the number is 1 then return 1

  32. return 1;

  33. else

  34. //else call the same function with the value - 1

  35. return b * fact(b-1);

  36. }

  37. }

  38.  

  39. /*

  40. Output of this Java example would be

  41.  

  42. Enter the number:

  43. 5

  44. Factorial of the number is: 120

  45. */

Java Factorial Example

  1. /*

  2.   Java Factorial Example

  3.   This Java Factorial Example shows how to calculate factorial of

  4.   a given number using Java.

  5. */

  6.  

  7. public class NumberFactorial {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. int number = 5;

  12.  

  13. /*

  14. * Factorial of any number is !n.

  15. * For example, factorial of 4 is 4*3*2*1.

  16. */

  17.  

  18. int factorial = number;

  19.  

  20. for(int i =(number - 1); i > 1; i--)

  21. {

  22. factorial = factorial * i;

  23. }

  24.  

  25. System.out.println("Factorial of a number is " + factorial);

  26. }

  27. }

  28.  

  29. /*

  30. Output of the Factorial program would be

  31. Factorial of a number is 120

  32. */

Java Class example

  1. /*

  2. Java Class example.

  3. This Java class example describes how class is defined and being used

  4. in Java language.

  5.  

  6. Syntax of defining java class is,

  7. <modifier> class <class-name>{

  8.   // members and methods

  9. }

  10. */

  11.  

  12. public class JavaClassExample{

  13. /*

  14.   Syntax of defining memebers of the java class is,

  15.   <modifier> type <name>;

  16.   */

  17. private String name;

  18. /*

  19.   Syntax of defining methods of the java class is,

  20.   <modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>{

  21.   ...

  22.   }

  23.   */

  24. public void setName(String n){

  25. //set passed parameter as name

  26. name = n;

  27. }

  28. public String getName(){

  29. //return the set name

  30. return name;

  31. }

  32. //main method will be called first when program is executed

  33. public static void main(String args[]){

  34. /*

  35.   Syntax of java object creation is,

  36.   <class-name> object-name = new <class-constructor>;

  37.   */

  38. JavaClassExample javaClassExample = new JavaClassExample();

  39. //set name member of this object

  40. javaClassExample.setName("Visitor");

  41. // print the name

  42. System.out.println("Hello " + javaClassExample.getName());

  43. }

  44. }

  45.  

  46. /*

  47. OUTPUT of the above given Java Class Example would be :

  48. Hello Visitor

  49. */

Hello World java

  1. /*

  2. Java Hello World example.

  3. */

  4.  

  5. public class HelloWorldExample{

  6.  

  7. public static void main(String args[]){

  8.  

  9. /*

  10.   Use System.out.println() to print on console.

  11.   */

  12. System.out.println("Hello World !");

  13.  

  14. }

  15.  

  16. }

  17.  

  18. /*

  19.  

  20. OUTPUT of the above given Java Hello World Example would be :

  21.  

  22. Hello World !

  23.  

  24. */

Find Largest and Smallest Number in an Array

  1. /*

  2.   Find Largest and Smallest Number in an Array Example

  3.   This Java Example shows how to find largest and smallest number in an

  4.   array.

  5. */

  6. public class FindLargestSmallestNumber {

  7.  

  8. public static void main(String[] args) {

  9.  

  10. //array of 10 numbers

  11. int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

  12.  

  13. //assign first element of an array to largest and smallest

  14. int smallest = numbers[0];

  15. int largetst = numbers[0];

  16.  

  17. for(int i=1; i< numbers.length; i++)

  18. {

  19. if(numbers[i] > largetst)

  20. largetst = numbers[i];

  21. else if (numbers[i] < smallest)

  22. smallest = numbers[i];

  23.  

  24. }

  25.  

  26. System.out.println("Largest Number is : " + largetst);

  27. System.out.println("Smallest Number is : " + smallest);

  28. }

  29. }

  30.  

  31. /*

  32. Output of this program would be

  33. Largest Number is : 98

  34. Smallest Number is : 23

  35. */

Odd Number Java

  1. /*

  2.   Even Odd Number Example

  3.   This Java Even Odd Number Example shows how to check if the given

  4.   number is even or odd.

  5. */

  6.  

  7. public class FindEvenOrOddNumber {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. //create an array of 10 numbers

  12. int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};

  13.  

  14. for(int i=0; i < numbers.length; i++){

  15.  

  16. /*

  17. * use modulus operator to check if the number is even or odd.

  18. * If we divide any number by 2 and reminder is 0 then the number is

  19. * even, otherwise it is odd.

  20. */

  21.  

  22. if(numbers[i]%2 == 0)

  23. System.out.println(numbers[i] + " is even number.");

  24. else

  25. System.out.println(numbers[i] + " is odd number.");

  26.  

  27. }

  28.  

  29. }

  30. }

  31.  

  32. /*

  33. Output of the program would be

  34. 1 is odd number.

  35. 2 is even number.

  36. 3 is odd number.

  37. 4 is even number.

  38. 5 is odd number.

  39. 6 is even number.

  40. 7 is odd number.

  41. 8 is even number.

  42. 9 is odd number.

  43. 10 is even number.

  44. */

Calculate Rectangle Perimeter using Java

  1. /*

  2. Calculate Rectangle Perimeter using Java Example

  3. This Calculate Rectangle Perimeter using Java Example shows how to calculate

  4. perimeter of Rectangle using it's length and width.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10.  

  11. public class CalculateRectPerimeter {

  12.  

  13. public static void main(String[] args) {

  14.  

  15. int width = 0;

  16. int length = 0;

  17.  

  18. try

  19. {

  20. //read the length from console

  21. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  22.  

  23. System.out.println("Please enter length of a rectangle");

  24. length = Integer.parseInt(br.readLine());

  25.  

  26. //read the width from console

  27. System.out.println("Please enter width of a rectangle");

  28. width = Integer.parseInt(br.readLine());

  29.  

  30.  

  31. }

  32. //if invalid value was entered

  33. catch(NumberFormatException ne)

  34. {

  35. System.out.println("Invalid value" + ne);

  36. System.exit(0);

  37. }

  38. catch(IOException ioe)

  39. {

  40. System.out.println("IO Error :" + ioe);

  41. System.exit(0);

  42. }

  43.  

  44. /*

  45. * Perimeter of a rectangle is

  46. * 2 * (length + width)

  47. */

  48.  

  49. int perimeter = 2 * (length + width);

  50.  

  51. System.out.println("Perimeter of a rectangle is " + perimeter);

  52. }

  53.  

  54. }

  55.  

  56. /*

  57. Output of Calculate Rectangle Perimeter using Java Example would be

  58. Please enter length of a rectangle

  59. 10

  60. Please enter width of a rectangle

  61. 15

  62. Perimeter of a rectangle is 50

  63. */

Calculate Rectangle Area using Java

  1. /*

  2. Calculate Rectangle Area using Java Example

  3. This Calculate Rectangle Area using Java Example shows how to calculate

  4. area of Rectangle using it's length and width.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10.  

  11. public class CalculateRectArea {

  12.  

  13. public static void main(String[] args) {

  14.  

  15. int width = 0;

  16. int length = 0;

  17.  

  18. try

  19. {

  20. //read the length from console

  21. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  22.  

  23. System.out.println("Please enter length of a rectangle");

  24. length = Integer.parseInt(br.readLine());

  25.  

  26. //read the width from console

  27. System.out.println("Please enter width of a rectangle");

  28. width = Integer.parseInt(br.readLine());

  29.  

  30.  

  31. }

  32. //if invalid value was entered

  33. catch(NumberFormatException ne)

  34. {

  35. System.out.println("Invalid value" + ne);

  36. System.exit(0);

  37. }

  38. catch(IOException ioe)

  39. {

  40. System.out.println("IO Error :" + ioe);

  41. System.exit(0);

  42. }

  43.  

  44. /*

  45. * Area of a rectangle is

  46. * length * width

  47. */

  48.  

  49. int area = length * width;

  50.  

  51. System.out.println("Area of a rectangle is " + area);

  52. }

  53.  

  54. }

  55.  

  56. /*

  57. Output of Calculate Rectangle Area using Java Example would be

  58. Please enter length of a rectangle

  59. 10

  60. Please enter width of a rectangle

  61. 15

  62. Area of a rectangle is 150

  63. */

Calculate Circle Perimeter using Java

  1. /*

  2. Calculate Circle Perimeter using Java Example

  3. This Calculate Circle Perimeter using Java Example shows how to calculate

  4. Perimeter of circle using it's radius.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10.  

  11. public class CalculateCirclePerimeterExample {

  12.  

  13. public static void main(String[] args) {

  14.  

  15. int radius = 0;

  16. System.out.println("Please enter radius of a circle");

  17.  

  18. try

  19. {

  20. //get the radius from console

  21. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  22. radius = Integer.parseInt(br.readLine());

  23. }

  24. //if invalid value was entered

  25. catch(NumberFormatException ne)

  26. {

  27. System.out.println("Invalid radius value" + ne);

  28. System.exit(0);

  29. }

  30. catch(IOException ioe)

  31. {

  32. System.out.println("IO Error :" + ioe);

  33. System.exit(0);

  34. }

  35.  

  36. /*

  37. * Perimeter of a circle is

  38. * 2 * pi * r

  39. * where r is a radius of a circle.

  40. */

  41.  

  42. //NOTE : use Math.PI constant to get value of pi

  43. double perimeter = 2 * Math.PI * radius;

  44.  

  45. System.out.println("Perimeter of a circle is " + perimeter);

  46. }

  47. }

  48.  

  49. /*

  50. Output of Calculate Circle Perimeter using Java Example would be

  51. Please enter radius of a circle

  52. 19

  53. Perimeter of a circle is 119.38052083641213

  54. */

Calculate Circle Area using Java

  1. /*

  2. Calculate Circle Area using Java Example

  3. This Calculate Circle Area using Java Example shows how to calculate

  4. area of circle using it's radius.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.IOException;

  9. import java.io.InputStreamReader;

  10.  

  11. public class CalculateCircleAreaExample {

  12.  

  13. public static void main(String[] args) {

  14.  

  15. int radius = 0;

  16. System.out.println("Please enter radius of a circle");

  17.  

  18. try

  19. {

  20. //get the radius from console

  21. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  22. radius = Integer.parseInt(br.readLine());

  23. }

  24. //if invalid value was entered

  25. catch(NumberFormatException ne)

  26. {

  27. System.out.println("Invalid radius value" + ne);

  28. System.exit(0);

  29. }

  30. catch(IOException ioe)

  31. {

  32. System.out.println("IO Error :" + ioe);

  33. System.exit(0);

  34. }

  35.  

  36. /*

  37. * Area of a circle is

  38. * pi * r * r

  39. * where r is a radius of a circle.

  40. */

  41.  

  42. //NOTE : use Math.PI constant to get value of pi

  43. double area = Math.PI * radius * radius;

  44.  

  45. System.out.println("Area of a circle is " + area);

  46. }

  47. }

  48.  

  49. /*

  50. Output of Calculate Circle Area using Java Example would be

  51. Please enter radius of a circle

  52. 19

  53. Area of a circle is 1134.1149479459152

  54. */

Iphone Wallpapers 8
















Iphone Wallpapers 7












Iphone Wallpapers 6














Iphone Wallpapers 5











Iphone Wallpapers 4