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
Related
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");
}
}
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).
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++;
}
}
`
I want to make the handling of which if the data I input is not found. However, when the data is not found, the program will print a message else as much repetition. Where should I put the else statement correctly?
public void Data(String x){
for (int i = 1; i < 6; i++) {
if (x.equalsIgnoreCase(table1[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table1[i][j]);
}
}
else if (x.equalsIgnoreCase(table2[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table2[i][j]);
}
}
else if (x.equalsIgnoreCase(table3[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table3[i][j]);
}
}
else if (x.equalsIgnoreCase(table4[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table4[i][j]);
}
}
else if (x.equalsIgnoreCase(table5[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table5[i][j]);
}
}
else if (x.equalsIgnoreCase(table6[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table6[i][j]);
}
}
else if (x.equalsIgnoreCase(table7[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table7[i][j]);
}
}
else System.out.println("Data not found!");
}
}
Try this:
public void Data(String x){
boolean b = false;
for (int i = 1; i < 6; i++) {
if (x.equalsIgnoreCase(table1[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table1[i][j]);
}
}
else if (x.equalsIgnoreCase(table2[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table2[i][j]);
}
}
else if (x.equalsIgnoreCase(table3[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table3[i][j]);
}
}
else if (x.equalsIgnoreCase(table4[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table4[i][j]);
}
}
else if (x.equalsIgnoreCase(table5[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table5[i][j]);
}
}
else if (x.equalsIgnoreCase(table6[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table6[i][j]);
}
}
else if (x.equalsIgnoreCase(table7[i][1])){
for (int j = 0; j < 4; j++) {
System.out.printf("|%-40s", table7[i][j]);
}
}
else b = true;
}
if (b) System.out.println("Data not found!");
}
If you want to terminate as soon as data is not found, you can change your else statement to this:
else {
System.out.println("Data not found!");
return;
}
I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}