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

JAVA Lab Examples

The document contains three Java programs: the first demonstrates the creation of a class and objects by calculating the factorial of a number, the second sorts a list of names in ascending order, and the third calculates the sum of the digits of a number while checking if it is a palindrome. Each program includes user input and output functionality. The code snippets illustrate fundamental programming concepts in Java.

Uploaded by

Janki Sharan
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)
2 views

JAVA Lab Examples

The document contains three Java programs: the first demonstrates the creation of a class and objects by calculating the factorial of a number, the second sorts a list of names in ascending order, and the third calculates the sum of the digits of a number while checking if it is a palindrome. Each program includes user input and output functionality. The code snippets illustrate fundamental programming concepts in Java.

Uploaded by

Janki Sharan
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

Dt.

04-09-2024

1. Demonstration of Creation of Class, Objects

class Main {

int factorial(int a) {
int i, fact = 1;
for (i = 1; i <= a; i++) {
fact = fact * i;
}
return fact;
}
public static void main(String args[]) {
int a, fact;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
a = sc.nextInt();
Main ff = new Main();
fact = ff.factorial(a);
System.out.println("Factorial of "+a+" is:" + fact);
}
}

2. Write a Java program for sorting a given list of names in ascending order
import java.util.*;
class Sorting
{
void sortStrings()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = s.nextInt();
String[] str = new String[n];
System.out.println("Enter strings: ");
for(int i = 0; i < n; i++)
{
str[i] = new String(s.next());
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(str[i].compareTo(str[j])>0)
{
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.println("Sorted list of strings is:");
for(int i = 0; i < n ; i++)
{
System.out.println(str[i]);
}
}
}
class Sort
{
public static void main(String[] args)
{
Sorting obj = new Sorting();
obj.sortStrings();
}
}

3.Write a java program to print sum of Sum of Digits and check for
palindrome.
import java.util.*;
class sod_pal
{
public static void main(String args[])
{
int n,rev=0, t,res,sum=0;
Scanner s= new Scanner(System.in);
System.out.print("Please Enter No. = ");
n=s.nextInt();
t=n;
System.out.print("Sum of digits of a number" +n+ "is = ");
while(n>0)
{
res=n%10;
rev = rev *10 + res;
n=n/10;
sum=sum+res;
}
System.out.println(+sum);
if(rev==t)
System.out.println("Given number is a palindrome");
else
System.out.println("Given number is not a palindrome");

}
}

You might also like