[Go to site: main page, start]

0% found this document useful (0 votes)
7 views7 pages

Java Lab Programs

The document contains multiple Java programs demonstrating various functionalities including calculating factorials, finding prime numbers, sorting elements, and performing string operations. Each program includes exception handling to manage potential input errors. Outputs for each program are also indicated, showcasing their respective results.

Uploaded by

tnchaithra631
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Java Lab Programs

The document contains multiple Java programs demonstrating various functionalities including calculating factorials, finding prime numbers, sorting elements, and performing string operations. Each program includes exception handling to manage potential input errors. Outputs for each program are also indicated, showcasing their respective results.

Uploaded by

tnchaithra631
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sample program

class Sample{
public static void main(String[] args)
{
[Link]("welcome to cs lab");
}
}
Output

Program 1: WRITE A PROGRAM XTO FIND FACTORIAL OF


LIST OF NUMBER READING INPUT AS COMMAND LINE
ARGUMENT

class FactorialList
{
public static void main(String args[])
{
int i, j, num;
long fact;

for(i = 0; i < [Link]; i++)


{
num = [Link](args[i]);
fact = 1;

for(j = 1; j <= num; j++)


{
fact = fact * j;
}

[Link]("Factorial of " + num + " = " + fact);


}
}
}
output

PROGRAM 2: WRITE A JAVA PROGRAM TO DISPLAY ALL


PRIME NUMBERS BETWEEN TWO GIVEN LIMITS.

public class PrimeNumber {


public static void main(String[] args) {

int start = 10;


int end = 30;

[Link]("Prime numbers between " + start + "


and " + end + " are:");

for (int i = start; i <= end; i++) {


int count = 0;

for (int j = 1; j <= i; j++) {


if (i % j == 0) {
count++;
}
}

if (count == 2) {
[Link](i);
}
}
}
}
Output

Program 3: Java Program to Sort Elements in Ascending


& Descending Order with Exception Handling:

import [Link].*;

public class SortElements {


public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);

[Link]("Enter number of elements: ");


int n = [Link]();
int arr[] = new int[n];

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

// Ascending Order
[Link](arr);
[Link]("Ascending Order:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}

// Descending Order
[Link]("\nDescending Order:");
for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}

} catch (InputMismatchException e) {
[Link]("Invalid input! Please enter integers
only.");
} catch (Exception e) {
[Link]("Error occurred: " + e);
}
}
}

Output
Program 4:
Java program to perform basic string operations with
exception handling:
import [Link].*;

public class StringOperations {


public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);

[Link]("Enter first string: ");


String str1 = [Link]();

[Link]("Enter second string: ");


String str2 = [Link]();
String result = [Link](str2);
[Link]("Concatenated String: " + result);

[Link]("Length of string: " +


[Link]());
[Link]("Uppercase: " +
[Link]());
[Link]("Lowercase: " +
[Link]());
[Link]("First character: " +
[Link](0));
[Link]("Substring (0 to 3): " +
[Link](0,3));
[Link]("Replaced string: " +
[Link]('a','@'));

}
catch (StringIndexOutOfBoundsException e) {
[Link]("String index error occurred.");
}
catch (Exception e) {
[Link]("Error: " + e);
}
}
}

Output

You might also like