Java Tutorial

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

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.

 
by Dinesh Thakur Category: Control Structures

Java.io.Console class is a new class in Java 6 that may be used for reading text from character based console device. The data can be read from the console device provided it is associated with the Java Virtual Machine (JVM).

 
by Dinesh Thakur Category: Control Structures

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

 
by Dinesh Thakur Category: Control Structures

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

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

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

 
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

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

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

 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

 
by Dinesh Thakur Category: Control Structures

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

 
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

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 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)

 
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

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

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

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

 

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