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

Java Basics Programs

The document contains multiple simple Java programs demonstrating basic programming concepts such as printing messages, handling command line arguments, converting temperature, swapping numbers, getting user input, performing arithmetic operations, and using conditional statements. Each program is self-contained and illustrates a specific functionality or concept in Java. These examples serve as a foundational guide for beginners learning Java programming.
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)
4 views3 pages

Java Basics Programs

The document contains multiple simple Java programs demonstrating basic programming concepts such as printing messages, handling command line arguments, converting temperature, swapping numbers, getting user input, performing arithmetic operations, and using conditional statements. Each program is self-contained and illustrates a specific functionality or concept in Java. These examples serve as a foundational guide for beginners learning Java programming.
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/ 3

Simple Java Program

public class SimpleProgram {


public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}

Print Integer in Java


public class PrintInteger {
public static void main(String[] args) {
int number = 25;
System.out.println("The number is: " + number);
}
}

Command Line Arguments


public class CommandLine {
public static void main(String[] args) {
System.out.println("Argument passed: " + args[0]);
}
}

Convert Fahrenheit to Celsius


import java.util.Scanner;

public class FahrenheitToCelsius {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Fahrenheit: ");
float f = sc.nextFloat();
float c = (f - 32) * 5/9;
System.out.println("Celsius: " + c);
}
}

Swap Two Numbers Using Variable


public class SwapWithTemp {
public static void main(String[] args) {
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);
}
}

Swap Two Numbers Without Variable


public class SwapWithoutTemp {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + ", b = " + b);
}
}

Get Input Using Scanner


import java.util.Scanner;

public class GetInput {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}

Add Two Numbers


import java.util.Scanner;

public class AddTwoNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Sum: " + sum);
}
}

Find Largest of Two Numbers


import java.util.Scanner;

public class LargestNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();

if (a > b) {
System.out.println("Largest: " + a);
} else {
System.out.println("Largest: " + b);
}
}
}

If-Else and Nested If


import java.util.Scanner;

public class IfElseExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

if (num > 0) {
if (num % 2 == 0) {
System.out.println("Positive and Even");
} else {
System.out.println("Positive and Odd");
}
} else {
System.out.println("Number is not positive");
}
}
}

Check Odd or Even


import java.util.Scanner;

public class OddEvenCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}

You might also like