0% found this document useful (0 votes)
48 views

Java w2

The document contains code for three problems: 1) A recursive function that calculates the number of ways to climb a staircase by taking 1 or 2 steps at a time. 2) Code to check if a number is an Armstrong number and display all Armstrong numbers up to a given value. 3) Code including methods to reverse a number, check if a number is a palindrome, and display all palindromes up to a given value.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java w2

The document contains code for three problems: 1) A recursive function that calculates the number of ways to climb a staircase by taking 1 or 2 steps at a time. 2) Code to check if a number is an Armstrong number and display all Armstrong numbers up to a given value. 3) Code including methods to reverse a number, check if a number is a palindrome, and display all palindromes up to a given value.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

WEEK -2

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?

public class Main {

public static int climbStairs(int n) {

if (n == 1) {

return 1;

int[] dp = new int[n + 1];

dp[1] = 1;

dp[2] = 2;

for (int i = 3; i <= n; i++) {

dp[i] = dp[i - 1] + dp[i - 2];

return dp[n];

public static void main(String[] args) {

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

public class Armstrong {


public static boolean isArmstrong(int num) {

int originalNum = num;

int result = 0;

int n = 0;

while (originalNum != 0) {

originalNum /= 10;

++n;

originalNum = num;

while (originalNum != 0) {

int remainder = originalNum % 10;

result += Math.pow(remainder, n);

originalNum /= 10;

return result == num;

public static void displayArmstrong(int n) {

for (int i = 1; i <= n; ++i) {

if (isArmstrong(i)) {

System.out.print(i + " ");

public static void main(String[] args) {

int num = 153;


if (isArmstrong(num)) {

System.out.println(num + " is an Armstrong number.");

} else {

System.out.println(num + " is not an Armstrong number.");

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.

public class Palindrome {

// method to reverse a given number n

public static int findReverse(int n) {

int reverse = 0;

while (n != 0) {

reverse = reverse * 10 + n % 10;

n /= 10;

return reverse;

// method to check whether a given number is palindrome or not

public static boolean isPalindrome(int n) {

return n == findReverse(n);

// method to display all palindrome numbers up to n

public static void displayPalindromeNos(int n) {


for (int i = 0; i <= n; i++) {

if (isPalindrome(i)) {

System.out.println(i);

//Access

public class PalindromeDemo {

public static void main(String[] args) {

// reverse a given number

int reverse = Palindrome.findReverse(12345);

System.out.println("Reverse of 12345: " + reverse);

// check whether a given number is palindrome or not

boolean isPalindrome = Palindrome.isPalindrome(12321);

System.out.println("Is 12321 a palindrome? " + isPalindrome);

// display all palindrome numbers up to n

System.out.println("Palindrome numbers up to 100: ");

Palindrome.displayPalindromeNos(100);

http://bit.ly/CTOOD-W2SKILL1

public class Person {


private int age;

public Person(int initialAge) {

if (initialAge < 0) {

age = 0;

System.out.println("Age is not valid, setting age to 0.");

} else {

age = initialAge;

public void yearPasses() {

age++;

public void amIOld() {

if (age < 13) {

System.out.println("You are young.");

} else if (age >= 13 && age < 18) {

System.out.println("You are a teenager.");

} else {

System.out.println("You are old.");

http://bit.ly/CTOOD-W2SKILL2

import java.util.ArrayList;

import java.util.List;

class Solution {

public List<List<Integer>> generate(int numRows) {


List<List<Integer>> triangle = new ArrayList<>();

if (numRows == 0) {

return triangle;

triangle.add(new ArrayList<>());

triangle.get(0).add(1);

for (int rowNum = 1; rowNum < numRows; rowNum++) {

List<Integer> row = new ArrayList<>();

List<Integer> prevRow = triangle.get(rowNum - 1);

row.add(1);

for (int j = 1; j < rowNum; j++) {

row.add(prevRow.get(j - 1) + prevRow.get(j));

row.add(1);

triangle.add(row);

return triangle;

http://bit.ly/CTOOD-W2SKILL3

public class Solution {

public boolean divisorGame(int n) {


// Alice wins if n is even, Bob wins if n is odd

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;

public class Armicable {

public static void main(String[] args) {

int n = 1000; // hard-coded input

displayAllAmicableNos(n);

public static void displayAllAmicableNos(int n) {

for (int i = 2; i <= n; i++) {

for (int j = i + 1; j <= n; j++) {

if (isAmicable(i, j)) {

System.out.println("(" + i + ", " + j + ")");

}
public static boolean isAmicable(int a, int b) {

return a != b && isFactor(a, b) && isFactor(b, a);

public static boolean isFactor(int n, int m) {

return m % n == 0;

You might also like