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

Direct Java Code

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)
3 views5 pages

Direct Java Code

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
You are on page 1/ 5

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) {
System.out.println("Hello World");
}
}

2. Name, Age, City


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

3. Add Two Numbers


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

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

5. Celsius to Fahrenheit
class TempConvert {
public static void main(String[] args) {
double celsius = 25;
double fahrenheit = (celsius * 9/5) + 32;
System.out.println("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;
System.out.println("a = " + a + ", b = " + b);
}
}

7. Square and Cube


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

8. Even or Odd
class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
System.out.println("Even");
else
System.out.println("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;
System.out.println("Simple Interest: " + si);
}
}
10. Perimeter of Circle
class CirclePerimeter {
public static void main(String[] args) {
double radius = 7;
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter: " + perimeter);
}
}

11. Largest of Two Numbers


class LargestTwo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println(a + " is largest");
else
System.out.println(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)
System.out.println(a + " is largest");
else if (b >= a && b >= c)
System.out.println(b + " is largest");
else
System.out.println(c + " is largest");
}
}

13. Positive, Negative, or Zero


class CheckNumber {
public static void main(String[] args) {
int num = -5;
if (num > 0)
System.out.println("Positive");
else if (num < 0)
System.out.println("Negative");
else
System.out.println("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))
System.out.println("Leap Year");
else
System.out.println("Not a Leap Year");
}
}

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

16. Multiplication Table


class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for (int i = 1; i <= 10; i++)
System.out.println(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;
System.out.println("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;
}
System.out.println("Reversed: " + rev);
}
}

19. Fibonacci Series


class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 3; i <= n; i++) {
int c = a + b;
System.out.print(" " + 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;
System.out.println("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?

You might also like