user3452963
user3452963

Reputation: 117

for loop to generate "1,4,9,16,25,36,49,64,81,100"

how can I write a for loop to give this output? I was thinking a nested loop?

for (int i = 0; i < 100; i++){
    for (int j = 0; j < i; j++) {

but i don't know how to go from there?

thanks

Upvotes: 0

Views: 49612

Answers (5)

user14755843
user14755843

Reputation: 1

class forloops
{
    public static void main ()
    {
        for (int i = 1; i <= 20; i = i + 1)
        {
            System.out.println (i * i + "ans");
        }
    }
}

Upvotes: -1

Lena
Lena

Reputation: 1

You have to iterate the numbers, and then append the list:

for (int i =1; i <= 10; i++) {    
    System.out.print(i*i + " ");
}

Upvotes: 0

user6044559
user6044559

Reputation: 1

for loop to generate “1,4,9,16,25,36,49,64,81,100”

class series
{
     public static void main(String[]args)
     {
        int i,j;
        for(i=1; i<=10; i++) {
             j=i*i;
             System.out.println(j);
        }
     }
}

Upvotes: 0

anirudh
anirudh

Reputation: 4176

I won't give out the answer but I'll give you a hint. That is a list of the first 10 perfect squares. So you just need one loop to go through 10 values and get their square.

Upvotes: 6

NeoP5
NeoP5

Reputation: 619

try this... ;)

for (int i = 1; i <= 10; i++){
   int result = i * i;
   System.out.printLn(result);
}

Upvotes: 0

Related Questions