Skip to content

Commit 9e8c62c

Browse files
committed
Add More Bonus Problems
1 parent 3e238ad commit 9e8c62c

File tree

15 files changed

+415
-7
lines changed

15 files changed

+415
-7
lines changed

BONUS/Arrays/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
Some more problems on arrays!
44

5-
To Be Added...
5+
## Questions
6+
7+
1. [WAP to reverse the given arary](./Reverse/)

BONUS/Arrays/Reverse/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Array Reversal
2+
3+
WAP to reverse the given array
4+
5+
## Solution
6+
7+
### [Java Implementation](./Reverse.java)
8+
9+
```java
10+
/**
11+
* Array Reversal
12+
* @author MadhavBahlMD
13+
* @date 26/01/2019
14+
*/
15+
16+
import java.util.Scanner;
17+
18+
public class Reverse {
19+
public static void inputArr (int arr[], int num) {
20+
Scanner input = new Scanner(System.in);
21+
System.out.println("Enter the elements: ");
22+
for (int i=0; i<num; i++) {
23+
System.out.print("arr[" + i + "] = ");
24+
arr[i] = input.nextInt();
25+
}
26+
}
27+
28+
public static void printArr (int arr[], int num) {
29+
for (int i=0; i<num; i++)
30+
System.out.print(arr[i] + " ");
31+
}
32+
33+
public static void reverse (int arr[], int num) {
34+
int temp;
35+
for (int i=0; i<=num/2; i++) {
36+
temp = arr[i];
37+
arr[i] = arr[num-i-1];
38+
arr[num-i-1] = temp;
39+
}
40+
}
41+
42+
public static void main(String[] args) {
43+
Scanner input = new Scanner(System.in);
44+
System.out.println("/* ===== bubble Sort ===== */");
45+
46+
// Input the array
47+
System.out.print("\nEnter the number of elements in the array: ");
48+
int num = input.nextInt();
49+
int arr[] = new int[num];
50+
inputArr(arr, num);
51+
52+
// Print the array
53+
System.out.print("\nOriginal Array: ");
54+
printArr(arr, num);
55+
56+
// Reverse the array
57+
reverse(arr, num);
58+
59+
// Print the array
60+
System.out.print("\n\nReversed Array: ");
61+
printArr(arr, num);
62+
}
63+
}
64+
```

BONUS/Arrays/Reverse/Reverse.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Array Reversal
3+
* @author MadhavBahlMD
4+
* @date 26/01/2019
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class Reverse {
10+
public static void inputArr (int arr[], int num) {
11+
Scanner input = new Scanner(System.in);
12+
System.out.println("Enter the elements: ");
13+
for (int i=0; i<num; i++) {
14+
System.out.print("arr[" + i + "] = ");
15+
arr[i] = input.nextInt();
16+
}
17+
}
18+
19+
public static void printArr (int arr[], int num) {
20+
for (int i=0; i<num; i++)
21+
System.out.print(arr[i] + " ");
22+
}
23+
24+
public static void reverse (int arr[], int num) {
25+
int temp;
26+
for (int i=0; i<=num/2; i++) {
27+
temp = arr[i];
28+
arr[i] = arr[num-i-1];
29+
arr[num-i-1] = temp;
30+
}
31+
}
32+
33+
public static void main(String[] args) {
34+
Scanner input = new Scanner(System.in);
35+
System.out.println("/* ===== bubble Sort ===== */");
36+
37+
// Input the array
38+
System.out.print("\nEnter the number of elements in the array: ");
39+
int num = input.nextInt();
40+
int arr[] = new int[num];
41+
inputArr(arr, num);
42+
43+
// Print the array
44+
System.out.print("\nOriginal Array: ");
45+
printArr(arr, num);
46+
47+
// Reverse the array
48+
reverse(arr, num);
49+
50+
// Print the array
51+
System.out.print("\n\nReversed Array: ");
52+
printArr(arr, num);
53+
}
54+
}

BONUS/Misc/MenuAdd/MenuAdd.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Scanner;
2+
3+
class MenuAdd {
4+
public static void main(String[] args) {
5+
Scanner input = new Scanner (System.in);
6+
char choice = 'n';
7+
8+
do {
9+
System.out.print("Enter the first number: ");
10+
int n1 = input.nextInt();
11+
System.out.print("Enter the second number: ");
12+
int n2 = input.nextInt();
13+
14+
System.out.println(n1 + " + " + n2 + " = " + (n1+n2));
15+
16+
System.out.println("do you want to continue? (y/n)");
17+
choice = input.next().charAt(0);
18+
} while (choice == 'y');
19+
}
20+
}

BONUS/Misc/MenuAdd/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Menu Add
2+
3+
Provide the option of adding two numbers to the user until the user wants to exit
4+
5+
## Solution
6+
7+
### [Java Implementation](./MenuAdd.java)
8+
9+
```java
10+
import java.util.Scanner;
11+
12+
class MenuAdd {
13+
public static void main(String[] args) {
14+
Scanner input = new Scanner (System.in);
15+
char choice = 'n';
16+
17+
do {
18+
System.out.print("Enter the first number: ");
19+
int n1 = input.nextInt();
20+
System.out.print("Enter the second number: ");
21+
int n2 = input.nextInt();
22+
23+
System.out.println(n1 + " + " + n2 + " = " + (n1+n2));
24+
25+
System.out.println("do you want to continue? (y/n)");
26+
choice = input.next().charAt(0);
27+
} while (choice == 'y');
28+
}
29+
}
30+
```

BONUS/Misc/OddEve/OddEven.class

