by Dinesh Thakur Category: Control Structures

Different types of control statements: the decision making statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and branching statements (break, continue and return).

 
by Dinesh Thakur Category: Control Structures

Java supports three jump statements: break, continue, and return. These statements transfer control to another part of the program.

 
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

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

final – constant declaration.

 

finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

 
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

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

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

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

 

Page 1 of 7



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.



Related Articles