Java_Programs
Java_Programs
*** ***
** **
**
**
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
import java.util.Scanner;
public class PatternNuberPyramidPrevRev
{
public static void main(String args[])
{
int s = 1;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the N values");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
while (s <= i)
{
System.out.print(s);
s++;
}
s--;
while (s > 1)
{
System.out.print(--s);
}
System.out.println();
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15
public class PatternNumberPyramidUptoN
{
public static void main(String args[])
{
{
int i, j, n = 1;
for (i = 0; i < 5; i++)
{
for (j = 0; j <= i; j++)
{
System.out.print(n + " ");
n++;
}
System.out.println();
}
}
}
}
1
12
123
1234
12345
import java.util.Scanner;
public class PatternNumberPyramid
{
public static void main(String args[])
{
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Row value n");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
System.out.print(" " + j);
System.out.print("\n");
}
}
}
13. Print numbers in pyramid vice:
import java.util.Scanner;
public class PatternNumberPyramidArrow
{
public static void main(String args[])
{
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the values ");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
System.out.print(" " + j);
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j <= i; j++)
System.out.print(" " + j);
System.out.print("\n");
}
}
}
1
12
123
1234
12345
1234
123
12
1
import java.util.Scanner;
public class PatternNumberPyramidRev
{
public static void main(String args[])
{
int i, j, k, n, a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the n values");
n = sc.nextInt();
a = n;
for (i = 1; i <= n; i++)
{
for (j = a; j > 1; j--)
{
System.out.print(" ");
}
for (k = i; k != 0; k--)
{
System.out.print(k);
}
a--;
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
}
}
1
212
32123
4321234
543212345
int[][] b = new int[][] { { 10, 11, 12},{ 13, 14, 15},{ 16, 17, 18} };
int[][] c = new int[3][3];
if(a.length == b.length && a[0].length == b[0].length)
{
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
else
{
System.out.println("'A' and 'B' Matrix are not SAME");
return;
}
System.out.println("The Matrix 'A' Value:");
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("The Matrix 'B' Value:");
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(b[i][j]+ " ");
}
System.out.println();
}
System.out.println("The Addition Matrix of 'A' and 'B' Value:");
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
36. Program to count each words and total number of words in given string:
import java.io.IOException;
public class FindTtalCountWords
{
public static void main(String args[]) throws IOException
{
countWords("apple banna apple fruit fruit apple hello hi hi hello hi");
}
static void countWords(String st)
{
String[] words = st.split("\\s");
int[] fr = new int[words.length];
for (int i = 0; i < fr.length; i++)
fr[i] = 0;
for (int i = 0; i < words.length; i++)
{
for (int j = 0; j < words.length; j++)
{
if (words[i].equals(words[j]))
{
fr[i]++;
}
}
}
for (int i = 0; i < words.length; i++)
{
for (int j = 0; j < words.length; j++)
{
if (words[i].equals(words[j]))
{
if (i != j)
{
words[i] = "";
}
}
}
}
int total = 0;
System.out.println("Words and words count:");
for (int i = 0; i < words.length; i++)
{
if (words[i] != "")
{
System.out.println(words[i] + "=" + fr[i]);
total += fr[i];
}
}
System.out.println("Total words counted: " + total);
}
}
37. Program to reverse the string and check whether it is palindrome or not:
public class PalindromeChecking
{
public static void main(String[] args)
{
String inpstr ="AMMA";
char[] inpArray = inpstr.toCharArray();
char[] revArray = new char[inpArray.length];
int j=0;
for (int i = inpArray.length - 1; i >= 0; i--) {
revArray[j]=inpArray[i];
j++;
}
String revstr=String.valueOf(revArray);
if(inpstr.equals(revstr))
{
System.out.println("The given string is a Palindrome");
}
else
{
System.out.println("The given string is not a Palindrome");
}
}
}
46. Program to find difference of minimum and maximum numbers of array in java:
import java.util.Scanner;
class MinMaxInArray
{
int getMax(int[]inputArray)
{
int maxValue=inputArray[0];
for(int i=1;i<inputArray.length;i++)
{
if(inputArray[i]>maxValue)
{
maxValue=inputArray[i];
}
}
return maxValue;
}
int getMin(int[]inputArray)
{
int minValue=inputArray[0];
for(int i=1;i<inputArray.length;i++)
{
if(inputArray[i]<minValue)
{
minValue=inputArray[i];
}
}
return minValue;
}
}
public class ExArrayDifference
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements you wants to enter :" );
n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<arr.length;i++)
{
System.out.print("Enter ["+(i+1)+"] element :" );
arr[i]=sc.nextInt();
}
MinMaxInArray mm=new MinMaxInArray();
System.out.println("Maximum value is :" +mm.getMax(arr));
System.out.println("Minimum value is :" +mm.getMin(arr));
int Difference=mm.getMax(arr)-mm.getMin(arr);
System.out.print("Difference between Minnimum and Maximum in array is : "
+Difference );
}
}