Unit 3 Java-Exercises
import java.util.Scanner;
public class Exercise1 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int input = in.nextInt();
if (input > 0) //boolean
{
System.out.println("Number is positive");
}
else if (input < 0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
}
}
Exercise1.main(null);
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Input a: ");
double a = input.nextDouble();
System.out.print("Input b: ");
double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.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.");
}
}
}
Exercise2.main(null);
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the 1st number: ");
int num1 = in.nextInt();
System.out.print("Input the 2nd number: ");
int num2 = in.nextInt();
System.out.print("Input the 3rd number: ");
int num3 = in.nextInt();
if (num1 > num2)
if (num1 > num3)
System.out.println("The greatest: " + num1);
if (num2 > num1)
if (num2 > num3)
System.out.println("The greatest: " + num2);
if (num3 > num1)
if (num3 > num2)
System.out.println("The greatest: " + num3);
}
}
Exercise3.main(null);
import java.util.Scanner;
public class Exercise4 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input value: ");
double input = in.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) // Math Function: Absolute Value
{
System.out.println("Negative large number");
}
else
{
System.out.println("Negative number");
}
}
else
{
System.out.println("Zero");
}
}
}
Exercise4.main(null);
import java.util.Scanner;
public class Exercise5 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int day = in.nextInt();
System.out.println(getDayName(day));
}
// Get the name for the Week
public static String getDayName(int day) {
String dayName = "";
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default:dayName = "Invalid day range";
}
return dayName;
}
}
Exercise5.main(null);
import java.util.Scanner;
public class Exercise6 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input floating-point number: ");
double x = in.nextDouble(); // wrapper classes: using methods
System.out.print("Input floating-point another number: ");
double y = in.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");
}
}
}
Exercise6.main(null);
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
int number_Of_DaysInMonth = 0;
String MonthOfName = "Unknown";
System.out.print("Input a month number: ");
int month = input.nextInt();
System.out.print("Input a year: ");
int year = input.nextInt();
switch (month) {
case 1:
MonthOfName = "January";
number_Of_DaysInMonth = 31;
break;
case 2:
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
MonthOfName = "March";
number_Of_DaysInMonth = 31;
break;
case 4:
MonthOfName = "April";
number_Of_DaysInMonth = 30;
break;
case 5:
MonthOfName = "May";
number_Of_DaysInMonth = 31;
break;
case 6:
MonthOfName = "June";
number_Of_DaysInMonth = 30;
break;
case 7:
MonthOfName = "July";
number_Of_DaysInMonth = 31;
break;
case 8:
MonthOfName = "August";
number_Of_DaysInMonth = 31;
break;
case 9:
MonthOfName = "September";
number_Of_DaysInMonth = 30;
break;
case 10:
MonthOfName = "October";
number_Of_DaysInMonth = 31;
break;
case 11:
MonthOfName = "November";
number_Of_DaysInMonth = 30;
break;
case 12:
MonthOfName = "December";
number_Of_DaysInMonth = 31;
}
System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days\n");
}
}
Exercise7.main(null);
import java.util.Scanner;
public class Exercise8 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input an alphabet: ");
String input = in.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("Error. Not a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
}
else if (vowels)
{
System.out.println("Input letter is Vowel");
}
else
{
System.out.println("Input letter is Consonant");
}
}
}
Exercise8.main(null);
import java.util.Scanner;
public class Exercise9 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the year: ");
int year = in.nextInt();
boolean x = (year % 4) == 0;
boolean y = (year % 100) != 0;
boolean z = ((year % 100 == 0) && (year % 400 == 0));
if (x && (y || z))
{
System.out.println(year + " is a leap year");
}
else
{
System.out.println(year + " is not a leap year");
}
}
}
Exercise9.main(null);
public class Exercise10 {
public static void main(String[] args)
{
int i;
System.out.println ("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
}
}
Exercise10.main(null);
import java.util.Scanner;
public class Exercise11 {
public static void main(String[] args)
{
int i, n, sum=0;
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
n = in.nextInt();
}
System.out.println("The first n natural numbers are : "+n);
for(i=1;i<=n;i++)
{
System.out.println(i);
sum+=i;
}
System.out.println("The Sum of Natural Number upto "+n+ " terms : " +sum);
}
}
Exercise11.main(null);
import java.util.Scanner;
public class Exercise12 {
public static void main(String[] args)
{
int i,n=0,s=0;
double avg;
{
System.out.println("Input the 5 numbers : ");
}
for (i=0;i<5;i++)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
s +=n;
}
avg=s/5;
System.out.println("The sum of 5 no is : " +s+"\nThe Average is : " +avg);
}
}
Exercise12.main(null);
import java.util.Scanner;
public class Exercise13 {
public static void main(String[] args)
{
int i,n;
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
System.out.println("Number is : " +i+" and cube of " +i+" is : "+(i*i*i));
}
}
}
Exercise13.main(null);
import java.util.Scanner;
public class Exercise14 {
public static void main(String[] args)
{
int j,n;
System.out.print("Input the number(Table to be calculated): ");
{
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("\n");
for(j=0;j<=n;j++)
System.out.println(n+" X "+j+" = " +n*j);
}
}
}
Exercise14.main(null);
import java.util.Scanner;
public class Exercise15 {
public static void main(String[] args)
{
int i,n,sum=0;
System.out.print("Input number of terms is: ");
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("\nThe odd numbers are :");
for(i=1;i<=n;i++)
{
System.out.println (2*i-1);
sum+=2*i-1;
}
System.out.println ("The Sum of odd Natural Number upto " +n+" terms is: " +sum);
}
}
}
Exercise15.main(null);
import java.util.Scanner;
public class Exercise16 {
public static void main(String[] args)
{
int i,j,n;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println("");
}
}
}
Exercise16.main(null);
import java.util.Scanner;
public class Exercise17 {
public static void main(String[] args)
{
int i,j,n;
System.out.print("Input number of n : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(i);
System.out.println("");
}
}
}
Exercise17.main(null);
import java.util.Scanner;
public class Exercise18 {
public static void main(String[] args)
{
int i,j,n,k=1;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(k++);
System.out.println("");
}
}
}
Exercise18.main(null);
import java.util.Scanner;
public class Exercise19 {
public static void main(String[] args)
{
int i,j,n,s,x;
System.out.print ("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
s=n+4-1;
for(i=1;i<=n;i++)
{
for(x=s;x!=0;x--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
s--;
}
}
}
Exercise19.main(null);
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int numberOfRows;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
numberOfRows = in.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();
}
}
}
Main.main(null);