19 September 2020

# Study Anytime Any Where

19 September 2020

# We Will Open The Knowledge For You

19 September 2020

# Anish Sir 24x7 Learning solution

19 September 2020

# It Is Time to join JOB

## Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

Call +91 74 88 34 7779  | Email : anishsingh@live.com

Website URL: http://toshiyas.in
Tuesday, 18 July 2017 06:30

### Java Program to check for Disarium Number

#### Question:

Write a Program in Java to input a number and check whether it is a Disarium Number or not.

Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.

For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)

#### Programming Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 `/**` `* The class Disarium inputs a number and checks whether` `* it is a Disarium number or not` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.io.*;` `class` `Disarium` `    ``{` `    ``public` `static` `void` `main(String[] args)``throws` `IOException` `        ``{` `            ``BufferedReader br=``new` `BufferedReader (``new` `InputStreamReader(System.in));` `            ``System.out.print(``"Enter a number : "``);` `            ``int` `n = Integer.parseInt(br.readLine());` `            ``int` `copy = n, d = ``0``, sum = ``0``;` `            ``String s = Integer.toString(n); ``//converting the number into a String` `            ``int` `len = s.length(); ``//finding the length of the number i.e. no.of digits` `            `  `            ``while``(copy>``0``)` `            ``{` `                ``d = copy % ``10``; ``//extracting the last digit` `                ``sum = sum + (``int``)Math.pow(d,len);` `                ``len--;` `                ``copy = copy / ``10``;` `            ``}` `            `  `            ``if``(sum == n)` `                ``System.out.println(n+``" is a Disarium Number."``);` `            ``else` `                ``System.out.println(n+``" is not a Disarium Number."``);` `        ``}` `    ``}`

#### Output:

Enter a Number : 135
135 is a Disarium Number.

Enter a Number : 219
219 is not a Disarium Number.

Enter a Number : 89
89 is a Disarium Number.

Tuesday, 18 July 2017 06:28

### Program to Remove Duplicate Characters from a Word

#### Question:

Write a program to input a word from the user and remove the duplicate characters present in it.

Example:

INPUT – abcabcabc
OUTPUT – abc

INPUT – javaforschool
OUTPUT – javforschl

INPUT – Mississippi
OUTPUT – Misp

#### Programming Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 `/**` ` ``* The class RemoveDupChar inputs a word and removes duplicate characters` ` ``* @author : www.anishsir.in` ` ``* @Program Type : BlueJ Program - Java` ` ``*/`   `import` `java.io.*;` `class` `RemoveDupChar` `{` `    ``public` `static` `void` `main(String args[])``throws` `IOException` `    ``{` `        ``BufferedReader br = ``new` `BufferedReader(``new` `InputStreamReader(System.in));` `        ``System.out.print(``"Enter any word : "``);` `        ``String s = br.readLine();` `        ``int` `l = s.length();` `        ``char` `ch;` `        ``String ans=``""``;` `        `  `        ``for``(``int` `i=``0``; i

#### Output:

Example 1:
Enter any word : Mississippi
Word after removing duplicate characters : Misp

Example 2:
Enter any word : Attitude
Word after removing duplicate characters : Atiude

Tuesday, 18 July 2017 06:27

### Java program to find the GCD of two numbers (Division Method)

#### Question:

Write a Program in Java to input 2 numbers and find their Greatest Common Divisor (GCD).

Note: If the 2 numbers are 54 and 24, then the divisors (factors) of 54 are: 1, 2, 3, 6, 9, 18, 27, 54.

Similarly the divisors (factors) of 24 are: 1, 2, 3, 4, 6, 8, 12, 24.

The numbers that these two lists share in common are the common divisors (factors) of 54 and 24: 1, 2, 3, 6.

The greatest (highest) of these is 6. That is the greatest common divisor or the highest common factor of 54 and 24.

Calculating HCF / GCD by Prime Factorisation Method is long and more time-consuming, and due to these disadvantages new method was evolved by Mathematician namely, Successive Division Method

Under Successive Division Method, HCF / GCD = The Last Divisor of the given numbers .

Following example can guide you How to calculate HCF / GCD by Successive Division Method ?

Example : By using Successive Division Method, find the GCD of 24 & 18 ?

Answer : Steps and the way of finding GCD by Successive Division Method is as :-

Step 1 = Divide the larger number 24 by the smaller number 18. And this division will give remainder 6.
Step 2 = Now, divide 18 (divisor of step 1) with 6 (remainder of step 1)
Step 3 = Division in Step 2 give us remainder 0 (Zero). And The Last Divisor is the GCD of 24 & 18.
Hence, GCD = 6

 `18 | 24 | 1                 ` `     ``18` `   ``______  ` `           `  `      ``6 | 18 | 3           ` `          ``18 ` `        ``______`   `           ``0`

