Tuesday, December 4, 2018

C program counts down from 100 to count up to 100


One Compiler Directive
One Line of Code
Count 'em.



C++

At the developers’ group I went to one Saturday morning they were talking about “programmers” who show up for programming interviews who can’t even do this countdown from 100 to 1 with the constraint in the article:
http://www.thousandtyone.com/blog/EasierThanFizzBuzzWhyCantProgrammersPrint100To1.aspx
I just wrote this in about a minute:

#include <iostream>
using namespace std;
int main()
{
   for(int i=0;i<100;i++)
     {
       cout<<100-i<<endl;
     }
   system(“pause”);
   return 0;
}

It works.
THEN I squeezed it down to four lines INCLUDING the header. That’s the whole program. The only function is the main function.

NOW I got it down to TWO lines including the header and it still runs.


The source doesn’t look as pretty but it still works and produces the same output. That “for” loop is really just one line.

The compiler I’m using pauses the output in the console window so you can read it before it closes without the system(“pause”). Using the standard library class “std” with the scope resolution operator “::” eliminates the “using namespace std;” line and I only did that to reduce the line count.

Here is my two-line program:
#include <iostream>
int main() {for(int i=0;i<100;i++){std::cout<<100-i<<std::endl;}return 0;}