Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Java Programs for Practice : Know the Simple Java Programs for Beginners

      Know the Simple Java Programs for Beginners
      Blog

      Java Programs for Practice : Know the Simple Java Programs for Beginners

      Java is the universal standard for creating and distributing embedded and mobile apps, gaming, Website content, and enterprise solutions, and is the basis for nearly any other sort of networked program. Java, used by more than 9 million developers worldwide, streamlines the creation, distribution, and consumption of innovative software and services.

      Practice and working through exercises are the most effective ways to learn new stuff. By working through the tasks, ranging from the simplest to the most advanced, you can get some hands-on experience with the Java programming language’s concepts.

      Hopefully, you’ll put your newfound knowledge of Java to good use by completing these exercises. Enrich your Java fundamentals & programming skills by practicing exercises. Enroll in the top Java training in Chennai and elevate your career.

      Java Code for Printing an Integer

      This program will teach you how to print a user-entered number in Java. System.in stores the integer in a variable, whereas System.out displays it on the screen.

      import java.util.Scanner;

      public class HelloWorld {

      public static void main(String[] args) {

              // Creates a reader instance which takes

              // input from standard input – keyboard

              Scanner reader = new Scanner(System.in);

              System.out.print(“Enter a number: “);

              // nextInt() reads the next integer from the keyboard

              int number = reader.nextInt();

              // println() prints the following line to the output screen

              System.out.println(“You entered: ” + number);

          }

      Output : 

      Enter a number: 22

      Entered by user: 22

      Addition of Two Integers Using Java

      This program will teach you how to save and add two integers in Java. The full sum is displayed on the screen following addition.

      class Main {

        public static void main(String[] args) {

          int first = 20;

          int second = 30;

          // add two numbers

          int sum = first + second;

          System.out.println(first + ” + ” + second + ” = ”  + sum);

        }

      }

      Output

      Enter two numbers

      20 30

      The sum is: 50

      Improve your coding skills by working out these Java programs for beginners.

      Computing Quotient and Remainder Using Java

      This Java program will teach you how to determine the quotient and remainder from a given dividend and divisor.

      public class QuotientRemainder {

       public static void main(String[] args) {

      int dividend = 26, divisor = 6;

      int quotient = dividend / divisor;

      int remainder = dividend % divisor;

      System.out.println(“Quotient = ” + quotient);

      System.out.println(“Remainder = ” + remainder);

        }

      }

      Output

      Quotient = 4

      Remainder = 2

      Multiplication of two Floating-Point Numbers Using Java

      This application will teach you how to multiply two floating-point values in Java, save the result, and output it to the screen.

      public class MultiplyTwoNumbers

      {

      public static void main(String[] args) {

      float first = 2.5f;

      float second = 3.0f;

      float product = first * second;

      System.out.println(“The product is: ” + product);

          }

      }

      Output

      The product is: 7.5

      Determine whether a given integer is a palindrome

      A palindrome is a number that produces the same result when reversed. We find the inverse of the provided integer and determine whether or not it is equivalent.

      public static void main(String[] args)

      {

          Scanner in = new Scanner(System.in);

          String str = in.nextLine();

          int length = str.length();         

          boolean isPalindrome = true;          

          for(int i = 0; i < length; i++)

          {

              if(str.charAt(i) != str.charAt(length-1-i)) {

                  System.out.println(“Snot a palindrome.”);

                  isPalindrome = false;

                  break;

              }

          }          

          if(isPalindrome) {

              System.out.println(“palindrome.”);

          }

      }

      Programming in Java to Determine If a Number Is Even or Odd

      This software teaches you how to determine whether a user-inputted number is even or odd. The Java if…else statement and ternary operator will be used to do this.

      import java.util.Scanner;

      public class EvenOdd {

          public static void main(String[] args) {

              Scanner reader = new Scanner(System.in);

              System.out.print(“Enter a number: “);

              int num = reader.nextInt();

              if(num % 2 == 0)

                  System.out.println(num + ” is even”);

              else

                  System.out.println(num + ” is odd”);

          }

      }

      Output

      Enter a number: 17

      12 is odd

      Determine Whether a Letter Is a Vowel or Consonant with a Java Program

      You will discover how to use Java’s if..else and switch statements to determine whether an alphabet is a vowel or a consonant in this program.

      public class VowelConsonant {

          public static void main(String[] args) {

              char ch = ‘i’;

              if(ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ )

                  System.out.println(ch + ” is vowel”);

              else

                  System.out.println(ch + ” is consonant”);

          }

      }

      Output

      i is vowel

      Finding the Largest of Three Numbers in Java

      You will discover how to use Java’s if else and nested if..else statements to determine which of three values is the largest in this application.

      public class Largest {

          public static void main(String[] args) {

              double n1 = -2.5, n2 = 5.1, n3 = 7.5;

              if( n1 >= n2 && n1 >= n3)

                  System.out.println(n1 + ” is the largest number.”);

              else if (n2 >= n1 && n2 >= n3)

                  System.out.println(n2 + ” is the largest number.”);

              else

                  System.out.println(n3 + ” is the largest number.”);

          }

      }

      Output

      7.5 is the largest number.

      These Java Programs for practice will enhance your coaching speed and expertise.

      Check the Leap Year with Java

      You will learn how to determine whether a given year is a leap year in this program. A if statement is used to check this.

      public class Main {

        public static void main(String[] args) {

          // year to be checked

          int year = 2008;

          boolean leap = false;

          // if the year is divided by 4

          if (year % 4 == 0) {

            // if the year is century

            if (year % 100 == 0) {

              // if year is divided by 400

              // then it is a leap year

              if (year % 400 == 0)

                leap = true;

              else

                leap = false;

            }

            // if the year is not century

            else

              leap = true;

          }

          else

            leap = false;

          if (leap)

            System.out.println(year + ” is a leap year.”);

          else

            System.out.println(year + ” is not a leap year.”);

        }

      }

      Output

      2008 is not a leap year.

      Programming in Java to Determine a Number's Positive or Negative Sign

      You’ll discover how to determine whether a given number is positive or negative in this program. In Java, this is accomplished by using an if else statement.

      public class PositiveNegative {

          public static void main(String[] args) {

              double number = 11.8;

              // true if number is less than 0

              if (number < 0.0)

                  System.out.println(number + ” is a negative number.”);

              // true if number is greater than 0

              else if ( number > 0.0)

                  System.out.println(number + ” is a positive number.”);

       

             // if both test expression is evaluated to false

              else

                  System.out.println(number + ” is 0.”);

          }

      }

      Output

      11.8 is a positive integer

      Finding each Root of a Quadratic Equation Using Java

      This application teaches you how to use Java’s format() method to locate the roots of a quadratic equation and print them.

      public class Main {

        public static void main(String[] args) {

          // value a, b, and c

          double a = 2.3, b = 4, c = 5.6;

          double root1, root2;

          // calculate the determinant (b2 – 4ac)

          double determinant = b * b – 4 * a * c;

          // check if determinant is greater than 0

          if (determinant > 0) {

            // two real and distinct roots

            root1 = (-b + Math.sqrt(determinant)) / (2 * a);

            root2 = (-b – Math.sqrt(determinant)) / (2 * a);

            System.out.format(“root1 = %.2f and root2 = %.2f”, root1, root2);

          }

          // check if determinant is equal to 0

          else if (determinant == 0) {

            // two real and equal roots

            // determinant is equal to 0

            // so -b + 0 == -b

            root1 = root2 = -b / (2 * a);

            System.out.format(“root1 = root2 = %.2f;”, root1);

          }

          // if determinant is less than zero

          else {

            // roots are complex number and distinct

            double real = -b / (2 * a);

            double imaginary = Math.sqrt(-determinant) / (2 * a);

            System.out.format(“root1 = %.2f+%.2fi”, real, imaginary);

            System.out.format(“\nroot2 = %.2f-%.2fi”, real, imaginary);

          }

        }

      }

      Output

      root1 = -0.87+1.30i and root2 = -0.87-1.30i

      There is no doubt that Java is a structured Query language and mastering it is essential to land on empowering career. Join the Java training in Chennai at SLA.

      Calculating the Sum of Natural Numbers using Java

      You’ll discover how to use Java’s for loop and while loop to compute the sum of natural numbers in this application.

      public class SumNatural

      {

          public static void main(String[] args)

      {

              int num = 100, sum = 0;

              for(int i = 1; i <= num; ++i)

              {

                  // sum = sum + i;

                  sum += i;

              }

              System.out.println(“Sum = ” + sum);

          }

      }

      Output

      Sum = 5050

      Java Code to Determine the Factorial of a Number

      In this program, you’ll discover how to use Java’s for and while loops to calculate the factorial of a given number.

      public class Factorial {

          public static void main(String[] args) {

              int num = 10;

              long factorial = 1;

              for(int i = 1; i <= num; ++i)

              {

                  // factorial = factorial * i;

                  factorial *= i;

              }

              System.out.printf(“Factorial of %d = %d”, num, factorial);

          }

      }

      Output

      Factorial of 5 = 120

      Create multiplication tables using a Java program

      You will discover how to create a multiplication table for a given integer in this program. Java’s for and while loops are used to do this.

      public class MultiplicationTable {

          public static void main(String[] args) {

              int num = 5;

              for(int i = 1; i <= 10; ++i)

              {

                  System.out.printf(“%d * %d = %d \n”, num, i, num * i);

              }

          }

      }

      Output

      4 * 1 = 4

      4 * 2 = 8

      4 * 3 = 12

      4 * 4 = 16

      4 * 5 = 20

      4 * 6 = 24

      4 * 7 = 28

      4 * 8 = 32

      4 * 9 = 36

      4 * 10 = 40

      Java Program to Calculate the GCD of Two Numbers

      You will learn how to find the GCD of two numbers in Kotlin in this application. If else statements are used in conjunction with for and while loops to accomplish this.

      class Main {

        public static void main(String[] args) {

          // find GCD between n1 and n2

          int n1 = 81, n2 = 153;

          // initially set to gcd

          int gcd = 1;

          for (int i = 1; i <= n1 && i <= n2; ++i) {

       

            // check if i perfectly divides both n1 and n2

            if (n1 % i == 0 && n2 % i == 0)

              gcd = i;

          }

          System.out.println(“GCD of ” + n1 +” and ” + n2 + ” is ” + gcd);

        }

      }

      Output

      GCD of 81 and 153 is 9

      Java Code to Calculate the LCM of Two Numbers

      You will learn how to find the LCM of two numbers both using and without the GCD in this application. Java’s for and while loops are used to do this.

      public class Main

      {

        public static void main(String[] args)

      {

          int n1 = 72, n2 = 120, lcm;

          // maximum number between n1 and n2 is stored in lcm

          lcm = (n1 > n2) ? n1 : n2;

          // Always true

          while(true) {

            if( lcm % n1 == 0 && lcm % n2 == 0 ) {

              System.out.printf(“The LCM of %d and %d is %d.”, n1, n2, lcm);

              break;

            }

            ++lcm;

          }

        }

      }

      Output

      The LCM of 72 and 120 is 360.

      Learn Java Training in Chennai at SLA to power your job prospects.

      Loop-based Java Program to Present the Alphabet (A to Z)

      This application teaches you how to use Java’s for loop to print the English alphabet in both upper- and lowercase.

      class Main {

        public static void main(String[] args) {

          char c;

          for(c = ‘A’; c <= ‘Z’; ++c)

            System.out.print(c + ” “);

          }

      }

      Output

      A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

      class Main {

        public static void main(String[] args) {

          char c;

          for(c = ‘a’; c <= ‘z’; ++c)

            System.out.print(c + ” “);

          }

      }

      Output

      a b c d e f g h i j k l m n o p q r s t u v w x y z

      Programming in Java to Reverse a Number

      This application teaches you how to use Java’s while loop and for loop to reverse a number.

      class Main {

        public static void main(String[] args) {

          int num = 1234, reversed = 0;

          System.out.println(“Original Number: ” + num);

          // run loop until num becomes 0

          while(num != 0) {

            // get last digit from num

            int digit = num % 10;

            reversed = reversed * 10 + digit;

            // remove the last digit from num

            num /= 10;

          }

          System.out.println(“Reversed Number: ” + reversed);

        }

      }

      Output

      Reversed Number: 4321

      Java Program for Power-of-Number Calculation

      You will discover how to compute a number’s power both with and without the help of the pow() function in this application.

      class Main {

        public static void main(String[] args) {

          int base = 3, exponent = 4;

          long result = 1;

          while (exponent != 0) {

            result *= base;

            –exponent;

          }

          System.out.println(“Answer = ” + result);

        }

      }

      Output

      Answer = 81

      Create a Java program to swap two numbers

      There are two ways to accomplish this: with and without a third variable.

      public static void swapNumberswithtemp(int a, int b) { //using 3rd variable

      int temp = x;

      x = y;

      y = temp;

      }

      public static void swapNumberswithouttemp(int a, int b) {//without using 3rd variable

      y = y + x;

      x = y – x;

      x = y – x;

      }

      public static void main(String[] args) {

      int x = 15;

      int y = 14;

      swapNumbers(x, y);

      System.out.printf(“%d %d”, x, y);

      Create a program that will print each element of the Fibonacci series

      A Fibonacci sequence is represented by Fib(n)=Fib(n-1)+Fib (n-2)

      public static int fibonacci(int number){

          if(number == 1 || number == 2){ //base case

              return 1;

          }

          return fibonacci(number-1) + fibonacci(number -2); 

      }  

      public static void main(String args[]) {

          int number = new Scanner(System.in).nextInt();

          for(int i=1; i<=number; i++){

              System.out.print(fibonacci2(i) +” “);

          }    

      }

      Learn Java thoroughly by practicing these programs. Enroll in the best Java course in Chennai and learn Java from basic to advanced concepts to emerge successful.

      public static int fibonacci(int number) {
      if (number == 1 || number == 2) { //base case
      return 1;
      }
      return fibonacci(number – 1) + fibonacci(number – 2);
      }
      public static void main(String args[]) {
      int number = new Scanner(System.in).nextInt();
      for (int i = 1; i <= number; i++) {
      System.out.print(fibonacci2(i) + ” “);
      }
      }

      1
      Softlogic-Academy