10 Oct 2013

Increasing Number

Problem Statement: 
      For given range of digits , print  the numbers such that they are in the increasing sequence from left to right.
   For e.g.
  3 digit numbers : 123 , 124, ....234, 235 , 236 , ..., 345, 346 .. ......, 687,789
  4 digit numbers : 1234, 1235 , 1236 .....2345, 2346, ....3456,3457, 3458 .......,5678,5679,5789, 6789.

Solution:

Refer to the working Source Code

Code for printing the increasing numbers within the given range of digits


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * Print numbers with increasing digits
 * @author Prateek
 *
 */
public class IncreasingNumber {

 private static int count=0; 
 public static void main(String[] args) {

  IncreasingNumber obj=new IncreasingNumber();
  
  System.out.println("The numbers are :");
  obj.arrangeNum(3,5); // inclusive
  System.out.println("Total Numbers are : ");
  System.out.println(count);
  
 }

 //print increasing numbers within range of digits
 private void arrangeNum(int startRange , int endRange){

  if(startRange == endRange +1 )
   return ;

  arrangeNumUtil(startRange , 0);

  arrangeNum(startRange + 1 , endRange) ;
 }

    // Prints numbers with a given Number of digits
  private void arrangeNumUtil(int numDigits , int currentNum ){

  int digit = (currentNum % 10) + 1;   //next digit from current Number
  currentNum *= 10;                  // move number to left
  for (; digit <= 9; digit++)
  {
   if (numDigits == 1){
    count++;
    System.out.println(currentNum + digit);
    return;
   }

   arrangeNumUtil(numDigits - 1, currentNum + digit);
  }
 }

}




2 comments:

  1. You really make it seem so easy with your presentation but I find
    this topic to bee really something which I think I would never understand.
    It seems too complicated and extremely broad for me.
    I'm looking forwad for your next post, I will
    try to get the hang of it!

    Here iss myy web page :: Las Vegas Washer Service

    ReplyDelete
  2. I was suggested this web site by means of my cousin. I am no longer sure whether this post is written through him as nobody else
    know such precise approximately my problem.

    You are incredible! Thanks!

    my web-site - search engine marketing in uk

    ReplyDelete