Posts

Showing posts from February, 2019

program to calculate product of each column in matrix

Write a java program to calculate product of each column in matrix :  import java.util.Random; import java.util.Scanner; public class ColumnWiseProduct { public static void main(String[] args) { ColumnWiseProduct cwp=new ColumnWiseProduct(); int arr[][]=cwp.readmatrix(); cwp.dispMat(arr); int prod[]=cwp.prodColWise(arr); System.out.println("Entered matrix Column wise product is :"); cwp.dispSingleArray(prod); } int[] prodColWise(int mat[][]){ int prod[]=new int[mat[0].length]; for(int i=0;i<mat[0].length;i++) { prod[i]=1; for(int j=0;j<mat.length;j++) { prod[i]=prod[i]*mat[j][i]; } } return prod; } int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int

calculate sum column wise in a matrix:

Write a java program to calculate sum column wise in a matrix: import java.util.Random; import java.util.Scanner; public class ColumnWiseLeast { public static void main(String[] args) { int mat[][]=readmatrix(); dispMat(mat); System.out.println("Sum of Diagonal element is : "); int arr[]=leastColWise(mat); dispSingleArray(arr); } static int[] leastColWise(int mat[][]) { int least[]=new int[mat[0].length]; for(int i=0;i<mat[0].length;i++) { least[i]=mat[0][i]; for(int j=0;j<mat.length;j++) { if(least[i]>mat[j][i]) { least[i]=mat[j][i]; } } }return least; } static int[][] readmatrix(){ Random rx=new Random(); Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { fo

Write a java program to sum of Diagonal elements in matrix:

Write a java program to sum of Diagonal elements in matrix: import java.util.Random; import java.util.Scanner; public class SumOfDiagonalElements { public static void main(String[] args) { int arr[][]=readmatrix(); dispMat(arr); int sum[]=sumOfDiagonal(arr); System.out.println("primary Diagonal element sum is : "+sum[0]); System.out.println("Secondary Diagonal element sum is :"+sum[1]); } static int[] sumOfDiagonal(int mat[][]) { int []sum=new int[2]; int n=mat.length; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j) { sum[0]=sum[0]+mat[i][j]; } if(i+j==n-1) { sum[1]=sum[1]+mat[i][j]; } } } return sum; } static int[][] readmatrix(){ Random rx=new Random(); Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row]

Rotate matrix /2-D array 90 degrre left and Right

Write a java program to Rotate matrix 90 degree left and rotate 90 degree right :  import java.util.Random; import java.util.Scanner; public class RotateMatrix { public static void main(String[] args) { System.out.println("Enter matrix : "); int arr[][]=readmatrix(); System.out.println("Matrix is : "); dispMat(arr); int arr2[][]=rotate90Left(arr); System.out.println("90 Degree left rotate matrix is :"); dispMat(arr2); System.out.println("90 degree right rotate Matrix is :"); arr=rotate90Right(arr); dispMat(arr); } static int[][] rotate90Left(int mat[][]){ ColumnWiseReverse cw=new ColumnWiseReverse(); mat=transposeMat(mat); cw.colWiseReverse(mat); return mat; } static int[][] rotate90Right(int mat[][]){ RowWiseReverse rw=new RowWiseReverse(); mat=transposeMat(mat); rw.rowWiseReverse(mat); return mat; } static int[][] transposeMat(int m[][])

Count negative number in a matrix

Write a java program to count negative number in a matrix : import java.util.Scanner; public class CountNegValue { public static void main(String[] args) { int a[][]=readmatrix(); dispMat(a); int count=countNeg(a); System.out.println("Entered negative number is : "+count); } static int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { mat[i][j]=sc.nextInt(); } }return mat; } static void dispMat(int mat[][]) {   for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[i].length;j++) { System.out.print(mat[i][j]+" "); }System.out.println(); }   } static int countNeg(int mat[][]) { int count=0;

Sum of Each element Of matrix.....

Write a java program to calculate sum of each element of matrix using Random input :  import java.util.Random; import java.util.Scanner; public class SumOfMatrixElement { static Random rx=new Random(); public static void main(String[] args) { System.out.println("Enter a matrix : "); int [][]mat=readmatrix(); dispMat(mat); int sum=sumOfMatrix(mat); System.out.println("Sum of matrix is : "+sum); } static int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { mat[i][j]=rx.nextInt(50); } }return mat; } static void dispMat(int mat[][]) {   for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[i].le

2-D array Random input and Find Biggest Element

Write a java program to find biggest element from matrix using random input :  import java.util.Random; import java.util.Scanner; public class BiggestElementMatrix { static Random rx=new Random(); public static void main(String[] args) { System.out.println("Enter a matrix : "); int [][]mat=readmatrix(); int big=biggestMatrix(mat); System.out.println("Matrix is "); dispMat(mat); System.out.println("Biggest element is : "+big); } static int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { mat[i][j]=rx.nextInt(50); } }return mat; } static int biggestMatrix(int[][] mat) { int temp=0; for(i

Matrix Addition using simplest way...........

Write a java program to add two matrices :  import java.util.Scanner; public class AddTwoMatrix { public static void main(String[] args) { System.out.println("Enter Read first matrix : "); int[][] a=readmatrix(); System.out.println("Enter second matrix : "); int b[][]=readmatrix(); int res[][]=addMat(a, b); System.out.println("First matrix is : "); dispMat(a); System.out.println("Second matrix is : "); dispMat(b); System.out.println("Result matrix is : "); dispMat(res); }  static int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { mat[i][j]=sc.nextInt(); } }return mat;

Diamond inside Rectangle/square

Image
Write a java program to print following pattern: import java.util.Scanner; public class DiamondInsideSqure { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Row size :"); int n = sc.nextInt(); int states = n, space = 0; for (int i = 0; i < 2 * n-1; i++) { if(i==0) { states=states; space=space; } else if (i <n) { states = states - 1; space = space + 2; } else { states = states + 1; space = space - 2; } for (int j = 0; j < states; j++) { System.out.print("*"); } for (int k = 0; k < space; k++) { System.out.print(" "); } for (int j = 0; j < states; j++) { System.out.print("*"); } System.out.println(); } } }
Write a java program to calculate each word length in sentence : import java.util.Scanner; public class CountCharInEachWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); charLength(str); } static void charLength(String str) { for(int i=0;i<str.length();i++) { int k=i; String temp=""; while(i<str.length() && str.charAt(i)!=' ' ) { temp=temp+str.charAt(i); i++; } System.out.println(temp+" Length is "+(i-k)); } } }
Write a java program to change all vowels in Capital and other in small : import java.util.Scanner; public class VowelsIntoCap { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); str = vowelsCap(str); System.out.println("After changing vowels in capital \n" + str); } private static String vowelsCap(String str) { char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u') { ch[i] = (char) (ch[i] - 32); } else if (ch[i] >= 'A' && ch[i] <= 'Z') { if ((ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' || ch[i] == 'U')) ch[i] = (char) (ch[i]); else { ch[i] = (char) (ch[i] + 32); } } else {
Write a java program to change first char of every word in initCap :  import java.util.Scanner; public class ConvertWordInitCap { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Before init Caps"); System.out.println(str); str = intitCaps(str); System.out.println("After init Cap "); System.out.println(str); } private static String intitCaps(String str) { char ch[] = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (i == 0 && ch[i] != ' ' || (ch[i] != ' ' && ch[i - 1] == ' ')) { if (ch[i] >= 'a' && ch[i] <= 'z') { ch[i] = (char) (ch[i] - 32); } } else { if (ch[i] >= 'A' && ch[i] <= 'Z') { ch[i] = (char) (ch[i] + 32); }
Write a java program to count words in a string :  import java.util.Scanner; public class CountWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); int c=countWord(str); System.out.println(c); } private static int countWord(String str) { char ch[] = str.toCharArray(); int count=0; for (int i = 0; i < ch.length; i++) { if((i==0 && ch[i]!=' ' )|| (ch[i]!=' ' && ch[i-1]==' ')) { count++; } } return count; } }
Write a java program to change First char in small and other in capital :  import java.util.Scanner; public class FirstCharIntoSmall { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Before small caps \n"+str); System.out.println("After changing String "); str=initSmall(str); System.out.println(str); } private static String initSmall(String str) { char ch[] = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (i == 0 && ch[i] != ' ' || (ch[i] != ' ' && ch[i - 1] == ' ')) { if (ch[i] >= 'A' && ch[i] <= 'Z') { ch[i] = (char) (ch[i] + 32); } } else { if (ch[i] >= 'a' && ch[i] <= 'z') { ch[i] = (char) (ch[i] - 32); } }
Write a java program to match with word: import java.util.Scanner; public class CheckSpecifiedWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Enter Substring : "); String substr=sc.nextLine(); boolean rs=isMatchWord(str, substr); if(rs) { System.out.println("Matched"); } else { System.out.println("unmatched"); } } static boolean isMatchWord(String str,String substr) { int j=0; for(int i=0;i<str.length();i++) { String temp=""; while(i<str.length() && str.charAt(i)!=' ') { temp=temp+str.charAt(i); i++; } System.out.println(temp); System.out.println(substr); if(temp.equals(substr)) return true; } return false; } }
Write a java program to count how many times substring is present in Sentence :  import java.util.Scanner; public class CountSubString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Sentence "); String str = sc.nextLine(); System.out.println("Enter Substring : "); String substr=sc.nextLine(); int count=toCountSubstring(str,substr); System.out.println("Entered Substring is present "+count+" times"); } private static int toCountSubstring(String str, String substr) { char []string =str.toCharArray(); char []subst=substr.toCharArray(); int count=0; for(int i=0;i<string.length;i++) { int j=0,k=i; while(k<string.length && j<subst.length && (string[k]==subst[j])) { k++; j++; } if(j==subst.length) { count++; } } return count; } }
Write a java program to check two string is Anagram or not :  import java.util.Scanner; class Anagram { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter first Sentence "); String st=sc.nextLine(); int[] string1=indexNumber(st); System.out.println("Enter Second Sentence "); String st2=sc.nextLine(); int[] string2=indexNumber(st2); CompareTwoArray ct=new CompareTwoArray(); boolean rs=ct.isAnagram(string1, string2); if(rs) { System.out.println("String is anagram "); } else { System.out.println("Not a anagram"); } } static int[] indexNumber(String str) { int count[]=new int[26]; for(int i=0;i<str.length();i++) { char ch=str.charAt(i); if(ch>=65 && ch<=90) { count[ch-65]++; } else if(ch>=97 && ch<=122) { count[ch-97]++; } } return count; } }
Write a java program to check string is Panagram or not ?  import java.util.Scanner; public class PanagramString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String : "); String str = sc.nextLine(); if (str.length() < 26) { System.out.println("Entered String is not Panagram "); } else { boolean re = isPanagram(str); if (re) { System.out.println("Entered string is panagram "); } else System.out.println("Not a panagram string"); } } static boolean isPanagram(String str) { int count[] = new int[26]; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch >= 65 && ch <= 90) { count[ch - 65]++; } else if (ch >= 97 && ch <= 122) { count[ch - 97]++; } } for (int i = 0; i < count.length; i++) { if (count[i] == 0)
Write a java program to print following pattern : ABCDEFGHI  ABCDEFG    ABCDE     ABC       A import java.util.Scanner; public class DecreseCharIncreasingOrder { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of rows"); int n=sc.nextInt(); for(int i=0;i<n;i++) { int k=0; for(int j=0;j<i;j++) { System.out.print(" "); } for(int j=0;j<2*(n-i)-1;j++) System.out.print((char)(65+k++)+""); System.out.println(); } } }
Write a java program to print following pattern *********  *******   *****    ***     * import java.util.Scanner; public class StarPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row Size : "); int n=sc.nextInt(); for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { System.out.print(" "); } for(int k=2*(n-i)-1;k>0;k--) { System.out.print("*"); } System.out.println(); } } }
Write a java program to print following pattern:-         A      ABA    ABCAB  ABCDABC import java.util.Scanner; public class CharPattern20 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row size : "); int n=sc.nextInt(); for(int i=0;i<n;i++) { int k=1,l=2*i-1; for(int j=0;j<n-i;j++) { System.out.print(" "); } for(int j=0;j<l;j++) { System.out.print((char)(64+k++)+""); if(j==i-1) k=1; } System.out.println(); } } }
Write a java program to print following pattern          1        1 2 1      1 2 3 2 1    1 2 3 4 3 2 1  import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Row Size "); int n=sc.nextInt(); for(int i=0;i<n;i++) { int k=1,l=2*i-1; for(int j=0;j<n-i;j++) { System.out.print("  "); } for(int j=0;j<l;j++) { System.out.print(k+" "); if(j<l/2) k++; else k--; } System.out.println(); } } }

count each character

Write a java program to count frequency of each char using Array :- import java.util.Scanner; class CharFreq{ static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter a String : "); String str=sc.nextLine(); int fr[]=new int[26]; for(int i=0;i<str.length();i++) { if(str.charAt(i)>=65 && str.charAt(i)<=90) { fr[str.charAt(i)-65]++; } else if(str.charAt(i)>=97 && str.charAt(i)<=122) { fr[str.charAt(i)-97]++; } } for(int i=0;i<26;i++) { if(fr[i]>0) System.out.println((char)(i+65)+" | "+fr[i]); } } }
Write a java program to print following pattern:- 1 2 3 4 5  10 9 8 7 6  11 12 13 14 15  20 19 18 17 16  21 22 23 24 25  import java.util.Scanner; public class CountPattern27 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); int k=1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i%2 != 0) {   System.out.print(k+" "); } else { System.out.print((-j+i*n+1)+" "); } k++; } System.out.println(); } } }
Write a java program to print following pattern:- 1 3 2 4 5 6 10 9 8 7 11 12 13 14 15  import java.util.Scanner; public class CountPattern31 { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter Size : "); int n=sc.nextInt(); int k=1; for(int i=1;i<=n;i++) { int l=k+i-1; for(int j=1;j<=i;j++) { if (i%2 != 0) {   System.out.print(k+" "); } else { System.out.print(l+" "); } k++; l--; } System.out.println(); } } }
Write a java program to print following pattern when n=5: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1   import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 1; i <=2*n-1; i++) { int k=1; if(i<=n) { for(int j=1;j<=i;j++)  { System.out.print(k+" "); k++; } }  else { for(int j=1;j<2*n-i+1;j++) { System.out.print(k+" "); k++; } } System.out.println(); } } }
Write a java program to print following number pattern  1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 16 17 18 19 20 6  7  8  9 10 import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); int k=0,l=1; for (int i = 1; i <= n; i++) { if(i <= n/2+1) { if(i!=1)    k=k+2; } else { if (k%2==0) { k--; } else { k=k-2; } } for (int j = 1; j <= n; j++) { System.out.print((k*n+j)+" "); } System.out.println(); } } }
Write a java program to print following pattern 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25  import java.util.Scanner; public class NumberPattern { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 0; i < n; i++) { int k=i+1; for (int j = 0; j < n; j++) { System.out.print(k+" "); k+=n; } System.out.println(); } } }

