Basic java programs
🟢 1. Program to print a name
public class PrintName
{
public static void main(String[] args)
{
[Link]("My name is JAVA");
}
}
🟢 2. Program to Add Two Numbers
public class AddTwoNumbers {
public static void main(String[] args) {
int a = 10;
int b = 15;
int sum = a + b;
[Link]("Sum: " + sum);
}
}
🟢 3. Program to Multiply Three Numbers
public class MultiplyThreeNumbers {
public static void main(String[] args) {
int x = 2, y = 3, z = 4;
int result = x * y * z;
[Link]("Multiplication Result: " + result);
}
}
🟢 4. Program to Swap two numbers
public class SwapExample {
public static void main(String[] args)
{
int a = 10;
int b = 20;
[Link]("Before swapping:");
[Link]("a = " + a + ", b = " + b);
a = a + b; // a = 10 + 20 = 30
b = a - b; // b = 30 - 20 = 10
a = a - b; // a = 30 - 10 = 20
[Link]("After swapping:");
[Link]("a = " + a + ", b = " + b);
}
}
🟢 5. Program to Calculate Area of a Rectangle
public class AreaRectangle {
public static void main(String[] args) {
int length = 8;
int breadth = 5;
int area = length * breadth;
[Link]("Area: " + area);
}
}
🟢 6. Program to Convert Celsius to Fahrenheit
public class CelsiusToFahrenheit {
public static void main(String[] args) {
double celsius = 30.0;
double fahrenheit = (celsius * 9/5) + 32;
[Link]("Fahrenheit: " + fahrenheit);
}
}
🟢7. Program to Calculate Simple Interest
public class SimpleInterest {
public static void main(String[] args) {
double principal = 1000;
double rate = 5;
double time = 2;
double si = (principal * rate * time) / 100;
[Link]("Simple Interest: " + si);
}
}
🟢 8. Program to Concatenate Two Strings
public class ConcatenateStrings {
public static void main(String[] args) {
String str1 = "Hello, ";
String str2 = "World";
String result = str1 + str2;
[Link](result);
}
}
🟢 9. Program to Calculate Average of Three Numbers
public class Average {
public static void main(String[] args)
{
int a = 10, b = 20, c = 30;
double average = (a + b + c) / 3.0;
[Link]("Average: " + average);
}
}
🟢 10. Program to Calculate Profit and Loss Amount
public class ProfitLoss {
public static void main(String[] args)
{
int costPrice = 500;
int sellingPrice = 600;
double profit = sellingPrice - costPrice;
double loss = costPrice - sellingPrice;
[Link]("Profit : " + profit);
[Link]("Loss : " + loss);
}
}