Patterns

public class Trianglepattern2 {

public static void main(String[] args) {
 int n=5;
 for(int row=0;row<n;row++)
 {
 for (int col=0; col<n;col++ )
 {
 if (row+col>=n-1)
 
 {
 System.out.print("* ");

}
 else 
 {
 System.out.print(" ");
 }
 
 }
 System.out.println();

}
 }
}

Output:-

        * 
      * * 
    * * * 
  * * * * 
* * * * *

patterns

public class Trianglepattern1 {

public static void main(String[] args) {
 int n=5;
 for(int row=0;row<n;row++)
 {
 for (int col=0; col<n;col++ )
 {
 if (row<=col)
 
 {
 System.out.print("* ");

}
 else 
 {
 System.out.print(" ");
 }
 
 }
 System.out.println();

}
 }
}

Output:-

*  *  *  *  *
*  *  *  *
*  *  *
*  *
*

Program to convert Number into words

import java.util.Scanner;

public class Strings_NumtoWords 
{
static String [] s1 = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
 
static String [] s2 = {"","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twleve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
 
public static void concat(int num , String str)
{
 
if(num > 19) 
System.out.print(s1[num/10]+" "+s2[num%10]);
else
System.out.print(s2[num]);
if(num>0)
System.out.print(str);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number :- ");
int n = sc.nextInt();
concat(n/10000000 , " crore ");
concat((n/100000)%100 , " lakh ");
concat((n/1000)%100 , " thousand ");
concat((n/100)%10 , " hundred ");
concat((n)%100 , " ");
sc.close();
}

}

Program for ANAGRAM

import java.util.Scanner;

public class Strings_AnaGrams_removeSpace_sort_LowerCase_Compare
{
public static String removeSpace(String s1)
{
String res = "";
char[] ch = s1.toCharArray();
for (int i = 0; i < ch.length; i++) 
{
if (ch[i] != ' ')
{
res = res+ch[i];
}
}
return res;
}
 
public static String toLowerCase(String s1) 
{
String res = "";
char[] ch = s1.toCharArray();
for (int i = 0; i < ch.length; i++) 
{
if (ch[i] >= 65 && ch[i] <= 90) 
{
ch[i] = (char)(ch[i]+32);
}
res = res + ch[i];
}
return res;
}
 
public static String Sort(String s1)
{
char[] ch = s1.toCharArray();
String res = "";
for (int i = 0; i < ch.length-1; i++) 
{
for (int j = i+1; j < ch.length; j++) 
{
if (ch[i] > ch[j]) 
{
 
char temp = ch[i]; 
ch[i] = ch[j];
ch[j] = temp;
}
}
res = res + ch[i];
}
return res;
}
 
public static boolean compare (String s1 , String s2)
{
 
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for (int i = 0; i < c2.length; i++) 
{
if (c1[i] != c2[i])
{
return false;
}
}
return true;
}
 
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the First String :-");
String s1 = sc.nextLine();
System.out.println("Enter the Second String :-");
String s2 = sc.nextLine();
 
System.out.println("The given Strings are :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
s1 = removeSpace(s1);
s2 = removeSpace(s2);
 
System.out.println("The Strings after removing the spaces are :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
if (s1.length() == s2.length())
{
s1 = toLowerCase(s1);
s2 = toLowerCase(s2);
//logic for Anagram
System.out.println("The Strings after Converting :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
s1 = Sort(s1);
s2 = Sort(s2);
 
System.out.println("The Strings after Sorting :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
boolean res = compare(s1,s2);
if(res)
{
System.out.println("Strings are ANAGRAM");
}
else System.out.println("Strings are not ANAGRAM");
} 
else 
{
System.out.println("The given Strings are not ANAGRAM");
}
}

}

 

Program to count Number of Alphabets ; Numbers & Special Characters!

import java.util.Scanner;
public class Strings
{
 public static void count(String s1)
 {
 int countAlpha=0; 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=65 && s1.charAt(i)<=122)
 countAlpha++; 
 }
 System.out.println("the no. of Alphabet is = "+countAlpha);
 }
 public static void countNum(String s1)
 {
 int countNume=0;
 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=48 && s1.charAt(i)<=57)
 
 countNume++; 
 }
 System.out.println("the no. of Numeric is = "+countNume);
 } 
 public static void countSpe(String s1)
 {
 int countS=0;
 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=32 && s1.charAt(i)<=47 || (s1.charAt(i)>=58 
 && s1.charAt(i)<=64) )
 countS++; 
 }
 System.out.println("the no. of Special characters is = "+countS); 
 } 
 public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the string");
 String str = sc.nextLine();
 count(str);
 countNum(str);
 countSpe(str);
 }

}

