nested loop right triangle java - java

so I want to create a numerical right triangle in java using a nested loop and I created this:
public class nestedloop
{
public nestedloop()
{
loop();
}
public static void main(String [] args)
{
new nestedloop();
}
public static void loop()
{
System.out.println(" Enter a number ");
int n= IBIO.inputInt();
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
However, as you can see, it creates a right triangle formed out of asterisks and I want it to create it through increasing numbers like this:
1
23
456
78910
I'm confused as to what I need to replace the Asterix with. I was thinking it could be another loop but everything I have tried failed miserably.

Just create a counter:
public static void loop(){
System.out.println(" Enter a number ");
int n= IBIO.inputInt();
int counter = 0;
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(++counter);
}
}
}

Related

printing v shape in java

I am a newbie to java programming and I am working on this excercise from my textbook. The goal is to print a V shape pattern of numbers. From the picture below, you can see what the output should look like. I am having trouble creating the other half of numbers. I have pasted my code down below for reference.
for (int i = 7; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
System.out.print(i);
for (int k = 1; k >= i*2; k++) {
System.out.print(" ");
}
System.out.println(i);
Use the following code (just made a few modifications to your code, did not check its efficiency):
public static void main(String[] args) {
for (int i = 7; i >= 1; i--) {
for (int k = 7; k >= i; k--) {
System.out.print(" "); // Print 7-i number of spaces before start of each line
}
System.out.print(i); // Print i
for (int j = 1; j <= i*2; j++) {
System.out.print(" "); // Print i*2 number of spaces after printing i
}
System.out.println(i); // Print i
}
}
Rather then nesting loops (and iterating backwards), I would decompose the generating of white-space with a method to repeat a given String a given number of times. Like,
private static String repeat(String s, int n) {
return Stream.generate(() -> s).limit(n).collect(Collectors.joining());
}
Then I would prefer a StringBuilder and a single call to println like
public static void main(String[] args) {
int start = 6;
for (int i = 0; i < start; i++) {
int v = start - i;
StringBuilder sb = new StringBuilder();
sb.append(repeat(" ", i)).append(v);
sb.append(repeat(" ", 2 * v)).append(v);
System.out.println(sb);
}
}

How can I alter this code so that the output is flipped?

I am making this code that displays a series of asterisks in rows that get smaller by one each time you go down a row. It's supposed to start at 10 and stop at 1 on the very bottom row but for some reason my code starts with 1 at the top and grows bigger as it goes down. I was wondering how I could go about flipping the output so that it looks like the example. Thanks in advance
Example:
**********
*********
********
*******
******
*****
****
***
**
*
My code:
public class RowsOfAsterisks {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 11; i++)
{
for (int x = 0; x < i; x++)
{
System.out.print("*");
}
System.out.println();
}
}
}
You can create another method in order to avoid two for statment
Check if it can help you:
public class RowsOfAsterisks
{
public static String repeat(String str, int times)
{
return new String(new char[times]).replace("\0", str);
}
public static void main(String[] args)
{
for(int i = 10; i > 0; i--)
{
System.out.println(repeat("*", i));
}
}
}
You just have to start the outer loop from 10 to achieve this.
I hope the below code helps you:
public class RowsOfAsterisks {
public static void main(String[] args) {
for (int i = 10; i > 0; i--)
{
for (int x = 0; x < i; x++)
{
System.out.print("*");
}
System.out.println();
}
}
}
int rows = 10;
int columns = 10;
for(int i = 0; i<rows; i++)
{
for(int j = 0; int j < columns; j++)
{
System.out.print("*");
}
System.out.println();
--columns;
}
You're on the right track you just need to fix up the nested loop like so:
public static void main(String[] args) {
for (int i = 1; i < 11; i++)
{
for (int x = 11; x > i; x--)
{
System.out.print("*");
}
System.out.println();
}
}

how can I print a four row and column numbers from one to four and restart after it reaches four

I feel like the code I wrote is wrong but I don't know why. looks very unprofessional to me.
/* 1234
2341
3412
4123
*/
public class pattern{
public static void main(String args[]){
for(i=1; i<=4; i++)
{for(j=1; j<=4; j++)
{System.out.print(i);
}
System.out.println();
while(i>4)
{ int i= 1;
i++;
System.out.print(i);}
System.out.println();
}
It's hard to tell what your asking for exactly, but—judging from the comment above your class—you're probably looking for something like this:
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
It goes without saying, but I have to; you should always try to follow the Java naming/formatting standards.
Take care at these:
for(int i=1; i<=4; i++)
{for(int j=1; j<=4; j++)
System.out.println();
You should initialize the variables i and j and System not system
And the parameter in main method should be like String args:
public static void main(String args[]){
Here is your code in nice format:
public static void main(String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
}
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++) {
int k=i+j-1;
if(k>4) {
System.out.print(k-4 + " ");
}
else {
System.out.print(k + " ");
}
}
System.out.print(" ");

Different ways to print the first 5 multiples of 2 in Java as "*"

I'm trying to write a program that prints the output:
**
****
******
********
**********
What I came up with:
public class Loops {
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(" ");
}
}
This gives me the desired output, but despite my searching I still don't know how I can modify this code to use multiplication instead of just tacking on another * to the print statement.
public static void main(String[] args)
{
StringBuilder s = new StringBuilder();
for (int i = 1; i <= 5; i++)
{
s.append("**");
System.out.println(s.toString());
}
}
Is this what you want?
public class Loops {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 0; j < (2 * i); j++) {
System.out.print("*");
}
System.out.println();
}
}

