-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
105 lines (78 loc) · 2.81 KB
/
Solution.java
File metadata and controls
105 lines (78 loc) · 2.81 KB
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
/*question 1 ..........................................
//Write a Java method to compute the average of three numbers..
public class functionQue {
public static void Avg(int a,int b, int c){
int avg = (a+b+c)/3;
System.out.println("The Average is :" + avg);
}
public static void main(String[] args) {
Avg(10, 20, 30);
}
}
// or -----------------------------------------------
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input the first number: ");
double x = sc.nextDouble();
System.out.print("Input the second number: ");
double y = sc.nextDouble();
System.out.print("Input the third number: ");
double z = sc.nextDouble();
System.out.print("The average value is " + average(x, y, z) + "\n");
}
public static double average(double x, double y, double z) {
return (x + y + z) / 3;
}
}
*/
/* Question 2........................
Write a method named isEven that accepts an int argument. The method should return true if
the argument is even, or false otherwise. Also write a program to test
your method.
import java.util.Scanner;
public class functionQue {
public static boolean isEven(int number) {
if(number % 2 == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter an integer: ");
num = sc.nextInt();
if (isEven(num)) {
System.out.println("Number is even");
} else {
System.out.println("Number is odd");
}
}
}
*/
/*Question 3.............................................
Write a Java program to check if a number is a palindrome in Java? ( 121 is
a palindrome, 321 is not) A number is called a palindrome if the number is equal to the reverse
of a number e.g., 121 is a palindrome because the reverse of 121 is 121 itself. On the other hand, 321 is not a
palindrome because the reverse of 321 is 123, which is not equal to 321.
*/
//Palindrone no.
class Solution {
public boolean isPalindrome(int x) {
// Special cases:
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + x % 10;
x /= 10;
}
// When length is even -> x == reversedHalf
// When length is odd -> x == reversedHalf/10
return x == reversedHalf || x == reversedHalf / 10;
}
}