I want to print this! (+Blank, and use character) - java

i made ms excel example:
Anyway, I made code.
But this code is print only half. Why, why,!
for (int i = 1 ; i <= 5 ; i++) {
for (int j = 1 ; j <= 9 ; j++) {
if ( j < 10 - i ) {
System.out.print(" ");
} else
System.out.print("*");
} System.out.println();
}
Why printed only half.: (

Your condition is wrong, it should be:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j <= 5 - i || j >= 5 + i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}

Related

how to print reverse number pattern triangle in java

This is my code:
for (int i = 4; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 4; k++) {
System.out.print(k+"");
}
System.out.println();
}
Current output:
4
34
234
1234
Desired output:
1
21
321
4321
What changes are necessary in order for me to get the desired output as shown above?
Let the first loop (i) run from 1 to 4 and the second (j) from 4 to i.
This reverses your output.
You did every thing right, just the last for should have a very minor change:
for (int k = 5-i; k >= 1; k--){
Here you go:
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k + "");
}
System.out.println();
}
}
Your loops are incorrect, you can refer the below code with inline comments:
for (int i = 1; i <= 4; i++) { //iterate from 1 to 4
//Loop from i+1 to insert spaces first
for (int j = i+1; j <= 4; j++) {
System.out.print(" ");
}
//Loop from i to insert the number next to each other
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); //insert a new line
}
for (int i = 1; i <= 4; i++)
{
for (int k = i; k <= 4; k++)
{
System.out.print(" ");
}
for (int j = 1; j < i; j++)
{
System.out.print(j);
}
System.out.println();
}

How would you draw an arrow using a loop ? (triangle rotated 90 degrees to the right)

I can print a triangle but I'd like to know how to get it to print in the form of an arrow like below and yes it is homework.
for (int i = 1; i < 10; i += 2)
{
for (int k = 0; k < (4 - i / 2); k++)
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
You can use the following code:
public static void main (String[] args)
{
int mid = 10/2; //where 10 is number of lines
for (int i = 0; i < 10; i ++) {
if(i < mid){
for(int j = 0; j < i; j++){
if(j == 0 || j == i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
} else{
for(int j = 10 - i; j > 0; j--){
if(j == 10-i || j == 1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println("");
}
}

Number Patterns using loops in Java

I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
345
45
5
Pattern 4
1
123
12345
123
1
My code that almost matched pattern 1 is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
public class PrintPattern {
public static void main(String[] args){
printPattern1();
printPattern2();
printPattern3();
printPattern4();
}
public static void printPattern1(){
for (int i = 0; i<5; i++){
for(int j = 5; j>i; j--)
System.out.print(j);
System.out.println();
}
}
public static void printPattern2(){
for (int i = 0; i<5; i++){
for(int k = 0; k<4-i; k++)
System.out.print(" ");
for(int j = 1; j<=i+1; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern3(){
for (int i = 0; i<5; i++){
for(int k = 0; k<i; k++)
System.out.print(" ");
for(int j = i+1; j<=5; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern4(){
for (int i = 0; i<5; i++){
for(int k = 0; k<Math.abs(2-i); k++)
System.out.print(" ");
for(int j = 1; j<=5-2*Math.abs(2-i); j++)
System.out.print(j);
for (int p = 0; p<Math.abs(2-i); p++)
System.out.print(" ");
System.out.println();
}
}
}
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}

Reversing Java For Loop

I have this code, which I am trying to print the following shape out...
****
* *
**
*
Current Code:
System.out.print("\nEnter number of rows: ");
rows = kybd.nextInt();
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == rows - 1 || j == i) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
For some reason, it prints out like this:
****
*
**
* *
Any idea how to reverse the second for loop, so it prints the other way?
I feel you should change your i loop
for (int i = rows - 1; i > 0 ; i--)
Try this:
Scanner kybd = new Scanner(System.in);
System.out.print("\nEnter number of rows: ");
int rows = kybd.nextInt();
if (rows > 1) {
for (int i = 0; i < rows; i++)
System.out.print("*");
System.out.println();
for (int i = rows - 1; i > 1; i--) {
System.out.print("*");
for (int j = 2; j < i; j++)
System.out.print(" ");
System.out.println("*");
}
}
System.out.println("*");
Sample output:
Enter number of rows: 6
******
* *
* *
* *
**
*
Notice, that you are printing out the first line of asterisk in a separate cycle, not inside the main one. If you want to follow this road, you need to put the first cycle behind the second one.
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if ((j == 1) || (j == (rows - 1)) || (j == i)) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
for (int i = 0; i < rows; i++) {
System.out.print("*");
}

Java to print number triangle with nested loop

I am trying to print the following using a nested loop in Java:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
but it's coming out like the following:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
Here is my code:
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
Any help would be appreciated. Thanks
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
System.out.println(" ");
}
for (int j = i; j <= 6; j++) {
System.out.println(j +" ");
}
}
Your Program should be -
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= (6-i+1); j++)
{
System.out.print(j + " ");
}
System.out.println();
}
Thanks
Set this condition in inner second loop.
for (int j = 1; j <= 7 - i ; j++)
Edit :
Complete code is
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 7 - i ; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 2; i <= 7; i++) {
for (int j = 2; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j <= 7; j++) {
System.out.print(j-1 + " ");
}
System.out.println();
}
This is giving the same output ... Please check
public static void main(String[] args)
{ int c=0;
for(int i=6;i>0;i--)
{
for(int k=0;k<c;k++)
{
System.out.print(" ");
}
for (int j=1;j<=i;j++)
{
System.out.print(j +" ");
}
c++;
System.out.println(" ");
}
}
}
Try this :
for (int i = 1; i <= 7; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 7-i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}

Categories