Showing posts with label numbers with same starting and ending digits in java. Show all posts
Showing posts with label numbers with same starting and ending digits in java. Show all posts

StartingAndEndingDigit

 // Find the numbers which have the same starting and ending digits in the given range such as 1 to 9, 11, 22, 33, 191, 101, etc within the given range

 

package abc;

 

public class SameStartingAndEndingDigit {

 

       public static void main(String[] args) {

             int range = 200 ;

             String firstdigit;

             String lastdigit;

             for(int i=1; i<=range; i++) { //make sure it is <=

 

                    String convertedToString = Integer.toString(i);

                    int stringLength = convertedToString.length();

                    char firstcharValue = convertedToString.charAt(0);

                    char lastcharValue = convertedToString.charAt(stringLength-1);

                    firstdigit = Character.toString(firstcharValue);

                    lastdigit = Character.toString(lastcharValue);

                   

                    if(firstdigit.equals(lastdigit)) {

                           System.out.println("For the given range of "+range+" , these are the expected values "+i);

                    }

             }

 

       }

 

}

 

OutPut:

For the given range of 200 , these are the expected values 1

For the given range of 200 , these are the expected values 2

For the given range of 200 , these are the expected values 3

For the given range of 200 , these are the expected values 4

For the given range of 200 , these are the expected values 5

For the given range of 200 , these are the expected values 6

For the given range of 200 , these are the expected values 7

For the given range of 200 , these are the expected values 8

For the given range of 200 , these are the expected values 9

For the given range of 200 , these are the expected values 11

For the given range of 200 , these are the expected values 22

For the given range of 200 , these are the expected values 33

For the given range of 200 , these are the expected values 44

For the given range of 200 , these are the expected values 55

For the given range of 200 , these are the expected values 66

For the given range of 200 , these are the expected values 77

For the given range of 200 , these are the expected values 88

For the given range of 200 , these are the expected values 99

For the given range of 200 , these are the expected values 101

For the given range of 200 , these are the expected values 111

For the given range of 200 , these are the expected values 121

For the given range of 200 , these are the expected values 131

For the given range of 200 , these are the expected values 141

For the given range of 200 , these are the expected values 151

For the given range of 200 , these are the expected values 161

For the given range of 200 , these are the expected values 171

For the given range of 200 , these are the expected values 181

For the given range of 200 , these are the expected values 191