954,086 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

500 ways to print [1..10]!

*Hopefull this hasn't been done already*

Write a program that when run, will print out the numbers 1 through 10. The program can be in any language, and can be as complicated or simple as you want.

Lets see how creative people are, and get up to 500 unique ways!

I'll start out with something simple:

C++

#include <iostream>

int main() {
    int x = 1, y = 10;

    while(y--)
        std::cout << x++ << std::endl;
}


EDIT: I guess we'll let the post count determine the number of different ways that have been produced.

Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

C#

using System;

class onetoten
{
	public static void Main ()
	{
		for (int x = 0; x < 11; x++)
		{
			Console.Write(x);
		}
	}
}
tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

Scheme

(define range
  (lambda (s e)
    (cond ((< s e) (cons s
                         (range (+ s 1) e)))
          ((= s e) (list s))
          (else "Invalid function call"))))

(range 1 10) ;=> (1 2 3 4 5 6 7 8 9 10)
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

3.) VB Script

MsgBox "1, 2, 3, 4, 5, 6, 7, 8, 9, 10", vbOKOnly, "Count!"


:)

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

C++

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main() {
    std::vector<int> numbs(0);

    for(int x = 1; x <= 10; x++)
        numbs.push_back(x);

    std::copy(numbs.begin(), numbs.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

C#

using System;

class onetoten
{
	public static void Main ()
	{
		for (int x = 0; x < 11; x++)
		{
			Console.Write(x);
		}
	}
}

Blah... you have to show off the features of object orientation:

using System;
using System.Collections.Generic;
using System.Text;

namespace counting
{
    public class Counter
    {
        private int count = 1;

        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
            }
        }
    }

    class Program
    {
        public static void Main()
        {
            Counter number = new Counter();
            do
            {
                Console.WriteLine(number.Count.ToString());
                number.Count++;
            } while (number.Count <= 10);

        }
    }
}


Looks like a lot more code... Well, it is, but now I've created a reusable object that does all of the work, and I could change it later on so to have a different starting number, or even determine the number some other way. :cool:

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

That is indeed a better way you did it though. But alot more code :)


Another one; Doesnt really 'count" but it prints .

C#

MessageBox.Show("1, 2, 3, 4, 5, 6, 7, 8, 9, 10");


Yea, it's beginner, yet it gets the job done. Now off to create a complicated one.

Jess, was this thread inspire by the one on PFO ;).

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

Perl:

print "$_\n" for 1..10
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

FASM Assembly:

format PE console
entry start

include 'C:\fasmw\include\win32a.inc'

;======================================
section '.data' data readable writeable
;======================================

num_fmt db '%d',10,0

;=======================================
section '.code' code readable executable
;=======================================

start:
	mov	ebx,10
    .again:
	lea	eax,[ebx-11]
	neg	eax
	ccall	[printf],num_fmt,eax
	dec	ebx
	jnz	.again
	stdcall	[ExitProcess],0

;=====================================
section '.import' import data readable
;=====================================

library kernel,'kernel32.dll',\
	msvcrt,'msvcrt.dll'

import kernel,\
       ExitProcess,'ExitProcess'

import msvcrt,\
       printf,'printf'
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Haskell:

main = mapM (putStr . (++ "\n") . show) [1..10]
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

Well, since the goal is to PRINT 1 - 10, I give you:

PostScript

%!PS

% position constants and procs
/x 72 def 
/yPos 700 def
/y {yPos lnHeight sub dup /yPos exch def} bind def

% font constants
/fontName /Courier def
/ptSize 12 def
/lnHeight ptSize 1.2 mul def

% set the font
fontName ptSize selectfont

% for loop to paint numbers
1 1 10 { 5 string cvs x y moveto show} for

% commit page image to currentpage
showpage
tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

In C

#include <stdio.h>

#define MIN_RANGE 1
#define MAX_RANGE 10

int main (void)
{
       int i;

       for (i = MIN_RANGE; i <= MAX_RANGE; i++)
              printf("%i\n", i);

       return 0;
}
CaliVagabond
Light Poster
35 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

I would've done one in Tama, but it's not Turing complete so doesn't deserve it.

Ruby:

(1..10).each { |x| puts x }
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

My Tama, or some other language I haven't heard of? If you're talking about mine, yea, it's pretty lame. :) I never got around to really improving it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Jess, was this thread inspire by the one on PFO ;).

Of course not. ;)

Bah, I wanted to be the first to use Ruby. Anyways...Ruby

puts *(1..10)
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

Here's um, another Scheme version:

(let loop ((i 1))
  (cond ((<= i 10)
         (display i)
         (loop (+ i 1))))

And another...

(define (range n m)
  (if (> n m)
      '()
      (cons n (range (+ n 1) m))))
(for-each display (range 1 10))
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

Ohhh yeahh. Predicates with internal state.

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optc="")
{
    std::cout << optc;
    typename T::const_iterator pos;
    for (pos=coll.begin(); pos != coll.end(); ++pos)
    {
      std::cout << *pos << " ";
    }
}


class IntSequence
{
  public:
    //constructor
    IntSequence(int initialValue)
    : value(initialValue) { }

    //function call
    int operator() ()
    {
      return value++;
    }

  private:
    int value;
};

int main()
{
  list<int> coll;

  //insert values from 1 to 9
  generate_n(back_inserter(coll), //start
             10,                   //number of elements
             IntSequence(1));     //generates values

  PRINT_ELEMENTS(coll);
  cout << endl;
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

C

#include <stdio.h>

void count(int start, int end);

int main(void) {
    count(1, 10);

    return 0;
}

void count(int start, int end) {
    int x;

    for(x = start; x <= end; x++)
        printf("%d\n", x);
}
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

Python

for x in range(10):
    print x + 1
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

Java

class Pedantic
{
   public static void main(String[] args)
  {
      for(int i=1; i <=10; i++)
     {
       System.out.println(i);
      }
   }
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: