*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.

C#

using System;

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

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)

3.) VB Script

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

:)

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"));
}

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:

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 ;).

Perl:

print "$_\n" for 1..10

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'

Haskell:

main = mapM (putStr . (++ "\n") . show) [1..10]

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

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;
}

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 }

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.

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)

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))

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;
}

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);
}

Python

for x in range(10):
    print x + 1
Member Avatar for iamthwee

Java

class Pedantic
{
   public static void main(String[] args)
  {
      for(int i=1; i <=10; i++)
     {
       System.out.println(i);
      }
   }
}

Using binary arithmetic operators in C++ :)

#include <iostream>

int main()
{
    int a(0);
    while(a!=10)
    {
        int r,c, b(1);
        do
        {
            r=a^b;
            c=(a&b)<<1;
        } while( (a=c) && (b=r) );
        std::cout << (a=(r?r:c)) << '\n';
    }
}

I'm sure somebody who knows what they're talking about could make a much better implementation (or find improvements for mine ;) ), but here is a linked list in C.

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct LinkedList {
    struct Node *first;
};

struct Node *newNode(int data) {
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;

    return node;
}

void newLinkedList(struct LinkedList *list, int data) {
    list->first = newNode(data);
}

void displayLinkedList(const struct LinkedList *list) {
    struct Node *current = list->first;

    //traverse the list, printing the data at each Node.
    while(current != NULL) {
        printf("%d\n", current->data);
        current = current->next;
    }
}

void appendNode(struct LinkedList *list, int data) {
    struct Node *current = list->first;
    
    if(list->first->next == NULL) //only one Node
        list->first->next = newNode(data);

    else {
        //go to the last Node in the list
        do {
            current = current->next;
        } while (current->next != NULL);

        //add on a Node that the current Node points to
        current->next = newNode(data);
    }
}

void deleteLinkedList(struct LinkedList *list) {
    struct Node *current;
    struct Node *temp;
    
    if(list->first->next == NULL) //only one Node in the list
        free(list->first);
    else {
        current = list->first;
        
        while(current != NULL) {
            temp = current;
            current = current->next;
            free(temp);
        }
    }
}

/*int findNode(const struct LinkedList *list, int data) {
    struct Node *current = list->first;

    while(current != NULL) {
        if(current->data == data)
            return 1;

        current = current->next;
    }
    return 0;
}*/
    
int main(void) {
    struct LinkedList ll;
    int x;
    
    newLinkedList(&ll, 1);
    for(x = 2; x <= 10; x++)
        appendNode(&ll, x);

    displayLinkedList(&ll);

    deleteLinkedList(&ll);
    return 0;
}

Here's a recursive one in Java since it seems no one has done one recursively or done one in Java.

Java

public static void main(String[] args) {
    System.out.println(count(1, 10));
}

private static String count(int min, int max) {
    if (min > max) {
        return "";
    } else {
        return min + " " + count(min + 1, max);
    }
}

in a somewhat similar vein to using binary operators - here's one using base-2 logarithms in C++ :)

#include <iostream>
#include <cmath>

int main()
{
    for(int x(1<<1); x<=1<<10; x=x<<1)
        std::cout << (std::log(static_cast<double>(x)) 
                      / std::log(2.0)) << "\n";
}

This is so unnecessary it's not even funny. At least I had fun writing it. ;)

Ruby

((10..90).reject { |x| not (x % 10).zero? }.collect { |x| x.to_s.reverse.to_i } << 10 ).each { |x| p x }

And now.. fun with names and pointers ;)

#include <iostream>

template<typename T, int i>
int J(T (&arr)[i]) { return i; }

class C{};

int main()
{
    C plus_plus[10];
    C* c(plus_plus);
    do
    {
        c++;
        std::cout << c-plus_plus << '\n';
    }while( (c-plus_plus) != J(plus_plus));
}

:D


!multiple edits! - had to change the code since it wasn't pasting properly :confused:

Here's a recursive one in Java since it seems no one has done one recursively or done one in Java.

Jessehk and I both did recursive ones, actually.

Here's a Boolsmurf version:

+[>>[>]+<[+
<]>;>;+>;+>
;>+;;+;;;+;
+;+;+;;;;<[
<]+>[+>]+<<
<<<[<+]>>>>
+[+<+]>+[>+
]<<+<+<<<]+
;>;;;>;;<;;
;;;;>;;<;;;
+;+;>;+;;;;
#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"));
}

Why is my post edited? Boolfuck just happens to be the name of the programming language. If that offends people, then there's some disease that they have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.