Java Tutorial

by Dinesh Thakur Category: Control Structures

In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly. Java supports three jump statements: break, continue, and return.

 
by Dinesh Thakur Category: Control Structures

Suppose, the value entered is say 121. It is assigned to an integer variable num. Now, num is divided by all the numbers from 2 to 8 (because a number can be divided by 1 and itself) (% rehrrns remainder). If the number is divided by any number from 2 to 8, its remainder will come to be 0 and in that case the message "Number is not prime" is displayed on the screen. 121 is not divisible by 2 but is no doubt divisible by 3 which returns remainder as 0 and hence it is not a prime number.

 
by Dinesh Thakur Category: Control Structures

If sum of factorial of each digit of the number is same as the actual number then that number is called as strong number.

 
by Dinesh Thakur Category: Control Structures

A Perfect Number is sum of its positive divisors, excluding the number itself. The First Perfect number is 6, For example 6 is a perfect number as 1,2 and3 are its divisors and the sum of divisors= ( 1 + 2 + 3)  = 6.

 
by Dinesh Thakur Category: Control Structures

final: Variables are useful when you need to store information that can change as the program runs. However, there may be certain situations in the program in which the value of the variable should not be allowed to modify. It is accomplished using a particular type of variable known as the final variable. The final variable also called constant variable. It is a variable with a value that cannot modify during the execution of the program.



 
by Dinesh Thakur Category: Control Structures

In this example, we input number of elements n (i.e.) whose average is to be calculated. When for loop begins executing, the loop control variable i is declared and initialized to 1. Then the test condition (i<=n) is checked. As it is true in this case because (1<=5) and the statements in the body of the loop are executed which inputs the first number (5 in our case) and add this value to variable sum. Then the increment expression i++ increases the value of variable i by 1 (i+ 1=2). After one complete iteration, the test condition in the for loop is checked again which is true again as (2<=5) and the body of the loop is executed again. This process continues until the loop control variable (i) is incremented to 6. Now when the test condition (6<=5) is evaluated again it becomes false and the execution of for loop terminates and control transfers to the next statement following the for loop that calculates the average of n (5) numbers which is then displayed.

 
by Dinesh Thakur Category: Control Structures

In this example, the sum of first 10 natural numbers is displayed. First, input the value of n (10 in this case) i.e. number of natural numbers whose sum is to be calculated. Then, after initializing the variables i to 1 and sum to 0, we enter the do-while loop. The execution of the body of loop continues as long as condition (i<=n) evaluates to true. When variable 1’s is value becomes 11, the condition becomes false and this terminates the do-while loop and program execution continues with the next statement after the loop which displays the sum of first 10 natural numbers.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Check Number is Binary or Not:

step 1: Set r=0,c=0

step 2: Read num

step 3: Set b=num

step 4: Repeat through step-7 while num greater than 0

 
by Dinesh Thakur Category: Control Structures

Algorithm for Remove Vowels from a Sentence:

step 1: read String word

step 2: set char array c[]=word. to CharArray()

step 3: create new char array cc[] of size 80

 
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.

 

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.