0% found this document useful (0 votes)
10 views3 pages

Java Programs

Uploaded by

pnkumar88222
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)
10 views3 pages

Java Programs

Uploaded by

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

Here are some beginner-friendly Java programs to help you get started.

These cover basic syntax,


control flow, and simple operations.

1. Hello World
java
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Simple Calculator
java
Copy code
import java.util.Scanner;

public class SimpleCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");


double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");


double num2 = scanner.nextDouble();

System.out.println("Addition: " + (num1 + num2));


System.out.println("Subtraction: " + (num1 - num2));
System.out.println("Multiplication: " + (num1 * num2));
System.out.println("Division: " + (num1 / num2));

scanner.close();
}
}

3. Check if a Number is Even or Odd


java
Copy code
import java.util.Scanner;

public class EvenOrOdd {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = scanner.nextInt();

if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}

scanner.close();
}
}

4. Factorial of a Number
java
Copy code
import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = scanner.nextInt();
int factorial = 1;

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


factorial *= i;
}

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

scanner.close();
}
}

5. Fibonacci Series
java
Copy code
import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of terms: ");


int terms = scanner.nextInt();
int num1 = 0, num2 = 1;

System.out.print("Fibonacci Series: " + num1 + ", " + num2);

for (int i = 2; i < terms; i++) {


int nextTerm = num1 + num2;
System.out.print(", " + nextTerm);
num1 = num2;
num2 = nextTerm;
}

scanner.close();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy