Java Tutorial

by Dinesh Thakur Category: Control Structures

Fibonacci Series is series of Natural Number in the Sequence of: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...., The first two number in Fibonacci series are 0 and 1, where next number is equivalent to sum of previous two number. in this example we use Recursion method , Recursion means calling the same function again and again to reduce the complexity of the problem solved.

 
by Dinesh Thakur Category: Control Structures

In this program, we first input the number (Say num = 12345). Next the control reaches the while loop where it checks the condition (num>0) which is true as (12345>0) so the body of the loop is executed.

 
by Dinesh Thakur Category: Control Structures

Algorithm for GCD of Two Numbers :

step 1:Read a, b

step 2: Repeat through step-5 while a not equals to 0

step 3: Set gcd=a

step 4: a=b%a

step 5:b=gcd

step 6: Print gcd

step 7: Exit

 
by Dinesh Thakur Category: Control Structures

This example computes the sum of positive numbers input by the user. When a negative number is input, the condition (num<0) become true and break statement is executed which leads to the termination of the while loop and the next statement following the loop is executed which displays the sum of positive numbers. The condition of the while loop always remains true as we have specified a non-zero value 1 which makes it run infinitely. The only way to exit this loop is to. use break statement.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Armstrong Number with in Range:

step 1:Read range

step 2: Set count=0, b= 1

step 3: Repeat through step-11 while b less than or equals to range

step 4: Set sum=0

step 5: Set c=b

step 6: Repeat through step-9 while c greater than 0

 
by Dinesh Thakur Category: Control Structures

Algorithm For Prime Numbers between Range:

step 1: Set num=1

step 2: Read range

step 3: Repeat through step-9 while num less than or equals to range

step 4: Initialize b= 1, c=0

 
by Dinesh Thakur Category: Control Structures

When there is requirement of several branching i.e. if we need several if statements, it is better to use switch statement. In other words, switch is a variation of if statement which performs multiway branching.

 
by Dinesh Thakur Category: Control Structures

In the switch statement, since same statements has to be executed corresponding to different cases ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') so we write the statements with the last case. The break statement causes the switch statement to terminate when any of the vowels is entered.

 
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.



 

Page 7 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.