0% found this document useful (0 votes)
9 views

Java Addtional Lab Programs

Uploaded by

Tejaswini Beri
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)
9 views

Java Addtional Lab Programs

Uploaded by

Tejaswini Beri
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
You are on page 1/ 7

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++)
{
System.out.print(j);
}
System.out.println();
}
}
}

Output:1
12
123
1234
2. Design a java program to find the factorial of a given number.
A.Source code:
import java.util.Scanner;
class Factorial{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number : ");
int fact=1,n;
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("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++)
{
System.out.print(“*”);
}
System.out.println();
}
}
}
Output:*
**
***
****
4. Write a java method to find minimum value in given two values.
A. Source Code:
import java.util.Scanner;
class Display{
void min(int n1,int n2)
{
if(n1<n2)
{
System.out.println(n1+" is minimum value");
}
else
{
System.out.println(n2+" is minimum value");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two Values :");
int n1,n2;
n1=sc.nextInt();
n2=sc.nextInt();
Display d=new Display();
d.min(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.

A.Source Code:

import java.util.Scanner;
class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int temp = number;
int result = 0;
int n = String.valueOf(number).length();
while (temp!= 0) {
int remainder = temp% 10;
result +=+Math.pow(remainder, n);
temp=temp/10;
}
if (result == number) {
System.out.println(number + " is an Armstrong number.");
}
else {
System.out.println(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.
A.Source Code:
import java.util.Scanner;
class Palindrome {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int temp = number;
int rev=0;
while (temp!= 0) {
int remainder = temp% 10;
rev=rev*10+remainder;
temp=temp/10;
}
if (rev== number) {
System.out.println(number + " is Palindrome.");
}
else {
System.out.println(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()
{
System.out.println("sum is "+(a+b+c));
}
public static void main(String[] args)
{
Sum s1=new Sum(10,20);
Sum s2=ne Sum(10,20,30);
s1.display();
s2.display();
}
}

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.
A.Source Code:
class Thread1 extends Thread{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("Thread 1 i= "+i);
}
}
}
class Thread2 extends Thread{
public void run(){
for(int i=10;i>=0;i--){
System.out.println("Thread 2 i= "+i);
}
}
}
class ThreadRun
{
public static void main(String[] args){
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
t1.start();
t2.start();
}
}
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!”.


A.Source code:

File name:HelloWorldApplet.java

import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 20, 20);
}
}
File name: HelloWorldApplet.html

<html>
<head>
<title>HelloWorld Applet</title>
</head>
<body>
<applet code="HelloWorldApplet.class" width="300" height="200"></applet>
</body></html>
Compilation :javacHelloWorld Applet.java
appletviewerHelloWorld Applet.html

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy