Reputation: 117
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
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
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
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
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
Reputation: 619
try this... ;)
for (int i = 1; i <= 10; i++){
int result = i * i;
System.out.printLn(result);
}
Upvotes: 0