Java Tutorial

by Dinesh Thakur Category: Control Structures

Algorithm for Highest Prime Number:

step 1: Read num

step 2: Set i== 1

step 3: Initialize j==2

step 4: Repeat through step-7 while j less than or equals to num

step 5: If j equals to num then Set i==num and goto 8

step 6: If (num mod)) equals to 0 then num==num-1

 
by Dinesh Thakur Category: Control Structures

Like the nesting of if and other looping statements, the for loop can also be nested. In other words, we can have a for loop in the body of other for loop. There is no restriction on the level of nesting of loops but one should take care while nesting at multiple levels otherwise unexpected results may arise.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Right Angled Triangle:

 

step 1: Set i= 0,j 0, row=0,osc=0,var=0

step 2: Read row

step 3: Move to new line

step4: If (row>O) then execute following steps else goto step-23

step 5: Initialize isc=0

step 6: Print “*”

step 7: Initialize i=2

 
by Dinesh Thakur Category: Control Structures

The switch statement is a multi-way decision making statement which selects one of several alternatives based on the value of an integer variable or expression. The switch statement is mainly used to replace multiple if-else-if statement. The if-else-if statement causes performance degradation as several conditions need to be evaluated before a particular condition is satisfied.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Floyd Triangle:

step 1: Set a= 1

step 2: Read row

step 3: Initialize i=0

step 4: Repeat through step-12 until i less than row

step 5: Initialize j=0

step 6: Repeat through step-10 until j less than or equals to i

step 7: Print a

 
by Dinesh Thakur Category: Control Structures

Fibonacci series numbers are in the following Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144..., The first two number in Fibonacci series are 0 and 1, and each subsequent next number is the sum of the previous two Numbers.

 
by Dinesh Thakur Category: Control Structures

Factorial Number is the product of natural numbers from one to that particular number.  Mathematically, n! = 1 * 2 * 3 * .... * (n-1) * n

 
by Dinesh Thakur Category: Control Structures

In this example, we input the interval values a and b. Each time loop is executed the value of fx is calculated and displayed on the screen for the value of x which ranges from a to b. On each iteration, x is incremented by 0.05. This continues until x is less than equal to the value of b that user inputs.

 
by Dinesh Thakur Category: Control Structures

Stack-This class is a predefined class of java.uti1package. A stack represents a group of elements stored in LIFO (Last In Fast Out) order. This means that the element stored as a last element in the stack will be the first element to be removed from the stack.



 
by Dinesh Thakur Category: Control Structures

A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.

 
by Dinesh Thakur Category: Control Structures

The Java Example to check if a Number is a palindrome or not.  A Number is palindrome if it remains unchanged when it is reversed, for example "151" is a palindrome as its reverse is "151".

 
by Dinesh Thakur Category: Control Structures

There will be times when we will want to include more than one statement in the initialization and iteration sections of the For loop. For example, consider the loop in the following program:

 
by Dinesh Thakur Category: Control Structures

Nested loop means one loop inside the other. When a loop is written inside the other loop, a condition is essential to maintain: the inner loop will not cross the boundary of the outer loop. That is, the inner loop will begin and end inside the outer loop. We can have any number of loops one inside the other.

 
by Dinesh Thakur Category: Control Structures

step 1: Read n, x

step 2: Set r= 1

step 3: If value of n is 1 then print n and goto step - 8

step 4: Repeat through step-6 until j > 0

step 5: r=r*i

step 6: Update j as j=j-1

step 7: Print r

step 8: Exit

 
by Dinesh Thakur Category: Control Structures

In "PigLatin" a word such as KING is replaced by INGKAYand TROUBLE is replaced by OUBLETRAY and so on. The first vowel of the original word becomes the start of the translation, any proceeding letters being shifted towards the end and followed by AYwords that begin with vowels are left on changed.

 
by Dinesh Thakur Category: Control Structures

we have seen how to test whether a given number is prime or not. The program segment given below uses a for loop to test each number in a given range and print it if it is prime.

 
by Dinesh Thakur Category: Control Structures

Length():-This method is a predefined method of String class present in java. Lang package. It is used to determine the length of string and return type of this method is int type.



 
by Dinesh Thakur Category: Control Structures

When we have if or if else statement within another if construct or else construct, we call it nested if.

 

Page 8 of 40



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.