0
For Loop in Java find a series numbers of 1*1 =1, 2*2=4, ;n*n =n etc.
Instructions: Enter a Number: 5 Output: 1* 1 = 1 2 *2 = 2 3* 3 = 9 4* 4 = 16 5* 5 = 25 FIX THE CODE: import java.util.Scanner; public class Main { public static void main(String[] args ) { int i, sum, n; Scanner scan = new Scanner(System.in); System.out.println ("Enter a number: "); n = scan.nextInt(); { for (i = 1; i <= n; i++); sum = i*i; System.out.print (i + "*" + i +"=" +(i*i)); } } }
4 Respuestas
+ 2
Hello Romela Cathrille Mijares Zabala
After the for loop comes a curly brackett.
And you should use System.out.println()
import java.util.Scanner;
public class Main {
public static void main(String[] args )
{
int i, sum, n;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a number: ");
n = scan.nextInt();
for (i = 1; i <= n; i++){
sum = i*i;
System.out.println(i + "*" + i +"=" +(i*i));
}
}
}
+ 1
Btw:
It is not a sum, it is a product.
a + b = sum
a * b = product
0
i**2
using variable named "result" is more logical rather than sum, total, next etc