Tuesday, December 31, 2013

A Broken Loop C puzzle

Question:

Find three ways to make the following program to print 20 times - (dash) by changing / adding just one character.

int i, n=20;
for(i=0; i < n; i--)
printf("-");


Answers:

/* FileName: printdash.c */

#include <stdio.h>
void main()
{
  int i, n=20;

  for(i=0; i+n; i--)
  printf("- ");
  printf("\n");

  for(i=0; -i < n; i--)
  printf("- ");
  printf("\n");

  for(i=0; i < n; n--)
  printf("- ");
  printf("\n");

}

To Run: gcc printdash.c
               ./a.out



No comments:

Post a Comment