Skip to content

Commit 3e238ad

Browse files
committed
Add Misc questions
1 parent 9e8aa0f commit 3e238ad

File tree

19 files changed

+385
-1
lines changed

19 files changed

+385
-1
lines changed

BONUS/Misc/CircleArea/CircleArea.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 25/01/2019
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class CircleArea {
9+
public static double area (double radius) {
10+
return Math.PI*radius*radius;
11+
}
12+
13+
public static void main(String[] args) {
14+
Scanner input = new Scanner(System.in);
15+
System.out.println("/* ===== Area of Circle ===== */");
16+
System.out.print("\nEnter the radius: ");
17+
double radius = input.nextDouble();
18+
System.out.println("Area of circle is: " + area(radius));
19+
}
20+
}

BONUS/Misc/CircleArea/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Circle Area
2+
3+
WAP to read the radius and print the area of a circle
4+
5+
## Solution
6+
7+
### [Java Implementation](./CircleArea.java)
8+
9+
```java
10+
/**
11+
* @author MadhavBahlMD
12+
* @date 25/01/2019
13+
*/
14+
15+
import java.util.Scanner;
16+
17+
public class CircleArea {
18+
public static double area (double radius) {
19+
return Math.PI*radius*radius;
20+
}
21+
22+
public static void main(String[] args) {
23+
Scanner input = new Scanner(System.in);
24+
System.out.println("/* ===== Area of Circle ===== */");
25+
System.out.print("\nEnter the radius: ");
26+
double radius = input.nextDouble();
27+
System.out.println("Area of circle is: " + area(radius));
28+
}
29+
}
30+
```

BONUS/Misc/LeapYear/LeapYear.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 26/01/2019
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class LeapYear {
9+
public static Boolean checkLeapYear (int year) {
10+
if ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) return true;
11+
else return false;
12+
}
13+
14+
public static void main(String[] args) {
15+
Scanner input = new Scanner (System.in);
16+
System.out.println("/* ===== Check Leap Year ===== */");
17+
System.out.print("\nEnter the year: ");
18+
int year = input.nextInt();
19+
20+
if (checkLeapYear(year)) System.out.println("Yes, " + year + " is a Leap Year!");
21+
else System.out.println("No, " + year + " is not a leap year!");
22+
}
23+
}

BONUS/Misc/LeapYear/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Leap Year
2+
3+
WAP to check whether the given year is a leap year or not
4+
5+
## Solution
6+
7+
### [Java Implementation](./LeapYear.java)
8+
9+
```java
10+
/**
11+
* @author MadhavBahlMD
12+
* @date 26/01/2019
13+
*/
14+
15+
import java.util.Scanner;
16+
17+
public class LeapYear {
18+
public static Boolean checkLeapYear (int year) {
19+
if ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) return true;
20+
else return false;
21+
}
22+
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner (System.in);
25+
System.out.println("/* ===== Check Leap Year ===== */");
26+
System.out.print("\nEnter the year: ");
27+
int year = input.nextInt();
28+
29+
if (checkLeapYear(year)) System.out.println("Yes, " + year + " is a Leap Year!");
30+
else System.out.println("No, " + year + " is not a leap year!");
31+
}
32+
}
33+
```

BONUS/Misc/NaturalSum/NaturalSum.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Natural Sum using recursion
3+
* @author MadhavBahlMD
4+
* @date 26/01/2019
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class NaturalSum {
10+
public static int naturalSum (int n) {
11+
if (n <= 0) return 0;
12+
else return n+naturalSum(n-1);
13+
}
14+
15+
public static void main(String[] args) {
16+
Scanner input = new Scanner(System.in);
17+
System.out.println("/* ===== Sum N Natural Numbers ===== */");
18+
System.out.print("\nEnter a natural number n: ");
19+
int n = input.nextInt();
20+
System.out.println("The sum of first " + n + " natural numbers = " + naturalSum(n));
21+
}
22+
}

