Google+

Saturday, 1 November 2014

Algorithm: Solutions to some Star and other pattern problems: Post 4

So lets see the solutions of the pattern problems, once that were asked in previous post and some other too.

Lets start with previously asked problem
As I am more into java I am writing java solution but you can easily implement then in C and C++ too

1.Draw pattern
*****
****
***
**
*


Sol>

public class OpPyramid {
public static void main(String[] args) {

int no = 5;
for(int i=no;i>0;i--)
{
for(int j=0;j<no-i;j++)
System.out.print(" ");

for(int k=i;k>0;k--)
System.out.print("* ");

System.out.println("");
}
}
}
_________________________________________________________________________________

2. Write
11111
2222
333
44
5

Sol>
public class patnum {

public static void main(String args[])
{
int no=5;
int k=1;
for(int i=0;i<5;i++)
{
for(int j=no;j>i;j--)
{
System.out.print(k);
}
k++;
System.out.println("");
}
}

}
_________________________________________________________________________________

3. Draw pyramid
*
***
*****
*******
*********
Sol>

public class pyramid {
public static void main(String[] args) {
int no =5;
int i=1,j=1;
for(i=1;i<=no;i++)
{
int k=i;
for(;no-k>0;k++)
System.out.print(" ");
for(j=1;j<=(2*i-1);j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
_________________________________________________________________________________

4. Draw Kite
   *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *


class kite
{


      public static void main(String[] args)
      {
            int n = 5;
            for (int i = 1; i <= n; i++)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }

            for (int i = n - 1; i >= 1; i--)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }

            System.out.println();
      }
}
_________________________________________________________________________________
5.Histogram


import java.util.Scanner;

class HistogramPattern
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("How many numbers do you want to enter: ");
int size = sc.nextInt();

int array[] = new int[size];

for (int i = 0; i < array.length; i++)
{
System.out.print("Enter " + (i + 1) + " element: ");
array[i] = sc.nextInt();
}

System.out.println("\n\t***Horizontal pattern***\n");

for (int i = 0; i < array.length; i++)
{
for (int j = 1; j <= array[i]; j++)
{
System.out.print("* ");
}

System.out.println();
}

System.out.println("\n\t***Vertical***\n");

int max = 0;

for (int i = 0; i < array.length; i++)
{
if (max < array[i])
{
max = array[i];
}
}

for (int i = max; i > 0; i--)
{
for (int j = 0; j < size; j++)
{
if (array[j] >= i)
{
System.out.print("* ");
}
else
{
System.out.print("  ");
}
}

System.out.println();
}
}
}
_________________________________________________________________________________
6.draw
1 2 3 4 5 4 3 2 1 
1 2 3 4    4 3 2 1 
1 2 3          3 2 1 
1 2                2 1 
1                      1 


sol>
package marlo;
import java.util.Scanner;

class OutPyramid
{
public void draw(int rows)
{
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= rows; j++)
{
if (j <= i)
{
System.out.print(j + " ");
}
else
{
System.out.print("  ");
}
}

for (int j = rows; j >= 1; j--)
{
if (j == rows)
{
continue;
}

if (j <= i)
{
System.out.print(j + " ");
}
else
{
System.out.print("  ");
}
}

System.out.println();
}
}
}

class OutPyramidMain
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
OutPyramid op = new OutPyramid();

System.out.print("Enter number of rows: ");
int rows = sc.nextInt();

op.draw(rows);
}
}

No comments:

Post a Comment