#### Programming Code [Method 1]:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 `/**` `* The class Gcd inputs two numbers and finds their Gcd` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `Gcd` `    ``{` `        ``public` `static` `void` `main(String args[])``throws` `Exception` `        ``{` `            ``Scanner sc = ``new` `Scanner(System.in);` `            ``System.out.print(``"Enter the First no : "``);` `            ``int` `n1=sc.nextInt();` `            ``System.out.print(``"Enter the Second no : "``);` `            ``int` `n2=sc.nextInt();` `            ``int` `r;` `            `  `            ``while``(n2 != ``0``)` `            ``{` `                ``r = n1 % n2;` `                ``n1 = n2;` `                ``n2 = r;` `            ``}` `            ``System.out.print(``"GCD = "``+n1);` `        ``}` `    ``}`

#### Programming Code [Method 2]:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 `/**` `* The class Gcd inputs two numbers and finds their Gcd` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `Gcd` `    ``{` `        ``public` `static` `void` `main(String args[])``throws` `Exception` `        ``{` `            ``Scanner sc = ``new` `Scanner(System.in);` `            ``System.out.print(``"Enter the First no : "``);` `            ``int` `n1=sc.nextInt();` `            ``System.out.print(``"Enter the Second no : "``);` `            ``int` `n2=sc.nextInt();` `            `  `            ``while``(n1 != n2)` `            ``{` `                ``if``(n1 > n2)` `                    ``n1 = n1-n2;` `                ``else` `                    ``n2 = n2-n1;` `            ``}` `            ``System.out.print(``"GCD = "``+n1);` `        ``}` `    ``}`

#### Output:

Enter the First no : 54
Enter the Second no : 24
GCD = 6

Tuesday, 18 July 2017 06:25

### Java program to check for Harshad Number (Niven Number)

#### Question:

Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or not..

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits.

Let’s understand the concept of Harshad Number through the following example:

• The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
• The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
• The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)

The first few Harshad numbers in base 10 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.

#### Programming Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 `/**` `* The class HarshadNumber inputs a number and checks if it a Harshad Number or not` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `HarshadNumber` `{` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``Scanner sc = ``new` `Scanner(System.in);` `        `  `        ``System.out.print(``"Enter a number : "``);` `        ``int` `n = sc.nextInt();` `        ``int` `c = n, d, sum = ``0``;` `        `  `        ``//finding sum of digits` `        ``while``(c>``0``)` `        ``{` `            ``d = c%``10``;` `            ``sum = sum + d;` `            ``c = c/``10``;` `        ``}` `        `  `        ``if``(n%sum == ``0``)` `            ``System.out.println(n+``" is a Harshad Number."``);` `        ``else` `            ``System.out.println(n+``" is not a Harshad Number."``);      ` `    ``}` `}`

#### Output:

 `Enter a number : 195` `195 is a Harshad Number.`   `Enter a number : 194` `194 is not a Harshad Number.`   `Enter a number : 190` `190 is a Harshad Number.`   `Enter a number : 111` `111 is a Harshad Number.`
Tuesday, 18 July 2017 06:22

### Java program to check for Pronic Number (Heteromecic Number)

#### Question:

Write a Program in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not.

Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).

The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.

#### Programming Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 `/**` `* The class HarshadNumber inputs a number and checks if it a Pronic Number or not` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `PronicNumber` `{` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``Scanner sc = ``new` `Scanner(System.in);` `        `  `        ``System.out.print(``"Enter a number : "``);` `        ``int` `n = sc.nextInt();` `        ``int` `flag = ``0``;` `   `  `        ``for``(``int` `i=``0``; i

#### Alternate Programming Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 `/**` `* The class HarshadNumber inputs a number and checks if it a Pronic Number or not` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `PronicNumber` `{` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``Scanner sc = ``new` `Scanner(System.in);` `        `  `        ``System.out.print(``"Enter a number : "``);` `        ``int` `n = sc.nextInt();` `        ``int` `k = (``int``)(Math.sqrt(n));` `        ``if``(k*(k+``1``)==n)` `            ``System.out.println(n+``" is a Pronic Number."``);` `        ``else` `            ``System.out.println(n+``" is not a Pronic Number."``);      ` `    ``}` `}`

#### Output:

 `Enter a number : 110` `110 is a Pronic Number.`   `Enter a number : 73` `73 is not a Pronic Number.`   `Enter a number : 342` `342 is a Pronic Number.`   `Enter a number : 15` `15 is not a Pronic Number.`
Tuesday, 18 July 2017 03:25

### Data Structure Interview Questions | Set 1

What is a Data Structure?
A data structure is a way of organizing the data so that the data can be used efficiently. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, B-trees are particularly well-suited for implementation of databases, while compiler implementations usually use hash tables to look up identifiers.

