1. Write a Java program to print “Hello Java”
public class Hello {
public static void main(String[] args) {
System.out.println(“Hello Java”);
}
}
Output:
Hello Java
2. Write a java program to print the following:
Hello
Welcome to the world of Java
Let us learn Java
public class Welcome {
public static void main(String[] args) {
System.out.println(“Hello”);
System.out.println(“Welcome to the world of Java”);
System.out.println(“Let us learn Java”);
}
}
Output:
Hello
Welcome to the world of Java
Let us learn Java
3. Write a Java program to print the sum of two numbers.
public class sum
{
public static void main(String[] args)
{
int a=10,b=20,c;
c=a+b;
System.out.println(“The sum is: ” + c);
}
}
Output:
The sum is: 30
4. Write a program to add two numbers taking input from user
import java.util.Scanner;
public class sum2
{
public static void main(String[] args)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers: “);
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println(“The sum is ” + c);
}
}
Output:
Enter two numbers:
12 34
The sum is: 46
5. Write a Java program that takes two numbers as input and displays the product of two numbers.
import java.util.Scanner;
public class product
{
public static void main(String[] args)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers: “);
a=sc.nextInt();
b=sc.nextInt();
c=a*b;
System.out.println(“The product is: ” + c);
}
}
Output:
Enter two numbers:
5 8
The product is: 40
6. Write a Java program to divide two numbers and print them on the screen.
import java.util.Scanner;
public class division {
public static void main(String[] args)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers: “);
a=sc.nextInt();
b=sc.nextInt();
c=a/b;
System.out.println(“The division value is: ” + c);
}
}
Output:
Enter two numbers:
40 8
The division value is: 5
7. Write a Java program to print the sum, multiply, subtract, divide and remainder of two numbers.
import java.util.Scanner;
public class program
{
public static void main(String[] args)
{
int a, b, result;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter two numbers: “);
a = sc.nextInt();
b = sc.nextInt();
result = a + b;
System.out.println(a + “+” + b + “=” + result);
result = a – b;
System.out.println(a + “-” + b + “=” + result);
result = a * b;
System.out.println(a + “x” + b + “=” + result);
result = a / b;
System.out.println(a + “/” + b + “=” + result);
result = a % b;
System.out.println(a + “%” + b + “=” + result);
}
}
Output:
Enter two numbers:
25 5
25+5=30
25-5=20
25×5=125
25/5=5
25%5=0
8. Write a program to calculate the distance that light travels in 1000 days using long variables. Approximate speed of light in miles per second is 86000.
public class Light
{
public static void main(String[] args)
{
int lightspeed=86000;
long days=1000, seconds, distance;
seconds=days*24*60*60;
distance=lightspeed*seconds;
System.out.println(“In ” + days + ” days “);
System.out.println(“light will travel about”);
System.out.println(distance + ” miles”);
}
}
Output:
In 1000 days light will travel about 7430400000000 miles
9. Write a program to calculate the area of a circle using double.
public class Area {
public static void main(String[] args)
{
double pi,r,a;
r=10.5;
pi=3.1416;
a=pi*r*r;
System.out.println(“Area of circle is :” + a);
}
}
Output:
Area of circle is :346.3614
10. Write a program to use char variable.
public class CharDemo
{
public static void main(String[] args)
{
char ch1=88,ch2=’Y’;
System.out.println(“ch1 and ch2: “);
System.out.println(ch1 + ” ” +ch2);
ch1++;
System.out.println(“Now ch1 is is :” + ch1);
}
}
Output:
ch1 and ch2:
X Y
Now ch1 is is :Y
11. Write a program to use Boolean values.
public class BoolTest
{
public static void main(String[] args)
{
boolean b;
b=false;
System.out.println(“b is :” + b);
b = true;
System.out.println(“b is :” + b);
if (b)
System.out.println(“This is executed”);
b = false;
if (b)
System.out.println(“This is NOT executed”);
}
}
Output:
b is :false
b is :true
This is executed
12. Write a Java Program to Calculate Simple Interest
public class SimpleInterest
{
public static void main(String[] args)
{
int p =5000, r = 4, t = 2;
float si = (p * t * r) / 100;
System.out.println(“Simple interest = ” + si);
}
}
Output:
Simple interest = 400.0
13. Write a program to calculate simple interest taking input form user.
import java.util.Scanner;
public class siminterest {
public static void main(String[] args)
{
float p,r,si;
int t;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter principal amount: “);
p=sc.nextInt();
System.out.print(“Enter the rate: “);
r=sc.nextInt();
System.out.print(“Enter the time: “);
t=sc.nextInt();
si=p*r*t/100;
System.out.println(“The simple interest is: ” + si);
}
}
Output:
Enter principal amount: 5000
Enter the rate: 4
Enter the time: 2
The simple interest is: 400.0
14. Write a Java program to multiply two Floating-Point numbers.
public class FloatValue {
public static void main(String[] args)
{
float f1 = 1.5f;
float f2 = 2.0f;
float p = f1 * f2;
System.out.println(“The product is: ” + p);
}
}
Output:
The product is: 3.0
15. Write a Java program to swap two numbers.
import java.util.Scanner;
public class swap
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b,temp;
System.out.print(“Enter two numbers: “);
a=sc.nextInt();
b=sc.nextInt();
System.out.println(“Before swapping…”);
System.out.println(“a= ” + a + ” b= ” + b);
temp=a;
a=b;
b=temp;
System.out.println(“After swapping…”);
System.out.print(“a= ” + a + ” b= ” + b);
}
}
Output:
Enter two numbers: 4 6
Before swapping…
a= 4 b= 6
After swapping…
a= 6 b= 4
16. Write a Java program to swap two numbers without using third variable.
import java.util.Scanner;
public class swap1 {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.print(“Enter two numbers: “);
a=sc.nextInt();
b=sc.nextInt();
System.out.println(“Before swapping…”);
System.out.println(“a= ” + a + ” b= ” + b);
a=a+b;
b=a-b;
a=a-b;
System.out.println(“After swapping…”);
System.out.print(“a= ” + a + ” b= ” + b);
}
}
Output:
Enter two numbers: 4 6
Before swapping…
a= 4 b= 6
After swapping…
a= 6 b= 4
17. Write a Java program to print the area and perimeter of a circle. Given radius=6.7
Area of Circle=πr2
Perimeter of a circle=2πr
public class circle {
public static void main(String[] args)
{
double a,c,r=6.7, pi=3.14;
a=pi*r*r;
c=2*pi*r;
System.out.println(“Area of the circle: ” + a);
System.out.println(“Circumference of the circle: ” + c);
}
}
Output:
Area of the circle: 140.9546
Circumference of the circle: 42.076
18. Write a Java program to find the area and perimeter of a circle whose radius is user input.
import java.util.Scanner;
public class AreaPerimeter {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the radius of the circle: “);
double r=sc.nextDouble();
double a=Math.PI*r*r;
double c=2*Math.PI*r;
System.out.println(“Area of the circle: ” + a);
System.out.println(“Circumference of the circle: ” + c);
}
}
Output:
Enter the radius of the circle: 2
Area of the circle: 12.566370614359172
Circumference of the circle: 12.566370614359172
19. Write a Java program to find area and perimeter of a square.
import java.util.Scanner;
public class square {
public static void main(String[] args)
{
float side,area,peri;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the length of side of square: “);
side=sc.nextFloat();
area=side*side;
peri=4*side;
System.out.println(“Area of the square: ” + area);
System.out.println(“Perimeter of the square: ” + peri);
}
}
Output:
Enter the length of side of square: 10
Area of the square: 100.0
Perimeter of the square: 40.0
20. Write a JAVA program to convert temperature from Fahrenheit to Celsius degree.
import java.util.Scanner;
public class temp {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print(“Enter temperature in Fahrenheit: “);
double F=sc.nextDouble();
double C=(F-32)*5/9;
System.out.println(“Temperature in Celsius: “+ C);
}
}
Output:
Enter temperature in Fahrenheit: 208.4
Temperature in Celsius: 98.0
21. Write a Java program to calculate total and average marks of three subjects of a student.
import java.util.Scanner;
public class totavg {
public static void main(String[] args)
{
int sub1,sub2,sub3,total;
float avg;
System.out.print(“Enter subject1 marks:”);
Scanner sc=new Scanner(System.in);
sub1=sc.nextInt();
System.out.print(“Enter subject2 marks:”);
sub2=sc.nextInt();
System.out.print(“Enter subject3 marks:”);
sub3=sc.nextInt();
total=sub1+sub2+sub3;
System.out.println(“Total marks is ” + total);
avg=total/3;
System.out.println(“The average marks is ” + avg);
}
}
Output:
Enter subject1 marks:50
Enter subject2 marks:40
Enter subject3 marks:60
Total marks is 150
The average marks is 50.0
22. The basic salary of an employee is input through the keyboard. The DA is 15% of the basic salary while the HRA is 25% of the basic salary. Write a program to calculate his total salary.
import java.util.Scanner;
public class tsalary {
public static void main(String[] args)
{
long bs;
float da,hra,ts;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter basic salary: “);
bs=sc.nextLong();
da=(bs*15)/100;
hra=(bs*25)/100;
ts=bs+da+hra;
System.out.println(“Basic Salary: ” + bs);
System.out.println(“Dearness Allowance: ” + da);
System.out.println(“House Rent Allowance: ” + hra);
System.out.println(“Total Salary: ” + ts);
}
}
Output:
Enter basic salary: 40000
Basic Salary: 40000
Dearness Allowance: 6000.0
House Rent Allowance: 10000.0
Total Salary: 56000.0
23. Write a java program to find the square of a number.
import java.util.Scanner;
public class sq {
public static void main(String[] args) {
int num,square;
Scanner sc= new Scanner(System.in);
System.out.print(“Enter a number: “);
num=sc.nextInt();
square = num*num;
System.out.println(“Square of “+ num + ” is: “+ square);
}
}
Output:
Enter a number: 5
Square of 5 is: 25
24. Write a java program to find the square of a number using pow() .
import java.util.Scanner;
import java.lang.Math;
public class sq1 {
public static void main(String[] args) {
double num, square;
Scanner sc= new Scanner(System.in);
System.out.print(“Enter a number: “);
num = sc.nextInt();
square = Math.pow(num,2);
System.out.println(“Square of “+ num + ” is: “+ square);
}
}
Output:
Enter a number: 6
Square of 6.0 is: 36.0
25. Write a java program to find the square root of a number.
import java.util.Scanner;
import java.lang.Math;
public class sqroot {
public static void main(String[] args) {
double num, result;
Scanner sc= new Scanner(System.in);
System.out.print(“Enter a number: “);
num = sc.nextDouble();
result = Math.sqrt(num);
System.out.println(“Square root of ” + num + ” is: “+ result);
}
}
Output:
Enter a number: 64
Square root of 64.0 is: 8.0
26. Write a Java program to check a number is even or odd
public class EvenOdd
{
public static void main(String[] args)
{
int num = 20;
if (num % 2 == 0)
{
System.out.println(“Number is Even”);
}
else
{
System.out.println(“Number is Odd”);
}
}
}
Output:
Number is Even
27. Write a Java program to find a year is a Leap Year or not
import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print(“Enter a year: “);
int year=sc.nextInt();
if(year%4==0)
System.out.println(year + ” is a leap year”);
else
System.out.println(year + “is not a leap year”);
}
}
Output:
Enter a year: 2020
2020 is a leap year
28. Take three numbers from the user and print the greatest number.
import java.util.Scanner;
public class largest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter 3 numbers: “);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int n3 = sc.nextInt();
if (n1 > n2 && n1 > n3)
System.out.print(n1 + ” is the greatest number”);
else if (n2 > n1 && n2 > n3)
System.out.print(n2 + ” is the greatest number”);
else
System.out.print(n3 + ” is the greatest number”);
}
}
Output:
Enter 3 numbers: 65 34 98
98 is the greatest number
29. Write a Java program to find the largest among three numbers using conditional operator.
import java.util.Scanner;
public class largest3 {
public static void main(String[] args)
{
int a,b,c,large,largest;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter three numbers: “);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
large=a>b?a:b;
largest=c>large?c:large;
System.out.println(“Largest no is=” +largest);
}
}
Output:
Enter three numbers: 7 2 5
Largest no is=7
30. Write Java program to check a character is vowel or not.
public class vowel {
public static void main(String[] args)
{
char x=’d’;
switch(x)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
System.out.print(x + ” is a vowel”);
break;
default:
System.out.print(x + ” is not a vowel”);
}
}
}
Output:
d is not a vowel
31. Write Java program to check a character is vowel or not taking input from user.
import java.util.Scanner;
public class vowel
{
public static void main(String[] args)
{
char c;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter a character: “);
c=sc.next().charAt(0);
switch(c)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
System.out.print(c + ” is a vowel”);
break;
default:
System.out.print(c + ” is not a vowel”);
}
}
}
Output:
Enter a character: e
e is a vowel
Enter a character: z
z is not a vowel
32. Write a Java program to print 1st 20 natural numbers.
OR
Write a program in Java to print the number from 1 to 20.
public class natural {
public static void main(String[] args)
{
int i;
System.out.println(“The first 20 natural numbers: “);
for(i=1;i<=20;i++)
System.out.print(i + “\t”);
}
}
Output:
The first 20 natural numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
33. Write a program in Java to display series of number from 10 to 60.
public class print {
public static void main(String[] args) {
int i;
System.out.println(“Numbers from 10 to 60: “);
for(i=10;i<=60;i++)
System.out.print(i + “\t”);
}
}
Output:
Numbers from 10 to 60:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
34. Write a java program to calculate sum of first n natural numbers.
import java.util.Scanner;
public class series {
public static void main(String[] args)
{
int num,i,sum=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter number to find the sum: “);
num=sc.nextInt();
for(i=1;i<=num;i++)
{
sum=sum+i;
}
System.out.println(“Sum of first ” + num + ” Natural Numbers=” + sum);
}
}
Output:
Enter number to find the sum: 10
Sum of first 10 Natural Numbers=55
35. Write a Java program to print the following pattern:
*
* *
* * *
* * * *
* * * * *
public class pattern
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(” *”);
}
System.out.print(“\n”);
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
36. Write a Java program to print the following pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
public class display {
public static void main(String[] args)
{
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(” “);
System.out.print(i);
}
System.out.print(“\n”);
}
}
}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
37. Write a Java program to display a specific multiplication table.
OR
Write a Java program that takes a number as input and prints its multiplication table up to 10.
import java.util.Scanner;
public class multiplication {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print(“Input a number: “);
int n=sc.nextInt();
System.out.print(“Multiplication Table of ” + n);
for(int i=1;i<=10;i++)
{
System.out.println(n + “x” + i+”=”+ n*i);
}
}
}
Output:
Input a number: 2
Multiplication Table of 2
2×1=2
2×2=4
2×3=6
2×4=8
2×5=10
2×6=12
2×7=14
2×8=16
2×9=18
2×10=20
38. Write a Java program to display multiplication table.
public class multip {
public static void main(String[] args)
{
System.out.println(“Multiplication table from 1 to 10”);
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
{
System.out.println(i + “x” + j + “=” + i*j);
}
}
}
Output:
Multiplication table from 1 to 10
1×1=1
1×2=2
1×3=3
1×4=4
1×5=5
1×6=6
1×7=7
1×8=8
1×9=9
1×10=10
……………
…………….
……………
10×9=90
10×10=100
Very interesting
Thank you