10 Beginner Java Programs Explained
10 Beginner Java Programs Explained
In the "Print 1 to 10 Using Loop" Java program, loops are used to iterate over a sequence of numbers from 1 to 10. This is achieved through the use of a 'for' loop which initializes a counter variable 'i' to 1 and increments it by 1 in each iteration. The condition 'i <= 10' ensures that the loop continues as long as 'i' is less than or equal to 10, allowing each number within this range to be printed sequentially. This loop structure effectively automates the repetition of outputting numbers without having to write multiple 'System.out.println()' statements .
The "Check Even or Odd" program uses a conditional statement to determine if a number is even or odd. It reads an integer input from the user and checks the remainder when this number is divided by 2 using the modulus operator (%). If the remainder is 0, the number is even, and the program prints "Even"; otherwise, it prints "Odd". This uses the fundamental property of even numbers, where they are divisible by 2 without a remainder .
The "Simple Calculator" program uses basic arithmetic operations by reading two integer inputs from the user and then applying addition, subtraction, multiplication, and division operations on these numbers. It utilizes the Scanner class for input and prints the result of each operation right after it computes them. Specifically, the program calculates the sum using '+', difference using '-', product using '*', and quotient using '/' operators. This straightforward approach allows the program to compute and display results for multiple arithmetic operations efficiently within a single execution flow .
The "Check Positive or Negative" program differentiates among positive, negative, and zero values using a series of conditional checks. It reads an integer input from the user and first checks if the number is greater than 0, in which case it prints "Positive". If the first condition fails, it checks if the number is less than 0 and, if so, prints "Negative". If neither condition is met, the program defaults to the else case, recognizing the number as zero and printing "Zero". These checks ensure the program correctly categorizes the number based on its sign .
The "Reverse a String" program reverses an input string by manipulating it character by character. It begins by reading a string from the user and initializes an empty string 'rev' to store the reversed result. The program then executes a 'for' loop starting from the last character of the string (utilizing 'str.length() - 1') and continues to index 0. In each iteration, it appends the current character to 'rev', effectively building the reversed string in order. The program concatenates characters one by one from the end of the original string to the beginning, thus achieving the reversal upon loop completion .
The "Factorial of a Number" program calculates the factorial of a non-negative integer using an iterative approach. It prompts the user to input an integer 'n', initializes a variable 'fact' to 1, and then uses a 'for' loop to iterate from 1 to 'n'. In each iteration, it multiplies 'fact' by the current loop variable 'i' and updates 'fact' with the result. After the loop completes, 'fact' holds the product of all integers from 1 to 'n', resulting in the factorial value which is then printed out .
The "Largest of Two Numbers" program uses conditional logic to compare two integers, 'a' and 'b'. It employs an 'if-else' statement where it first checks if 'a' is greater than 'b'. If true, it prints that 'a' is larger. Otherwise, the program executes the 'else' block and prints that 'b' is larger. This straightforward conditional comparison determines the larger number between the two inputs .
The "Add Two Numbers" program employs a simple algorithm that initializes two integer variables 'a' and 'b' with given values (10 and 20, respectively) and computes their sum by directly using the '+' operator. It stores the result in a variable called 'sum' and then outputs the total using 'System.out.println()', displaying the result as 'Sum = 30'. This straightforward addition makes the program a fundamental demonstration of basic arithmetic operations in Java .
A primary pitfall when using the divide operation in the "Simple Calculator" program is the risk of a division by zero error, which occurs if the second number ('b') is zero. Attempting to divide by zero in Java throws an ArithmeticException, leading to runtime failure if not properly handled. To prevent this, the program would require additional logic to check if 'b' equals zero before performing division and handle the situation appropriately, such as by displaying an error message or suggesting valid input .
The "Sum of Digits" program uses a 'while' loop to repeatedly extract and sum the digits of a number provided by the user. The loop continues executing as long as 'num' is greater than zero. During each iteration, the program calculates the current digit by taking 'num % 10', which gives the last digit of the number. It then adds this digit to 'sum' and updates 'num' by dividing it by 10 to remove the last digit. This process repeats until all digits have been added to 'sum', at which point the loop exits and the total sum is printed .