PATTERN PROGRAM 2 : ENTER NUMBER OF TERMS FROM 1 TO 5 (NOT MORE)AND YOU WILL GET A PATTERN OF ALPHABET 'a'.
Example: you have entered the number '4' ; then output is:
PROGRAM:
import java.util.Scanner;
public class assign11
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms to be printed in the series(FROM 1 TO 5):");
int n = sc.nextInt();
if(n<1||n>5)
System.out.println("Wrong input.");
else{
int h = 0;
int d = n-1;
for(int i = 1 ; i<=n ; i++){
for(int s = 1 ; s<= d ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int t = 1 ; t<= h ; t++){
if(t%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
h = h + 2;
d = d - 1;
System.out.println();
}
if(n%2==0){
int c = n+2;
for(int k = 1 ; k<= n ; k++){
for(int s = 1 ; s<=k-1 ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int j = 1 ; j<=c ; j++){
if(j%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
c = c - 2;
System.out.println();
}
}
else{
int c = n +3;
for(int k = 1 ; k<= n ; k++){
for(int s = 1 ; s<=k-1 ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int j = 1 ; j<=c ; j++){
if(j%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
c = c - 2;
System.out.println();
}
}
}
}
}
public class assign11
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms to be printed in the series(FROM 1 TO 5):");
int n = sc.nextInt();
if(n<1||n>5)
System.out.println("Wrong input.");
else{
int h = 0;
int d = n-1;
for(int i = 1 ; i<=n ; i++){
for(int s = 1 ; s<= d ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int t = 1 ; t<= h ; t++){
if(t%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
h = h + 2;
d = d - 1;
System.out.println();
}
if(n%2==0){
int c = n+2;
for(int k = 1 ; k<= n ; k++){
for(int s = 1 ; s<=k-1 ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int j = 1 ; j<=c ; j++){
if(j%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
c = c - 2;
System.out.println();
}
}
else{
int c = n +3;
for(int k = 1 ; k<= n ; k++){
for(int s = 1 ; s<=k-1 ; s++){
System.out.print(" ");
}
System.out.print("a");
for(int j = 1 ; j<=c ; j++){
if(j%2==0){
System.out.print("a");
}
else{
System.out.print(" ");
}
}
c = c - 2;
System.out.println();
}
}
}
}
}
No comments:
Post a Comment