TREE HOUSE HIGH SCHOOL(GSEB)
JAVA PROGRAM WORKSHEET(2025-26)
Std: XII (Perfection) Sub : Computer
Name: ________________ Java Programs Date: _________
1) Write a java program to print 'Hello to Java World!!!'.
Program:
public class First
{
public static void main(String args[])
{
System.out.println("Hello to Java World!!!");
}
}
Output:
Hello to Java World!!!
2) Write a java program to Add two numbers.
Program:
public class Add
{
public static void main(String args[])
{
int a=16, b=5, c;
c=a+b;
System.out.println("Addition of " + a + " and " + b + " is: " +c);
}
}
Output:
Addition of 16 and 5 is : 21
3) Write a java program to Subtract two numbers.
Program:
public class Sub
{
public static void main(String args[])
{
int a=16, b=5, c;
c=a-b;
System.out.println("Subtraction of " + a + " and " + b + " is: " +c);
}
}
Output:
Subtraction of 16 and 5 is : 11
4) Write a java program to Divide two numbers.
OR
Write a java program to find Quotient.
Program:
public class Div
{
public static void main(String args[])
{
int a=16, b=5, c;
c=a/b;
System.out.println("Division of " + a + " and " + b + " is: " +c);
}
}
Output:
Division of 16 and 5 is : 3
5) Write a java program to Modulo of two numbers.
OR
Write a java program to find Remainder after division.
Program:
public class Mod
{
public static void main(String args[])
{
int a=16, b=5, c;
c=a%b;
System.out.println("Remainder after division of " + a + " and " + b + " is: " +c);
}
}
Output:
Remainder after division of 16 and 5 is : 1
6) Write a java program to Multiplication of two numbers.
Program:
public class Mul
{
public static void main(String args[])
{
int a=16, b=5, c;
c=a*b;
System.out.println("Multiplication of " + a + " and " + b + " is: " +c);
}
}
Output:
Remainder after division of 16 and 5 is : 80
7) Write a java program to find Area of a circle.
Program:
public class Area_Circle
{
public static void main(String args[])
{
float PI=3.14f, r=5.0f, Area;
Area=PI * r * r;
System.out.println("Area of a Circle with radius " + r + " is: " + Area);
}
}
Output:
Area of a Circle with radius 5.0 is : 78.5
8) Write a java program to find Area of a Square and Rectangle.
Program:
public class Area_Squa_Rect
{
public static void main(String args[])
{
float L=5.0f, W=8.0f, Area_Squa, Area_Rect;
Area_Squa = L * L;
Area_Rect = L * W;
System.out.println("Area of a Square with Length " + L +" is: " + Area_Squa);
System.out.println("Area of a Rectangle with Length " + L +" And Width "+ W + " is: " +
Area_Rect);
}
Output:
Area of a Square with Length 5.0 is: 25.0
Area of a Rectangle with Length 5.0 And Width 8.0 is: 40.0
9) Write a java program to find Perimeter of a Square and Rectangle.
Program:
public class Peri_Squa_Rect
{
public static void main(String args[])
{
float L=5.0f, W=8.0f, Peri_Squa, Peri_Rect;
Peri_Squa = 4 * L;
Peri_Rect = 2 * (L + W);
System.out.println("Perimeter of a Square with Length " + L +" is: " + Peri_Squa);
System.out.println("Perimeter of a Rectangle with Length " + L +" And Width "+ W + " is: " +
Peri_Rect);
}
}
Output:
Area of a Square with Length 5.0 is: 20.0
Area of a Rectangle with Length 5.0 And Width 8.0 is: 26.0
10) Write a java program to find smaller number from given two number.
Program:
public class Smaller
{
public static void main(String args[])
{
float a=12.5f, b=17.2f;
if(a < b)
System.out.println(a + " is smaller than " + b);
else
System.out.println(b + " is smaller than " + a);
}
}
Output:
12.5 is smaller than 17.2.
11) Write a java program to check whether the given number is even or odd.
Program:
public class even_odd
{
public static void main(String args[])
{
int a=25;
if(a % 2 == 0)
System.out.println(a + " is even number.");
else
System.out.println(a + " is odd number");
}
}
Output:
25 is odd number.
12) Write a java program to print 1 to 10 in ascending order.
Program:
public class for_asc
{
public static void main(String args[])
{
int n;
System.out.println("1 to 10 in ascending order:");
for(n=1;n<=10;n++)
System.out.println(n);
}
}
Output:
1 2 3 4 5 6 7 8 9 10
13) Write a java program to print 1 to 10 in descending order.
Program:
public class for_dsc
{
public static void main(String args[])
{
int n;
System.out.println("1 to 10 in descending order:");
for(n=10;n>=1;n--)
System.out.println(n);
}
}
Output:
10 9 8 7 6 5 4 3 2 1
14) Write a java program to find even numbers between 1 to 50.
Program:
public class for_even
{
public static void main(String args[])
{
int n;
System.out.println("Even numbers between 1 to 50:");
for(n=1;n<=50;n++)
{
if (n%2 == 0)
System.out.println(n);
}
}
}
Output:
2 4 6 8 10................ 50
15) Write a java program to find odd numbers between 1 to 50.
Program:
public class for_odd
{
public static void main(String args[])
{
int n;
System.out.println("Odd numbers between 1 to 50:");
for(n=1;n<=50;n++)
{
if (n%2 != 0)
System.out.println(n);
}
}
}
Output:
1 3 5 7 9................ 49
16) Write a java program to print following pattern.
*
* *
* * *
Program:
public class Pattern1
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print("* ");
System.out.println();
}
}
Output:
*
* *
* * *
17) Write a java program to print following pattern.
* * *
* *
*
Program:
public class Pattern2
{
public static void main(String args[])
{
int i, j;
for(i=3;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print("* ");
System.out.println();
}
}
Output:
* * *
* *
*
18) Write a java program to print following pattern.
1
1 2
1 2 3
Program:
public class Pattern3
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print(j + " ");
System.out.println();
}
}
Output:
1
1 2
1 2 3
19) Write a java program to print following pattern.
1 2 3
1 2
1
Program:
public class Pattern4
{
public static void main(String args[])
{
int i, j;
for(i=3;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(j + " ");
System.out.println();
}
}
Output:
1 2 3
1 2
1
20) Write a java program to print following pattern.
1
2 2
3 3 3
Program:
public class Pattern5
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print(i + " ");
System.out.println();
}
}
Output:
1
2 2
3 3 3
21) Write a java program to print following pattern.
1 1 1
2 2
3
Program:
public class Pattern6
{
public static void main(String args[])
{
int i, j, n=1;
for(i=3;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(n + " ");
}
System.out.println();
n++;
}
}
}
Output:
1 1 1
2 2
3
22) Write a java program to print 1 to 10 using while and do while loop.
Program: (using While loop)
public class while_demo
{
public static void main(String args[])
{
int n=1;
System.out.println("1 to 10 in ascending order using while loop:");
while(n<11)
{
System.out.println(n);
n++;
}
}
}
Output:
1 2 3 4 5 6 7 8 9 10
Program: (using do while loop)
public class do_while_demo
{
public static void main(String args[])
{
int n=1;
System.out.println("1 to 10 in ascending order using do while loop:");
do
{
System.out.println(n);
n++;
}while(n<11);
}
}
Output:
1 2 3 4 5 6 7 8 9 10
23) Write a java program to swap two numbers without using 3rd variable .
Program:
public class swap
{
public static void main(String args[])
{
int a=10, b=20;
System.out.println("Before swap::");
System.out.println("A=" + a + "\tB="+b);
a=a+b; b=a-b; a=a-b;
System.out.println("After swap::");
System.out.println("A=" + a + "\tB="+b);
}
}
Output:
Before swap::
A=10 B=20
After swap::
A=20 B=10
24) Write a java program to find addition of digits of given number.
Program:
public class Digit_Add
{
public static void main(String args[])
{
int n=356, sum=0, rem=0, temp;
temp=n;
while(n>0)
{
rem=n%10;
n=n/10;
sum+=rem;
}
System.out.println("Number is:" + temp);
System.out.println("Addition of digits is:" + sum);
}
}
Output:
Number is: 356
Addition of digits is:14
25) Write a java program to check whether the given alphabet is vowel or consonant.
Program:
public class Vowel
{
public static void main(String args[])
{
char ch='B';
if( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
System.out.println( ch + " is a Vowel!!!");
else
System.out.println(ch + " is a Consonant!!!");
}
}
Output:
B is a Consonant!!!
26) Write a java program to check whether given number is positive or negative.
Program:
public class check_num
{
public static void main(String args[])
{
float n=-11.23f;
if(n<0)
System.out.println(n + " is negative number!!!");
else if (n>0)
System.out.println(n + " is a positive number!!!");
else
System.out.println(n + " is a zero!!!");
}
}
Output:
-11.23 is negative number!!!
27) Write a java program to find factorial of given number.
Program:
public class Fact1
{
public static void main(String args[])
{
int n=4, fact=1, temp;
temp=n;
while(n>0)
{
fact*=n;
n--;
}
System.out.println( temp +"!:: " + fact);
}
}
Output:
4!::24
28) Write a java program to find sum of first 10 natural numbers.
Program:
public class First_10
{
public static void main(String args[])
{
//temp=n;
int n=1, sum=0;
while(n<=10)
{
sum+=n;
n++;
}
System.out.println("Addition of first 10 natural number is:: " + sum);
}
}
Output:
Addition of first 10 natural number is::55
29) Write a java program to demonstrate object oriented concept.
Program:
class Room
{
float length, width, height;
byte no_windows;
void SetAttr(float dl,float dw, float dh, byte dn)
{
length=dl; width=dw; height=dh;
no_windows=dn;
}
double Find_Area()
{
return (length*width);
}
void Display()
{
System.out.println("length is:: " + length);
System.out.println("Width is:: " + width);
System.out.println("Height is:: " + height);
System.out.println("Number of windows in the room:: " + no_windows);
}
}
public class Room_demo
{
public static void main(String args[])
{
Room r1=new Room();
Room r2=new Room();
//dispay default values
r1.Display();
r2.Display();
r1.SetAttr(18, 12.5f,10,(byte)2);
r2.SetAttr(14, 11,10,(byte)3);
//dispay after value assignment
r1.Display();
r2.Display();
//Display area
System.out.println("\nArea of the room with length " + r1.length + " width " + r1. width + " is "
+ r1.Find_Area());
System.out.println("\nArea of the room with length " + r2.length + " width " + r2. width + " is "
+ r2.Find_Area());
}
}
Output:
30) Write a java program to demonstrate constructor.
class Room
{
float length, width, height;
byte no_windows;
static int tot_win;
Room(float dl, float dw, float dh, byte dn) // constructor 1
{
length=dl; width=dw; height=dh;
no_windows=dn;
tot_win += dn;
}
Room(float dl, float dw) // constructor 1
{
length=dl; width=dw; height=10;
no_windows=1;
tot_win++;
}
double Find_Area()
{
return (length*width);
}
void Display()
{
System.out.println("length is:: " + length);
System.out.println("Width is:: " + width);
System.out.println("Height is:: " + height);
System.out.println("Number of windows in the room:: " + no_windows);
}
}
public class Cons_demo
{
public static void main(String args[])
{
Room r1=new Room(16,12.5f);
Room r2=new Room(20, 14, 12, (byte)2);
//dispay after using constructor
r1.Display();
r2.Display();
//Display area
System.out.println("\nTotal number of windows: " + Room.tot_win);
}
}
Output: