Java w2
Java w2
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb or 2
steps. In how many distinct ways can you climb to the top?
if (n == 1) {
return 1;
dp[1] = 1;
dp[2] = 2;
return dp[n];
System.out.println(climbStairs(1)); // 1
System.out.println(climbStairs(2)); // 2
System.out.println(climbStairs(3)); // 3
System.out.println(climbStairs(4)); // 5
System.out.println(climbStairs(5)); // 8
Write a class Armstrong with 2 static methods is Armstrong() to check whether a given number is
Armstrong or not and displayArmstrong) to displays all Armstrong numbers up to n". Acce them
using main () method from the same class. In java
int result = 0;
int n = 0;
while (originalNum != 0) {
originalNum /= 10;
++n;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
if (isArmstrong(i)) {
} else {
displayArmstrong(500);
Write a class Palindrome with 3 static methods findReverse() which reverses a given number n",
isPalindrome) to check whether a given number " is palindrome or not and displayPalindromeNos()
to displays all palindrome numbers up to .n". Access them using main 0 of PalindromeDemo Class
which is in the same package.
int reverse = 0;
while (n != 0) {
n /= 10;
return reverse;
return n == findReverse(n);
if (isPalindrome(i)) {
System.out.println(i);
//Access
Palindrome.displayPalindromeNos(100);
http://bit.ly/CTOOD-W2SKILL1
if (initialAge < 0) {
age = 0;
} else {
age = initialAge;
age++;
} else {
http://bit.ly/CTOOD-W2SKILL2
import java.util.ArrayList;
import java.util.List;
class Solution {
if (numRows == 0) {
return triangle;
triangle.add(new ArrayList<>());
triangle.get(0).add(1);
row.add(1);
row.add(prevRow.get(j - 1) + prevRow.get(j));
row.add(1);
triangle.add(row);
return triangle;
http://bit.ly/CTOOD-W2SKILL3
return n % 2 == 0;
____________________________-
Amicable numbers are two different natural numbers related in such a way that the sum of the
proper divisors of each is equal to the other mumber. The smallest pair of amicable numbers is (220,
284) They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, $5 and
110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the
sum is 220. Create a class Armicable with 3 static methods inFactor () which finds whether a given
number n" is a factor of m", isAmicable () to check whether a given number hand,m" are Amicable or
not and displayAllAmicahteNos () to displays all pairs of Amicable numbers up to,n. Access them
using main () from the same class. (Take hard coded input)
import java.util.ArrayList;
import java.util.List;
displayAllAmicableNos(n);
if (isAmicable(i, j)) {
}
public static boolean isAmicable(int a, int b) {
return m % n == 0;