Java Programming Lab Programs
Additional Lab Programs:
1. Develop a java program to display the following output.
1
12
123
1234
A. Source Code:
class Display{
public static void main(String[] args){
for(short i=1;i<=4;i++)
{
for(short j=1;j<=i;j++)
{
[Link](j);
}
[Link]();
}
}
}
Output:1
12
123
1234
2. Design a java program to find the factorial of a given number.
[Link] code:
import [Link];
class Factorial{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
[Link]("Enter a number : ");
int fact=1,n;
n=[Link]();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
[Link]("Factorial of"+n+" is "+fact);
}
Output:
Enter a number :
5
Factorial of5 is 120
3. Develop a java program to display the following output.
*
**
***
****
Source Code:
class Display1 {
public static void main(String[] args) {
for(short i=1;i<=4;i++)
{
for(short j=1;j<=i;j++)
{
[Link](“*”);
}
[Link]();
}
}
}
Output:*
**
***
****
4. Write a java method to find minimum value in given two values.
A. Source Code:
import [Link];
class Display{
void min(int n1,int n2)
{
if(n1<n2)
{
[Link](n1+" is minimum value");
}
else
{
[Link](n2+" is minimum value");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter two Values :");
int n1,n2;
n1=[Link]();
n2=[Link]();
Display d=new Display();
[Link](n1,n2);
}
}
Output:
Enter two Values :
16
24
16 is minimum value
5. Write a Java program to check whether given number is Armstrong or not.
[Link] Code:
import [Link];
class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int temp = number;
int result = 0;
int n = [Link](number).length();
while (temp!= 0) {
int remainder = temp% 10;
result +=+[Link](remainder, n);
temp=temp/10;
}
if (result == number) {
[Link](number + " is an Armstrong number.");
}
else {
[Link](number + " is not an Armstrong number.");
}
}
}
Output:
Enter a number:153
153 is an Armstrong number.
6. Write a java program that checks whether the given number is Palindrome or not.
[Link] Code:
import [Link];
class Palindrome {
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int temp = number;
int rev=0;
while (temp!= 0) {
int remainder = temp% 10;
rev=rev*10+remainder;
temp=temp/10;
}
if (rev== number) {
[Link](number + " is Palindrome.");
}
else {
[Link](number + " is not Palindrome.");
}
}
}
Output:
Enter a number: 121
121 is Palindrome.
7. Write a java program to implement constructor overloading.
Source Code:
class Sum{
int a,b,c;
Sum(int x, int y)
{
a=x;
b=y;
}
Sum(int x, int y,int z)
{
a=x;
b=y;
c=z;
}
void display()
{
[Link]("sum is "+(a+b+c));
}
public static void main(String[] args)
{
Sum s1=new Sum(10,20);
Sum s2=ne Sum(10,20,30);
[Link]();
[Link]();
}
}
Output: Sum is 30
Sum is 60
8. Write a program that creates two threads. Fist thread prints the numbers from 1 to 10
and the other thread prints the numbers from 10 to 1.
[Link] Code:
class Thread1 extends Thread{
public void run(){
for(int i=1;i<=10;i++){
[Link]("Thread 1 i= "+i);
}
}
}
class Thread2 extends Thread{
public void run(){
for(int i=10;i>=0;i--){
[Link]("Thread 2 i= "+i);
}
}
}
class ThreadRun
{
public static void main(String[] args){
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
[Link]();
[Link]();
}
}
Output:
Thread 1 i= 1
Thread 1 i= 2
Thread 1 i= 3
Thread 1 i= 4
Thread 1 i= 5
Thread 1 i= 6
Thread 1 i= 7
Thread 1 i= 8
Thread 2 i= 10
Thread 1 i= 9
Thread 1 i= 10
Thread 2 i= 9
Thread 2 i= 8
Thread 2 i= 7
Thread 2 i= 6
Thread 2 i= 5
Thread 2 i= 4
Thread 2 i= 3
Thread 2 i= 2
Thread 2 i= 1
Thread 2 i= 0
9. Write an applet code to display “Hello World!”.
[Link] code:
File name:[Link]
import [Link];
import [Link];
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello World!", 20, 20);
}
}
File name: [Link]
<html>
<head>
<title>HelloWorld Applet</title>
</head>
<body>
<applet code="[Link]" width="300" height="200"></applet>
</body></html>
Compilation :javacHelloWorld [Link]
appletviewerHelloWorld [Link]
Output: