/*
*
Enter an even number : 4
* * * *
* *
* *
* * * *
*/
/*
* No negative inputs please
*/
/**
* A quote: there are more than one ways to do it
*/
package javaapplication2;
import java.util.Scanner;
/**
*
* @author Umang
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// loop variables
int j = 0, k = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
System.out.print("\n");
for (j = 0; j < n; j++)
{
for (k = 0; k < n; k++)
{
if (k == 0 || k == n - 1 || j == 0 || j == n - 1)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
Sunday, March 13, 2011
Pattern Program
Pattern Program
/**
* user inputs a number greater than 2
*/
/**
run:
Enter an even number : 4
*
* *
* *
* *
* *
* *
*
*/
/*
* No negative inputs please
*/
/**
* A quote: there are more than one ways to do it
*/
package javaapplication2;
import java.util.Scanner;
/**
*
* @author Umang
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
boolean b = false;
// to print inner stars
int n1 = 0;
int n2 = 0;
// loop variables
int j = 0, k = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter an even number : ");
int n = s.nextInt();
n1 = n2 = n / 2 + 1;
System.out.print("\n");
for (j = 0; j <= n +2 ; j++)
{
for (k = 0; k < n * 2; k++)
{
if (k == n1 || k == n2)
{
System.out.print( " * " );
}
else
{
System.out.print(" ");
}
}
if(j-1==n/2)
{
b = true;
}
if (b == false)
{
n1--;
n2++;
}
else
{
n1++;
n2--;
}
System.out.print("\n");
}
}
}
Pattern Program
/********************************************************************
*i wrote this program when one great person was sitting with me :-)*
********************************************************************/
/*
Output:
Enter an Odd number : 5
* * * * *
* * *
* * * *
* * *
* * * * *
*/
// you can also do the same program using 3 loops
/*
* No negative inputs please
*/
/**
* A quote: there are more than one ways to do it
*/
package javaapplication2;
import java.util.Scanner;
/**
*
* @author Umang
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
boolean b = false;
// to print inner stars
int n1 = 0;
int n2 = 0;
// n1 and n2 indicate in which column the inner stars should be
// placed.
// in each iteration (of outer loop), the value of
// these two variables are changed
// loop variables
int j = 0, k = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter an Odd number : ");
int n = s.nextInt();
n1 = n2 = n / 2;
System.out.print("\n");
for (j = 0; j < n; j++)
{
for (k = 0; k < n; k++)
{
// if it is first or last line then print only *
// no spaces in first and last line
if (j == 0 || j == n - 1)
{
System.out.print(" * ");
}
else
{
// we come here if it is not first or last line
// n1 and n2 are the variables that hold informatin regarding where
// we want to print inner *s
// print * if it is the first or last column
// or the column value = n1 or n2
if (k == n1 || k == n2 || k == 0 || k == n - 1)
{
System.out.print(" * ");
}
else
{
// print blank space
System.out.print(" ");
}
}
}
// prnit new line
System.out.println();
// now change the place of inner *s
// this if changes the increment decrement pattern
if (j == (n / 2))
{
// if j == n / 2 then we need to change the pattern
// in which the numbers are incrementd
// b is a flag variable
b = true;
}
if (j != 0 && j != n - 1)
{
if (b == false)
{
// we come here if we are in first upper part of pattern
n1--;
n2++;
}
else
{
// we come here if we are in lower part of pattern
n1++;
n2--;
}
}
}
System.out.print("\n");
}
}
Subscribe to:
Posts (Atom)