-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecondTask.java
111 lines (95 loc) · 3.13 KB
/
SecondTask.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package intro;
import java.util.Scanner;
public class SecondTask {
static Scanner scanner=new Scanner(System.in);
// - Write a program to print whether a number is even or odd, also take input from the user.
// Return true is even, false if not
public static boolean evenOrOdd(){
System.out.println("Please enter a number: ");
int num = scanner.nextInt();
return num % 2 == 0;
}
// - Take name as input and print a greeting message for that particular name.
public static void greetings(){
System.out.println("Enter a name and I will greet: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
}
// - Write a program to input principal, time, and rate (P, T, R) from the user and find Simple Interest.
// Formula = (P * R * T) / 100
public static double simpleInterest(){
System.out.println("Enter Principle: ");
double P = scanner.nextDouble();
System.out.println("Enter Time: ");
double T = scanner.nextDouble();
System.out.println("Enter Rate: ");
double R = scanner.nextDouble();
return (P * R * T) / 100;
}
// - Take in two numbers and an operator (+, -, *, /) and calculate the value. (Use if conditions)
public static int getCalculations(int num1, int num2) throws Exception {
System.out.println("Enter an operator: ");
String c=scanner.nextLine();
char operator=c.charAt(0);
int result;
if (operator == '+'){
result = num1 + num2;
}
else if (operator == '*'){
result = num1 * num2;
}
else if (operator == '/'){
if (num2 > 0) {
result = num1 / num2;
}
else {
throw new Exception("Not divisible by 0. ");
}
}
else {
result = num1 - num2;
}
return result;
}
// Take 2 numbers as input and print the largest number.
public static int largest(int a, int b){
if (a != b) {
if (a > b) {
return a;
}
else {
return b;
}
}
return -1;
}
// - To calculate Fibonacci Series up to n numbers.
public static int fibonacciRecur(int n){
if (n <= 1){
return n;
}
return fibonacciRecur(n - 1) + fibonacciRecur(n - 2);
}
public static void fibonacciIterate(int n){
int a = 0, b = 1, i = 0;
while (i < n){
System.out.print(a + " ");
int temp = a + b;
a = b;
b = temp;
i++;
}
}
// To find out.txt whether the given String is Palindrome or not.
public static boolean isPalindrome(String input){
int start = 0, end = input.length() - 1;
while (start < end){
if (input.charAt(start) != input.charAt(end)) {
return false;
}
start++; // Keep going towards the end string
end--; // Keep going towards the start string
}
return true;
}
}