Program for subString()

import java.util.Scanner;
public class Strings_SubString 
{
 public static String subString(String s , int start ,int end)
 {
 char[] ch = s.toCharArray();
 String res = "";
 for (int i = start; i < end; i++) 
 {
 res = res + ch[i];
 }
 return res;
 }
 public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the String :- ");
 String s1 = sc.nextLine();
 System.out.println("Enter the starting index :- ");
 int a = sc.nextInt();
 System.out.println("Enter the ending index :- ");
 int b = sc.nextInt();
 System.out.println(subString(s1,a,b));
 sc.close();
}
}

Program for parseInt()

import java.util.Scanner;

public class Strings_parse 
{
 public static int parseInt(String s1)
 {
 int out = 0;
 char[] ch = s1.toCharArray();
 for (int i = 0; i < ch.length; i++)
 {
 if (ch[i] >= 48 && ch[i] <= 57) 
 {
 out = out*10+(ch[i]-48);
 }
 else return -1;
 }
 return out;
 }
public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the String :- ");
 String s1 = sc.next();
 System.out.println("Output :- "+parseInt(s1));
 sc.close();
 }
}

Write a program to print the sum of all the elements of an array of size N where N can be any integer between 1 and 100. 1≤N≤100

import java.util.Scanner;
public class Number_Sum_of_N
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Declare the size of array”);
int N = sc.nextInt();
int arr [] = new int [N];
System.out.println(“Enter “+N+” vlaues of array”);
for (int i = 0; i < arr.length; i++)
{
arr [i] = sc.nextInt();
}
int sum = 0;
for (int j = 0; j < arr.length; j++)
{
sum = sum + arr[j];
System.out.println(“arr[“+j+”] = “+sum);
}
System.out.println(“sum = “+sum);
sc.close();
}
}

Number Star pyramid

public class NumberStar {
public static void main(String[] args)
{

int n=5;
for (int row = 0; row < n; row++)
{
for (int space = 0; space <n-1-row; space++)
{
System.out.print(” “);
}
for (int star = 0; star <=2*row; star++)
{
if(star%2!=0)
System.out.print(“* “);
else
System.out.print(star+1+” “);
}
System.out.println();
}

}
}

Output:-

        1 
      1 * 3 
    1 * 3 * 5 
  1 * 3 * 5 * 7 
1 * 3 * 5 * 7 * 9

Number pattern

public class Numbertriangle6 {
 public static void main(String[] args) 
 {
int p=1;
 int n=4;
 for (int i = n; i >= 1; i--)
 {
 for (int j = 1; j <= i; j++) 
 {
 if(i==1)
 {
 System.out.print(" ");
 }
 System.out.print(p++ +" "); 
 System.out.print(" ");
 } 
 System.out.println();
 System.out.print(" ");
 if(i==3)
 {
 System.out.print(" ");
 }
 }
}
}

Output:-

  1  2  3  4 
   5  6  7 
    8  9 
     10 

Number pattern

public class Numbertriangle {
 public static void main(String[] args)
 {
 int n=4;
 int p=1;
 for (int i = 1; i <= n; i++)
 {
 if(i==1)
 {
 for (int j = 1; j <= i; j++) 
 {
 System.out.print(p++ +" ");
 }
 }
 else
 {
 for (int j = 1; j <= i; j++) 
 {
 System.out.print(p++ +" ");
 System.out.print(" ");
 }
 }
 System.out.println(" ");
 }
}
}

Output:-

1 
2 3 
4 5 6 
7 8 9 10

Program to print DIAMOND pattern

public class Pyramid_Diamond {
public static void main(String[] args) {
 int n=3;
 for (int row=0;row<n-1 ;row++ )
 {
 for (int space=0;space<n-1-row;space++ )
 {
 System.out.print(" ");
 }
 for (int stars=0;(2*row>=stars);stars++ )
 {
 if (stars==0||stars==2*row)
 {
 System.out.print("* ");
 }
 else 
 {
 System.out.print(" ");
 }
 }
 System.out.println();
 }
for (int row=0;row<n ;row++ )
 {
 for (int space=0;space<row;space++ )
 {
 System.out.print(" ");
 }
 for (int stars=0;(stars<2*(n-row)-1);stars++ )
 {
 if (stars==0||stars==2*(n-row)-2)
 {
 System.out.print("* ");
 }
 else 
 {
 System.out.print(" ");
 }
 }
 System.out.println();
 }
 }
}

Output:-

    * 
  *   * 
*       * 
  *   * 
    *

Program to print mixture of alphabets in Pyramid Pattern

public class Pyramid{
public static void main(String[] args)
{
int n=3;
 for (int row = 0; row < n; row++)
 {
 char a=65;
 for (int space = 0; space < n-1-row; space++) 
 {
 System.out.print(" ");
 }
 for (int star = 0; star <=2*row; star++) 
 {
 System.out.print(a+++" ");
 }
 System.out.println();
 }
 for (int row = n-2; row >=0; row--)
 {
 char a=97;
 for (int space = 0; space < n-1-row; space++) 
 {
 System.out.print(" ");
 }
 for (int star = 0; star <=2*row; star++) 
 {
 System.out.print(a+++" ");
 }
 System.out.println();
 }
}
}

Output:-

    A 
  A B C 
A B C D E 
  a b c 
    a