menu

Java Basic Programs for Beginners - Important Points

Java Basic Programs for Beginners - MCQ


Java is a widely used programming language for developing software applications. If you are new to Java programming, it's important to start with the basics. In this write-up, we will provide an overview of how to write basic Java programs.

How to write Basic Java Programs

Setting up the Development Environment: Before writing Java programs, you need to install the Java Development Kit (JDK) on your computer. The JDK includes a compiler, which is required to convert your Java code into executable code.

Creating a Java Program: To create a Java program, you need to create a file with a .java extension. The name of the file should be the same as the name of the class, which is the main container for the program. For example, if the class is called "HelloWorld", the file name should be "HelloWorld.java".

Writing the Code: In Java, the main method is the entry point for the program. It is where the program starts running. To write a basic Java program, you need to include the following code in your main method:

public static void main(String[] args) {

   // Your code here

}

Compiling the Code: Once you have written your code, you need to compile it using the Java compiler. To do this, open a command prompt and navigate to the directory where your .java file is located. Then, type the following command:

javac filename.java

This will compile your code and generate a .class file, which contains the bytecode that can be executed by the Java Virtual Machine (JVM).

Running the Program: To run your Java program, you need to use the java command followed by the name of the class containing the main method. For example, if your class is called "HelloWorld", you would run the following command:

java HelloWorld

This will execute your program and display the output in the console.

Basic Java Programs

Some basic Java programs that beginners should learn are:

Hello World Program

public class HelloWorld {

   public static void main(String[] args) {

      System.out.println("Hello, World!");

   }

}

Addition of Two Numbers

public class AddNumbers {

   public static void main(String[] args) {

      int num1 = 5, num2 = 10, sum;

      sum = num1 + num2;

      System.out.println("Sum of " + num1 + " and " + num2 + " is " + sum);

   }

}

Finding the Area of a Circle

public class AreaOfCircle {

   public static void main(String[] args) {

      double radius = 3.5, area;

      area = Math.PI * radius * radius;

      System.out.println("Area of circle with radius " + radius + " is " + area);

   }

}

Fibonacci Series

public class FibonacciSeries {

   public static void main(String[] args) {

      int n = 10, t1 = 0, t2 = 1;

      System.out.print("Fibonacci Series: ");

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

         System.out.print(t1 + " + ");

         int sum = t1 + t2;

         t1 = t2;

         t2 = sum;

      }

   }

}

Factorial of a Number

public class Factorial {

   public static void main(String[] args) {

      int num = 5, fact = 1;

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

         fact *= i;

      }

      System.out.println("Factorial of " + num + " is " + fact);

   }

}

Armstrong Number

public class ArmstrongNumber {

   public static void main(String[] args) {

      int num = 153, originalNum, remainder, result = 0;

      originalNum = num;

      while (originalNum != 0) {

         remainder = originalNum % 10;

         result += Math.pow(remainder, 3);

         originalNum /= 10;

      }

      if (result == num)

         System.out.println(num + " is an Armstrong number.");

      else

         System.out.println(num + " is not an Armstrong number.");

   }

}

Palindrome Number

public class PalindromeNumber {

   public static void main(String[] args) {

      int num = 121, reversedInteger = 0, remainder, originalInteger;

      originalInteger = num;

      while( num != 0 ){

         remainder = num % 10;

         reversedInteger = reversedInteger * 10 + remainder;

         num /= 10;

      }

      if (originalInteger == reversedInteger)

         System.out.println(originalInteger + " is a palindrome.");

      else

         System.out.println(originalInteger + " is not a palindrome.");

   }

}

Greatest Common Divisor (GCD) or Highest Common Factor (HCF) of Two Numbers

public class GCD {

   public static void main(String[] args) {

      int num1 = 81, num2 = 153;

      while (num1 != num2) {

         if(num1 > num2)

            num1 -= num2;

         else

            num2 -= num1;

      }

      System.out.println("GCD of given numbers is: " + num1);

   }

}

Sum of Natural Numbers

public class SumOfNaturalNumbers {

   public static void main(String[] args) {

      int num = 10, sum = 0;

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

         sum += i;

      }

      System.out.println("Sum of first " + num + " natural numbers is: " + sum);

   }

}

Reverse a String

public class ReverseString {

   public static void main(String[] args) {

      String str = "Hello World!";

      String reverse = "";

      for(int i = str.length() - 1; i >= 0; --i) {

         reverse += str.charAt(i);

      }

      System.out.println("Reversed string: " + reverse);

   }

}

Check if a Number is Positive or Negative

public class PositiveOrNegative {

   public static void main(String[] args) {

      int num = -12;

      if (num > 0)

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

      else if (num < 0)

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

      else

         System.out.println(num + " is neither positive nor negative.");

   }

}

Print Multiplication Table of a Number

public class MultiplicationTable {

   public static void main(String[] args) {

      int num = 7;

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

         System.out.println(num + " x " + i + " = " + (num*i));

      }

   }

}

These basic programs demonstrate concepts such as strings, conditionals, and loops. By understanding and practicing with these programs, beginners can further develop their Java programming skills.

Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now