Found 74 Articles for Java Technologies

Java program to print the factorial of the given number

Ankith Reddy
Updated on 13-Mar-2020 05:36:50

4K+ Views

Factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). Algorithm1. Take integer variable A 2. Assign a value to the variable 3. From value, A up to 1 multiply each digit and store 4. The final stored value is factorial of AExampleimport java.util.Scanner;    public class Factorial {       public static void main(String args[]){          int i, factorial=1, number;          System.out.println("Enter the number to which you need to find the factorial:");          Scanner sc = new Scanner(System.in);          number = sc.nextInt();          for(i = 1; i

Match all occurrences of a regex in Java

Arnab Chakraborty
Updated on 23-Jun-2020 14:46:06

139 Views

public class RegexOccur {    public static void main(String args[]) {       String str = "java is fun so learn java";       String findStr = "java";       int lastIndex = 0;       int count = 0;       while(lastIndex != -1) {          lastIndex = str.indexOf(findStr,lastIndex);          if(lastIndex != -1) {             count ++;             lastIndex += findStr.length();          }       }       System.out.println(count);    } }Output2

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

423 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

115 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

428 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

933 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

Java labelled statement

Kumar Varma
Updated on 15-Jun-2020 09:06:36

2K+ Views

Yes. Java supports labeled statements. You can put a label before a for statement and use the break/continue controls to jump to that label. ExampleSee the example below.Live 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

Java switch statement

Kumar Varma
Updated on 15-Jun-2020 07:38:32

218 Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.SyntaxThe syntax of enhanced for loop is −switch(expression) {    case value :       // Statements       break; // optional        case value :       // Statements       break; // optional        // You can have any number of case statements.    default : // Optional       // Statements }The following rules apply to ... Read More

Differences between | and || operators in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:21

3K+ Views

| is a bitwise operator and compares each operands bitwise.It is a binary OR Operator and copies a bit to the result it exists in either operands.Assume integer variable A holds 60 and variable B holds 13 then  (A | B) will give 61 which is 0011 1101.Whereas || is a logical OR operator and operates on boolean operands. If both the operands are false, then the condition becomes false otherwise it is true. Assume boolean variable A holds true and variable B holds false then (A && B) is true.| is to be used during bitwise operations and || is useful during logical operations.

Advertisements