Output:-

Enter the string
111Welcome@**Programming**123
the no. of Alphabet is = 18
the no. of Numeric is = 6
the no. of Special characters is = 5

Program for ‘SWAP ROWS’

import java.util.Scanner;

public class Array_2D_SwapRows
{
public static int[][] swaprows(int[][] ar)
{
for (int i = 0; i < ar.length/2; i++)
{
for (int j = 0; j < ar[i].length; j++)
{
int temp = ar[i][j]; //swap the rows with the help of i and j value...
ar[i][j] = ar[ar.length-1-i][j];
ar[ar.length-1-i][j] = temp;
}
}
return ar;
}
 
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Outer Array size : ");
int a = sc.nextInt();
System.out.println("Enter the Inner Array size : ");
int b = sc.nextInt();
 
int ar[][] = new int[a][b];
for (int i = 0; i < ar.length; i++) 
{
for (int j = 0; j < ar[i].length; j++) 
{
System.out.println("enter the value ar "+i+"th row and"+j+"the column");
ar[i][j] = sc.nextInt();
}
}
 
System.out.println("The array elements are :-");
for (int i = 0; i < ar.length; i++) 
{
for (int j = 0; j < ar[i].length; j++) 
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
int nar[][] = swaprows(ar);
System.out.println("The array elements after SWAPPING :-");
for (int i = 0; i < nar.length; i++) 
{
for (int j = 0; j < nar[i].length; j++) 
{
System.out.print(nar[i][j]+" ");
}
System.out.println();
}
}
}

Output:-

Enter the Outer Array size :
3
Enter the Inner Array size :
3
enter the value ar 0th row and0the column
1
enter the value ar 0th row and1the column
2
enter the value ar 0th row and2the column
3
enter the value ar 1th row and0the column
4
enter the value ar 1th row and1the column
5
enter the value ar 1th row and2the column
6
enter the value ar 2th row and0the column
7
enter the value ar 2th row and1the column
8
enter the value ar 2th row and2the column
9
The array elements are :-
1 2 3
4 5 6
7 8 9
The array elements after SWAPPING :-
7 8 9
4 5 6
1 2 3

Program for ‘Transpose’

import java.util.Scanner;
public class Array_2D_Transpose
{
 public static int[][] transpose(int[][] ar)
 {
 int row = ar.length;
 int col = ar[0].length;
 int nar[][] = new int[col][row];
 for (int i = 0; i < nar.length; i++) //decides number of rows... so taken new array (nar)
 {
 for (int j = 0; j < nar[i].length; j++)
 {
 nar[i][j] = ar[j][i];
 }
 }
 return nar;
 }
 
 public static void main(String[] args)
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the Outer Array size : ");
 int rows = sc.nextInt();
 System.out.println("Enter the Inner Array size : ");
 int col = sc.nextInt();
 
 int ar[][] = new int[rows][col];
 for (int i = 0; i < ar.length; i++) 
 {
 for (int j = 0; j < ar[i].length; j++) 
 {
 System.out.println("enter the value ar "+i+"th row and"+j+"the column");
 ar[i][j] = sc.nextInt();
 }
 }
 
 System.out.println("The array elements are :-");
 for (int i = 0; i < ar.length; i++) 
 {
 for (int j = 0; j < ar[i].length; j++) 
 {
 System.out.print(ar[i][j]+" ");
 }
 System.out.println();
 }
 int nar1[][] = transpose(ar);
 
 System.out.println("The array elements after TRANSPOSE :-");
 for (int i = 0; i < nar1.length; i++) 
 {
 for (int j = 0; j < nar1[i].length; j++) 
 {
 System.out.print(nar1[i][j]+" ");
 }
 System.out.println();
 }
 }
}

Output:-

Enter the Outer Array size :
3
Enter the Inner Array size :
3
enter the value ar 0th row and0the column
1
enter the value ar 0th row and1the column
2
enter the value ar 0th row and2the column
3
enter the value ar 1th row and0the column

4
enter the value ar 1th row and1the column
5
enter the value ar 1th row and2the column
6
enter the value ar 2th row and0the column
7
enter the value ar 2th row and1the column
8
enter the value ar 2th row and2the column
9
The array elements are :-
1 2 3
4 5 6
7 8 9
The array elements after TRANSPOSE :-
1 4 7
2 5 8
3 6 9

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();
}
}