Computer Science, asked by digvijaya175, 1 month ago

write the program to calculate and return the sum of distances between the adjacent numbers in an array of positive integers in c#​

Answers

Answered by dreamrob
0

Program:

using System;

class HelloWorld

{

 static void Main()

 {

     int n;

     Console.Write("Enter number of elements in the array : ");

     n = Convert.ToInt32(Console.ReadLine());

     int[] A = new int[n];

     Console.WriteLine("Enter elements in the array : ");

     for(int i = 0 ; i < n ; i++)

     {

         A[i] = Convert.ToInt32(Console.ReadLine());

     }

     int sum = 0;

     for(int i = 0 ; i < n-1 ; i++)

     {

         sum = sum + (Math.Max(A[i] , A[i+1]) - Math.Min(A[i] , A[i+1]));

     }

     Console.WriteLine("Sum = " + sum);

 }

}

Output:

Enter number of elements in the array : 5

Enter elements in the array :

1

2

3

4

5

Sum = 4

Similar questions