Program to print the star pattern triangle left to right


This program is about to print the star pattern triangle from left to right the row of triangle is defined 5 here in this program. So, only 5 rows will be printed. There are 2 loops will be execute to print the triangle.
There are one integer variables are used in each loop.
We are using for loop the outer loop will execute till the i variable is less then equals to (<=) row which is 5 and i will be incremented by 1 each time and i is initialized from 0.
In the next inner for loop the k variable we are using and initialized each time from 0 when it comes to inner loop from out loop and loop will execute till count of k is less then i so, each time when the inner loop will execute it will print the "* and space" and k will increment from 1.
After completing inner loop a new line will be printed or the cursor will move to the new line.

/******************************************************************************

Program to print the star pattern triangle left to right.

*******************************************************************************/


public class Main
{
                public static void main(String[] args) {
                                int row = 5;
                                for(int i = 0; i <= row; i++){
                                    for(int k = 0; k < i; k++){
                                        System.out.print("* ");
                                    }
                                    System.out.println();
                                }
                }
}


After completing the output is given below:

Output:











If you have any query regarding the program then please ask or comment below.

Program to find the digits from alphanumeric string and find the sum of digits.


This is program is about to find the digits from alpha numeric string and find the sum of all digits.
We will read the string character wise then it is possible to find the number from string. When we'll read each character we've to use the isDigit function of java which is defined in the Character class. isDigit function allows us to identify the given character is string or alphabet.

/******************************************************************************

Write a program to find the sum of digits found in given string.

*******************************************************************************/

public class Main
{
                public static void main(String[] args) {
                                int sum = 0;
                                String str = "sd54s4dfwer46df4d";
                                for(int i = 0; i < str.length(); i++){
                                                if(Character.isDigit(str.charAt(i))){
                                                                sum = sum + Integer.parseInt(String.valueOf(str.charAt(i)));
                                                                System.out.print(str.charAt(i)+" ");
                                                }
                                }
                                System.out.println("Sum of digits is : "+sum);
                }
}


Output:


If you have any question about it then please comment below.