Amazon Interview Question Software Engineer / Developers

  • amazon-interview-questions
    0
    of 0 votes
    2
    Answers

    write a program/function that converts a decimal number to its Roman numeral representation and vice versa.

    - JL on October 11, 2010 Report Duplicate | Flag
    Amazon Software Engineer / Developer C++



Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>


void decimal_to_roman(int num); /*Main function which processes the number and calls print_D2R().
									It also calls itself RECURSIVELY to process the residual number*/
void print_D2R(char ch, int x); /*Prints the character ch x times (ch ch ch...(x times)) */


int main()
{
	int val = 0;
	printf("Enter the number..\n");
	scanf("%d",&val);
	decimal_to_roman(val);
	printf("\n");
	return 0;
}

void decimal_to_roman(int num)
{
	int quotient = 0;
	int remainder = 0;
	int index = 0;
	int temp = 0;
	
	if(num >= 1000)
	{
		quotient = num/1000;
		remainder = num%1000;
		if(remainder == 0)
		{
			print_D2R('M', quotient);
		}
		else
		{
			print_D2R('M', quotient);
			decimal_to_roman(remainder);
		}
	}
	else if(num >= 500 && num < 1000)
	{
		if(num >= 900 && num < 1000)
		{
			temp = num - 900;
			print_D2R('C', 1);
			print_D2R('M', 1);
			decimal_to_roman(temp);
			temp = 0;
		}
		else
		{
			quotient = num/500;
			remainder = num%500;
			if(remainder == 0)
			{
				print_D2R('D', quotient);
			}
			else
			{
				print_D2R('D', quotient);
				decimal_to_roman(remainder);
			}
		}
	}
	else if(num >= 100 && num < 500)
	{
		if(num >= 400 && num < 500)
		{
			temp = num - 400;
			print_D2R('C', 1);
			print_D2R('D', 1);
			decimal_to_roman(temp);
			temp = 0;
		}
		else
		{
			quotient = num/100;
			remainder = num%100;
			if(remainder == 0)
			{
				print_D2R('C', quotient);
			}
			else
			{
				print_D2R('C', quotient);
				decimal_to_roman(remainder);
			}
		}
	}
	else if(num >= 50 && num < 100)
	{
		if(num >= 90 && num < 100)
		{
			temp = num - 90;
			print_D2R('X', 1);
			print_D2R('C', 1);
			decimal_to_roman(temp);
			temp = 0;
		}
		else
		{
			quotient = num/50;
			remainder = num%50;
			if(remainder == 0)
			{
				print_D2R('L', quotient);
			}
			else
			{
			
				print_D2R('L', quotient);
				decimal_to_roman(remainder);
			}
		}
	}
	else if(num >= 10 && num < 50)
	{
		if(num >= 40 && num < 50)
		{
			temp = num - 40;
			print_D2R('X', 1);
			print_D2R('L', 1);
			decimal_to_roman(temp);
			temp = 0;
		}
		else
		{
			quotient = num/10;
			remainder = num%10;
			if(remainder == 0)
			{
				print_D2R('X', quotient);
			}
			else
			{
				print_D2R('X', quotient);
				decimal_to_roman(remainder);
			}
		}
	}
	else if(num < 10)
	{
		switch (num)
		{
			case 1:
				printf("I");
				break;
			case 2:
				printf("II");
				break;
			case 3:
				printf("III");
				break;
			case 4:
				printf("IV");
				break;
			case 5:
				printf("V");
				break;
			case 6:
				printf("VI");
				break;
			case 7:
				printf("VII");
				break;
			case 8:
				printf("VIII");
				break;
			case 9:
				printf("IX");
				break;
			default:
				break;
		}
	}
}

void print_D2R(char ch, int times)
{
	int tIndex = 0;
	for(tIndex = 0; tIndex < times; tIndex++)
	{
		printf("%c",ch);
	}
}

- Jit on October 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ridiculous!

- yeah on November 06, 2010 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More