Mostly asked Programming Interview Questions:
Write a Program to display below Pattern?
**********
** **
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
** **
**********
=======================
package com.nataraja.b;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Pattern2 {
public static void main(String[] args) throws IOException {
System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
try{
n=Integer.parseInt(br.readLine());
int t=n;
int k=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==1||j==n||i==1||i==n||t==j||j==k){
System.out.print("*");
}else{
System.out.print(" ");
}
}
t--;
k++;
System.out.println(" ");
}
}catch(Exception e){
//e.printStackTrace();
}
}
}
Write a Program to display below Pattern?
**********
** **
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
** **
**********
=======================
package com.nataraja.b;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Pattern2 {
public static void main(String[] args) throws IOException {
System.out.println("Enter a Number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
try{
n=Integer.parseInt(br.readLine());
int t=n;
int k=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==1||j==n||i==1||i==n||t==j||j==k){
System.out.print("*");
}else{
System.out.print(" ");
}
}
t--;
k++;
System.out.println(" ");
}
}catch(Exception e){
//e.printStackTrace();
}
}
}
No comments:
Post a Comment