961 Bytes
Binary file not shown.

BONUS/Misc/Patterns/Pattern1.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Scanner;
2+
3+
class Pattern1 {
4+
public static void main(String[] args) {
5+
Scanner input = new Scanner (System.in);
6+
System.out.print("Enter n: ");
7+
int n = input.nextInt();
8+
9+
for (int i=1; i<=n; i++) {
10+
for (int j=1; j<=i; j++)
11+
System.out.print("*");
12+
System.out.println("");
13+
}
14+
}
15+
}

BONUS/Misc/Patterns/Pattern2.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
3+
class Pattern2 {
4+
public static void main(String[] args) {
5+
Scanner input = new Scanner (System.in);
6+
System.out.print("Enter n: ");
7+
int n = input.nextInt();
8+
9+
for (int i=n; i>=1; i--) {
10+
for (int j=1; j<=i; j++)
11+
System.out.print(j);
12+
System.out.println("");
13+
}
14+
}
15+
}

BONUS/Misc/Patterns/Pattern3.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
class Pattern3 {
4+
public static void main(String[] args) {
5+
Scanner input = new Scanner (System.in);
6+
System.out.print("Enter n: ");
7+
int n = input.nextInt();
8+
9+
// Upper Triangle
10+
for (int i=1; i<=n; i++) {
11+
for (int j=1; j<=i; j++)
12+
System.out.print(j);
13+
System.out.println("");
14+
}
15+
16+
// Lower Triangle
17+
for (int i=n; i>=1; i--) {
18+
for (int j=1; j<=i; j++)
19+
System.out.print(j);
20+
System.out.println("");
21+
}
22+
}
23+
}

BONUS/Misc/Patterns/README.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Patterns
2+
3+
Print the following patterns
4+
5+
## Pattern 1
6+
7+
```
8+
input: 5
9+
output:
10+
*
11+
**
12+
***
13+
****
14+
*****
15+
```
16+
17+
### [Java Implementation](./Pattern1.java)
18+
19+
```js
20+
import java.util.Scanner;
21+
22+
class Pattern1 {
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner (System.in);
25+
System.out.print("Enter n: ");
26+
int n = input.nextInt();
27+
28+
for (int i=1; i<=n; i++) {
29+
for (int j=1; j<=i; j++)
30+
System.out.print("*");
31+
System.out.println("");
32+
}
33+
}
34+
}
35+
```
36+
37+
## Pattern 2
38+
39+
```
40+
input: n=4
41+
output:
42+
1234
43+
123
44+
12
45+
1
46+
```
47+
48+
### [Java Implementation](./Pattern2.java)
49+
50+
```java
51+
import java.util.*;
52+
53+
class Pattern2 {
54+
public static void main(String[] args) {
55+
Scanner input = new Scanner (System.in);
56+
System.out.print("Enter n: ");
57+
int n = input.nextInt();
58+
59+
for (int i=n; i>=1; i--) {
60+
for (int j=1; j<=i; j++)
61+
System.out.print(j);
62+
System.out.println("");
63+
}
64+
}
65+
}
66+
```
67+
68+
## Pattern 3
69+
70+
```
71+
input: n=4
72+
1
73+
12
74+
123
75+
1234
76+
1234
77+
123
78+
12
79+
1
80+
```
81+
82+
### [Java Implementation](./Pattern3.java)
83+
84+
```java
85+
import java.util.*;
86+
87+
class Pattern3 {
88+
public static void main(String[] args) {
89+
Scanner input = new Scanner (System.in);
90+
System.out.print("Enter n: ");
91+
int n = input.nextInt();
92+
93+
// Upper Triangle
94+
for (int i=1; i<=n; i++) {
95+
for (int j=1; j<=i; j++)
96+
System.out.print(j);
97+
System.out.println("");
98+
}
99+
100+
// Lower Triangle
101+
for (int i=n; i>=1; i--) {
102+
for (int j=1; j<=i; j++)
103+
System.out.print(j);
104+
System.out.println("");
105+
}
106+
}
107+
}
108+
```

BONUS/Misc/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ Some Miscellaneous Problems to practise! Keep Coding :)
44

55
## Questions
66

7-
1. [Odd/Even](./OddEve/)
8-
2. [Area of Circle](./CircleArea)
9-
3. [Check Leap Year](./LeapYear)
10-
4. [Sum Natural Numbers](./NaturalSum/)
11-
5. [Sum of Squares of N natural numbers](./SumSquares/)
7+
1. [WAP to find whether the given number is odd/even](./OddEve/)
8+
2. [WAP to find the area of Circle](./CircleArea)
9+
3. [WAP to check Leap Year](./LeapYear)
10+
4. [WAP to sum n natural numbers](./NaturalSum/)
11+
5. [WAP to find sum of squares of N natural numbers](./SumSquares/)
12+
6. [WAP to reverse the given array](./Reverse/)
13+
7. [WAP to add 2 numbers unti user explicitly quits](./MenuAdd/)
14+
8. [Pattern Based Problems](./Patterns/)

BONUS/Misc/Reverse/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Reverse
2+
3+
WAP to reverse the given array
4+
5+
## Solution
6+
7+
### [Java Implementation](./Reverse.java)
8+
9+
```java
10+
public static void reverse (int arr[], int num) {
11+
int temp;
12+
for (int i=0; i<=num/2; i++) {
13+
temp = arr[i];
14+
arr[i] = arr[num-i-1];
15+
arr[num-i-1] = temp;
16+
}
17+
}
18+
```

0 commit comments

Comments
 (0)