Found 272 Articles for Java8

Find the 3rd smallest number in a Java array.

Kumar Varma
Updated on 12-Mar-2020 10:10:40

4K+ Views

ExampleFollowing is the required program.Live Demopublic class Tester {    public static int getThirdSmallest(int[] a) {       int temp;       //sort the array       for (int i = 0; i < a.length; i++) {          for (int j = i + 1; j < a.length; j++) {             if (a[i] > a[j]) {                temp = a[i];                a[i] = a[j];                a[j] = temp;             }          }       }       //return third smallest element       return a[2];    }    public static void main(String args[]) {       int a[] = { 11,10,4, 15, 16, 13, 2 };       System.out.println("Third Smallest: " +getThirdSmallest(a));    } }OutputThird smallest: 10

Factorial program in Java without using recursion.

Vikyath Ram
Updated on 18-Jun-2020 08:09:48

1K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int factorial(int n) {       if (n == 0)          return 1;       else          return (n * factorial(n - 1));    }    public static void main(String args[]) {       int i, fact = 1;       int number = 5;       fact = factorial(number);       System.out.println(number + "! = " + fact);    } }Output5! = 120

Prime number program in Java.

V Jyothi
Updated on 18-Jun-2020 07:00:54

1K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Fibonacci series program in Java using recursion.

V Jyothi
Updated on 05-Mar-2020 12:16:09

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int n1 = 0, n2 = 1, n3 = 0;    static void fibbonacci(int count) {       if (count > 0) {          n3 = n1 + n2;          n1 = n2;          n2 = n3;          System.out.print(" " + n3);          fibbonacci(count - 1);       }    }    public static void main(String args[]) {       int count = 5;       System.out.print(n1 + " " + n2);       fibbonacci(count - 2);    } }Output0 1 1 2 3

Fibonacci series program in Java without using recursion.

Syed Javed
Updated on 18-Jun-2020 06:48:02

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {        int n1 = 0, n2 = 1, n3, i, max = 5;        System.out.print(n1 + " " + n2);        for (i = 2; i < max; ++i) {           n3 = n1 + n2;           System.out.print(" " + n3);           n1 = n2;           n2 = n3;        }     }  }Output0 1 1 2 3

break, continue and label in Java loop

karthikeya Boyini
Updated on 17-Jun-2020 06:57:05

3K+ Views

Following example showcases labels 'first', 'second' before a for statement and use the break/continue controls to jump to that label. See the example below.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       first:          for (int i = 0; i < 3; i++) {             for (int j = 0; j< 3; j++){                if(i == 1){                   continue first;                }         ... Read More

branching statement in Java

Kumar Varma
Updated on 30-Jul-2019 22:30:22

436 Views

Java programming language provides following types of decision making or branching statements.Java programming language provides following types of decision making statements.Sr.No.Statement & Description1if statementAn if statement consists of a boolean expression followed by one or more statements.2if...else statementAn if statement can be followed by an optional else statement, which executes when the boolean expression is false.3nested if statementYou can use one if or else if statement inside another if or else if statement(s).4switch statementA switch statement allows a variable to be tested for equality against a list of values.

Incremental Java infinite loop

Abhinanda Shri
Updated on 12-Mar-2020 12:18:35

119 Views

ExampleFollowing is the required program −Live Demopublic class Tester {    public static void main(String args[]) {       int i = 0;       do {          i++;          System.out.println(i);       }while (true);    } }The output will keep printing numbers in sequential order.

Infinite while loop in Java

Syed Javed
Updated on 27-Feb-2020 05:25:01

436 Views

Yes. Following while loop is a valid statement and causes an infinite loop.while(true);

Java labelled for loop

Vikyath Ram
Updated on 15-Jun-2020 09:14:32

956 Views

Following program is using labeled for loops.ExampleLive Demopublic class Tester {    public static void main(String args[]) {             first:          for (int i = 0; i < 3; i++) {             for (int j = 0; j< 3; j++){                if(i == 1){                   continue first;                }                      System.out.print(" [i = " + i + ", ... Read More

Advertisements