to find every element frequency..........

Write a java program to check and display frequency of each element :-- import java.util.Scanner; public class FrequencyElements { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int arr[]=inputArray(); int [] fr = new int [arr.length];          int visited = -1;         for(int i = 0; i < arr.length; i++){              int count = 1;              for(int j = i+1; j < arr.length; j++){                  if(arr[i] == arr[j]){                      count++;                      //To avoid counting same element again                      fr[j] = visited;                  }              }              if(fr[i] != visited)                  fr[i] = count;          }   System.out.println("---------------------");          System.out.println(" Element | Frequency");          System.out.println("---------------------");          for(int i = 0; i < fr.length; i++){           
Write a java program to find Nth smallest number without sorting :- import java.util.Scanner; public class NthLeastNumber { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int arr[]=inputArray(); System.out.println("Enter which smallest element you want :"); int n=sc.nextInt(); int sm=nThsmallest(arr,n); System.out.println(n+" biggest is "+sm); } static int nThsmallest(int[] ar,int n) { for(int i=0;i<ar.length;i++) { int count=0; for(int j=0;j<ar.length;j++) { if(ar[j]<ar[i]) count++; } if(count==n-1) return ar[i]; } return 0; } static int[] inputArray() { Scanner sc=new Scanner(System.in); System.out.println("Enter Size of Array : "); int n=sc.nextInt(); int count=0; int arr[]=new int[n]; System.out.println("enter value in array : "); for (int i=0; i<n; i++) { arr[i]=sc.nextInt(); } return arr;
Write a java program to Define a method to Display pair of elements which sum is Equal to n import java.util.Scanner; public class PairInArray { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter Length of Array : "); int n=sc.nextInt(); int arr[]= new int[n];                 System.out.println("enter value in array : "); for (int i=0; i<n; i++) { arr[i]=sc.nextInt(); } System.out.println("\nEnter  a number you check pair : "); int pair=sc.nextInt(); for (int i = 0; i < arr.length; i++) { for (int j = i+1; j < arr.length; j++) { if(pair==arr[i]+arr[j]) System.out.println(i+"  "+j); } } } }
Image
Write a java program to print following pattern  import java.util.Scanner; public class StarPattern12 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if(i==0 ||  i==n-1 || i==j || i==n-j-1 ) System.out.print("* "); else System.out.print("  "); } System.out.println(); } } }
Image
Write  a java program to print following pattern and input given by user : import java.util.Scanner; public class StarPattern11 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Size: "); int n=sc.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if(i==0 || j==0 || j==n-1 || i==n-1 || i==j || i==n-j-1 ) System.out.print("* "); else System.out.print("  "); } System.out.println(); } } }
Write a java program to print following pattern       *******         *****           ***            *            *          ***        *****      ******* import java.util.Scanner; public class StarPattern10 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter n value"); int n=sc.nextInt(); int sp=0, st=n; for(int i=1;i<=n;i++) { for(int j=1;j<=sp;j++) System.out.print(" "); for(int j=1;j<=st;j++) System.out.print("*"); if (i<=n/2){ sp++; st=st-2; } else { sp--; st=st+2; } System.out.println(); } } }

union of two array

Write a program to union two arrays import java.util.Scanner; import java.util.Arrays; public class UnionTwoArray { public static void main(String[] args) { System.out.println("For First Array : "); int[] arr1=inputArray(); System.out.println("For Second Array : "); int[] arr2=inputArray(); int[] arr3=mergeSort(arr1, arr2); int []arr4=removeDuplicates(arr3); displayArray(arr4); } static int[] inputArray() { Scanner sc=new Scanner(System.in); System.out.println("Enter Size of Array : "); int n=sc.nextInt(); int count=0; int arr[]=new int[n]; System.out.println("enter value in array : "); for (int i=0; i<n; i++) { arr[i]=sc.nextInt(); } return arr; } static int[] mergeSort(int a[],int b[]) { int c[]=new int[a.length+b.length]; int i=0,j=0,k=0; while(i<a.length && j<a.length) { if(a[i]<b[j] || a[i]==b[j]) { c[k++
Write a java program for sum of n natural number using user input :- import java.util.Scanner; public class SumNaturalNumber { public static void main(String[] args) { System.out.println("Enter Natural Number"); int n=sc.nextInt(); int sum=0; int t=n; while(n>0) { sum=sum+n; n--; } System.out.println("Sum of first "+t+" is "+sum); } static Scanner sc=new Scanner(System.in);  }

Add Two Number

Write a java program to add two numbers class Add { public static void main(String[] args) { int a=5;              int b=10;              int c=a+b; System.out.println("Sum of "+a+" and "+b+" is :"+c); } }

print Hello

Write a java program to print "Hello" class Hello { public static void main(String[] args) { System.out.println("Hello"); } }

Armstrong Number

Write a program to print armstrong number public class Armstrong { public static void main(String[] args) { int i=1; f or(;i<10000;i++) { boolean arms=isArmStrong(i); if(arms) System.out.println(i); } }             static boolean isArmStrong(int n) { int sum=0,t=n; int pow=countDigit(n); do { int rem=n%10; sum=sum+Pow(rem, pow); n=n/10; }while(n!=0); return sum==t; } static int countDigit(int n) { int count=0; do { count++; n/=10; } while (n!=0); return count; } static int Pow(int n,int p) { int pow=1; while(p>0) { pow=pow*n;

Patterns

Write a java program to print star pattern Output is following :  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  import java.util.Scanner;           public class StarPattern { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter n value"); int n=sc.nextInt(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { System.out.print(" * "); } System.out.println(); } } }