Alex Rivera | Logout

Display numbers from 1 to 100 without loops or conditions

Asked 2010-01-11T18:42:00.630
33

Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"? We can easily do using recursion but that again has an if condition. Is there a way to do without using "if" as well? Also no repetitive print statements, or a single print statement containing all the numbers from 1 to 100.

A solution in Java is preferable.

Edit
Report

10 Answers

16

Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"?

Using an optimized version of this:

System.out.println("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 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100"); 

Next question?

answered 2010-01-11T19:21:36.150
15

Or if you like to use reflection :-)

public class Print100 {

    public static void emit0(int index) throws Exception {
        System.out.println(index);

        String next = new StringBuilder()
                          .append("emit")
                          .append(index / 100)
                          .toString();

        Print100.class.getMethod(next, Integer.TYPE)
                          .invoke(null, index+1);
    }

    public static void emit1(int index) {

    }

    public static void main(String[] args) throws Exception {
        emit0(1);
    }

}
answered 2010-01-11T23:45:24.497
9

No conditions (no short-cut boolean operators, no ?-operator, no exceptions), no loops:

import java.util.Vector;

public class PrintOneToHundered {
  static int i;
  PrintOneToHundered() {}
  public String toString() { return ++i+""; }
  public static void main(String[] args) {
    Vector v1  =new Vector(); v1  .add(new PrintOneToHundered());
    Vector v2  =new Vector(); v2  .addAll(v1 ); v2  .addAll(v1 );
    Vector v4  =new Vector(); v4  .addAll(v2 ); v4  .addAll(v2 );
    Vector v8  =new Vector(); v8  .addAll(v4 ); v8  .addAll(v4 );
    Vector v16 =new Vector(); v16 .addAll(v8 ); v16 .addAll(v8 );
    Vector v32 =new Vector(); v32 .addAll(v16); v32 .addAll(v16);
    Vector v64 =new Vector(); v64 .addAll(v32); v64 .addAll(v32);
    Vector v100=new Vector(); v100.addAll(v64); v100.addAll(v32); v100.addAll(v4);
    System.out.println(v100);
  }
}

Explanation:

  • define a class, whose toString-method returns consecutive ints at repeated calls
  • create a vector with 100 elements, that are instances of the class
  • print the vector (toString-method of a Vector returns a string of the toString-values of all its elements)
answered 2010-01-12T13:42:48.227
5

My solution without verbosity. It doesn't use any control structure other than function application. It also doesn't use library code to help out. My code is easily extensible to print out the range [a, b]. Just change conts [n / 100] to conts [(n - a) / (b - a)] and of course change new Printable (1) to new Printable (a).

To100.java:

class Printable {
  private static final Continuation[] conts = {new Next (), new Stop ()};

  private final int n;
  private final Continuation cont;

  Printable (int n) {
    this.n = n;
    this.cont = conts [n / 100];
  }

  public void print () {
    System.out.println (n);
    cont.call (n);
  }
}

interface Continuation {
  public void call (int n);
}

class Next implements Continuation {
  public void call (int n) {
    new Printable (n + 1).print ();
  }
}

class Stop implements Continuation {
  public void call (int n) {
    // intentionally empty
  }
}

class To100 {
  public static void main (String[] args) {
    new Printable (1).print ();
  }
}

EDIT: Since this question was closed (why???) I'll post my second answer here. It is inspired by Tom Hawtin's notice that the program doesn't have to terminate. Also the question doesn't require that only the numbers 1-100 are printed (or even in order).

To100Again.java:

class To100Again extends Thread {
  private static byte n;
  public void run () {
    System.out.println (n++);
    new To100Again ().start ();
    System.gc();
  }
  public static void main (String[] args) {
    new To100Again ().start ();
  }
}
answered 2010-01-12T00:59:07.283
4

System.out.println("numbers from 1 to 100")

answered 2010-01-11T20:52:23.597
3

without any loop and condition :

public static void recfunc(int a[], int i)
{
    System.out.println(i);
    int s = a[i];
    recfunc(a, i + 1);
}

public static void main(String[] args)
{
    int[] a = new int[100];

    try
    {
        recfunc(a, 1);
    }
    catch (Exception e)
    {

    }
}

with recursion and without if I think use "?" for conditioning :

public static int recfunc(int i)
{
    System.out.println(i);
    return (i < 100) ? recfunc(i + 1) : 0;

}


public static void main(String[] args)
{
    recfunc(1);
}
answered 2010-01-11T19:01:25.923
2

This reminds me of programming my TI-55 years and years ago. It had 32 programmable instruction steps, and a RESET instruction that would jump to instruction zero, so simple looping could be implemented. The problem was getting it to stop, which boiled down to getting it to do an operation that caused an error, e.g., divide by zero.

Thus:

public static void main(String[] args)
{
    printN(100);
}

private static void printN(int n)
{
    try
    {
        int  t = 1/n;    // Exception when n is 0
        printN(n-1);     // Recurse, down to 0
        System.out.println(n);
    }
    catch (Exception ex)
    {
        // Stop recursing
    }
}

Note: Yes, I know this is similar to @Yacoby's solution.

answered 2010-01-11T23:15:16.457
1

Does it have to be Java? If ruby's allowed:

puts [*1..100].join("\n")

I'd like to see anything this concise in Java.

answered 2010-01-12T00:16:45.303
0

Here's a solution that seems very simple compared to what has been posted so far. It uses short-circuit evaluation to end recursion:

public class Foo {
  static int x = 1;
  public static void main(String[] args) {
    foo();
  }

  private static boolean foo() {
    System.out.println(x++);
    return x > 100 || foo();
  }
}
answered 2011-07-20T21:12:15.127
-1
 system("seq 1 100");

in C will give the desired result. Find the equivalent Java call to run a shell command.

answered 2010-11-09T04:17:51.017

Your Answer