I want to print the below pattern
v
v v
v v
v v
Below is the code which I have tried.
public static void main(String[] args) {
System.out.println("The Pattern is");
for (int i = 0; i < 4; i++) {
//System.out.println(i);
for (int k = 0; k <= i - 2; k++) {
System.out.print("v ");
}
for (int j = 0; j <= 4 - i; j++) {
System.out.print(" ");
}
System.out.println();
}
}
I'm getting the below output:
v
v v
Can anyone help me to solve this pattern?
Another way to do the job (dynamic number of rows):
public static void main(String[] args) {
final int numRows = 4;
for (int row = 0; row < numRows; row++) {
for (int preSpace = numRows - row; preSpace >= 0; preSpace--) {
System.out.print(" ");
}
if (row > 0) {
System.out.print("v");
for (int postSpace = 1; postSpace < row * 2; postSpace++) {
System.out.print(" ");
}
}
System.out.println("v");
}
}
Move the j loop before k loop
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("The Pattern is");
for(int i=0;i<4;i++) {
for(int j=0;j<=4-i;j++)
System.out.print(" ");
for(int k=0;k<=i-2;k++)
System.out.print("V ");
System.out.println();
}
}
DEMO
The following code will print it
public static void main(String[] args) {
System.out.println("The Pattern is");
// startup parameter
final int rowSize = 4;
final int startPos = 2;
int leftPos = startPos;
int rightPos = startPos;
// for each row
for(int row=0; row<rowSize; row++){
// find and print v (max is right most v)
for(int col=0; col<rightPos+1; col++){
// when reach left pos
if(col==leftPos){
System.out.print("v");
// when left pos and right pos is same
if(leftPos==rightPos){
break;
}
}
// when reach right pos
if (col==rightPos){
System.out.print("v");
break;
}
// print space when it is not match
System.out.print(" ");
}
// next line
System.out.println();
// adjust position
if(leftPos!=0) leftPos--;
rightPos++;
}
}
This works for me:
public class DrawPattern {
public static void main(String[] args) {
int i, j;
int num = 7;
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
if (isConditionMatch(num, i, j)) {
System.out.print("V");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
// this pattern will decide where to print 'V'
private static boolean isConditionMatch(int num, int i, int j) {
return (i + j == (num - 1) / 2 || j - i == (num - 1) / 2);
}
}
Output:
V
V V
V V
V V
Related
So I'm trying to create a program that creates a randomly generated array with numbers between 0 and 10.
Every time a number inside the 4x4 array is odd I want it to generate a brand new array and print every array discarded aswell until it creates a 4x4 array with only even numbers.
The problem right now is that I can't understand how to fix the last for and make it work properly with the boolean b that is supposed to restart the creation of the array.
import java.util.Scanner;
public class EvenArrayGenerator {
public static void main(String a[]) {
Boolean b;
do {
b = true;
int[][] Array = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
Array[i][j] = (int) (Math.random() * 11);
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (Array[i][j] % 2 != 0)
b = false;
}
}
} while (b);
}
}
public class ArrayGen {
private int[][] array = new int[4][4];
private int iterations = 1; // you always start with one iteration
public static void main (String[] args) {
ArrayGen ag = new ArrayGen();
ag.reScramble();
while(!ag.isAllEven()) {
ag.reScramble();
ag.iterations++;
}
// this is just a nice visualisation
for (int i = 0; i < 4; i++) {
System.out.print("[");
for (int j = 0; j < 4; j++) {
System.out.print(ag.array[i][j] +((j != 3)? ", " : ""));
}
System.out.print("]\n");
}
System.out.println(ag.iterations + " iterations needed to get all-even array.");
}
private void reScramble () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array[i][j] = (int)(Math.random() * 11);
}
}
}
private boolean isAllEven () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (array[i][j] % 2 == 1) {
return false;
}
}
}
return true;
}
}
I think this is a good solution. Refactoring your code into structured methods is never a bad idea. I hope this helps!
You are looping until you get an array that's all even. You should initialize b to be false, and update it to true in the (nested) for loop. Note that once's you've set it to false, there's no reason checking the other members of the array, and you can break out of the for loop.
Note, also, that using stream could make this check a tad more elegant:
b = Arrays.stream(arr).flatMapToInt(Arrays::stream).anyMatch(x -> x % 2 != 0)
What about generating random numbers up to 5 and double it? Then you don't have two check if they are even.
Instead of your last for loop:
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(Array[i][j] % 2!=0){
b=false;
break;
}
}
if(!b){
break;
}
}
if(!b){
break;
}
Alternatively, you could do an oddity check when you are generating the elements. Something like:
int element;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
do{
element = (int)(Math.random()*11);
}while(element % 2 !=0)
Array[i][j] = element;
}
}
That way you don't have to check the values, they will always be even.
This should work:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
anyOdd |= Array[i][j] % 2!=0;
}
}
} while(anyOdd);
}
}
As you can see, I just modified the condition from b to anyOdd, so if there is any odd number, it will iterate again.
Also, you can check it when you generate the random numbers, so you avoid a second loop:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
anyOdd |= array[i][j] % 2 != 0;
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
} while(anyOdd);
}
}
public class EvenArrayGenerator {
public static void main(String a[]) {
int[][] arr = createAllEvenArray(4);
printArray(arr);
}
private static int[][] createAllEvenArray(int size) {
while (true) {
int[][] arr = createArray(size);
printArray(arr);
if (isAllEven(arr))
return arr;
}
}
private static int[][] createArray(int size) {
int[][] arr = new int[size][size];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
arr[i][j] = (int)(Math.random() * 11);
return arr;
}
private static void printArray(int[][] arr) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j > 0)
System.out.print("\t");
System.out.format("%2d", arr[i][j]);
}
System.out.println();
}
System.out.println();
}
private static boolean isAllEven(int[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
if (arr[i][j] % 2 != 0)
return false;
return true;
}
}
I am trying to make a program that outputs
So far I have done:
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
System.out.println("0");
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
max++;
}
}
}
But the output I am getting is
The "0" is there to show the spaces, but I want to remove the entire line which contains the first "0" so that the output starts with a "1". I am unsure what to change. Any help would be much appreciated. Thank You.
I suggest adding conditions (if we need to print out delimiters):
for (int i = 1; i <= a; ++i) {
if (i > 1)
System.out.println(); // more than 1 line, need delimiter (new line)
for (int j = 1; j <= i; ++j) {
if (j > 1)
System.out.print(" "); // more than 1 column, need delimiter (space)
System.out.print(j);
}
}
Most shortest form:
String str = "";
for (int i = 1; i <= 6; i++) {
str = str + " " + i;
System.out.println(str);
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here what I've got
Here you can check https://code.sololearn.com/c9ALHSGAa6ZZ
class printPattern {
public static void main(String[ ] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for (i = 1; i <= a; i++) {
num = 1;
for (j = 1; j <= max; j++) {
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println();
max++;
}
}
}
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println(" ");
max++;
}
}
}
this is working as you asked. just remove 0 from print statement
How about this ?
public void pyramid(int size) {
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}
I am working on a simple star pattern program in Java. I have the code running but it is not doing what it's supposed to. My code is:
public class q3 {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--){
for (int j = i; j >= 1; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
What I want is something like this below:
$
$$
$$$
$$$$
$$$$$
..........
$$$$$$$$$$
Can someone please help me figure out how I would get the above pattern. Thank You.
Try this :
public static void main(String[] args) {
final int length = 10;
for (int i = 1; i < length; i++) {
//Print spaces first
for (int j = length - 1; j > i; j--) {
System.out.print(" ");
}
//Then print "*"
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Output is for length = 10 :
*
**
***
****
*****
******
*******
********
We can achieve this pattern using a single for loop:
char [] mirrorascval = new char[5];
for (int i = 4; i >= 0; i--) {
mirrorascval[i] ='*';
System.out.println(mirrorascval);
}
class StarTriangle {
public static void main(String[] args) {
int i, j, k;
for (i = 8; i >= 1; i--) {
for (j = 1; j < i; j++) {
System.out.print(" ");
}
for (k = 8; k >= i; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
public void starPrint(){
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
import java.util.*;
public class firstStar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int i=1;
while(i<=n) {
int j=1;
while(j<=i) {
System.out.print("*");
j=j+1;
}
System.out.println();
i=i+1;
}
}
}
package star.pattern;
import java.util.Scanner;
public class StarPattern {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int l;
System.out.print("Enter No Of Line You Want:- ");
l=scn.nextInt();
int x=1;
while(x<=l){
int j=x;
while(j<l){
System.out.print(" ");
j++;
}
int y=1;
while(y<=x){
System.out.print("*");
y++;
}
System.out.println("");
x++;
}
}
}
public class q3 {
public static void main(String[] args) {
for (int i = 1; i <=10; i++){
for (int j = 10; j >= i; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Try this...
int i, j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
System.out.print(" ");
}
k = k - 2;
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code:
for(int i=1;i<=10;i++){
for(int j=10;j>i;j--){
System.out.println(" ");
}
for(int k=1;k<=i;k++){
System.out.print("*");
}
for(int l=10;l<=1;l++){
for(int h=1;h<=10;h++){
System.out.print(" ");
}
}
System.out.println();
}
What I am trying to do is:
*
***
*****
*******
Try this much simpler code:
public class ChristmasTree {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i; j++)
System.out.print(" ");
for (int k = 0; k < (2 * i + 1); k++)
System.out.print("*");
System.out.println();
}
}
}
It uses 3 loops:
first one for the number of rows,
second one for printing the spaces,
third one for printing the asterisks.
You can do it with simple logic
for (int i = 0; i < 4; i++)
System.out.println(" *******".substring(i, 4 + 2*i));
import java.util.Scanner;
public class cmastree{
public static void main (String[]args){
Scanner keyboard=new Scanner (System.in);
int j;
System.out.println ("Enter a number");
j=keyboard.nextInt();
/*take the above part out and change the j variable if you want to set
the size*/
for(int i=1; i<=j; i+=2){
int numSpaces = (j-i)/2;
for (int k=0; k<numSpaces; k++){
System.out.print(" ");
}
for(int k=0; k<numSpaces; k++){
System.out.print("*");
}
System.out.println();
}
}
}
public class ChrismasTree {
public static void main(String[] args) {
int sizeOfTree = 9;
double remainderVal = sizeOfTree % 2 ;
double ans = sizeOfTree / 2 ;
if (remainderVal == 0) {
System.out.println("Invalid number enter 9,19 calculat rest yourself u looser ..");
System.exit(0);
}
int middlePos = (int) Math.round(ans + .5);
for (int i = 0; i <= sizeOfTree; i++) {
int lStar = middlePos - i;
int rStar = middlePos + i;
if (lStar < 1) {
break;
}
printleaves(lStar, rStar, sizeOfTree);
}
}
public static void printleaves(int a,int b, int size){
System.out.println();
for (int i = 1; i <= size; i++) {
if (i > a && i < b ){
System.out.print("*");
}else System.out.print(" ");
}
}
}
public class Stars {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter Row/Column Value::");
int i,j,k,n;
n=s.nextInt();
for(i=1; i<n; i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n+(n/2); i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n-(n/2); i++){
for(j=n+(n/2); j>1; j--){
System.out.print(" ");}
for(k=n/2; k<=(n/2)+1; k++){
System.out.print("*");}
System.out.println("");
}
}
}
public class ChristmasTree {
public static void printStars(int number) {
for (int i = 1; i <= number; i++) {
System.out.print("*");
}
System.out.println("");
}
public static void printSpaces(int number) {
for (int i = 0; i < number; i++) {
System.out.print(" ");
}
}
public static void christmasTree(int height) {
for (int i = 1; i <= height; i++) {
printSpaces(height - i);
printStars(i + (i - 1));
}
}
public static void main(String[] args) {
// int x = pick some number, but not TOO big )))
christmasTree(x);
}
}
def fist(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def second(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def stem(m)
k=11
for i in range(0,5):
for j in range(0,k):
print(end=" ")
for j in range(0,3):
print("*",end=" ")
print()
first(7)
second(7)
steam(3)
Sorry if my question is not clear, lets say I have int a = 1234;.
How can I print as follows, for a number of any length?
123
132
213
231
321
312
Thanks in advance.
public class Test {
private static void swap(int[] p, int i, int j) {
int t= p[i];
p[i]= p[j];
p[j]= t;
return;
}
private static boolean nextPerm(int[] p) { // need p.length > 1
int n= p.length;
int i= n;
if (i-- < 1) return false;
for(;;) {
int ii= i--;
if (p[i] < p[ii]) {
int j= n;
while (!(p[i] < p[--j]));
swap(p, i, j);
for (j= n; j > ii; swap(p, --j, ii++));
return true;
}
if (i == 0) {
for (int j= n; j > i; swap(p, --j, i++));
return false;
}
}
}
public static void main(String[] args) {
int x = 123;
String s = "" + x;
int n = s.length();
int[] p = new int[n];
for (int i = 0; i < n; i++){
p[i] = i;
}
do {
for (int i = 0; i < n; i++){
System.out.print(s.charAt(p[i]));
}
System.out.println();
}
while (nextPerm(p));
}
}
finally I found, below is the code,
class ShuffleNumber {
private static int[] a = {1,2,3};
private static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
System.out.println();
}
private static void shuffle(){
int[] b = (int[])a.clone();
for (int i = b.length - 1; i > 0; i--) {
int j = (int)Math.floor(Math.random() * (i+1));
int temp = b[j];
b[j] = b[i];
b[i] = temp;
}
print(b);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
shuffle();
}
}