Write a program which accepts two integers as a minimum and maximum limit and calculates total of how many 1s were their within the limit

Do it with Java.util.Scanner. Thats a really basic thing in java. I recommend you to do some research 🙂 Here is an easy tutorial on that:
http://www.homeandlearn.co.uk/java/user_input.html

here an answere anyway:

public static void main(String[] args){

    Scanner user_input = new Scanner( System.in );
    String anInputString;

    anInputString = user_input.next( );


    if (anInputString.equals("1 11")){
    System.out.println("4")
    }
    if (anInputString.equals("11 111"){
    System.out.println("34")
    }
}

edit:

if you want to achieve an output of “4” while your input lies between “1” and “11”. Or “34” if its between “10” and “112” you should try this:

 public static void main(String[] args){

        Scanner user_input = new Scanner( System.in );
        int anInputInt;

        anInputInt = user_input.nextInt( );

        if (0 < anInputInt && anInputInt < 11 ){ //if input is (1-10)
        System.out.println("4")
        }
        if (11 <= anInputInt && anInputInt <= 111){ //if input is (11-111)
        System.out.println("34")
        }
    }

Anyway you should edit your question 😀 I think many people are happy to help you, but just cant understand your question at all 0.0

finally after I understood what you really want to do:

   public static void main(String[] args){

        Scanner user_input = new Scanner( System.in );
        int anInputInt;

        anInputInt = user_input.nextInt( );

        if(anInputString < 0){
        anInputString = anInputstring*(-1);
        }

        int oneCount = 0;

        //gets all numbers in your specified range
        //if you want to start at number "xx" change the value of aNumberInRange likewise
        for (int aNumberInRange = 0; aNumberInRange < anInputInt; aNumberInRange++){ 

            String str = String.valueOf(aNumberInRange);

            //iterates through every character of an aNumberInRange and counts if a "1" occurs
            for(int i = 0; i < str.length(); i++){
               char c = str.charAt(i);        
               if (c == 1){
                oneCount++
               }
            }
        }
        system.out.println(oneCount);
      }

For whoever reads this solution. The client wanted to count all the occurences of the number “1” in a specified range. This should also work with negative Numbers

Leave a Comment