0% found this document useful (0 votes)
22 views12 pages

Java Programming Exercises for Students

Uploaded by

manoj chavda
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)
22 views12 pages

Java Programming Exercises for Students

Uploaded by

manoj chavda
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

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[])
{
[Link]("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;
[Link]("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;
[Link]("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;
[Link]("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;
[Link]("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;
[Link]("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;
[Link]("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;
[Link]("Area of a Square with Length " + L +" is: " + Area_Squa);
[Link]("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);
[Link]("Perimeter of a Square with Length " + L +" is: " + Peri_Squa);
[Link]("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)
[Link](a + " is smaller than " + b);
else
[Link](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)
[Link](a + " is even number.");
else
[Link](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;
[Link]("1 to 10 in ascending order:");
for(n=1;n<=10;n++)
[Link](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;
[Link]("1 to 10 in descending order:");
for(n=10;n>=1;n--)
[Link](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;
[Link]("Even numbers between 1 to 50:");
for(n=1;n<=50;n++)
{
if (n%2 == 0)
[Link](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;
[Link]("Odd numbers between 1 to 50:");
for(n=1;n<=50;n++)
{
if (n%2 != 0)
[Link](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++)
[Link]("* ");
[Link]();

}
}
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++)
[Link]("* ");
[Link]();

}
}
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++)
[Link](j + " ");
[Link]();

}
}
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++)
[Link](j + " ");
[Link]();

}
}
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++)
[Link](i + " ");
[Link]();

}
}
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++)
{
[Link](n + " ");
}
[Link]();
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;
[Link]("1 to 10 in ascending order using while loop:");
while(n<11)
{
[Link](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;
[Link]("1 to 10 in ascending order using do while loop:");
do
{
[Link](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;
[Link]("Before swap::");
[Link]("A=" + a + "\tB="+b);
a=a+b; b=a-b; a=a-b;
[Link]("After swap::");
[Link]("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;
}
[Link]("Number is:" + temp);
[Link]("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')
[Link]( ch + " is a Vowel!!!");
else
[Link](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)
[Link](n + " is negative number!!!");
else if (n>0)
[Link](n + " is a positive number!!!");
else
[Link](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--;
}
[Link]( 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++;
}
[Link]("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()
{
[Link]("length is:: " + length);
[Link]("Width is:: " + width);
[Link]("Height is:: " + height);
[Link]("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


[Link]();
[Link]();

[Link](18, 12.5f,10,(byte)2);
[Link](14, 11,10,(byte)3);

//dispay after value assignment


[Link]();
[Link]();

//Display area
[Link]("\nArea of the room with length " + [Link] + " width " + r1. width + " is "
+ r1.Find_Area());
[Link]("\nArea of the room with length " + [Link] + " 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()
{
[Link]("length is:: " + length);
[Link]("Width is:: " + width);
[Link]("Height is:: " + height);
[Link]("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


[Link]();
[Link]();

//Display area
[Link]("\nTotal number of windows: " + Room.tot_win);
}
}

Output:

Common questions

Powered by AI

The document showcases two programs to print numbers in ascending and descending order using 'for' loops. The 'for_asc' class prints numbers 1 to 10 in ascending order, incrementing the counter 'n' in each iteration until it exceeds 10. Conversely, the 'for_dsc' class prints numbers from 10 to 1 in descending order, decrementing the counter until it goes below 1. These examples illustrate how loop control structures in Java enable orderly iteration over numbers, showing the versatility and power of controlling iteration starting points, endpoints, and update steps .

The programs demonstrate nested 'for' loops to generate pattern outputs. For instance, the 'Pattern1' and 'Pattern2' classes illustrate how outer loops can determine the number of rows, while inner loops dictate the elements per row, like numbers or asterisks. Understanding these helps in grasping how nested loops control complex iterative tasks and pattern generation. Practical applications of such patterns include generating UI elements, algorithmic problem-solving, or creating game elements where patterns and structures are needed .

Simple programs like adding two numbers provide foundational knowledge in Java programming concepts such as variable declaration, data types, and arithmetic operations. They demonstrate basic syntax and output mechanisms—vital skills for any programmer. Such programs also instill confidence in beginners, allowing them to grasp control flow structures and debugging, making abstract programming principles more tangible and approachable .

The modulus operation, demonstrated in the program, calculates the remainder of the division between two numbers. In the example, with 'a' as 16 and 'b' as 5, the modulus 'a % b' results in a remainder of 1. The significance of the modulus operator in programming is its utility in numerous algorithms and procedures where determining divisibility or calculating remainder is essential, such as in checks for even/odd numbers and assigning cyclic values .

The program for swapping two numbers without using a third variable uses arithmetic operations to swap values. Initially, 'a' is assigned the sum of 'a' and 'b'. Then, 'b' becomes 'a - b', effectively assigning 'b' the initial value of 'a'. Finally, 'a' is updated to 'a - b', which gives 'a' the initial value of 'b'. This approach is beneficial because it conserves memory usage by eliminating the need for an additional variable and exemplifies a clever manipulation of arithmetic to achieve value swapping .

The program calculates the area of a circle using the formula 'Area = PI * r * r', with a pre-defined constant 'PI' as 3.14 and the radius 'r' set to 5.0. While this approach is straightforward and functional, an improvement would be to use Java's built-in constant 'Math.PI' for better precision, as 'Math.PI' offers a more accurate value for Pi compared to 3.14. This would enhance the precision of the area calculation .

To modify the 'Smaller' program to indicate when numbers are equal, you can add another condition to the existing 'if-else' statement. First, ensure the existing comparison logic remains intact. Then, include an additional condition: 'if(a == b)' before the else statement to check for equality and print a message such as 'a is equal to b'. This extension improves the program's capability by covering all potential comparisons between two numbers .

A constructor in Java, as shown in the sample program, is primarily used to initialize objects. In the provided classes 'Room' and 'Cons_demo', constructors are used to set the initial state of an object by assigning values to the object's fields. The document demonstrates two constructors: one that takes parameters for 'length', 'width', 'height', and 'number of windows', and another that defaults 'height' to 10 and 'number of windows' to 1. These constructors streamline the creation of 'Room' objects, ensuring they are given valid, initial values that represent a room's dimensions and window count. This concept exemplifies encapsulation and aids in maintaining the integrity of the object's state .

The factorial program uses a 'while' loop to decrementally multiply numbers until it reaches zero to calculate the factorial. While effective for modest values, a major drawback is its inefficiency with large numbers due to stack overflow risks or computational limits, as the factorial grows exponentially. Moreover, using iterative or recursive functions for excessively large values could lead to performance bottlenecks, indicating a need for limits on input size or alternative algorithms like tail recursion or memoization techniques .

In the 'Room_demo' program, each object of the 'Room' class contains attributes such as 'length', 'width', 'height', and 'no_windows', which are essential in describing the physical characteristics of a room. These attributes are used within methods like 'SetAttr', 'Display', and 'Find_Area' to initialize, present, and compute the room's attributes, respectively. Their purpose underscores OOP principles by providing encapsulation, attribute-specific operations, and object-specific data, integral to modeling real-world entities in a flexible and maintainable fashion .

You might also like