Category Archives: java example

java program to print pattern

Java Porgram to print pattern

Write a java program to prints a staircase of size  n like below show .

Java Porgram to print pattern

Java Porgram to print pattern

Input Data

An integer n , that represent the size of the staircase (pattern).

Output Data

Print a staircase (pattern) of size using # symbols and spaces using Java.

Explanation

The staircase (pattern) is right-aligned, composed of # symbols and spaces, and has a height and width of n=6

import java.util.*;

public class Pattern {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
       for (int k=1;k<=n;k++){
        for (int k1=1;k1<=n-k;k1++){
            System.out.printf(" ");
        }
        for (int i=1;i<=k;i++){
            System.out.print("#");
        }
        System.out.println("");
    }
    }
}


Java program to remove string spaces

Java program to remove string spaces ?

Java program to remove leading and trailing spaces from string – the following java program or java source code to remove the space from string using toCharArray() and length with the help of for loop. The toCharArray() is used to convert the string to the character array and length is used to get the length of string.

java example

class A8

{

public static void main(String arr[])

{

int x[],i=0,j=0;

String s="        UmarKhan     ";

char ch[]=s.toCharArray();

int c=ch.length;

x=new int;

for(i=0;i<c;i++)

{


x[i]=ch[i];

}

for(i=0;i<c;i++)

{

if(x[i]!=32)

{

j=i;

break;

}

}

for(i=c-1;i>=0;i--)

{

if(x[i]!=32)

{

c=i;

break;

}

}

for(i=j;i<=c;i++)

System.out.print(ch[i]);

}

}

output

UmarKhan

The following java source code removes the leading and trailing spaces only not in between spaces.

 

For more c example, c tutorial orc tutorial and example PDF visit – java example

Do not forgot to like share and comment