How to make diamond pattern using java? - java

How to make this pattern
if input N = 5
Output :
Mine is like this, it become 2N
if input N = 5
Output
Here's my code
int i,j;
for(i = 0; i <= n; i++)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
for(i = n - 1; i >= 1; i--)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
What should i fix??

You can check odd numbers in your loop. Please see the following example:
public static void main(String[] args) {
printPattern(5);
}
private static void printPattern(int n) {
int i, j;
for (i = 0; i <= n; i++) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
for (i = n - 1; i >= 1; i--) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j <i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Instead of running these two loops from 0 to N twice. Just run half N/2 in each loop.
Example:
public static void main(String[] args) {
int n = 10;
for (int i = 0; i <= (n / 2 + 1); i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
// N/2
for (int i = n / 2 - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
}

Related

For loop for increasing pyramid pattern

I am trying to figure out how to increment a pyramid based on input from the user.\
If a user enters the number 3, my program will print a pyramid of height 3, three times.\
What I would like it to do instead, is to print 3 pyramids, but the first pyramid should have a height of 1, and the second a height of 2, and the third a height of 3.
Here is my code:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int l = 0; l < n; l++) {
System.out.println("Pyramid " + n);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < n - i; k++) {
System.out.print(".");
}
System.out.println();
}
}
You use l to keep track of the number of pyramid you're working on, so you could just use it in the loop instead of n. Note that l starts with 0, not 1, so you may want to amend the loop accordingly and run from 1 to n, not from 0 to n-1
for (int l = 1; l <= n; l++) { // Note the loop starts at 1
System.out.println("Pyramid " + l);
for (int i = 1; i <= l; i++) { // Note the usage of l instead on n
for (int j = 0; j < l - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < l - i; k++) { // Note the usage of l instead on n
System.out.print(".");
}
System.out.println();
}
}
All you have to change is this part of code
System.out.println("Pyramid " + (l+1));
for (int i = 1; i <= l+1; i++) {

how to print the below pattern

for the given input I need to print the pattern. For example for input = 6 I have to print:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I have tried but couldn't go further than this could anyone help
public class tgk {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int half = ((2*size)+1)/2;
for (int i = 0; i < size ; i++)
{
for (int j = size; j > i; j--)
{
System.out.print("M");
}
for (int k = half+1 ; k > half - i; k--)
{
System.out.print("S");
}
System.out.println();
}
for(int i = size; i > 0; i--)
{
for (int j = size; j >= i; j--) {
System.out.print("C");
}
for (int k = 0; k < (i * 2 - 1); k++) {
System.out.print("S");
}
System.out.println();
}
}
}
if input = 3 it should be
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
You can use two sets of for loops to print each half of the pattern. Assuming input variable holds the size of the problem
int input = 3;
for (int i = 0; i < input; i++) {
for (int j = 0; j < input - i; j++) {
System.out.print('M');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('D');
}
System.out.println();
}
for (int i = input - 1; i >= 0; i--) {
for (int j = 0; j < input - i; j++) {
System.out.print('C');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('K');
}
System.out.println();
}
will print for input = 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
and for input = 6:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I don't know why, but I really wanted it to work with only one set of for-loops:
int number = 8;
for (int i = 0; i < number * 2; i++) {
for (int j = 0; j < (number * 2) + 1; j++) {
System.out.print(
i < number && j+i < number ? 'M' :
i < number && j-i > number ? 'D' :
i < number ? 'S' :
i >= number && i-j >= number ? 'C' :
i >= number && j+i >= number*3 ? 'K' :
'S'
);
}
System.out.println();
}
So for 8 (like in the code) it prints:
MMMMMMMMSDDDDDDDD
MMMMMMMSSSDDDDDDD
MMMMMMSSSSSDDDDDD
MMMMMSSSSSSSDDDDD
MMMMSSSSSSSSSDDDD
MMMSSSSSSSSSSSDDD
MMSSSSSSSSSSSSSDD
MSSSSSSSSSSSSSSSD
CSSSSSSSSSSSSSSSK
CCSSSSSSSSSSSSSKK
CCCSSSSSSSSSSSKKK
CCCCSSSSSSSSSKKKK
CCCCCSSSSSSSKKKKK
CCCCCCSSSSSKKKKKK
CCCCCCCSSSKKKKKKK
CCCCCCCCSKKKKKKKK
...or for 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK

How to create a square with numbers?

I'm trying to create a square shape with numbers like this:
1234
2341
3412
4123
How would I go about doing that?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
System.out.println();
}
This prints:
1234
234
34
4
How do I get it to restart at 1 again?
You can do by using modulo
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print((j + i) % 4 + 1);
}
System.out.println();
}
output
1234
2341
3412
4123
You need to update to
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j < i; j++) {
System.out.print(j);
}
System.out.println();
}
add one more for loop
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j <i; j++) {
System.out.print(j);
}
System.out.println();
}
There's probably a simpler way, but this should work:
for (int i = 1; i <= 4; i++) {
for (int j = 3; j <= 6; j++) {
System.out.print ((i + j) % 4 + 1);
}
System.out.println();
}
try this:
public class MyClass {
public static void main(String args[]) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
count += 1;
}
if(count < 4){
int remain = 4 - count; // to identify how many times it goes again
for(int k=0;k<remain;k++){
System.out.print(k+1);
}
}
count = 0;
System.out.println();
}
}
}
Output:
1234
2341
3412
4123
How about this?
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print(((i + j + 2) % 4) + 1);
}
System.out.println();
}
If you look at the expected output, it has a pattern, which is restricted to a finite and known sequence of numbers. Modulus is the ideal option to handle cases like this.
for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 3; j++) {
System.out.print(((i + j) % 4) + 1);
}
System.out.println();
}
Modulus (%) gives the remainder of one number divided by another, so (i+j) % 4 always gives a result between 0 and 3.
Add one to that to get the value you want (between 1 and 4).

Pyramid of numbers using (for) Java

a pyramid of numbers thats core is increasing by one which would be seen horizontally as 12345.
I figured out this code
but its not as the img I shown up there ^
the code I wrote is:
int rowCount = 1;
for (int i = 6; i > 0; i--)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
rowCount++;
}
}
`

How to execute a print x amount of times?

What I have:
public static void nestedForLoops() {
int k = 5;
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print("*");
}
System.out.println(i);
}
}
Output:
****1
***2
**3
*4
5
Trying to achieve:
****1
***22
**333
*4444
55555
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (5 - j >= i) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
Here you are:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i <= 5 - j) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
for i in range(1,6,1):
print "*" *(5-i),str(i) *i

Categories

Resources