WRITE A JAVA PROGRAM TO PRINT THE FOLLOWING PATTERN!
Answers
Code:-
import java.util.*;
class Pattern
{
static void main (int n)
{
int k[]=new int[n];
Scanner sc=new Scanner (System.in);
System.out.println("Enter the numbers");
for(int a=0;a<n;a++)
{
k[a]=sc.nextInt();
}
for(int b=n;b>0;b--)
{
for(int j=0;j<b;j++)
{
System.out.print(k[j] + "\t");
if(j!=(b-1))
{
k[j]+=k[j+1];
}
}
System.out.println();
}
}
}
___________________________
Input :-
1
3
2
4
5
Output :-
1 3 2 4 5
4 5 6 9
9 11 15
20 26
46
(Proof in the Attachment)
import java.util.Scanner;
public class Patterns {
static void printAdjacentSum(int[ ] array) {
int length = array.length - 1;
if (length = = 0) return;
int[ ] newArray = new int[length];
for (int i = 0; i < length; i++) {
newArray[i] = array[i] + array[i + 1];
System.out.print(newArray[i] + " ");
}
System.out.println();
printAdjacentSum(newArray);
}
public static void main(String[ ] args) {
System.out.print("Enter n - ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt( );
System.out.println("Enter numbers - ");
int[ ] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = sc.nextInt( );
printAdjacentSum(array);
}
}