Learn Programming through Logical Thinking – Series – 4

Learn Programming through Logical Thinking – Series – 4

This blog explains about Learn Programming through Logical Thinking – Series – 4 and is below :

_______________________________________________________________________________

This is Learn Programming through Logical Thinking Series – 4.  In this series, we are converting the logical problems into Java Programs.  We are discussing about “Rearranging letters in a given word” problem in this blog post.

Rearranging Letters Problem with Answer in Java

How many different ways can the letter of the word “leading” be arranged in such a way that the vowels always come together?

Algorithm: 

Step 1: Get the word “leading” from the user.

Step 2: Separate the “vowels” and “consonants”.

Step 3: Identify the vowels (“eai”) and consonants (“ldng”).

Step 4: Vowels are always together.  Thus they can be formed as one letter.  So their value is ‘1’ Consonant values cannot be combined and Hence their value is ‘4’.

Step 5: Now add combined and non combined values(4+1=5).  Thus the chance will become 5!=5*4*3*2*1.

Step 6: At the same time, within vowels also we have chances of 3!=3*2*1.

Step 7: We can conclude now as number of chances is 5!*3!=720.

Learn Programming through Logical Thinking Series – 4: Rearranging Letters Problem with Answer in Java

package com.thilack;

public class LeadingDemo {

                public static void main(String[] args) {

                 //String a = “LEADING”; Given String

                 int i;

                 int num=1,num1=1,temp;

                 int vowels=3;

                 //int consonant=4;

                 int consonants=5;//Add Combined And Non Combined Values Add (4+1=5)

                 for(i=1;i<=consonants;i++)

                                 {

                                num=num*i;

                                }

                 System.out.println(“Factorial of 5! is: “+num);

                 for(i=1;i<=vowels; i++)

                                 {

                                 num1=num1*i;

                                 }

                 System.out.println(“Factorial of 3! is: “+num1);

                 temp=num*num1;

     System.out.println(“Multiply 5!&3! chances are: “+temp);

                }

}

OUTPUT:

Factorial of 5! is: 120

Factorial of 3! is: 6

Multiply 5!&3! chances are: 720

_______________________________________________________________________________________