Do-While Loops #73 (Takes two days to perfect a simple mistake)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name:Safe Square Root
/// File Name:SafeSquareRoot.java
/// Date Finished: 1/6/2016

 
import java.util.Scanner;

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

		
    
        System.out.println("SQUARE ROOT!");
        System.out.println("Enter a number: ");
        int n = input.nextInt();
        
        
        
while (n < 0 ){
        System.out.println("You can't take the square root of a negative number, silly.");
        System.out.print("Try again: ");
             n = input.nextInt();
	}
        
        System.out.println("The square root of " + n + " is " + Math.sqrt(n) + ".");
}
}




    

Picture(s) of the output

Assignment 7

Back to Homepage