Currently the definition of Greatest Common Divisor (GDC) can be formalized as well:
Let a, b and c nonzero integers, we say that c is a common divisor of a and b to c divides (write c | a) and c divides b (c | b). We call D (a, b) the set of all common divisors of a and b.
The code snippet below shows how to calculate the GDC two reported numbers:
Finalizer methods are almost the opposite of constructor methods. A constructor method is used to initialize an object, while finalizer methods are called just before the object is garbage-collected and its memory reclaimed. The syntax of the finalizer method is simply finalize(). The Object class defines a default finalizer method. To create a finalizer method, override the finalize() method using the following signature:
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.
In this example, the continue statement is placed in the body of inner for loop. While executing inner for loop, if the condition (m==n) evaluates to true then the continue statement is executed and the remaining statement for displaying values of m and n is skipped and control is transferred to the increment expression (n++) of inner for loop. This increments the value of n by 1 and test condition (n<=2) is evaluated again for this incremented value of n. This process continues. We have used continue statement in this program so that the same value of m and n should not be displayed.
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.
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.
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.
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.
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.
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.
The Random class provides a template for the creation of random number generators. The Math.random() method provides a static function for the generation of random double values.
Two constructors are provided for creating Random objects. The default constructor initializes the seed of the random number generator using the current system time. The other constructor allows the seed to be set to an initial long value.
The currentTimeMillis() method returns the current time in milliseconds since 00:00:00,January I, 1970.This method is used to know the time interval a process or job takes to perform. We record the time before and after the operation and the difference of two timings tells us the total time the operation took.
We can change the format of the date in two steps: First, we create a formatter with the getDateInstance method. Then, we invoke the format method, which returns a String containing the formatted date.
The Calendar class is an abstract class used to convert dates. We can use this class to convert a Date object to fields, such as YEAR, MONTH, HOUR, and so on. We can also use these fields to update a Date object.
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.
Algorithm for Number of Times a Word has Been Mentioned :
step 1: read String w
step 2: read String w1
step 3: set len=w1.length()
step 4: set result=0
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
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.
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:
Java supports three jump statements: break, continue, and return. These statements transfer control to another part of the program.
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.
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.
In this example we use BufferedReader class for Reading text from a character-input stream. After Enter any 5 Number we check if number ==0 then is zero if number greater than 0 then it is positive other wise negative.
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.
The for loop enables code to execute repeatedly until a boolean expression evaluates to false.
The do loop enables us to repeatedly execute a block of code until a boolean expression evaluates to false. It is almost identical to the while loop with the difference that here the logical expression is evaluated at the bottom of the loop rather than the top. This means that the contents of the loop will execute at least once.
The while loop is a construct that repeatedly executes a block of code as long as a boolean condition remains true. The logical expression in while loop is evaluated first. If the logical expression evaluates to false, the body of while loop will not execute even once. If the logical expression in while evaluates to true, the statements in the body of while loop are executed. After executing the body, control jumps again at the top to recheck whether the boolean expression is still true or not. The body of the loop will continue to execute until the expression evaluates to false. When the logical expression becomes false, control continues the execution with the statements following while loop.
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.
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.
When we have if or if else statement within another if construct or else construct, we call it nested if.
A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.
This statement helps in choosing one set of statement out of two sets depending on the validity of the logical expression included. It is also known as conditional branch statement as it is used to route program execution through two different paths.
Algorithm for Java Sum of Digits :
step 1: Set sum=0
step 2: Read num
step 3: Repeat through step-5 while num greater than 0
step 4: temp=num % 10
step 5: sum=sum+temp
Algorithm for Digits Are Present in the Number:
step 1: Set c=0
step 2: Read num
step 3: Repeat through step-5 while num greater than 0
step 4: c=c+1
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
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
Algorithm for Triangle of any size:
step 1: set i=0,j=0,row=0,osc=0,var=0
step 2: read row
step 3: if row greater than 0 then execute following codes otherwise goto
step-33
step 4: osc=row-l
step 5: set isc= 1
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
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
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
Algorithm for Swap Variables in a Different Way:
step 1: Read a, b
step 2: a=(a+b)-(b=a)
step 3: Print a,b
step 4: Exit
Algorithm for Swap Two Variables Using XOR Operator:
step 1: Read a, b
step 2: a=a^b
step 3: b=b^a
step 4: a=a^b
step 5: Print a,b
step 6: Exit
If sum of factorial of each digit of the number is same as the actual number then that number is called as strong number.
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
Algorithm for Armstrong Number:
step 1:Set s=0
step 2: Read num
step 3: Set b=num
step 4: Repeat through step-7 while num greater than 0
step 5: r=num%10
step 6: s=s+(r*r*r)
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
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.
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.
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".
Factorial Number is the product of natural numbers from one to that particular number. Mathematically, n! = 1 * 2 * 3 * .... * (n-1) * n
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.
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.
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
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).
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.