Unit 3 HW
// #2
import java.util.Scanner;
public class quadratic {
public static void main(String[] Strings) {
Scanner sc = new Scanner(System.in);
System.out.print("Input a: ");
double a = sc.nextDouble();
System.out.print("Input b: ");
double b = sc.nextDouble();
System.out.print("Input c: ");
double c = sc.nextDouble();
double result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real roots.");
}
}
}
quadratic.main(null);
// #4
import java.util.Scanner;
public class evenOdd {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Pick your number...");
double input = sc.nextDouble();
if (input > 0)
{
if (input < 1)
{
System.out.println("Positive small number");
}
else if (input > 1000000)
{
System.out.println("Positive large number");
}
else
{
System.out.println("Positive number");
}
}
else if (input < 0)
{
if (Math.abs(input) < 1)
{
System.out.println("Negative small number");
}
else if (Math.abs(input) > 1000000)
{
System.out.println("Negative large number");
}
else
{
System.out.println("Negative number");
}
}
else
{
System.out.println("Zero");
}
}
}
evenOdd.main(null);
// #6
import java.util.Scanner;
public class decimals {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Input floating-point number: ");
double x = sc.nextDouble();
System.out.println("Input floating-point another number: ");
double y = sc.nextDouble();
x = Math.round(x * 1000);
x = x / 1000;
y = Math.round(y * 1000);
y = y / 1000;
if (x == y)
{
System.out.println("They are the same up to three decimal places");
}
else
{
System.out.println("They are different");
}
}
}
decimals.main(null);
// #8
import java.util.Scanner;
public class vowelConsonant {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Input an alphabet: ");
String input = sc.next().toLowerCase();
boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
|| input.equals("o") || input.equals("u");
if (input.length() > 1)
{
System.out.println("Please enter a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("That's not a letter. Enter uppercase or lowercase letter.");
}
else if (vowels)
{
System.out.println(input + "is vowel");
}
else
{
System.out.println(input + " is consonant");
}
}
}
vowelConsonant.main(null);
// #10
public class naturalNumbers {
public static void main(String[] args)
{
int i;
System.out.print("The first 10 natural numbers are: ");
for (i=1; i<=10; i++)
{
System.out.print(i + ", ");
}
}
}
naturalNumbers.main(null);
// #12
import java.util.Scanner;
public class average {
public static void main(String[] args)
{
int i,n=0,s=0;
double avg;
{
System.out.println("Input 5 numbers ");
}
for (i=0;i<5;i++)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
s +=n;
}
avg=s/5;
System.out.println("The sum of those numbers is: " +s+"\nThe Average is: " +avg);
}
}
average.main(null);
// #14
import java.util.Scanner;
public class multiplicationTable {
public static void main(String[] args)
{
int j,n;
System.out.print("Input the number");
{
System.out.print("Input number of terms : ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println ("\n");
for(j=0; j<=n; j++)
System.out.println(n+" X "+j+" = " +n*j);
}
}
}
multiplicationTable.main(null);
// #16
import java.util.Scanner;
public class triangle {
public static void main(String[] args)
{
int i,j,n;
System.out.println("Input number of rows");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
System.out.print("*");
System.out.println("");
}
}
}
triangle.main(null);
// #18
import java.util.Scanner;
public class patternTriangle {
public static void main(String[] args)
{
int i,j,n,k=1;
System.out.println("Input number of rows");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i=1;i<=n;i++)
{
for(j=1; j<=i; j++)
System.out.print(k++ + " ");
System.out.println("");
}
}
}
patternTriangle.main(null);
// #20
import java.util.Scanner;
public class floydTriangle {
public static void main(String[] args)
{
int numberOfRows;
System.out.println("Input number of rows");
Scanner sc = new Scanner(System.in);
numberOfRows = sc.nextInt();
int number = 1;
for (int row = 1; row <= numberOfRows; row++)
{
for (int column = 1; column <= row; column++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}
floydTriangle.main(null);