0% found this document useful (0 votes)
11 views3 pages

Lab Programs 12,13,14,15,16

The document contains multiple Java programs demonstrating various concepts, including user-defined packages, applet life cycle, palindrome checking, factorial calculation from command-line arguments, and prime number generation between two limits. Each program is provided with code snippets and explanations of their functionality. The examples illustrate fundamental programming techniques in Java.

Uploaded by

rakeshrakhi0804
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)
11 views3 pages

Lab Programs 12,13,14,15,16

The document contains multiple Java programs demonstrating various concepts, including user-defined packages, applet life cycle, palindrome checking, factorial calculation from command-line arguments, and prime number generation between two limits. Each program is provided with code snippets and explanations of their functionality. The examples illustrate fundamental programming techniques in Java.

Uploaded by

rakeshrakhi0804
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/ 3

12. Java program to import user-defined packages.

Create a folder inside Desktop folder with the name Pack1.compile and check.
package Pack1;
public class Example1{
public void packMethod()
{
System.out.println("packMethod() in Example1 class");
}
}
Save this outside the Pack1 folder and compile only in C://Desktop path
import Pack1.*;
public class B{
public static void main(String[] args)
{
Example1 e = new Example1();
e.packMethod();
}
}

13. Java program to implement the Life cycle of the applet


import java.applet.Applet;
import java.awt.Graphics;
/* <applet code="Program10.class" width=500 height=600></applet> */
public class Program10 extends Applet {
public void init() {
System.out.println("Applet Initialized");
}
public void start() {
System.out.println("Applet Started");
}
public void stop() {
System.out.println("Applet Stopped");
}
public void destroy() {
System.out.println("Applet Destroyed");
}
public void paint(Graphics g) {
g.drawString("Applet Life Cycle Demo", 50, 50);
}
}

14. Java program to check whether a number is palindrome or not.


public class PalinD{
public static void main(String[] args){
int num=454;
int temp=num;
int rev=0,rem;
while(temp!=0)
{
rem=temp%10;
rev=((rev*10)+rem);
temp=temp/10;
}
if(num==rev)
{
System.out.println(num+" is a Palindrome");
}
else
{
System.out.println(num+" is not a Palindrome");
}
}
}

15. Java program to find the factorial of a list of numbers reading input as command line argument.
public class Fact{
static long factorial(int n)
{
long fact=1;
for(int i=1;i<=n;i++){
fact*=i;
}
return fact;
}
public static void main(String[] args)
{
if(args.length==0){
System.out.println("Enter the numbers in command-line");
return;
}
System.out.println("Factorial of numbers:Below ");
for(String arg : args){
int num=Integer.parseInt(arg);
System.out.println("Factorial of "+num+" is "+factorial(num));
}
}
}

16. Java program to display all prime numbers between two limits.
import java.util.Scanner;
public class Prime{
static boolean isPrime(int num) {
if (num < 2)
return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter lower limit: ");
int lower = s.nextInt();
System.out.print("Enter upper limit: ");
int upper = s.nextInt();
System.out.println("Prime numbers between " + lower + " and " + upper + ":");
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
s.close();
}
}

You might also like