[Go to site: main page, start]

0% found this document useful (0 votes)
30 views5 pages

20 Beginner-Friendly Java Programs

The document provides direct Java code for 20 beginner-friendly programs, covering basic concepts such as printing messages, arithmetic operations, conditional statements, loops, and more. Each program is presented with a class and a main method, demonstrating various functionalities like calculating areas, converting temperatures, and generating Fibonacci series. The author offers to combine all programs into a single Java file with a menu for easier execution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

20 Beginner-Friendly Java Programs

The document provides direct Java code for 20 beginner-friendly programs, covering basic concepts such as printing messages, arithmetic operations, conditional statements, loops, and more. Each program is presented with a class and a main method, demonstrating various functionalities like calculating areas, converting temperatures, and generating Fibonacci series. The author offers to combine all programs into a single Java file with a menu for easier execution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Alright, here’s the direct Java code for all 20 programs you listed.

I’ll keep them simple and beginner-friendly.

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

2. Name, Age, City


class PersonalInfo {
public static void main(String[] args) {
String name = "John";
int age = 25;
String city = "New York";
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("City: " + city);
}
}

3. Add Two Numbers


class AddNumbers {
public static void main(String[] args) {
int a = 5, b = 7;
int sum = a + b;
[Link]("Sum: " + sum);
}
}

4. Area of Rectangle
class RectangleArea {
public static void main(String[] args) {
double length = 5, width = 3;
double area = length * width;
[Link]("Area: " + area);
}
}

5. Celsius to Fahrenheit
class TempConvert {
public static void main(String[] args) {
double celsius = 25;
double fahrenheit = (celsius * 9/5) + 32;
[Link]("Fahrenheit: " + fahrenheit);
}
}

6. Swap Two Numbers


class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
[Link]("a = " + a + ", b = " + b);
}
}

7. Square and Cube


class SquareCube {
public static void main(String[] args) {
int num = 4;
[Link]("Square: " + (num * num));
[Link]("Cube: " + (num * num * num));
}
}

8. Even or Odd
class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}

9. Simple Interest
class SimpleInterest {
public static void main(String[] args) {
double p = 1000, r = 5, t = 2;
double si = (p * r * t) / 100;
[Link]("Simple Interest: " + si);
}
}
10. Perimeter of Circle
class CirclePerimeter {
public static void main(String[] args) {
double radius = 7;
double perimeter = 2 * [Link] * radius;
[Link]("Perimeter: " + perimeter);
}
}

11. Largest of Two Numbers


class LargestTwo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
[Link](a + " is largest");
else
[Link](b + " is largest");
}
}

12. Largest of Three Numbers


class LargestThree {
public static void main(String[] args) {
int a = 15, b = 30, c = 25;
if (a >= b && a >= c)
[Link](a + " is largest");
else if (b >= a && b >= c)
[Link](b + " is largest");
else
[Link](c + " is largest");
}
}

13. Positive, Negative, or Zero


class CheckNumber {
public static void main(String[] args) {
int num = -5;
if (num > 0)
[Link]("Positive");
else if (num < 0)
[Link]("Negative");
else
[Link]("Zero");
}
}
14. Leap Year
class LeapYear {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
[Link]("Leap Year");
else
[Link]("Not a Leap Year");
}
}

15. Numbers 1 to 10
class OneToTen {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
[Link](i);
}
}

16. Multiplication Table


class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for (int i = 1; i <= 10; i++)
[Link](num + " x " + i + " = " + (num * i));
}
}

17. Sum of First 10 Natural Numbers


class SumNatural {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
[Link]("Sum: " + sum);
}
}

18. Reverse a Number


class ReverseNumber {
public static void main(String[] args) {
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link]("Reversed: " + rev);
}
}

19. Fibonacci Series


class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
[Link](a + " " + b);
for (int i = 3; i <= n; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

20. Factorial
class Factorial {
public static void main(String[] args) {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++)
fact *= i;
[Link]("Factorial: " + fact);
}
}

If you want, I can combine all these into one single Java file with a menu so you can run all
programs together instead of separate files.

Do you want me to prepare that combined all-in-one Java menu program?

Common questions

Powered by AI

Incorporating user input would make these Java applications interactive and flexible. For example, using a Scanner object, users could input custom values for temperature in the 'TempConvert' program or specify their birth year in the 'LeapYear' program, adapting outputs based on personalized input .

The program 'CirclePerimeter' illustrates the use of constants and expressions by employing the constant 'Math.PI' to calculate the circumference of a circle with the expression '2 * Math.PI * radius'. This usage expresses the formula for the perimeter efficiently using a built-in constant for precision .

The program 'LeapYear' uses conditional logic to determine if a year is a leap year by checking two conditions: (1) if the year is divisible by 4 and not divisible by 100, or (2) if the year is divisible by 400. This ensures that only years satisfying these conditions are identified as leap years .

In the 'MultiplicationTable' program, a 'for' loop iterates from 1 to 10. Within each iteration, it prints the product of the number and the current iteration index, effectively printing the multiplication table for the specified number. Loops simplify this repetitive operation by automating the process, avoiding manual multiplication for each row .

Modularity, as exemplified by separate classes for each functionality, provides flexibility, ease of maintenance, and scalability. When creating extensive programs, modular classes can be reused, tested independently, and debugged easily. This also facilitates collaboration, as different developers can work on different modules without conflict .

The 'Fibonacci' program generates numbers iteratively, which is efficient compared to a recursive approach that can be costly due to repeated calculations. However, for excessive terms, the space complexity can be optimized by further reducing stored variables from 'a', 'b', and 'c' to only two variables, but the implemented algorithm is already optimized for basic use cases .

The 'LargestThree' program evaluates the largest number using a series of relational operator checks. It first checks if 'a' is greater than or equal to both 'b' and 'c', then assesses if 'b' is greater than or equal to the others if the first condition fails, defaulting to 'c' in the final else block. This sequence ensures that only one number is conclusively selected as the largest .

The 'ReverseNumber' program reverses an integer by iteratively extracting the last digit using 'num % 10' and appending this digit to a new number, 'rev', which is multiplied by 10 each time a digit is added. The original number is then divided by 10 in each iteration to remove the last digit until the number becomes zero .

When using 'double' data types in Java programs for calculations, it is important to consider rounding errors due to floating-point precision. For accurate results in applications like 'TempConvert' and 'RectangleArea', using libraries or formatting outputs (e.g., using printf) to control the number of decimal places can improve precision and display consistency .

The program 'TempConvert' converts Celsius to Fahrenheit using the formula fahrenheit = (celsius * 9/5) + 32. This demonstrates arithmetic operations by performing multiplication, division, and addition in a single expression to convert temperature units .

You might also like