Archive by Author | jananidhanween

Question 7

Print First N Alternate Prime Numbers

N – range of alternate prime numbers to be printed

Algorithm:

#1 Get the input range

#2 Initialize the number with ‘2’(since prime number starts with 2)

#3 Loop within the input range

#4 Check whether the number is prime

#5 If not prime break the loop

#6 Else it is a prime and check if its an alternate prime number, then add it to the output array

Question 6

Print First N Prime numbers

N – range of prime numbers to be printed

Algorithm:

#1 Get the input range

#2 Initialize the number with ‘2’(since prime number starts with 2)

#3 Loop within the input range

#4 Check whether the number is prime

#5 If not prime break the loop

#6 Else it is a prime add it to the output

 

Question 5

Check a given number is prime or not

Prime number: A number which is divisible by 1 and by itself.

Note: 1 is not a prime number. Prime number starts with ‘2’

Algorithm:

#1 Get the input number

#2 Initialize the loop variable by 2 and continue till the square root of the number

#3 If the input number is exactly divisible by loop variable then its not a prime number, hence break the loop

#4 Else the given number is prime

Question 4

Remove the duplicate elements in an integer array

Algorithm:

  1. Create an empty LinkedHashSet
  2. Loop(iterate) the input array till there are elements
  3. Add the iterated elements into the set
  4. End the loop
  5. Convert the set to an output array
  6. Print the output array

Example:

Input: {1,4,5,8,5,2}(Array)

Output:{1,4,5,8,2}(Array)

 

Question 3

Vowel Count

Find the number of vowels in a given string.

Input: Onion

Output:3(hint:’O’ & ‘o’ are of different cases so count is 2 and ‘i’ count is 1. Hence total count is 3)

NOTE: case sensitive

Question 2

Vowel Count

Find the number of vowels in a given string irrespective of case.

Input: Onion

Output:2(hint:’O’ & ‘o’ denotes the same vowel, so count is 1 and ‘i’ count is 1. Hence total count is 2)

NOTE: irrespective of case, case insensitive, ignore case all the term holds the same meaning

Question 1

LCM – Least Common Multiple of both the numbers

Rules:

  • Have any common multiple except 1
  • Always greater than or equal to the maximum number

Algorithm:

#1.Find the maximum and minimum number

#2.Loop from 1 till the minimum number

#3.Find the multiple of the maximum number

#4.Check the multiple of maximum number is also a multiple of minimum number

#5.If the multiple is exactly divisible, then it is the LCM and return from the loop

#6.If the multiple is not exactly divisible, then continue with the loop

Explanation:

Ex: LCM(5,7)

7=1*7(7),2*7(14),3*7(21),4*7(28),5*7(35)

5=1*5(5),2*5(10),3*5(15),4*5(20),5*5(25),6*5(30),7*5(35)

LCM(5,7)=35

max=7, min=5

for(int i=1;i<min;i++) multiple=i*max If(multiple%min==0) lcm=multiple
1;1<=5;1++ 1*7=7 7%5=2==0(false)
2;2<=5;2++ 2*7=14 14%5=4==0(false)
3;3<=5;3++ 3*7=21 21%5=1==0(false)
4;4<=5;4++ 4*7=28 28%5=3==0(false)
5;5<=5;5++ 5*7=35 35%5=0==0(true) lcm=35