What are linear and non linear data Structures?

• Linear: A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array. Linked List, Stacks and Queues
• Non-Linear: A data structure is said to be linear if traversal of nodes is nonlinear in nature. Example: Graph and Trees.

What are the various operations that can be performed on different Data Structures?

• Insertion − Add a new data item in the given collection of data items.
• Deletion − Delete an existing data item from the given collection of data items.
• Traversal − Access each data item exactly once so that it can be processed.
• Searching − Find out the location of the data item if it exists in the given collection of data items.
• Sorting − Arranging the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data.

How is an Array different from Linked List?

• The size of the arrays is fixed, Linked Lists are Dynamic in size.
• Inserting and deleting a new element in an array of elements is expensive, Whereas both insertion and deletion can easily be done in Linked Lists.
• Random access is not allowed in Linked Listed.
• Extra memory space for a pointer is required with each element of the Linked list.
• Arrays have better cache locality that can make a pretty big difference in performance.

What is Stack and where it can be used?

Stack is a linear data structure which the order LIFO(Last In First Out) or FILO(First In Last Out) for accessing elements. Basic operations of stack are : Push, Pop , Peek

Applications of Stack:

1. Infix to Postfix Conversion using Stack
2. Evaluation of Postfix Expression
3. Reverse a String using Stack
4. Implement two stacks in an array
5. Check for balanced parentheses in an expression

What is a Queue, how it is different from stack and how is it implemented?

Queue is a linear structure which follows the order is First IFirst Out (FIFO) to access elements. Mainly the following are basic operations on queue: Enqueue, DequeueFront, Rear
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Both Queues and Stacks can be implemented using Arrays and Linked Lists.

What are Infix, prefix, Postfix notations?

• Infix notation: + Y – Operators are written in-between their operands. This is the usual way we write expressions. An expression such as
`   A * ( B + C ) / D`
• Postfix notation (also known as “Reverse Polish notation”): X Y Operators are written after their operands. The infix expression given above is equivalent to
`   A B C + * D/`
• Prefix notation (also known as “Polish notation”): + X Y Operators are written before their operands. The expressions given above are equivalent to
`   / * A + B C D`

What is a Linked List and What are its types?

A linked list is a linear data structure (like arrays) where each element is a separate object. Each element (that is node) of a list is comprising of two items – the data and a reference to the next node.Types of Linked List :

1. Singly Linked List : In this type of linked list, every node stores address or reference of next node in list and the last node has next address or reference as NULL. For example 1->2->3->4->NULL
2. Doubly Linked List : Here, here are two references associated with each node, One of the reference points to the next node and one to the previous node. Eg. NULL<-1<->2<->3->NULL
3. Circular Linked List : Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list. Eg. 1->2->3->1 [The next pointer of last node is pointing to the first]

Which data structures are used for BFS and DFS of a graph?

• Queue is used for BFS
• Stack is used for DFS. DFS can also be implemented using recursion (Note that recursion also uses function call stack).

Can doubly linked be implemented using a single pointer variable in every node?
Doubly linked list can be implemented using a single pointer.

How to implement a stack using queue?

A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways:

• Method 1 (By making push operation costly)
• Method 2 (By making pop operation costly)

How to implement a queue using stack?

A queue can be implemented using two stacks. Let queue to be implemented be q and stacks used to implement q be stack1 and stack2. q can be implemented in two ways:

• Method 1 (By making enQueue operation costly)
• Method 2 (By making deQueue operation costly)

Which Data Structure Should be used for implementiong LRU cache?

We use two data structures to implement an LRU Cache.

1. Queue which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size).The most recently used pages will be near front end and least recently pages will be near rear end.
2. A Hash with page number as key and address of the corresponding queue node as value. See

How to check if a given Binary Tree is BST or not?
If inorder traversal of a binary tree is sorted, then the binary tree is BST. The idea is to simply do inorder traversal and while traversing keep track of previous key value. If current key value is greater, then continue, else return false.

Monday, 17 July 2017 03:55

### Write a program to Delete a Tree.

To delete a tree we must traverse all the nodes of the tree and delete them one by one. So which traversal we should use – Inorder or Preorder or Postorder. Answer is simple – Postorder, because before deleting the parent node we should delete its children nodes first

We can delete tree with other traversals also with extra space complexity but why should we go for other traversals if we have Postorder available which does the work without storing anything in same time complexity.

For the following tree nodes are deleted in order – 4, 5, 2, 3, 1

`#include<stdio.h>`
`#include<stdlib.h>`

`/* A binary tree node has data, pointer to left child `
`   ``and a pointer to right child */`
`struct` `node `
`{`
`    `