1.
Print Hello World
public class Hello {
public static void main(String[] args) {
[Link]("Hello World");
}
}
Output
Hello World
2. Read and Print a Number
import [Link];
public class Number {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link]();
[Link]("Number = " + num);
}
}
Input
25
Output
Number = 25
3. Read and Print a Name
import [Link];
public class Name {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String name = [Link]();
[Link]("Name: " + name);
}
}
Input
John
Output
Name: John
4. Add Two Numbers
import [Link];
public class Add {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int sum = a + b;
[Link]("Sum = " + sum);
}
}
Input
10
20
Output
Sum = 30
5. Subtract Two Numbers
import [Link];
public class Subtract {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Difference = " + (a - b));
}
}
Input
15
5
Output
Difference = 10
6. Multiply Two Numbers
import [Link];
public class Multiply {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Product = " + (a * b));
}
}
Input
5
4
Output
Product = 20
7. Divide Two Numbers
import [Link];
public class Divide {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Quotient = " + (a / b));
}
}
Input
20
5
Output
Quotient = 4
8. Find Remainder
import [Link];
public class Remainder {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Remainder = " + (a % b));
}
}
Input
17
5
Output
Remainder = 2
9. Calculate Square
import [Link];
public class Square {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
[Link]("Square = " + (n * n));
}
}
Input
6
Output
Square = 36
10. Calculate Cube
import [Link];
public class Cube {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
[Link]("Cube = " + (n * n * n));
}
}
Input
3
Output
Cube = 27
11. Swap Two Numbers (Using Third Variable)
import [Link];
public class Swap {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int temp = a;
a = b;
b = temp;
[Link]("A = " + a);
[Link]("B = " + b);
}
}
Input
10
20
Output
A = 20
B = 10
12. Find Average of Three Numbers
import [Link];
public class Average {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
double avg = (a + b + c) / 3.0;
[Link]("Average = " + avg);
}
}
Input
10
20
30
Output
Average = 20.0
13. Area of Rectangle
import [Link];
public class Rectangle {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int length = [Link]();
int breadth = [Link]();
[Link]("Area = " + (length * breadth));
}
}
Input
8
5
Output
Area = 40
14. Perimeter of Rectangle
import [Link];
public class Perimeter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int l = [Link]();
int b = [Link]();
[Link]("Perimeter = " + (2 * (l + b)));
}
}
Input
8
5
Output
Perimeter = 26
15. Area of Circle
import [Link];
public class Circle {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double r = [Link]();
double area = 3.14 * r * r;
[Link]("Area = " + area);
}
}
Input
7
Output
Area = 153.86
16. Celsius to Fahrenheit
import [Link];
public class Temperature {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double c = [Link]();
double f = (c * 9 / 5) + 32;
[Link]("Fahrenheit = " + f);
} }
Input
25
Output
Fahrenheit = 77.0
17. Simple Interest
import [Link];
public class Interest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int p = [Link]();
int t = [Link]();
int r = [Link]();
int si = (p * t * r) / 100;
[Link]("Simple Interest = " + si);
}
}
Input
1000
2
5
Output
Simple Interest = 100
18. Print ASCII Value
import [Link];
public class ASCIIValue {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
char ch = [Link]().charAt(0);
[Link]("ASCII = " + (int) ch);
}
}
Input
A
Output
ASCII = 65
19. Calculate Salary
import [Link];
public class Salary {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int days = [Link]();
int wage = [Link]();
int salary = days * wage;
[Link]("Salary = " + salary);
}
}
Input
30
500
Output
Salary = 15000
20. Total Marks and Percentage
import [Link];
public class Marks {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int m1 = [Link]();
int m2 = [Link]();
int m3 = [Link]();
int total = m1 + m2 + m3;
double percentage = total / 3.0;
[Link]("Total = " + total);
[Link]("Percentage = " + percentage);
}
}
Input
80
90
85
Output
Total = 255
Percentage = 85.0