java what is mising from my code i need the outpot to be lik
java
what is mising from my code
i need the outpot to be like this (
but i have this (The sum of pro is 10)
this is my coed
import java.util.Scanner;
public class Calculation
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
int [] pro = new int [9];
int i;
for (i = 0; i < pro.length; i++)
{
pro [i] = scan.nextInt();
}
int sum = 0;
for ( i = 0; i < pro.length; i++)
if (i%2 == 0)
{
sum+= pro[i];
}
else if (i%1 == 0)
{
sum-= pro[i];
}
System.out.println(\"The sum of pro is \"+ \"\" + sum);
}
}
Solution
import java.util.Scanner;
public class Calculation {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] pro = new int[10];
int i;
for (i = 0; i < pro.length; i++) {
pro[i] = scan.nextInt();
}
int sum = 0;
System.out.print(\"The sequence is \");
for (i = 0; i < pro.length; i++) {
if (i % 2 == 0) {
if (i == 0)
System.out.print(pro[i]);
else
System.out.print(\"+\" + pro[i]);
sum += pro[i];
} else if (i % 2 == 1) {
System.out.print(\"-\" + pro[i]);
sum -= pro[i];
}
}
System.out.println(\"=\" + sum);
System.out.println(\"The sum of pro is \" + \"\" + sum);
}
}
OUTPUT:
1 1 1 1 1 1 1 1 1 1
The sequence is 1-1+1-1+1-1+1-1+1-1=0
The sum of pro is 0
NOTE:
make the array size as 10

