Display Numbers from 1 to 100 Without Loops or Conditions

Display numbers from 1 to 100 without loops or conditions

Pseudo code. Uses an array to force an exception after 100 elements which is caught and does nothing.

function r(array a, int index){
a[index] = a[index-1]+1
print a[index]
r(a, index+1)
}

try{
array a;
a.resize(101)
r(a, 1)
}catch(OutOfBoundsException){
}

EDIT
Java code:

public void printTo100(){
int[] array = new int[101];
try{
printToArrayLimit(array, 1);
}catch(ArrayIndexOutOfBoundsException e){
}
}
public void printToArrayLimit(int[] array, int index){
array[index] = array[index-1]+1;
System.out.println(array[index]);
printToArrayLimit(array, index+1);
}

Printing 1 to 1000 without loop or conditionals

Compile time recursion! :P

#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<1000>::out(std::cout);
}

Printing 1 to 1000 without loop or conditionals

Compile time recursion! :P

#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<1000>::out(std::cout);
}

Display 1 to 100 without loop or recursion

To be accurate, Arrays.fill() use a loop in its own implementation.
In general, Arrays.fill fills an array by assigning the second parameter to every element of the first parameter (your array).

Your example has an Array of type Object and a length of 100 elements.
In Arrays.fill(...) you generate a so-called anonymous class of type Object, which reimplements the toString-method by increasing the value of a counter (int count) and printing it after that.

Now, by calling Arrays.toString the toString() method of every element inside the array is executed (which is the same instance of the anonymous class here), resulting in printing the numbers from 1-100

Print numbers in specific range without using any loop or conditions (Java)

Based on @Imposter's answer, a reduce version with compacted but readable code

class Sandbox
{
public static void main(String args[]) throws Exception
{
System.out.println(getfalse(10, 60));
}

public static String getfalse(Integer start, Integer stop) throws Exception
{
return
start + "\n" +
Sandbox.class.getMethod("get" + (start == stop), Integer.class, Integer.class)
.invoke(Sandbox.class, start+1, stop);
}

public static String gettrue(Integer start, Integer stop)
{
return "";
}
}

How to print 1 to 100 without any looping using C#

Recursion maybe?

public static void PrintNext(i) {
if (i <= 100) {
Console.Write(i + " ");
PrintNext(i + 1);
}
}

public static void Main() {
PrintNext(1);
}

Printing 1 to 1000 without loop or conditionals

Compile time recursion! :P

#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<1000>::out(std::cout);
}


Related Topics



Leave a reply



Submit