Coding Horror

June 3, 2012 at 2:17 pm | Posted in art, Programming | Leave a comment
Tags: , , ,

Recently I was reading an article on coding horror which mentioned that most of the software developers who come for interview can’t even program and later I came across another article regarding phone screening interview and he mentioned Steve Yegge’s 5 essential phone screen questions. I looked at them and I thought .. whoaaa.. so easy. I was with my friend on his Windows machine and did not have access to some Linux machine. So I downloaded Bloodshed’s Dev-C++ compiler and typed this code and ran several tests, all in 20 minutes. (Bloodshed Dev-C++ version 5.2.0.2). I worked through several problems, enjoy my C code 🙂

/* (1) Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the
number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

 (2) Write function to compute Nth fibonacci number

 (3) print all odd numbers between 1 and 99
 */

#include <stdio.h>

enum { LIMIT = 100 };

void fib(const int n);
void print_odds(const int limit);
void print_fizzbuzz(const int limit);

int main(void)
{
	print_fizzbuzz(LIMIT);
	printf("\n\n");
	print_odds(LIMIT-1);
	printf("\n\n");
	fib(20);
	return 0;
}

/* No int/unsigned-long overflow check */
void fib(const int n)
{
	unsigned long f0 = 0;
	unsigned long f1 = 1;
	unsigned long fnum = f1;
	int i;
	if((0 == n) || (1 == n))
	{
		printf("fib(%d) = %d\n", n, n);
		return;
	}
	for(i = 2; i <= n; ++i)
	{
		fnum = f1 + f0;
		f0 = f1;
		f1 = fnum;
	}

	printf("fib(%d) = %lu", n, fnum);
}

void print_odds(const int limit)
{
	int i;
	for(i = 1; i <= limit; i = i + 2)
	{
		printf("%d\t", i);
	}
	printf("\n");
}

void print_fizzbuzz(const int limit)
{
	int i;
	for(i = 0; i <= limit; ++i)
	{
		if((0 == i%3) && (0 == i%5))
		{
			printf("fizzbuzz\t");
		}
		else if(0 == i%3)
		{
			printf("fizz\t");
		}
		else if(0 == i%5)
		{
			printf("buzz\t");
		}
		else
		{
			printf("%d\t", i);
		}
	}
	printf("\n");
}


Copyright © 2012 Arnuld Uttre, Village – Patti, P.O – Manakpur, Tehsil – Nangal, Distt. – Ropar, Punjab (INDIA)

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

Blog at WordPress.com.
Entries and comments feeds.