remove first and last occurrence of string java

   

 remove first and last occurrence of string java

String: codedecode
input: e

Output: coddecod


public class Demo200 {


public static void main(String[] args) {

String s="codedecode";

char ch='e';

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

    {

 

       

        if (s.charAt(i) == ch)

        {

            s = s.substring(0, i) +

                s.substring(i + 1);

            break;

        }

    }

 

   

    for (int i = s.length() - 1; i > -1; i--)

    {

 

       

        if (s.charAt(i) == ch)

        {

            s = s.substring(0, i) +

                s.substring(i + 1);

            break;

        }

    }

  System.out.println(s);


}


}


Output:

coddecod


more programs visit http://sateeshm.com/

Count the unique characters in a given String

  

 Count the unique characters in a given String

String: Sateesh

Output: 3


public class Demo200 {


public static void main(String[] args) {

String s="sateesh";

StringBuffer str=new StringBuffer(s);

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

    {

        int flag = 0;

        for (int j = 0; j < str.length(); j++)

        {

            

            if (str.charAt(i) == str.charAt(j) && i != j)

            {

                flag = 1;

               

                break;

            }

        }

        if (flag == 0)

            System.out.print(str.charAt(i));

    }

}


}


Output:

3


more programs visit http://sateeshm.com/

Display unique characters in a given String

 

 Display unique characters in a given String

String: Sateesh

Output: ath


public class Demo200 {


public static void main(String[] args) {

String s="sateesh";

StringBuffer str=new StringBuffer(s);

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

    {

        int flag = 0;

        for (int j = 0; j < str.length(); j++)

        {

           

            if (str.charAt(i) == str.charAt(j) && i != j)

            {

                flag = 1;

               

                break;

            }

        }

        if (flag == 0)

            System.out.print(str.charAt(i));

    }

}


}


Output:

ath


more programs visit http://sateeshm.com/

Count the number of divisible numbers in a given range

 Count the number of divisible numbers in a given range

Range: 1-100
Number : 2

Output: 50

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,count=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%n==0)

{

count=count+1;

}

x++;

}while(x<=100);

System.out.println(count);

}



}



Output:

Enter n value

2

50


more programs visit http://sateeshm.com/

Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop

 

  • Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0 && n%2==0)

{

sum=sum+x;

}

else if(x%2!=0 && n%2!=0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println("sum is " +sum);

}



}



output:


Enter n value 5

sum is 9


Enter n value 6

sum is 12

Sum Of Even Numbers DoWhile Loop

  Sum Of Even Numbers DoWhile Loop 


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println(sum);

}



}


Output:

Enter n value

5

6


Sum Of Natural Numbers DoWhile Loop

 

  • Sum Of Natural Numbers DoWhile Loop

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

sum=sum+x;

x++;

}while(x<=n);

System.out.println(sum);

}



}



output:

Enter n value

5

15

Factorial Number Using DoWhile Loop -up to n Numbers

  

  • Factorial Number Using DoWhile Loop(up to n Numbers)

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,fact=1;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

fact=fact*x;

System.out.println(x+" Factorial =" +fact);

x++;

}while(x<=n);

}



}


Output:

Enter n value

5

1 Factorial =1

2 Factorial =2

3 Factorial =6

4 Factorial =24

5 Factorial =120

More Programs visit : http://sateeshm.com/

Factorial Number Using DoWhile Loop

 

  • Factorial Number Using DoWhile Loop

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,fact=1;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

fact=fact*x;

x++;

}while(x<=n);

System.out.println(fact);

}



}


Output:

Enter n value

5

120

More Programs visit : http://sateeshm.com/