BONUS/Misc/NaturalSum/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Natural Sum
2+
3+
WAP to sum first `n` natural numbers
4+
5+
## Solution
6+
7+
### [Java Solution](./NaturalSum.java)
8+
9+
```java
10+
/**
11+
* Natural Sum using recursion
12+
* @author MadhavBahlMD
13+
* @date 26/01/2019
14+
*/
15+
16+
import java.util.Scanner;
17+
18+
public class NaturalSum {
19+
public static int naturalSum (int n) {
20+
if (n <= 0) return 0;
21+
else return n+naturalSum(n-1);
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner input = new Scanner(System.in);
26+
System.out.println("/* ===== Sum N Natural Numbers ===== */");
27+
System.out.print("\nEnter a natural number n: ");
28+
int n = input.nextInt();
29+
System.out.println("The sum of first " + n + " natural numbers = " + naturalSum(n));
30+
}
31+
}
32+
```

BONUS/Misc/OddEve/OddEven.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* @author MadhavBahlMD
5+
* @date 25/01/2019
6+
*/
7+
8+
public class OddEven {
9+
public static void main(String[] args) {
10+
Scanner input = new Scanner (System.in);
11+
System.out.println("/* ===== Check Odd/Even ===== */");
12+
System.out.print("\nEnter the number: ");
13+
int num = input.nextInt();
14+
15+
if (num%2 == 0) System.out.println(num + " is Even!");
16+
else System.out.println(num + " is Odd!");
17+
}
18+
}

BONUS/Misc/OddEve/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Odd Even
2+
3+
Give a number, write a program to check whether it is odd or even
4+
5+
## Solution
6+
7+
### [Java Implementation](./OddEven.java)
8+
9+
```java
10+
import java.util.Scanner;
11+
12+
/**
13+
* @author MadhavBahlMD
14+
* @date 25/01/2019
15+
*/
16+
17+
public class OddEven {
18+
public static void main(String[] args) {
19+
Scanner input = new Scanner (System.in);
20+
System.out.println("/* ===== Check Odd/Even ===== */");
21+
System.out.print("\nEnter the number: ");
22+
int num = input.nextInt();
23+
24+
if (num%2 == 0) System.out.println(num + " is Even!");
25+
else System.out.println(num + " is Odd!");
26+
}
27+
}
28+
29+
```

BONUS/Misc/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Bonus Problems -- Miscellaneous
2+
3+
Some Miscellaneous Problems to practise! Keep Coding :)
4+
5+
## Questions
6+
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/)

BONUS/Misc/SumSquares/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sum Squares
2+
3+
WAP to print the sum of the series 12+22+32 up to n terms
4+
5+
## Solution
6+
7+
### [Java Implementation](./SumSquares.java)
8+
9+
```java
10+
import java.util.Scanner;
11+
12+
public class SumSquares {
13+
public static int sum (int n) {
14+
if (n <= 0) return 0;
15+
else return (n*n) + sum(n-1);
16+
}
17+
18+
public static void main(String[] args) {
19+
Scanner input = new Scanner(System.in);
20+
System.out.println("/* ===== Sum of squares of n natural numbers ===== */");
21+
System.out.print("\nEnter the number n: ");
22+
int n = input.nextInt();
23+
System.out.println("sum of squares of " + n + " natural numbers = " + sum(n));
24+
}
25+
}
26+
```

BONUS/Misc/SumSquares/SumSquares.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Sum of squares
3+
* @author MadhavBahlMD
4+
* @date 26/01/2019
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class SumSquares {
10+
public static int sum (int n) {
11+
if (n <= 0) return 0;
12+
else return (n*n) + sum(n-1);
13+
}
14+
15+
public static void main(String[] args) {
16+
Scanner input = new Scanner(System.in);
17+
System.out.println("/* ===== Sum of squares of n natural numbers ===== */");
18+
System.out.print("\nEnter the number n: ");
19+
int n = input.nextInt();
20+
System.out.println("sum of squares of " + n + " natural numbers = " + sum(n));
21+
}
22+
}

