How do I print this pattern in java? - java

I'm not able to print this pattern:-
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
I,ve already tried this:
class loop1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=9;j<10;j++)
{ System.out.print(j);
j--;
}
System.out.println("");
}
}
}
but get an output of unlimited 9, a never ending loop.

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

Try this its very simple
working example is here
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.insert(0, i);
System.out.println(s);
Result
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890

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

Try this:
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.append(i);
System.out.println(s);
}

Your outer loop is looping the wrong way; for a working solution, it should be going from 9 down to 1. Then, your inner for loop would be looping up to 9:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
System.out.print(j);
}
System.out.println(0);
}
The 0 can't be part of the loop because it doesn't fit with the 1-9 pattern. However, you could print the last digit of each number and cut the 0 part of the println statement:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
String number = j + "";
System.out.print(number.substring(number.length() - 1));
}
System.out.println();
}

Maybe i am late but i have to post this because of my love for loops
public class SeriesLoop {
public static void main(String a[]){
for(int i=9;i>=1;i--){
for(int j=i;j<=10;j++){
System.out.print(j%10);
}
System.out.println();
}
}
}
DEMO
Cheers

Related

Need to invert a loop in java

import java.util.Scanner;
public class PrintVshape {
public static void main(String [] args){
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=6;k>i;k--){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
}
This prints out the following:
xxxxxxVpppppppV
xxxxxVppppppV
xxxxVpppppV
xxxVppppV
xxVpppV
This is a homework problem. It's the x's that I'm stuck on. I need to invert them so they become more instead of fewer.
The loop you have for x says that run it from 6 to i and i moves from 0 to 5;
for(int k=6;k>i;k--){
System.out.print("x");
}
Which means x will be printed 6 times , 5 times .. ..
Correct the loop for x and it will be fine.
You need to loop on k up to some increasing value, such as i, or in this case, i+2, to get the range between two xs in the first row to six xs in the last:
for (int k = 0; k < i+2; k++) {
System.out.print("x");
}
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=0;k<i+2;k++){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
result:
xxVpppppppV
xxxVppppppV
xxxxVpppppV
xxxxxVppppV
xxxxxxVpppV
i+2 in k cycle, mean how many times it will print x on beginnig (if you want start with xx then i=0+2)
I am not quite sure, is this the thing you are after now?
public static void main(String[] args) {
int N = 8;
printV(N);
}
private static void printV(int N) {
for (int i = 0; i < N; i++) {
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("V");
for (int j = 0; j < (N - i - 1) * 2; ++j) {
System.out.print("p");
}
System.out.print("V");
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("\n");
}
}
Output:
VppppppppppppppV
xVppppppppppppVx
xxVppppppppppVxx
xxxVppppppppVxxx
xxxxVppppppVxxxx
xxxxxVppppVxxxxx
xxxxxxVppVxxxxxx
xxxxxxxVVxxxxxxx

I want to print the following format in java program for the pryamid

1**
2**
3***
4****
Till then I have got this snippet of code
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
You can print your index before the loop :
for (int i = 1; i <= 4; i++) {
System.out.print(i);//<<----------Print the index i
for (int j = 0; j < i; j++) {
System.out.print(i == 1 ? "**" : "*");//check if i == 1 then print 2 stars else 1
}
System.out.println("");
}
In case you mean 1* you can replace System.out.print(i == 1 ? "**" : "*"); with System.out.print("*");
you just need to add i index to print
here you go
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
system.out.println(i);
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Java 8 simplifies it:
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
System.out.print(i);
System.out.println(String.join("", Collections.nCopies(i, "*")));
}
}
}

Can't print the pattern

