Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:54:32+00:00 2026-06-17T08:54:32+00:00

I’m supposed to write a program that asks for the number of rows the

  • 0

I’m supposed to write a program that asks for the number of rows the user wants. For example is the user entered 5 it will display all numbers from 25 to 1 arranged in 5 columns and 5 rows. Something like this should be the output if 5 is entered:

25  24  23  22  21
16  17  18  19  20  
15  14  13  12  11  
 6   7   8   9  10  
 5   4   3   2   1

As you can see there is a pattern. the first number to appear is the square of the number. then the next number is number squared minus 1. Until it reached 21, 5 will be subtracted bringing 16. Then it will add by 1 until it reach 20. As you can see it is like a snake.

The problem is it works for any number EXCEPT when 1 is entered. 0 is the current result when 1 is entered.

Here’s my current codes: please help me thanks

import java.util.*;
public class ArrayOutput2 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int number = 0;

        System.out.print("Enter number of rows: ");
        number = input.nextInt();

        int[][] num = new int[number][number];
        int k=1, i, j;

            while(k< (number*number))
            {
                for(i=number; i>=1; i--)
                {
                   if (i%2==1)
                   {
                     for(j=number-1; j>=0; j--)
                         {
                             num[i-1][j]=k;
                             k++;
                         }
                   }

                   else
                         for(j=0; j<=number-1; j++)
                         {
                             num[i-1][j]=k;
                             k++;
                         }
                 }
           }

        for(i=0;i<number;i++)
        {
             for(j=0;j<number;j++)

             System.out.print(num[i][j]+"\t");
             System.out.println();   
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:54:32+00:00Added an answer on June 17, 2026 at 8:54 am

    Suppose that the user inputs 1 as a value, therefore number == 1.
    You allocate an array num[1][1], that is an array with only one possible cell, number[0][0]

    Then the loop is initiated

    k=1;
    while (k<(number*number)); // which is like while(1<1*1)==FALSE
    

    therefore the loop is never used. You can use:

    1) Either a do-while loop to run the loop at least once
    2) or add an if statement just after while() loop ends:

    // Using an IF statement immediately after the unmodified while()
    if (number==1)
    {
       num[0][0]=1;
    }
    
    
    // or with a loop DO-WHILE
        do
        {
            for(i=number; i>=1; i--)
            {
               if (i%2==1)
               {
                 for(j=number-1; j>=0; j--)
                  {
                    num[i-1][j]=k;
                    k++;
                  }
               }
               else
                  for(j=0; j<=number-1; j++)
                  {
                    num[i-1][j]=k;
                    k++;
                  }
             }
       }while(k<(number*number));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.