Skip to content

Commit 986b12b

Browse files
committed
Done some question on methods
1 parent d371af4 commit 986b12b

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

13_Methods/Question1.class

594 Bytes
Binary file not shown.

13_Methods/Question1.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Write a Java Method to compute the average of three numbers;
2+
3+
import java.util.Scanner;
4+
5+
public class Question1 {
6+
public static int avg(int a, int b, int c) {
7+
return (a + b + c) / 3;
8+
}
9+
10+
public static void main(String[] args) {
11+
System.out.println(avg(2, 3, 4));
12+
System.out.println(avg(5, 3, 4));
13+
14+
}
15+
}

13_Methods/Question2.class

1017 Bytes
Binary file not shown.

13_Methods/Question2.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
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.
3+
import java.util.Scanner;
4+
5+
public class Question2 {
6+
public static boolean isEven(int a) {
7+
8+
return ((a % 2) == 0);
9+
}
10+
11+
public static void main(String[] args) {
12+
System.out.println(isEven(5) + " is a even number");
13+
14+
System.out.println(isEven(10) + " is a odd number");
15+
16+
}
17+
18+
}

13_Methods/Question3.class

1.33 KB
Binary file not shown.

13_Methods/Question3.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Wap to check if a number is palindrome or not.
2+
3+
public class Question3 {
4+
public static boolean isPalindrome(int num) {
5+
int rev = 0;
6+
int originalnum = num;
7+
while (num > 0) {
8+
int rem = num % 10;
9+
rev = rev * 10 + rem;
10+
num /= 10;
11+
}
12+
return (originalnum == rev);
13+
14+
}
15+
16+
public static boolean isPalindrome(String str) {
17+
int strlen = str.length();
18+
String rev = "";
19+
for (int i = (strlen - 1); i >= 0; i--) {
20+
rev = rev + str.charAt(i);
21+
}
22+
return (str.toLowerCase().equals(rev.toLowerCase()));
23+
}
24+
25+
public static void main(String[] args) {
26+
System.out.println(isPalindrome(101));
27+
System.out.println(isPalindrome("ababaa"));
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)