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)); } } }

7th Jan 2022, 3:29 PM
Romela Cathrille Mijares Zabala
Romela Cathrille Mijares Zabala - avatar
4 Answers
+ 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)); } } }
7th Jan 2022, 3:42 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 4
Shadoff i**2 works in python, but not in java.
7th Jan 2022, 5:59 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 1
Btw: It is not a sum, it is a product. a + b = sum a * b = product
7th Jan 2022, 3:43 PM
Denise RoƟberg
Denise RoƟberg - avatar
0
i**2 using variable named "result" is more logical rather than sum, total, next etc
7th Jan 2022, 3:51 PM
Shadoff
Shadoff - avatar