A program that prints the following pattern

I have to code a program to print this output:
1
212
32123
4321234
543212345
I have successfully coded this portion of the pattern:
1
12
123
1234
12345
However, I am not reaching the second portion. Here's my code:
for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
Why not recursive? Just because it's fun ;)
public static void main(String args[]) {
System.out.println(pyramid(5));
}
public static String pyramid(int rank) {
if (rank == 1) {
return "1\n";
}
return pyramid(rank - 1) + mirror(rank) + "\n";
}
public static String mirror(int rank) {
if (rank == 1) {
return "1";
} else {
return rank + mirror(rank - 1) + rank;
}
}
You just need another j for loop before your current j for loop that counts down from 5 to (but not including) 1. Decide whether to print a space or j itself, depending on if j is greater than i.
for (int j=5; j > 1; j--) {
if (j > i)
System.out.print(' ');
else
System.out.print(j);
}
package recAaA;
public class testA {
static void rec(int startVal, int endVal)
{
if(startVal==0)startVal=-2;
if(startVal<-endVal) return;
System.out.print(Math.abs(startVal));
rec(startVal-1,endVal);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int imax=5;
for(int i=1;i<imax+1;i++)
{
for(int j=0;j<imax+1-i;j++)
{
System.out.print(" ");
}
rec(i,i);
System.out.println();
}
}
}
Output:
1
212
32123
4321234
543212345
if you give i to startVal and i+1 to endVal, the output becomes
12
2123
321234
43212345
5432123456
As someone already noted, you can accomplish this easily with a double for-loop, one counting down from i, one counting up:
for(int i=1; i<=5; i++) {
for(int j=i; j>=2; j--) {
System.out.print(j);
}
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
You can also accomplish the same thing in a single for loop if you don't print right away. For example you can build an StringBuffer with one, then add the numbers on either side until i==j, and then print outside of the inner loop.
for(int i=1; i<=5; i++) {
StringBuffer buffer = new StringBuffer();
buffer.append(1);
for(int j=2; j<=i; j++) {
buffer.insert(0, j);
buffer.append(j);
}
System.out.println(buffer);
}
Here's code that outputs the numbers; all you have to do is fix the spacing.
for (int i = 1; i <= 9; i++) {
BigInteger b =
BigInteger.TEN.pow(2*i-1)
.subtract(BigInteger.ONE)
.divide(BigInteger.valueOf(9))
.multiply(BigInteger.valueOf(i+1))
.subtract(BigInteger.TEN.pow(i)
.subtract(BigInteger.ONE)
.divide(BigInteger.valueOf(9))
.pow(2));
System.out.println(b);
}
That is, for each integer in the range 1-9, it prints ((102n-1-1)/9)(n+1)-((10n-1)/9)2. Very simple.
/* Basically logic is that in a horizontal line upto the mid ,the number is decrementing and after the mid, the number starts incrementing
*/
public class java {
public static void main(String[] args) throws IOException {
int n;
InputStreamReader io = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
System.out.println("Enter the height in the number of lines vertically");
n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i++) {
int k = i;
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(k);
if (j >= (((2 * i - 1) / 2) + 1))
k++;
else if(j<(2*i-1)/2+1) k--;
}
System.out.println();
}
}
}
I hope this will help !
import java.util.*;
public class test {
public static void main(String[] args) {
int i,j,k,l,n,a;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = n;
for(i=1 ; i<=n ; i++){
for(j=a ; j>1 ; j--){
System.out.print(" ");
}
for(k=i ; k!=0; k--){
System.out.print(k);
}
a--;
for(l=2; l<=i ; l++){
System.out.print(l);
}
System.out.println(" ");
}
}
}
for(int i=1;i<=100; i++) {
for(int j=100;j>i;j--)
System.out.print(" ");
for(int k=i;k>1;k--)
System.out.print(k+" ");
for(int j=1; j<=i; j++)
System.out.print(j+" ");
System.out.println();
}
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("Enter no ");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
int l=n-i;
while(l!=0)
{
System.out.print(" ");
l--;
}
for(int j=i;j>=2;j--)
{
System.out.print(j);
}
for(int k=1;k<=i;k++)
{
System.out.print(k);
}
System.out.println();
}
}
}

Categories