109k views
5 votes
• Reads an integer n (you can assume n won’t be negative and don’t worry about exception handling) • Using a recursive method, compute and write to the screen all integers with n digits in which the digit values are strictly increasing. For example, if n =3, the result is 012 013 014 015 016 017 018 019 023 024 025 026 027 028 029 034 035 036 037 038 039 045 046 047 048 049 056 057 058 059 067 068 069 078 079 089 123 124 125 126 127 128 129 134 135 136 137 138 139 145 146 147 148 149 156 157 158 159 167 168 169 178 179 189 234 235 236 237 238 239 245 246 247 248 249 256 257 258 259 267 268 269 278 279 289 345 346 347 348 349 356 357 358 359 367 368 369 378 379 389 456 457 458 459 467 468 469 478 479 489 567 568 569 578 579 589 678 679 689 789

User Joebert
by
5.2k points

1 Answer

3 votes

Answer:

The Codes and Screenshot to answer the question above are both given below:(the Screenshot is attached to offer you an additional readability of code)

note: File Name must be the same as our class name ( which is Increasing.java in our case.)

Step-by-step explanation:

Code is given below:

/* File Name Should be Increasing.java

* Java program to print all n-digit numbers whose digits

* are strictly increasing from left to right

*/

import java.util.Scanner;

class Increasing

{

// Function to print all n-digit numbers whose digits

// are strictly increasing from left to right.

// out --> Stores current output number as string

// start --> Current starting digit to be considered

public static void findStrictlyIncreasingNum(int start, String out, int n)

{

// If number becomes N-digit, print it

if (n == 0)

{

System.out.print(out + " ");

return;

}

// Recall function 9 time with (n-1) digits

for (int i = start; i <= 9; i++)

{

// append current digit to number

String str = out + Integer.toString(i);

// recurse for next digit

findStrictlyIncreasingNum(i + 1, str, n - 1);

}

}

// Driver code for above function

public static void main(String args[])

{ //User Input

Scanner sc = new Scanner( System.in);

System.out.println("Enter a number:");

int n = sc.nextInt();

//Function to calcuclate and print strictly Increasing series

findStrictlyIncreasingNum(0, " ", n);

}

}

//End of Code

• Reads an integer n (you can assume n won’t be negative and don’t worry about exception-example-1
• Reads an integer n (you can assume n won’t be negative and don’t worry about exception-example-2
• Reads an integer n (you can assume n won’t be negative and don’t worry about exception-example-3
User Gabor Garami
by
4.7k points