Computer Science, asked by amulyaamandla, 1 month ago

write a 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 : 3

Enter elements in the array :

34

23

54

Sum = 42

Similar questions