Trying to display a sequence of numbers beginning from 1 using loops - java

I'm trying to write a program that accepts an integer from 5 to 9 from the user. If the user enters 6, the output to the user will be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6
I tried using a loop like:
for(int i = 0; i < no; i++)
{
for(int j = 0; j < no; j++)
{
System.out.print(no);
}
System.out.println();
}
But the output I got was:
666666
666666
666666
666666
666666
import java.util.*;
public class numberSequence
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number in the range 5 to 9: ");
int no = Integer.parseInt(in.nextLine());
if(no < 5 || no > 9)
System.out.println("Please enter a number in the range 5 to 9.");
else
{
// continuing from here
}
}
}

Loop would be -
for(int i = 1; i <= no; i++){
for(int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for(int i = 1; i <= no; i++){
for(int j = no; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
Input - no = 6
Output -
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6

You need to use 2 for loops, the first one for increasing until value of n, the 2nd one for decreasing from value n to 1.
import java.util.Scanner;
public class numberSequence
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number in the range 5 to 9: ");
int no = Integer.parseInt(in.nextLine());
if(no < 5 || no > 9)
System.out.println("Please enter a number in the range 5 to 9.");
else
{
for(int i = 1; i <=no; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j);
}
System.out.println();
}
for(int i = no; i > 0; i--)
{
for(int j = i; j >=1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
}
Output:
1
12
123
1234
12345
123456
654321
54321
4321
321
21
1

You can try this
for (int i = 1; i <= no; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
for (int i = no; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println("");
}

you are printing "no" , you should print "j".
program print desire output is
public class Simple {
public static void main(String[] args) {
int no= 6;
for(int i = 1; i <= no; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
for(int i = no; i >= 1; i--)
{
for(int j = i; j >= 1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}

Related

Java Number Pyramid

I want to create a number pyramid like this,
7
5 6 5
3 4 5 4 3
1 2 3 4 3 2 1
but I couldn't create it. I pasted my code and its output is like this,
7
5 6 7 6 5
3 4 5 6 7 6 5 4 3
1 2 3 4 5 6 7 6 5 4 3 2 1
What should I do to fix this ? Is my code totally wrong or litte changes are enough ?
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
for (int i = width; i >= 1; i=i-2)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = i; j <= width; j++)
{
System.out.print(j+" ");
}
for (int j = width-1; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
You may compute the row index, then it's easy to get the number ranges
for (int i = width; i >= 1; i -= 2) {
int row_idx = (width - i) / 2;
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= i + row_idx; j++) {
System.out.print(j + " ");
}
for (int j = i - 1 + row_idx; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
You just need to count the level. You can do this by adding additional count variable. Lets take an example:
On level 0 you just print 7 and count is 0.
On level 1 count is 1. So you need to only print from i's current value + count amount on left and right. But since we counted 1 extra on left so a value will be less on right.
.........
And code will go on like this.
See code for better explanation:
public static void main( String[] args ) {
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
int counter = 0;
for (int i = width; i >= 1; i = i - 2) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= width - counter; j++) {
System.out.print(j + " ");
}
for (int j = width - 1 - counter; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
counter++;
}
}
I just changed a little bit on your code to make it work. Cheers mate !

How to make a numbers triangle in Java using loops, numbers starting from right

my project to create a triangle in Java with numbers starting from the right
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// new line
System.out.println();
}
}
}
Here is one way. It uses String.repeat() to pad with leading spaces.
outer loop controls the rows.
Then print the leading spaces
inner loop iterates backwards, printing the value and a space
then print a newline
int rows = 6;
for (int i = 1; i <= rows; i++) {
System.out.print(" ".repeat(rows-i));
for (int k = i; k > 0; k--) {
System.out.print(k+ " ");
}
System.out.println();
}
prints
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
If you don't want to use String.repeat() then use a loop.
Well the only thing you have to do is change order like you are first adding space and then number instead add number and then space.
code -
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// new line
System.out.println();
}
}
}
Now we get the result -
start k=i then decrement it till k>=1.
for(int k= i; k >=1 ; k--)
System.out.print(k+" ");
output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lines = scanner.nextInt();
for (int i = 0; i <= lines; i++) {
//space
int spaceNum = lines - I;
for (int j = 0; j < spaceNum; j++) {
System.out.print(" ");
}
//numbers
int numbers = lines - spaceNum;
for (int j = numbers; j > 0; j--) {
System.out.print(" " + j);
}
System.out.println();
}
}
}

Showing print integer with pattern in java

I wanna ask you something about my code. I want to display output as below:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
But the output what is shown:
2 3 4 1
6 3 0 -3
2 -1 -4 -7
-2 -5 -8-11
Here is my current code:
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
System.out.printf("%3d", number);
if(j==2){
plus=-3;
}
number+=plus;
}
number+=8;
System.out.println("");
}
Can you tell me what's wrong with it? Thank you
You need to reset plus to 1 at the end of each iteration of the outer loop. See the below code in action here.
int number = 2;
int plus = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", number);
if (j == 2) {
plus = -3;
}
number += plus;
}
number += 8;
plus = 1;
System.out.println("");
}

How to display n by n square pattern from numbers 1 through nine then 0 in Java

I have already tried putting values in an array, using matrices and even using recursive arrays but those only return error to me.
Now the code I have made looks like this
import java.util.*;
public class SQf1t9t0 {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
f19t0[i][j] = i+1;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
}
The input for instance has to look like this if the entered value for n is 4:
1234
5678
9012
I don't really think you need array just to print values. You can simply have counter and rest it once it more that 9. Something like that:
int counter = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print(counter);
if(++counter > 9) {
counter = 0;
}
}
System.out.println();
}
You just need to use modulo to start back at 0 after 9.
import java.util.Scanner;
public class DisplayMatrix
{
public static void main(String[] args) {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
f19t0[i][j] = (i * n + j + 1) % 10;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
yar.close();
}
}
For example :
Enter size for n square:
6
1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 0 1 2 3 4
5 6 7 8 9 0
1 2 3 4 5 6
PS: Don't forget to close your scanner.
You don't have a method in your class. I don't know whether you didn't put it mistakenly or not! But the following worked.
import java.util.*;
public class test {
public static void main(String[] args) {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
f19t0[i][j] = (i*n+j+1) % 10;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
yar.close();
}
}
For input 5, it outputs:
Enter size for n square:
5
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5

Printing pyramid of numbers

I have homework to make a triangle that looks like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I have been able to create almost half the triangle with the following code:
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
I have been unable to figure out how to mirror the other half of the triangle with my code to look like the triangle above. The instructor hinted that using the for loop with the tab return \t is the way to do this.
Like #Maroun's answer but simpler
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
or
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i - size + 1; j <= i; j++)
System.out.print((j > 0 ? j : "") + " ");
System.out.println();
}
prints
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
You can try this one:
int x = 0;
for(int i = 1; i <= 5; i++) {
for(int k = x; k < 4; k++) {
System.out.print(" ");
}
x++; //less spaces will be printed in the next iteration
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
I wrote another loop that begins with 4 spaces and then the amount of spaces is reduced.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Tip: Always open { for your loops and conditions, even for one operation, this might prevent bugs.

Categories