25. Floyd Triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows of floyd's triangle : ");
n = in.nextInt();
System.out.println("Floyd's triangle :-");
for (c = 1; c <= n; c++)
{
for (d = 1; d <= c; d++)
{
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
@java_codings
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows of floyd's triangle : ");
n = in.nextInt();
System.out.println("Floyd's triangle :-");
for (c = 1; c <= n; c++)
{
for (d = 1; d <= c; d++)
{
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
@java_codings
26. Number pattern 1.
1
12
123
1234
12345
class NumberPat1
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
1
12
123
1234
12345
class NumberPat1
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
27. Number pattern 2.
54321
5432
543
54
5
class NumberPat2
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
54321
5432
543
54
5
class NumberPat2
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
28. Number pattern 3.
12345
1234
123
12
1
class NumberPat3
{
public static void main(String arg[])
{
for (int i = 1, r = 5; i <= 5; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
12345
1234
123
12
1
class NumberPat3
{
public static void main(String arg[])
{
for (int i = 1, r = 5; i <= 5; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
@java_codings
29. Number pattern 4.
1
12
123
1234
12345
1234
123
12
1
class NumberPat4
{
public static void main(String arg[])
{
int ck = 0, c = 2;
while (c > 0)
{
if (ck == 0)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for (int i = 1, r = 5 - 1; i <= 5 - 1; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
@java_codings
1
12
123
1234
12345
1234
123
12
1
class NumberPat4
{
public static void main(String arg[])
{
int ck = 0, c = 2;
while (c > 0)
{
if (ck == 0)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for (int i = 1, r = 5 - 1; i <= 5 - 1; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
@java_codings
30. Number pattern 5.
12345
1234
123
12
1
12
123
1234
12345
class NumberPat5
{
public static void main(String arg[])
{
int ck = 0, c = 2;
while (c > 0)
{
if (ck == 0)
{
for (int i = 1, r = 5; i <= 5; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for (int i = 2; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
@java_codings
12345
1234
123
12
1
12
123
1234
12345
class NumberPat5
{
public static void main(String arg[])
{
int ck = 0, c = 2;
while (c > 0)
{
if (ck == 0)
{
for (int i = 1, r = 5; i <= 5; i++, r--)
{
for (int j = 1; j <= r; j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for (int i = 2; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
@java_codings
31. Number pattern 6.
1
22
333
4444
55555
class NumberPat6
{
public static void main(String arg[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
@java_codings
1
22
333
4444
55555
class NumberPat6
{
public static void main(String arg[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
@java_codings
32. Number pattern 7.
1
23
456
7890
12345
class NumberPat7
{
public static void main(String arg[])
{
int t = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
if (t == 10)
t = 0;
System.out.print(t++);
}
System.out.println();
}
}
}
@java_codings
1
23
456
7890
12345
class NumberPat7
{
public static void main(String arg[])
{
int t = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
if (t == 10)
t = 0;
System.out.print(t++);
}
System.out.println();
}
}
}
@java_codings
33. Number pattern 8.
5
11111
0000
111
00
1
import java.util.Scanner;
class Pattern
{
public static void main(String args[])
{
int n, i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows ");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
for (j = i; j <= n; j++)
{
if (i % 2 == 0)
System.out.print("0");
else
System.out.print("1");
}
System.out.println();
}
}
}
@java_codings
5
11111
0000
111
00
1
import java.util.Scanner;
class Pattern
{
public static void main(String args[])
{
int n, i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows ");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
for (j = i; j <= n; j++)
{
if (i % 2 == 0)
System.out.print("0");
else
System.out.print("1");
}
System.out.println();
}
}
}
@java_codings
34. Number pattern 9.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
class PrintPattern
{
public static void main(String args[])
{
int n = 5;
for (int i = 1; i <= n; i++)
{
int j = n - i;
while (j > 0)
{
System.out.print(" ");
j--;
}
j = 1;
while (j <= i)
{
System.out.print(" " + j);
j++;
}
j = i - 1;
while (j > 0)
{
System.out.print(" " + j);
j--;
}
j = n - i;
while (j > 0)
{
System.out.print(" ");
j--;
}
System.out.println();
}
}
}
@java_codings
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
class PrintPattern
{
public static void main(String args[])
{
int n = 5;
for (int i = 1; i <= n; i++)
{
int j = n - i;
while (j > 0)
{
System.out.print(" ");
j--;
}
j = 1;
while (j <= i)
{
System.out.print(" " + j);
j++;
}
j = i - 1;
while (j > 0)
{
System.out.print(" " + j);
j--;
}
j = n - i;
while (j > 0)
{
System.out.print(" ");
j--;
}
System.out.println();
}
}
}
@java_codings
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