[Go to site: main page, start]

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

Basic Java Programs for Beginners

The document contains basic Java programs demonstrating fundamental concepts such as printing a name, performing arithmetic operations, swapping numbers, calculating area, converting temperature, computing simple interest, concatenating strings, finding averages, and determining profit and loss. Each program is presented with its respective class and main method. These examples serve as a practical introduction to Java programming for beginners.
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)
5 views3 pages

Basic Java Programs for Beginners

The document contains basic Java programs demonstrating fundamental concepts such as printing a name, performing arithmetic operations, swapping numbers, calculating area, converting temperature, computing simple interest, concatenating strings, finding averages, and determining profit and loss. Each program is presented with its respective class and main method. These examples serve as a practical introduction to Java programming for beginners.
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

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);
}
}

You might also like