1.
CommandLine Arguments
import [Link].*;
class CommandLine
{
public static void main(String args[])
{
int a,b;
a=[Link](args[0]);
b=[Link](args[1]);
[Link]("Sum= "+(a+b));
[Link]("Difference= "+(a-b));
[Link]("Product= "+(a*b));
[Link]("Quotient= "+(a/b));
[Link]("Reminder= "+(a%b));
}
}
2. Type of Triangle and It’s area
import [Link].*;
import [Link].*;
class Triangle
{
public static void main(String args[])
{
int a,b,c;
double area,s,val;
Scanner sc=new Scanner([Link]);
[Link]("Enter the three sides of triangle:");
a=[Link]();
b=[Link]();
c=[Link]();
if(a==b && b==c && c==a)
{
[Link]("The triangle is equilateral");
}
else if(a==b || b==c || c==a)
{
[Link]("The triangle is isosceles");
}
else
[Link]("The triangle is Scalene");
s=(a+b+c)/2.0;
val = (s * (s-a)*(s-b)*(s-c));
area= [Link](val);
[Link]("Area of the triangle is "+area);
}
}
3. Array- Largest, Smallest and Second Largest
elements
import [Link].*;
class Array
{
public static void main(String args[])
{
int a[]=new int[10];
int n=10,temp;
Scanner sc=new Scanner([Link]);
for(int i=0;i<10;i++)
{
a[i]=[Link]();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j]=temp;
}
}
}
[Link]("The Smallest element = "+ a[0]);
[Link]("The largest element = "+ a[n-1]);
[Link]("The Second largest element = "+ a[n-2]);
4. Number Conversions
import [Link].*;
class Conversions
{
public static void main(String args[])
{
int number,index=0;
int binary[]=new int[40];
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
int temp=number;
while(temp!=0)
{
binary[index] = temp%2;
temp=temp/2;
index++;
}
[Link]("Binary = ");
for(int i=index - 1;i>=0;i--)
{
[Link](binary[i]);
}
[Link]();
String oct = "";
int temp1=number,rem=0;
char octal[]={'0','1','2','3','4','5','6','7'};
while(temp1!=0)
{
rem=temp1%8;
oct=octal[rem]+oct;
temp1=temp1/8;
}
[Link]("Octal = "+oct);
String hex = "";
int temp2=number,rem1=0;
char hexadec[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(temp2!=0)
{
rem1=temp2%16;
hex=hexadec[rem1]+hex;
temp2=temp2/16;
}
[Link]("Hexa decimal = "+hex);
}
5. Merge Two arrays
import [Link].*;
class MergeArray
{
public static void main(String args[])
{
int n,m;
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of first array:");
n=[Link]();
int a[]=new int[n];
[Link]("Enter the first array values");
for(int i=0;i<[Link];i++)
{
a[i]=[Link]();
}
[Link]("Enter the size of second array:");
m=[Link]();
int b[]=new int[m];
[Link]("Enter the second array values");
for(int i=0;i<[Link];i++)
{
b[i]=[Link]();
}
int c[]= new int[[Link]+[Link]];
for(int i=0;i<[Link];i++)
{
c[i]=a[i];
}
for(int i=0;i<[Link];i++)
{
c[[Link]+i]=b[i];
}
[Link]("The merged array :");
for(int i=0;i<[Link];i++)
{
[Link](c[i]+"\t");
}
}
}
6. HCF and LCM
import [Link].*;
class LCMandHCF
{
public static void main(String args[])
{
int a,b,hcf=1,lcm;
Scanner s = new Scanner([Link]);
[Link]("Enter the first number:");
a = [Link]();
[Link]("Enter the second number:");
b = [Link]();
for(int i=1;i<=a && i<=b;i++)
{
if(a%i==0 && b%i==0)
{
hcf=i;
}
}
[Link]("The HCF of "+ a +" and "+ b+ " is "+hcf);
lcm = (a*b)/hcf;
[Link]("The LCM of "+ a +" and "+ b+ " is "+lcm);
}
}
7. Trace and Transpose of a matrix
import [Link];
public class Matrix
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
// Input matrix dimensions
[Link]("Enter the number of rows: ");
int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
// Input matrix elements
[Link]("Enter the matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
[Link]("The Original matrix is:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
[Link](matrix[i][j] + " ");
}
[Link]();
}
// Calculate trace if the rows and columns are equal or the matrix is a square matrix
if(rows==cols)
{
int trace=0;
for(int i=0;i<rows;i++)
{
trace+=matrix[i][i];
}
[Link]("Trace of the matrix= "+trace);
}
else
{
[Link]("Trace can not be calculated as the matrix is not square");
}
// Calculate transpose
int[][] transpose= new int[cols][rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
transpose[j][i]=matrix[i][j];
}
// Display transpose matrix
[Link]("Transpose of the matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
[Link](transpose[i][j] + " ");
}
[Link]();
}
}
}
8. Sum and Reverse of a number
import [Link].*;
class NumberOperation
{
int number,temp;
NumberOperation(int num)
{
number=num;
temp= number;
}
public void SumDigit()
{
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
[Link](" Sum of digit = "+ sum);
public void Reverse()
{
int rev = 0;
while (temp != 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
[Link]("Reverse = "+ rev);
}
class NumberValue
{
public static void main(String args[])
{
int num;
Scanner s= new Scanner([Link]);
[Link]("Enter a number:");
num = [Link]();
NumberOperation n= new NumberOperation(num);
[Link]();
[Link]();
9. Anagram
import [Link];
import [Link];
class AnagramCheck {
// Method to check if two strings are anagrams
public static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert strings to lowercase
str1 = [Link]("\\s", "").toLowerCase();
str2 = [Link]("\\s", "").toLowerCase();
// If lengths of the strings are different, they cannot be anagrams
if ([Link]() != [Link]()) {
return false;
}
// Convert strings to character arrays and sort them
char[] charArray1 = [Link]();
char[] charArray2 = [Link]();
[Link](charArray1);
[Link](charArray2);
// Compare sorted character arrays
return [Link](charArray1, charArray2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input two strings from the user
[Link]("Enter the first string:");
String str1 = [Link]();
[Link]("Enter the second string:");
String str2 = [Link]();
// Check if the strings are anagrams
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
}
10. Remove Vowels
import [Link];
public class RemoveVowel {
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String s = [Link]();
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == 'a' || [Link](i) == 'e'
|| [Link](i) == 'i' || [Link](i) == 'o'
|| [Link](i) == 'u' || [Link](i) == 'A'
|| [Link](i) == 'E' || [Link](i) == 'I'
|| [Link](i) == 'O'
|| [Link](i) == 'U') {
continue;
}
else {
[Link]([Link](i));
}
}
}
}
11. Student Details
import [Link].*;
class Student
{
int rollno;
String name;
Scanner sc = new Scanner([Link]);
public void ReadData()
{
[Link]("Enter the rollno:");
rollno = [Link]();
[Link]("Enter the Name:");
name = [Link]();
}
public void display()
{
[Link]("Rollno:"+rollno);
[Link]("Name:"+name);
}
}
class Mark extends Student {
private int[] marks = new int[5];
private int total;
private double average;
public void readMarks() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter marks for 5 subjects:");
total = 0;
for (int i = 0; i < 5; i++) {
[Link]("Subject " + (i + 1) + ": ");
marks[i] = [Link]();
total += marks[i];
}
average = total / 5.0;
}
public void displayResult() {
[Link]("Total Marks: " + total);
[Link]("Average Marks: " + average);
}
}
public class StudentDetails {
public static void main(String[] args) {
Mark student = new Mark();
[Link]();
[Link]();
[Link]("\nStudent Details and Result:");
[Link]();
[Link]();
}
}
12. Sum of Complex Number
import [Link].*;
class Complex
{
int real,img;
Complex(int real,int img)
{
[Link] = real;
[Link] = img;
}
public Complex add(Complex other)
{
int newReal = [Link] + [Link];
int newimg = [Link] + [Link];
return new Complex(newReal, newimg);
}
public void display() {
if (img >= 0) {
[Link](real + " + " + img + "i");
} else {
[Link](real + " - " + (-img) + "i");
}
}
}
class ComplexSum
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the real and imaginary part of first complex number");
int r1 = [Link]();
int m1 = [Link]();
[Link]("Enter the real and imaginary part of second complex number");
int r2 = [Link]();
int m2 = [Link]();
Complex c1 = new Complex(r1, m1);
Complex c2 = new Complex(r2, m2);
[Link]("First Complex Number: ");
[Link]();
[Link]("Second Complex Number: ");
[Link]();
// Add the two complex numbers
Complex sum = [Link](c2);
[Link]("Sum of Complex Numbers: ");
[Link]();
}
}