SUM OF ADJACENT PAIRS OF NUMBERS IN AN ARRAY

 SUM OF ADJACENT PAIRS OF NUMBERS IN ARRAY


I/P:

6

1 2 3 4 5 6

O/P:

3 7 11


PROGRAM:

import java.util.*;
class AdjacentPairSum
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int iNum=sc.nextInt();
        int arr[]=new int[iNum];
        for(int i=0;i<iNum;i++)
        {
            arr[i]=sc.nextInt();
        }
        if(iNum%2==0)
        {
            for(int i=0;i<iNum;i=i+2)
            {
                System.out.print((arr[i]+arr[i+1])+" ");
            }
        }
        else{
            for(int i=0;i<=iNum-3;i=i+2)
            {
                System.out.print((arr[i]+arr[i+1])+" ");
            }
        }
    }
}

// 6
// 1 2 3 4 5 6
// 3 7 11


Comments

Popular posts from this blog

PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

PROGRAM TO FIND THE COUNT OF PARTICULAR NUMBER IN AN ARRAY

TELEPHONE BOOK USING HASHMAP