Not able to proceed with Series sum Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Not able to proceed with Series sum without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I wanted to find the missing number in series so i thought a simple idea why not add all the numbers in array which are in series and hold it in one variable and then calculate the sum of series by formula Sn=n/2(a+l) but while calculating the series sum i am getting some error.

public class Missing {
    public static void main(String[] args) {


    int ar [] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    int sum = 0; int total=0;
    for(int num: ar)
    {
        sum = sum+num;
    }



        int n = ar.length;
        int a = ar[0];
        int l =ar[ar.length-1];
         total = [n/2*(a+l)];

         System.out.print("The missing number is "+(sum-total));

}}

total = [n/2*(a+l)]; ……………………….(1)

This is where i am getting error.

enter image description here

Answer

The first thing is in total = [n/2*(a+l)]; [] is not valid syntax in this context. The second thing I noticed, is that your formula to calculate the sum seems odd, maybe you meant Sn = (n * (a + l)) / 2?. After making those two changes the code should look as follows:

public class Missing {
    public static void main(String[] args) {

        int ar [] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        int sum = 0; 
        for(int num: ar)
        {
            sum = sum+num;
        }

        int n = ar.length;
        int a = ar[0];
        int l =ar[ar.length - 1];
        int total = (n * (a + l)) / 2;
        System.out.print("The missing number is "+(sum - total));
        // outputs 0 which is correct nothing is missing

        // Now if you remove say 12 from the array
        // by changing the array to int ar [] = {1,2,3,4,5,6,7,8,9,10,11,0,13};
        // you should get back -12 which means 12 is missing
    }
}
We are here to answer your question about Not able to proceed with Series sum - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji