Problem of the Day
A new programming or logic puzzle every Mon-Fri

Problem 100

In honor of the 100th problem of the day today's problem will be to print out the numbers 1-100 and 100-1. To be clear you must print out each number 1-100 and then 100-1. Try to optimize for the number of characters used. Fewest number of characters wins!

Permalink: http://problemotd.com/problem/problem-100/

Comments:

  • Lasharn - 10 years, 9 months ago

    This answer is kinda cheaty since it's not a programming language so to speak but on MATLAB just go: disp(1:100); disp(100:-1:1);

    reply permalink

  • Anonymous - 10 years, 9 months ago

    On shell I can just do

    seq 1 100;seq 100 -1 1

    reply permalink

  • Anonymous - 10 years, 9 months ago

    haskell :

    show([1..100]++[100,99..1])
    

    or print instead of show, but show is one char shorter :D maybe i get something better, but i dunno

    reply permalink

  • Anonymous - 10 years, 9 months ago

    It took longer in C++ but still, looks clean.

    #include <iostream>
    using namespace std;
    
    int main (){
        int x = 0; //starting integer x
        int y = 100; //ending integer y
    
        for(int i = x; i <= y;i++)
            cout << i << " ";       //print the integer
    
        swap(x,y); puts("\n");      //swap starting and ending integer - Put space between series
    
        for(int i = x; i >= y;i--)
            cout << i << " ";       //print the integer
    
        getchar();
        getchar();
        return 0;
    };
    

    reply permalink

  • Anonymous - 10 years, 9 months ago

    C#

    using System;
    using System.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            var r = Enumerable.Range(1, 100);
            Console.WriteLine(String.Join(" ", r.ToArray()) + " " + String.Join(" ", r.OrderByDescending(x => x).ToArray()));
            Console.ReadLine();
        }
    }
    

    reply permalink

  • Anonymous - 10 years, 9 months ago

    was able to make it a bit smaller

    using System;
    using System.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            var r = Enumerable.Range(1, 100).ToArray();
            Console.Write(String.Join(" ", r) + " " + String.Join(" ", r.Reverse()));
            Console.ReadLine();
        }
    }
    

    reply permalink

  • James - 10 years, 9 months ago

    Compile with 'gcc -std=99'

    #include<stdio.h>    
    main(){for(int i=0;i<202;i++) printf("%d\n",i<101?i:201-i);}
    

    reply permalink

  • Anonymous - 10 years, 9 months ago

    You can still do it C90 as

    #include<stdio.h>
    main(){int i=0;for(;i<202;i++)printf("%d\n",i<101?i:201-i);}

    reply permalink

  • Anonymous - 10 years, 9 months ago

    In Ruby:

    (1..100).each {|n| puts n}  
    100.step(0,-1) {|n| puts n}
    

    reply permalink

  • Nick Krichevsky - 10 years, 9 months ago

    Python print range(101),range(101)[::1]

    reply permalink

Content curated by @MaxBurstein