0% found this document useful (0 votes)
25 views8 pages

Java Coding Challenges for 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Java Coding Challenges for 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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) {

[Link](climbStairs(1)); // 1

[Link](climbStairs(2)); // 2

[Link](climbStairs(3)); // 3

[Link](climbStairs(4)); // 5

[Link](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 += [Link](remainder, n);

originalNum /= 10;

return result == num;

public static void displayArmstrong(int n) {

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

if (isArmstrong(i)) {

[Link](i + " ");

public static void main(String[] args) {

int num = 153;


if (isArmstrong(num)) {

[Link](num + " is an Armstrong number.");

} else {

[Link](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)) {

[Link](i);

//Access

public class PalindromeDemo {

public static void main(String[] args) {

// reverse a given number

int reverse = [Link](12345);

[Link]("Reverse of 12345: " + reverse);

// check whether a given number is palindrome or not

boolean isPalindrome = [Link](12321);

[Link]("Is 12321 a palindrome? " + isPalindrome);

// display all palindrome numbers up to n

[Link]("Palindrome numbers up to 100: ");

[Link](100);

[Link]

public class Person {


private int age;

public Person(int initialAge) {

if (initialAge < 0) {

age = 0;

[Link]("Age is not valid, setting age to 0.");

} else {

age = initialAge;

public void yearPasses() {

age++;

public void amIOld() {

if (age < 13) {

[Link]("You are young.");

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

[Link]("You are a teenager.");

} else {

[Link]("You are old.");

[Link]

import [Link];

import [Link];

class Solution {

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


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

if (numRows == 0) {

return triangle;

[Link](new ArrayList<>());

[Link](0).add(1);

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

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

List<Integer> prevRow = [Link](rowNum - 1);

[Link](1);

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

[Link]([Link](j - 1) + [Link](j));

[Link](1);

[Link](row);

return triangle;

[Link]

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 [Link];

import [Link];

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)) {

[Link]("(" + 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;

Common questions

Powered by AI

To determine if a number is an Armstrong number using the isArmstrong method, first calculate the number of digits, n, in the number. Then, raise each digit to the power of n and sum these values. If this sum equals the original number, it is an Armstrong number. The method uses integer division and modulo to extract digits and a loop to accumulate the sum of the digits raised to the required power .

The displayAllAmicableNos method identifies and displays amicable pairs by iterating over all pairs of numbers up to n. For each pair (i, j), it checks if they are amicable using the isAmicable method, which determines if each number's proper divisors add up to the other number without being the same number. If they are amicable, the pair is printed. This process requires ensuring that the sum of proper divisors of a and b equals each other, which is computationally intensive, especially for large n .

The isPalindrome method checks if a number is a palindrome by comparing it to its reverse derived using the findReverse method. If they are identical, the number is a palindrome. The displayPalindromeNos method leverages this property to loop through numbers up to n, printing those that are palindromes. This combined process is useful for identifying sequences with symmetry and is applied in scenarios where such properties are needed or desired .

The Armicable class implements amicable numbers using three static methods: isFactor, isAmicable, and displayAllAmicableNos. The isFactor method checks if one number is a divisor of another. The isAmicable method verifies if two numbers are amicable by ensuring they are not equal and each number's sum of factors equals the other number. Finally, displayAllAmicableNos uses these methods to identify and print pairs of amicable numbers up to a given n, facilitating the exploration and identification of such unique number pairs .

An Armstrong number is an integer such that the sum of its own digits, each raised to the power of the number of digits, equals the number itself. The displayArmstrong method checks each number up to n, using isArmstrong to verify if it is an Armstrong number, and prints those that are. This method can efficiently list all Armstrong numbers within a specified range by iterating through each number and applying the Armstrong condition .

The divisorGame method determines the winner between Alice and Bob based solely on the parity of n; Alice wins if n is even, Bob wins if n is odd. This logic is valid because if Alice starts with an even number, she can always choose a divisor such that Bob receives an odd number and eventually ends up with 1, making Bob lose. Conversely, if Alice starts with an odd number, Bob can always turn the number back to even, securing a win. This simple parity check captures the conditions under which Alice or Bob will win depending on the initial state .

The findReverse method reverses the digits of a given integer n. It initializes a reverse variable to 0 and iteratively adds the last digit of n to reverse while removing it from n using division. This continues until all digits are reversed. The method is useful in palindrome detection where a number is checked against its reverse to see if they are identical, indicating it is a palindrome .

The yearPasses method increments the age attribute by one, simulating the passage of a year. This change in age affects the output of the amIOld method, which categorizes the individual as 'young', 'teenager', or 'old'. Thus, yearPasses allows for simulating aging and how age categorization changes over time within the Person class .

The result of the climbStairs method with an input of 5 is 8. This is computed using dynamic programming. The method initializes an array dp such that dp[1] is 1 and dp[2] is 2. For each step i from 3 to n, the number of ways to reach step i is the sum of ways to reach the previous step and the step before that, i.e., dp[i] = dp[i - 1] + dp[i - 2]. Thus, for n = 5, dp[3] = 3, dp[4] = 5, and dp[5] = 8 .

The generate method creates Pascal's Triangle using a list of lists. A row is built by starting and ending with 1 and filling the middle elements with the sum of two adjacent elements from the previous row. This operation is repeated for the number of requested rows. The computational complexity of this method is O(numRows^2), as it involves iterating through each row and each element within a row-relative to its position in the preceding row .

You might also like