0% found this document useful (0 votes)
2 views4 pages

Java Programs Basics1

The document contains multiple Java classes demonstrating various programming concepts. It includes implementations for generating Pascal's Triangle, finding the largest elements in an array, creating a diamond shape pattern, removing duplicate characters from a string, and checking if a number is positive or negative. Each class has a main method that executes its respective functionality.

Uploaded by

vibhakundrapu
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 views4 pages

Java Programs Basics1

The document contains multiple Java classes demonstrating various programming concepts. It includes implementations for generating Pascal's Triangle, finding the largest elements in an array, creating a diamond shape pattern, removing duplicate characters from a string, and checking if a number is positive or negative. Each class has a main method that executes its respective functionality.

Uploaded by

vibhakundrapu
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/ 4

public class PascalsTriangle {

public static void main(String[] args) {


int rows = 5; // You can change this to any number of rows you want

for (int i = 0; i < rows; i++) {


// Print leading spaces for alignment
for (int j = 0; j < rows - i; j++) {
System.out.print(" ");
}

int number = 1;
for (int k = 0; k <= i; k++) {
System.out.print(number + " ");
number = number * (i - k) / (k + 1); // Compute next number in row
}
System.out.println();
}
}
}

import java.util.TreeSet;

import java.util.*;

public class firstlargest {


public static void main(String[] args){
int k = 3;
int num[] = {1,2,3,4,5,6,7,8,9,10,11};
largest(k, num);

}
public static void largest( int k , int [] arr){
TreeSet ts = new TreeSet<>(Comparator.reverseOrder());
for(int i : arr){
ts.add(i);
}
Iterator<Integer> i = ts.iterator();
for (int j = 1; j<=k ;j++)
System.out.println(i.next());

}
}

public class diamond {


public static void main(String[] args){
int n= 5;
for (int i =1; i<n;i++){

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


System.out.print(" ");
}
for(int k =1; k<=(i*2)-1;k++){
System.out.print("*");
}
System.out.println();

for (int i = n-2; i >=0;i--){

for(int j =n; j>i;j--){

System.out.print(" ");
}
for(int k =1; k<=(i*2)-1;k++){
System.out.print("*");
}

System.out.println();
}
}
}

mport java.util.*;
public class removedups {
public static void main(String[] args){
String s = "appleegh";
removedupes(s);
removedupsmaps(s);
}
public static void removedupes(String s){
char ca[] = s.toCharArray();

for(int i =0; i< ca.length;i++){


for ( int j= i+1;j< ca.length ;j++){
if (ca[i]==ca[j]){
ca[j]= ' ';
}
}
}
for(char c : ca){
if(c != ' '){
System.out.printf("%c", c);
}
}
}
public static void removedupsmaps(String s){
StringBuilder sb = new StringBuilder();
LinkedHashSet lh = new LinkedHashSet<>();
for( char c : s.toCharArray()){
if(lh.add(c)){
sb.append(c);
}

}
System.out.println(sb.toString());

}
}

public class posornegative {


public static void main(String[] args){
int n = -10;
posorneg(n);
}
public static void posorneg(int g){
if(g>0){
System.out.println("the number is positive");
}
else{
System.out.println(" the number is negative");
}
}

You might also like