BONUS/OOPS/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Bonus Problems -- Miscellaneous
2+
3+
Some Miscellaneous Problems to practise! Keep Coding :)
4+
5+
## Questions
6+
7+
To be added!

BONUS/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ In case you just want to contribute a question and not code, there is no need to
8585
### [9. Graphs](./Graphs/README.md)
8686

8787
- To be added
88+
89+
### [10. Miscellaneous](./Misc/README.md)
90+
91+
1.
92+
93+
### [11. Object Oriented Programming](./OOPS/README.md)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Natural Sum using recursion
3+
* @author MadhavBahlMD
4+
* @date 26/01/2019
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class NaturalSum {
10+
public static int naturalSum (int n) {
11+
if (n <= 0) return 0;
12+
else return n+naturalSum(n-1);
13+
}
14+
15+
public static void main(String[] args) {
16+
Scanner input = new Scanner(System.in);
17+
System.out.println("/* ===== Sum N Natural Numbers ===== */");
18+
System.out.print("\nEnter a natural number n: ");
19+
int n = input.nextInt();
20+
System.out.println("The sum of first " + n + " natural numbers = " + naturalSum(n));
21+
}
22+
}

BONUS/Recursion/NaturalSum/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Natural Sum
2+
3+
WAP to sum first `n` natural numbers
4+
5+
## Solution
6+
7+
### [Java Solution](./NaturalSum.java)
8+
9+
```java
10+
/**
11+
* Natural Sum using recursion
12+
* @author MadhavBahlMD
13+
* @date 26/01/2019
14+
*/
15+
16+
import java.util.Scanner;
17+
18+
public class NaturalSum {
19+
public static int naturalSum (int n) {
20+
if (n <= 0) return 0;
21+
else return n+naturalSum(n-1);
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner input = new Scanner(System.in);
26+
System.out.println("/* ===== Sum N Natural Numbers ===== */");
27+
System.out.print("\nEnter a natural number n: ");
28+
int n = input.nextInt();
29+
System.out.println("The sum of first " + n + " natural numbers = " + naturalSum(n));
30+
}
31+
}
32+
```

BONUS/Recursion/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Some more problems on Recursion!
1010
4. [WAP to calculate the sum of elements in a given integer array using recursion](./ArraySum/)
1111
5. [WAP to find the nth number in Fibonacci Series using recursion](./Fibonacci/)
1212
6. [WAP to reverse a string using recursion](./ReverseString/)
13-
7. [WAP to reverse an integer array using recursion](./ReverseArray/)
13+
7. [WAP to reverse an integer array using recursion](./ReverseArray/)
14+
8. [WAP to print the Sum of `n` Natural Numbers](./NaturalSum/)
15+
9. [WAP to print the Sum of Squares of N natural numbers](./SumSquares/)

BONUS/Recursion/SumSquares/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sum Squares
2+
3+
WAP to print the sum of the series 1^2+2^2+3^2 up to n terms
4+
5+
## Solution
6+
7+
### [Java Implementation](./SumSquares.java)
8+
9+
```java
10+
import java.util.Scanner;
11+
12+
public class SumSquares {
13+
public static int sum (int n) {
14+
if (n <= 0) return 0;
15+
else return (n*n) + sum(n-1);
16+
}
17+
18+
public static void main(String[] args) {
19+
Scanner input = new Scanner(System.in);
20+
System.out.println("/* ===== Sum of squares of n natural numbers ===== */");
21+
System.out.print("\nEnter the number n: ");
22+
int n = input.nextInt();
23+
System.out.println("sum of squares of " + n + " natural numbers = " + sum(n));
24+
}
25+
}
26+
```

0 commit comments

Comments
 (0)