I created the code that should print a pattern like
12345
2345
345
45
5
I have the code written below, the logic works fine in python but in java the output is different.
class Testing{
public static void main(String args[])
{
for (int i = 1; i<6;i++)
{
for (int j =0; j<i-1;j++)
{
System.out.print(" ");
}
while (i < 6){
System.out.print(k);
System.out.println();
i++;
}
}
}
}
The output is just 12345. I don't understand why does it iterate over first for loop for only once.
Use another variable for while control.
public class Testing {
public static void main(String args[]) {
int k;
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
k = i;
while (k < 6) {
System.out.print(k);
k++;
}
System.out.println();
}
}
}
You can see this in this link
this will show you :
12345
2345
345
45
5
Note: When 'while loop' increases. That increases i value bigger than 6. So next time it terminates the outer loop. That was your mistake.
package com.appointment.api;
class Testing {
public static void main(String args[]) {
for (int i = 1; i < 6; i++) {
System.out.println();
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
int x = i;
while (x < 5) {
System.out.print(i);
x++;
}
}
}
}
Following is a java-8 implementation of the problem:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.rangeClosed(1, MAX)
.mapToObj(j -> j == MAX ? j + "\n" : j >= i ? j : " ")
.forEach(System.out::print)
);
Set MAX = 5 and it will print your pattern.
Output:
12345
2345
345
45
5

Different variations for using loops to a pattern in java

I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern
1
121
12321
1234321
My code 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");
}
Your code prints only the suffix of each line, you are missing to write 12....i for each line.
In addition, the loop should start from i, not from rows-i+1.
for (int i = 1 ; i <= rows ; i++) {
//add an inner loop that prints the numbers 12..i
for (int j = 1 ; j < i ; j++ ) {
System.out.print(j);
}
//change where j starts from
for (int j = i ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.println(""); //to avoid inconsistency between different OS
}
First note that 11*11 = 121, 111*111=12321, etcetera.
Then that 10n - 1 is a number that consists of n 9's, so (10n - 1)/9 consists of n 1's.
So we get:
int powerOfTen = 1;
for (int len = 0; len < 5; len++)
{
powerOfTen = powerOfTen*10;
int ones = (powerOfTen-1)/9;
System.out.println(ones*ones);
}
Code explains everything!
public static void main(String[] args) {
String front = "";
String back = "";
int rows = 5;
for (int i = 1; i <= rows; i++) {
System.out.println(front+i+back);
front += i;
back = i + back;
}
}
Try this one: it may seems too much looping, but yet easy to understand and effective.
public static void main(String[] args) {
int rows=5;
int i,j;
for(i=1;i<=rows;i++)
{
/*print left side numbers form 1 to ...*/
for(j=1;j<i;j++)
{
System.out.printf("%d", j);
}
/*Print the middle number*/
System.out.printf("%d", i);
/*print right numbers form ... to 1*/
for(j=i-1;j>0;j--)
{
System.out.printf("%d", j);
}
System.out.println("");
}
}
int n=0;
for(int m =0; m<=5; m++){
for(n= 1;n<=m;n++){
System.out.print(n);
}
for(int u=n;u>=1;u--){
System.out.print(u);
}
System.out.print("");
}

Simple Java Loop Question

Assume that the int variables i and j have been declared, and that n has been declared and initialized.
Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.
For example, if the value of n is 4, the output should be
*
**
***
****
I won't do your homework, so here is a hint:
The first line is always 1 element long.
The last line is always N elements long.
The are a total of N lines.
Surely, with the above, you can create the necessary program.
Just for fun - single for-loop solution:
public void doIt(int n) {
String temp = String.copyValueOf(char[n]);
for (int i = 1; i <= n; i++)
System.out.println(temp.substring(n-i).replace((char) 0, 'x'));
}
And some recursion - zero for-loop solution:
public void doItAgain(int n, String display) {
if (n==0) return;
System.out.println(display);
doItAgain(n-1, display+'x');
}
(call it with doItAgain(4,"x") for your example)
My answer:
public class loop1
{
public static void main(String[] args)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
}
}
In case you're in school/college and more interested in getting some, more power to you buddy:
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
public class ForLoop {
public static void main(String[] args) {
for(int i = 0;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println("\n");
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
simple, easist way to do it using main method --> public static void main (Strings [] args){
for(int i = 1; i <= max; i++){
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println(" ");
}
}

Categories

Resources