Back to Home Page

/*******************************************************************************
Problem: The area of a rectangular garden is given by the expression:
Area = X(30-2X) X belongs [Xmin,Xmax] X is an  integer value that the user will input.
Write a program that show a table with the X values vs Area values
and also the program should output the maximum Area and the X value
for that maximum Area.
Example of Output:
X       Area
---    ------
2        52
3        72
4        88
.          ..
.          ..
.          ..
12     72

Maximum Area = 112
X value for maximum Area = 7

Programmer: Guillermo Julca
Mercy College

View Output


//*******************************************************************************

 using System;

namespace MaximaArea

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class MyClass

{

static void Main(string[] args)

{

int x;

int MinX,MaximX;

double Area,MaxArea,MaxX;

bool Isinteger;

MinX = 0;

MaximX =0;

do

{

Isinteger = true;

Console.Write("Enter the minimum x-value: ");

try

{

MinX = Convert.ToInt32(Console.ReadLine());

}

catch

{

Console.WriteLine("Invalid input. Please enter an integer number");

Isinteger = false;

}

finally

{

Console.WriteLine("Thanks!!!");

}

}while(!(Isinteger));

do

{

Isinteger = true;

Console.Write("Enter The maximum x-value: ");

try

{

MaximX = Convert.ToInt32(Console.ReadLine());

}

catch

{

Console.WriteLine("Invalid input. Please enter an integer number");

Isinteger = false;

}

finally

{

Console.WriteLine("Thanks!!!");

}

}while(!(Isinteger));

Console.WriteLine(" X Area");

Console.WriteLine("---- --------");

MaxArea = 0;

MaxX = 0;

for(x= MinX; x<=MaximX ;x++)

{

Area = x*(30 - 2*x);

Console.WriteLine(" {0} {1}",x,Area);

if (Area > MaxArea)

{

MaxArea = Area;

MaxX = x;

} // end if (Area > MaxArea)

} // end for(x= 2; x<=12;x++)

Console.WriteLine(" ");

Console.WriteLine("Maximun Area = {0}", MaxArea);

Console.WriteLine("X value for maximun Area = {0}",MaxX);

}

}

}

//***********************************************************************************************************

View Output

Back to Home Page


 GJ   Guillermo Julca