0% found this document useful (0 votes)
23 views4 pages

10 Beginner Java Programs Explained

The document presents 10 simple Java programs designed for beginners, covering fundamental concepts such as printing output, basic arithmetic operations, conditional statements, loops, and string manipulation. Each program is accompanied by code snippets demonstrating its functionality, including examples like 'Hello World', a simple calculator, and factorial calculation. These examples serve as practical exercises to help new programmers understand Java syntax and logic.

Uploaded by

Vandana Vijayan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

10 Beginner Java Programs Explained

The document presents 10 simple Java programs designed for beginners, covering fundamental concepts such as printing output, basic arithmetic operations, conditional statements, loops, and string manipulation. Each program is accompanied by code snippets demonstrating its functionality, including examples like 'Hello World', a simple calculator, and factorial calculation. These examples serve as practical exercises to help new programmers understand Java syntax and logic.

Uploaded by

Vandana Vijayan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

10 Simple Java Programs for Beginners

1. Hello World
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World");
}
}

2. Add Two Numbers


public class AddTwoNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
[Link]("Sum = " + sum);
}
}

3. Check Even or Odd


import [Link];

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if(num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}

4. Largest of Two Numbers


public class LargestOfTwo {
public static void main(String[] args) {
int a = 10, b = 20;

if(a > b)
[Link](a + " is larger");
else
[Link](b + " is larger");
}
}

5. Print 1 to 10 Using Loop


public class PrintNumbers {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++) {
[Link](i);
}
}
}

6. Simple Calculator
import [Link];

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");


int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

[Link]("Add: " + (a + b));


[Link]("Subtract: " + (a - b));
[Link]("Multiply: " + (a * b));
[Link]("Divide: " + (a / b));
}
}

7. Check Positive or Negative


import [Link];

public class PositiveNegative {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if(num > 0)
[Link]("Positive");
else if(num < 0)
[Link]("Negative");
else
[Link]("Zero");
}
}

8. Factorial of a Number
import [Link];

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();

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

[Link]("Factorial = " + fact);


}
}

9. Sum of Digits
import [Link];

public class SumOfDigits {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();

int sum = 0;
while(num > 0) {
sum += num % 10;
num = num / 10;
}

[Link]("Sum of digits = " + sum);


}
}
10. Reverse a String
import [Link];

public class ReverseString {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();

String rev = "";


for(int i = [Link]() - 1; i >= 0; i--) {
rev = rev + [Link](i);
}

[Link]("Reversed string: " + rev);


}
}

Common questions

Powered by AI

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 .

You might also like