You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

Given n, find the total number of full staircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

Example 1:

n = 5

The coins can form the following rows:

¤

¤ ¤

¤ ¤

Because the 3rd row is incomplete, we return 2.

Example 2:

n = 8

The coins can form the following rows:

¤

¤ ¤

¤ ¤ ¤

¤ ¤

Because the 4th row is incomplete, we return 3.

Java Program:

import java.util.Scanner;

public class staircase {
    public static void main(String[] args){
        System.out.println("Enter no number");
        int n =new Scanner(System.in).nextInt();
        if (n>0)
        System.out.println("The number of rows"+ ((int)Math.sqrt(8*(long)n+1)-1)/2);
    }
}

Java Program Challenge: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Example:

Input: 38

Output: 2

Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.

Since 2 has only one digit, return it.

Java Program:

public class number {
    public static void main(String[] args) {
       int sum = addDigits(38);
        System.out.println(sum);
    }

    public static int addDigits(int num) {
        if (num==0){
            return 0;
        }
        return num%9==0 ?9:num%9;

    }
}

Java Program Challenge-Suitcase Packing

Mr. Square is going on holiday. He wants to bring 2 of his favorite squares with him, so he put them in his rectangle suitcase.

Write a function that, given the size of the squares and the suitcase, return whether the squares can fit inside the suitcase.

fit_in(a,b,m,n)
a,b are the sizes of the squares
m,n are the sizes of the suitcase

Example
fit_in(1,2,3,2) should return True
fit_in(1,2,2,1) should return False
fit_in(3,2,3,2) should return False
fit_in(1,2,1,2) should return False

Code:

package com.company;

import static java.lang.Integer.max;

public class suitcase {
    public static boolean FitIn(int a,int b, int m, int n){
        if (a+b<=max(m,n)){
            System.out.println("True");
        }
        else {
            System.out.println("False");
        }
        return false;
    }
    public static void main(String[] args){
        FitIn(1,2,3,2);
        FitIn(1,2,2,1);
        FitIn(3,2,3,2);
        FitIn(1,2,1,2);
    }
}

Count of numbers whose sum of increasing powers of digits is equal to the number itself

Given an integer N, the task is to count all the integers less than or equal to N that follow the property where the sum of their digits raised to the power (starting from 1 and increased by 1 each time) is equal to the integer itself i.e. if D1D2D3…DN is an N digit number then for it to satisfy the given property (D11 + D22 + D33 + … + DNN) must be equal to D1D2D3…DN.

Examples:

Input: N = 100
Output: 11
01 = 0
11 = 1
21 = 2

91 = 9
81 + 92 = 8 + 81 = 89

Input: N = 200
Output: 13

Approach: Initialise count = 0 and for every number from 0 to N, find the sum of digits raised to the increasing power and if the resultant sum is equal to the number itself then increment the count. Finally, print the count.

Below is the implementation of the above approach:

package com.company;


public class powers {
    //Function to return the count of digits of n
    static int countDigits(int n)
    {
        int count =0;
        while (n>0)
        {
            count++;
            n=n/10;
        }
        return count;
    }
    double sum;
    //Function to return the sum of increasing powers of n
    static int digitPowsum(int n){
        int sum =0;
        /*count the digits in n which will be the power of the last digit*/
        int power = countDigits(n);
        //While there are digits left
        while(n>0){
            //Get the last digit
            int d=n%10;
            //Add the last digit after raising it to the rewuired power
            sum+=Math.pow(d,power);
            //Decrement the poer for the previous digit
            power--;
            //remove the last digit
            n/=10;


        }
     return sum;
    }
//Function which returns the count of integers which satisfy the given cndition
    static int countNum(int n){
        int count=0;
        for (int i=0;i<=n;i++)
        {
            //If current element satisfies the given condition
            if (i==digitPowsum(i))
            {
                count++;
            }

        }
        return count;
    }
    public static void main(String[] args){
        int n=100;
        System.out.println(countNum(n));
    }

}