1. Write a Java program to get a number from the user and print whether it is positive or negative.

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);
Input number: Number is positive

2. Write a Java program to solve quadratic equations (use if, else if and else).

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);
Input a: Input b: Input c: The equation has no real roots.

3. Take three numbers from the user and print the greatest number.

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);
Input the 1st number: Input the 2nd number: Input the 3rd number: The greatest: 9

4.Write a Java program that reads a floating-point number and prints "zero" if the number is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.

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);
Input value: Positive number

5. Write a Java program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday.

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);
Input number: Wednesday

6. Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

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);
Input floating-point number: Input floating-point another number: They are different

7. Write a Java program to find the number of days in a month.

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);
Input a month number: Input a year: February 2019 has 28 days

8. Write a Java program that takes the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message.

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);
Input an alphabet: Error. Not a letter. Enter uppercase or lowercase letter.

9. Write a Java program that takes a year from user and print whether that year is a leap year or not.

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);
Input the year: 42 is not a leap year

10. Write a program in Java to display the first 10 natural numbers.

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);
The first 10 natural numbers are:

1
2
3
4
5
6
7
8
9
10


11. Write a program in Java to display n terms of natural numbers and their sum.

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);
Input number: The first n natural numbers are : 48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
The Sum of Natural Number upto 48 terms : 1176

12. Write a program in Java to input 5 numbers from keyboard and find their sum and average

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);
Input the 5 numbers : 
The sum of 5 no is : 26
The Average is : 5.0

13. Write a program in Java to display the cube of the number upto given an integer.

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);
Input number of terms : Number is : 1 and cube of 1 is : 1
Number is : 2 and cube of 2 is : 8
Number is : 3 and cube of 3 is : 27
Number is : 4 and cube of 4 is : 64

14. Write a program in Java to display the multiplication table of a given integer.

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);
Input the number(Table to be calculated): Input number of terms : 

5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25

15. Write a program in Java to display the n terms of odd natural number and their sum.

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);
Input number of terms is: 
The odd numbers are :
1
The Sum of odd Natural Number upto 1 terms is: 1

16. Write a program in Java to display the pattern like right angle triangle with a number.

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);
Input number of rows : 1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
1234567891011
123456789101112
12345678910111213
1234567891011121314
123456789101112131415
12345678910111213141516
1234567891011121314151617
123456789101112131415161718
12345678910111213141516171819

17. Write a program in Java to make such a pattern like right angle triangle with a number which will repeat a number in a row.The pattern is as follows :

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);
Input number of n : 1
22
333
4444
55555
666666
7777777
88888888

18. Write a program in Java to make such a pattern like right angle triangle with number increased by 1.The pattern like :

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);
Input number of rows : 1
23
456

19. Write a program in Java to make such a pattern like a pyramid with a number which will repeat the number in the same row.

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);
Input number of rows :        1 
      2 2 
     3 3 3 
    4 4 4 4 

20. Write a program in Java to print the Floyd's Triangle.

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);
Input number of rows : 1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55