## Monday, December 25, 2017

### Do-While

Challenge: Write a program that computes the sum of all even numbers between 2 and 100 (inclusive), using a DO-WHILE loop.
class lab7_2
{
public static void main(String[] args)

{
//int number=1;
int sum = 0;
int i=0;
do
{
if (i % 2 == 0)
sum += i;
i++;
}while(i<=100);
System.out.println("The sum of all even numbers from 2 to 100 is: " + sum);
}
}

Challenge: Write a program that computes the average of a list of numbers entered by the user. The numbers are supposed to be in the range 50 – 100 (inclusive) and the user will indicate that he/she is done with data input by entering a value not in that range. At least one number will be in the range. Use a WHILE or a DO-WHILE loop.

import java.util.Scanner;

class lab7_3
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Average Calculator");
System.out.println("Enter a list of numbers between 50-100");
System.out.println("To exit, enter a value outsite the range");
int number=0;
int sum=0;
int counter =0;
do
{
try{
number=in.nextInt();
if((number<101 && number>49)){
counter++;

sum+=number;}
}catch(Exception e){
System.out.println("You have entered a value outside the parameters");
break;
}
}while(number<101 && number>49);

if((number<101 && number>49))
System.out.println("The average of the numbers is:" + sum/counter);
else {
System.out.println("Make sure your numbers are between 50 and 100");
if(counter==0)
{
System.exit(0);
}
System.out.println("The average of the numbers is:" + sum/counter);
System.exit(0);
}
}

}

Challenge: A variation of Exercise 3 in Lab 5 Write a program that presents the user a menu of four choices, “Circle”, “Square”, “Rectangle”, and “Exit” to calculate the area of one of these geometric objects. Once a choice is made, the program would ask the user for the data required to do the calculation. For example, if the user input were “Circle”, the program would prompt the user to enter the value of the radius of the circle. The program will let the user run area calculations until “Exit” is entered. Use a WHILE or a DO-WHILE loop.
import java.util.Scanner;

public class lab7_4
{
public static void main(String []args)
{
String a;
Scanner in=new Scanner(System.in);

while(true)
{
System.out.println("***Area Calculator***");
System.out.println("1. Circle");
System.out.println("2. Square");
System.out.println("3. Rectangle");
System.out.println("4. Exit");

int choice=in.nextInt();

if (choice==1)
{
System.out.println("Enter the radius of the circle");
double r =in.nextDouble();
double circleArea=Math.PI *  Math.pow(r, 2);

System.out.printf("The area of the circle is: %.2f", circleArea);
System.out.println("");
}

else if(choice==2)
{
System.out.println("Enter one dimension of the square");
double sideSquare = in.nextDouble();
double squareArea = Math.pow(sideSquare,2);
System.out.printf("The area of the square is: %.2f", squareArea);
System.out.println("");
}
else if(choice==3)
{
System.out.println("Enter the length and width of your rectangle: ");

double width = in.nextDouble();
double length = in.nextDouble();
double rectangleArea = width*length;
System.out.printf("The area of the rectangle is: %.2f", rectangleArea);
System.out.println("");
}

else if(choice==4)
{
System.exit(0);
}
else
{
System.out.println("Input Error!");
}
}
}

}