35. Number pattern 10.
1
121
12321
1234321
123454321
12345654321
1234567654321
import java.util.*;
class Pattern
{
public static void main(String[] args)
{
int i, j, k = 1, l, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of levels of pattern");
n = sc.nextInt();
System.out.println("\nPattern is : \n");
for (i = 1; i <= n; i++)
{
l = i;
for (j = 1; j <= k; j++)
{
if (j >= i + 1)
{
System.out.print(--l);
}
else
{
System.out.print(j);
}
}
k = k + 2;
System.out.println(" ");
}
}
}
@java_codings
1
121
12321
1234321
123454321
12345654321
1234567654321
import java.util.*;
class Pattern
{
public static void main(String[] args)
{
int i, j, k = 1, l, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of levels of pattern");
n = sc.nextInt();
System.out.println("\nPattern is : \n");
for (i = 1; i <= n; i++)
{
l = i;
for (j = 1; j <= k; j++)
{
if (j >= i + 1)
{
System.out.print(--l);
}
else
{
System.out.print(j);
}
}
k = k + 2;
System.out.println(" ");
}
}
}
@java_codings
36. Star Pattern 1.
****
***
**
*
class StarPattern1
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
****
***
**
*
class StarPattern1
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
37. Star Pattern.
*
**
***
****
*****
class StarPattern2
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
*
**
***
****
*****
class StarPattern2
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
38. Star Pattern 3.
class StarPattern3
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
class StarPattern3
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
39. Star Pattern 4.
**
****
******
********
**********
class StarPattern4
{
public static void main(String arg[])
{
int num = 12;
int f = 2;
int g = num - 1;
for (int i = 1; i <= (num / 2); i++)
{
for (int j = 1; j <= num; j++)
{
if (j >= f && j <= g)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
f = f + 1;
g = g - 1;
System.out.println();
}
}
}
@java_codings
**
****
******
********
**********
class StarPattern4
{
public static void main(String arg[])
{
int num = 12;
int f = 2;
int g = num - 1;
for (int i = 1; i <= (num / 2); i++)
{
for (int j = 1; j <= num; j++)
{
if (j >= f && j <= g)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
f = f + 1;
g = g - 1;
System.out.println();
}
}
}
@java_codings
40. Star Pattern 5.
class StarPattern5
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
class StarPattern5
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
@java_codings
The account of the user that created this channel has been inactive for the last 5 months. If it remains inactive in the next 30 days, that account will self-destruct and this channel will no longer have a creator.
41. Triangle Pattern.
import java.io.*;
class Triangle
{
public static void main(String arg[])
{
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(istream);
System.out.println("Enter Triangle Size : ");
int num = 0;
try
{
num = Integer.parseInt(read.readLine());
}
catch (Exception Number)
{
System.out.println("Invalid Number!");
}
for (int i = 1; i <= num; i++)
{
for (int j = 1; j < num - (i - 1); j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
for (int k1 = 1; k1 < k; k1 += k)
{
System.out.print("*");
}
}
System.out.println();
}
}
}
@java_codings
import java.io.*;
class Triangle
{
public static void main(String arg[])
{
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(istream);
System.out.println("Enter Triangle Size : ");
int num = 0;
try
{
num = Integer.parseInt(read.readLine());
}
catch (Exception Number)
{
System.out.println("Invalid Number!");
}
for (int i = 1; i <= num; i++)
{
for (int j = 1; j < num - (i - 1); j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
for (int k1 = 1; k1 < k; k1 += k)
{
System.out.print("*");
}
}
System.out.println();
}
}
}
@java_codings
The account of the user that created this channel has been inactive for the last 5 months. If it remains inactive in the next 19 days, that account will self-destruct and this channel will no longer have a creator.
The account of the user that created this channel has been inactive for the last 5 months. If it remains inactive in the next 10 days, that account will self-destruct and this channel will no longer have a creator.
42. Add two Matrix.
import java.util.*;
class AddTwoMatrix
{
int m, n;
int first[][] = new int[m][n];
int second[][] = new int[m][n];
AddTwoMatrix(int[][] first, int[][] second, int m, int n)
{
this.first = first;
this.second = second;
this.m = m;
this.n = n;
}
public static void main(String[] args)
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
first[c][d] = in.nextInt();
}
}
System.out.println("Enter the elements of second matrix");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
second[c][d] = in.nextInt();
}
}
System.out.println("\nElements of First matrix is : ");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(first[c][d] + "\t");
}
System.out.println();
}
System.out.println("\nElements of Second matrix is : ");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(second[c][d] + "\t");
}
System.out.println();
}
AddTwoMatrix a = new AddTwoMatrix(first, second, m, n);
//call by reference
a.addmatrix(a); //Passing Object
}
//Function for Adding two matrix and storing in third matrix
public void addmatrix(AddTwoMatrix a)
{
int c, d;
int sum[][] = new int[a.m][a.n];
for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
sum[c][d] = a.first[c][d] + a.second[c][d];
}
}
System.out.println("\nSum of the two matrices is : ");
for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
System.out.print(sum[c][d] + "\t");
}
System.out.println();
}
}
}
@java_codings
import java.util.*;
class AddTwoMatrix
{
int m, n;
int first[][] = new int[m][n];
int second[][] = new int[m][n];
AddTwoMatrix(int[][] first, int[][] second, int m, int n)
{
this.first = first;
this.second = second;
this.m = m;
this.n = n;
}
public static void main(String[] args)
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
first[c][d] = in.nextInt();
}
}
System.out.println("Enter the elements of second matrix");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
second[c][d] = in.nextInt();
}
}
System.out.println("\nElements of First matrix is : ");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(first[c][d] + "\t");
}
System.out.println();
}
System.out.println("\nElements of Second matrix is : ");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(second[c][d] + "\t");
}
System.out.println();
}
AddTwoMatrix a = new AddTwoMatrix(first, second, m, n);
//call by reference
a.addmatrix(a); //Passing Object
}
//Function for Adding two matrix and storing in third matrix
public void addmatrix(AddTwoMatrix a)
{
int c, d;
int sum[][] = new int[a.m][a.n];
for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
sum[c][d] = a.first[c][d] + a.second[c][d];
}
}
System.out.println("\nSum of the two matrices is : ");
for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
System.out.print(sum[c][d] + "\t");
}
System.out.println();
}
}
}
@java_codings
43. Array of Arrays.
class ArrayOfArraysAnimalDemo
{
public static void main(String[] args)
{
String[][] animals = {{"DanaDog", "WallyDog", "JessieDog", "AlexisDog", "LuckyDog"}, {"BibsCat", "DoodleCat", "MillieCat", "SimonCat"}, {"ElyFish", "CloieFish", "GoldieFish", "OscarFish", "ZippyFish", "TedFish"}, {"RascalMule", "GeorgeMule", "GracieMule", "MontyMule", "BuckMule", "RosieMule"}};
for (int i = 0; i < animals.length; i++)
{
System.out.print(animals[i][0] + ": ");
for (int j = 1; j < animals[i].length; j++)
{
System.out.print(animals[i][j] + " ");
}
System.out.println();
}
}
}
@java_codings
class ArrayOfArraysAnimalDemo
{
public static void main(String[] args)
{
String[][] animals = {{"DanaDog", "WallyDog", "JessieDog", "AlexisDog", "LuckyDog"}, {"BibsCat", "DoodleCat", "MillieCat", "SimonCat"}, {"ElyFish", "CloieFish", "GoldieFish", "OscarFish", "ZippyFish", "TedFish"}, {"RascalMule", "GeorgeMule", "GracieMule", "MontyMule", "BuckMule", "RosieMule"}};
for (int i = 0; i < animals.length; i++)
{
System.out.print(animals[i][0] + ": ");
for (int j = 1; j < animals[i].length; j++)
{
System.out.print(animals[i][j] + " ");
}
System.out.println();
}
}
}
@java_codings
44. Array operations.
class ArrayOperations
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
max = myList[i];
}
System.out.println("Max is " + max);
}
}
@java_codings
class ArrayOperations
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
max = myList[i];
}
System.out.println("Max is " + max);
}
}
@java_codings
45. Array Sort.
import java.util.Arrays;
class ArraySort
{
// This program is the example of array sorting
public static void main(String[] args)
{
// TODO Auto-generated method stub
// initializing unsorted array
String iArr[] = {"Programming", "Hub", "Alice", "Wonder", "Land"};
// sorting array
Arrays.sort(iArr);
// let us print all the elements available in list
System.out.println("The sorted array is:");
for (int i = 0; i < iArr.length; i++)
{
System.out.println(iArr[i]);
}
}
}
@java_codings
import java.util.Arrays;
class ArraySort
{
// This program is the example of array sorting
public static void main(String[] args)
{
// TODO Auto-generated method stub
// initializing unsorted array
String iArr[] = {"Programming", "Hub", "Alice", "Wonder", "Land"};
// sorting array
Arrays.sort(iArr);
// let us print all the elements available in list
System.out.println("The sorted array is:");
for (int i = 0; i < iArr.length; i++)
{
System.out.println(iArr[i]);
}
}
}
@java_codings
46. Array Sum and Average.
import java.io.*;
class ArrayAverage
{
public static void main(String[] args)
{
//define an array
int[] numbers = new int[]{10, 20, 15, 25, 16, 60, 100};
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
sum = sum + numbers[i];
}
double average = sum / numbers.length;
System.out.println("Sum of array elements is : " + sum);
System.out.println("Average value of array elements is : " + average);
}
}
@java_codings
import java.io.*;
class ArrayAverage
{
public static void main(String[] args)
{
//define an array
int[] numbers = new int[]{10, 20, 15, 25, 16, 60, 100};
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
sum = sum + numbers[i];
}
double average = sum / numbers.length;
System.out.println("Sum of array elements is : " + sum);
System.out.println("Average value of array elements is : " + average);
}
}
@java_codings
47. Basic Array.
import java.util.*;
class ArrayBasic
{
public static void main(String[] args)
{
int[] arr;
int size, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
size = sc.nextInt();
arr = new int[size];
System.out.println("\nEnter array elements");
for (i = 0; i < size; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("\nElements in the Array are : ");
for (i = 0; i < size; i++)
{
System.out.print(arr[i] + " ");
}
}
}
@java_codings
import java.util.*;
class ArrayBasic
{
public static void main(String[] args)
{
int[] arr;
int size, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
size = sc.nextInt();
arr = new int[size];
System.out.println("\nEnter array elements");
for (i = 0; i < size; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("\nElements in the Array are : ");
for (i = 0; i < size; i++)
{
System.out.print(arr[i] + " ");
}
}
}
@java_codings
48. Create & Display Matrix.
import java.util.Scanner;
class Matrix_Create {
Scanner scan;
int matrix[][];
int row, column;
void create() {
scan = new Scanner(System.in);
System.out.println("Matrix Creation");
System.out.println("\nEnter number of rows :");
row = Integer.parseInt(scan.nextLine());
System.out.println("Enter number of columns :");
column = Integer.parseInt(scan.nextLine());
matrix = new int[row][column];
System.out.println("Enter the data :");
for(int i=0; i<row ; i++) {
for(int j=0; j
<column ; j++) {
matrix[i][j] =scan.nextInt();
}
}
}
void display() {
System.out.println("\nThe Matrix is :");
for(int i=0; i <row ; i++) {
for(int j=0; j <column ; j++) {
System.out.print("\t" + matrix[i][j]);
}
System.out.println();
}
}
}
class CreateAndDisplayMatrix {
public static void main(String
args[]) {
Matrix_Create obj=new Matrix_Create();
obj.create();
obj.display();
}
}
@java_codings
import java.util.Scanner;
class Matrix_Create {
Scanner scan;
int matrix[][];
int row, column;
void create() {
scan = new Scanner(System.in);
System.out.println("Matrix Creation");
System.out.println("\nEnter number of rows :");
row = Integer.parseInt(scan.nextLine());
System.out.println("Enter number of columns :");
column = Integer.parseInt(scan.nextLine());
matrix = new int[row][column];
System.out.println("Enter the data :");
for(int i=0; i<row ; i++) {
for(int j=0; j
<column ; j++) {
matrix[i][j] =scan.nextInt();
}
}
}
void display() {
System.out.println("\nThe Matrix is :");
for(int i=0; i <row ; i++) {
for(int j=0; j <column ; j++) {
System.out.print("\t" + matrix[i][j]);
}
System.out.println();
}
}
}
class CreateAndDisplayMatrix {
public static void main(String
args[]) {
Matrix_Create obj=new Matrix_Create();
obj.create();
obj.display();
}
}
@java_codings
49. Display Array using for-each loop.
class DisplayArrayForEach
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element : myList)
{
System.out.println(element);
}
}
}
@java_codings
class DisplayArrayForEach
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element : myList)
{
System.out.println(element);
}
}
}
@java_codings
50. Double Matrix.
import java.util.*;
import java.text.DecimalFormat;
class DoubleMatrix
{
public static void main(String[] args)
{
double[][] a;
double[] s;
int row, col, k = 0, i, j;
double sum = 0.0;
DecimalFormat f = new DecimalFormat("##.####");
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column");
row = sc.nextInt();
col = sc.nextInt();
a = new double[row][col];
s = new double[col];
System.out.println("Enter elements of matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
a[i][j] = sc.nextDouble();
}
}
System.out.println("Double Matrix is : ");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println();
}
//sum of the elements of double matrix
for (i = 0; i < col; i++)
{
for (j = 0; j < row; j++)
{
sum = sum + a[j][i];
}
s[k] = sum;
k++;
sum = 0;
}
for (i = 0; i < col; i++)
{
System.out.println("Sum of Column " + (i + 1) + " is : " + f.format(s[i]));
}
}
}
@java_codings
import java.util.*;
import java.text.DecimalFormat;
class DoubleMatrix
{
public static void main(String[] args)
{
double[][] a;
double[] s;
int row, col, k = 0, i, j;
double sum = 0.0;
DecimalFormat f = new DecimalFormat("##.####");
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column");
row = sc.nextInt();
col = sc.nextInt();
a = new double[row][col];
s = new double[col];
System.out.println("Enter elements of matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
a[i][j] = sc.nextDouble();
}
}
System.out.println("Double Matrix is : ");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println();
}
//sum of the elements of double matrix
for (i = 0; i < col; i++)
{
for (j = 0; j < row; j++)
{
sum = sum + a[j][i];
}
s[k] = sum;
k++;
sum = 0;
}
for (i = 0; i < col; i++)
{
System.out.println("Sum of Column " + (i + 1) + " is : " + f.format(s[i]));
}
}
}
@java_codings
51. Example to Pass Arrays to function.
import java.util.*;
class PassingArraystoFunction
{
public static void main(String[] args)
{
int[] a;
int size;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
size = sc.nextInt();
a = new int[size];
System.out.println("Enter elements in the array");
for(int i = 0 ;i < size; i++)
{
a[i] = sc.nextInt();
}
System.out.println("The Elements of the array are : ");
for(int i = 0 ;i < size; i++)
{
System.out.print(a[i] + " ");
}
//Passing array to the function
addElements(a, size);
}
public static void addElements(int[] a , int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
{
sum += a[i];
}
System.out.println("\nSum of the elements of arrays is : "+sum);
}
}
@java_codings
import java.util.*;
class PassingArraystoFunction
{
public static void main(String[] args)
{
int[] a;
int size;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
size = sc.nextInt();
a = new int[size];
System.out.println("Enter elements in the array");
for(int i = 0 ;i < size; i++)
{
a[i] = sc.nextInt();
}
System.out.println("The Elements of the array are : ");
for(int i = 0 ;i < size; i++)
{
System.out.print(a[i] + " ");
}
//Passing array to the function
addElements(a, size);
}
public static void addElements(int[] a , int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
{
sum += a[i];
}
System.out.println("\nSum of the elements of arrays is : "+sum);
}
}
@java_codings