Math, asked by samarthjoshi1624, 1 year ago

find the sum of alternate number in array in c++

Answers

Answered by krishnasweethap8nkwf
0
C Program to Print the Alternate Elements in an Array

This program prints the alternate elements in an array.

Problem Description

This is a C program which implements an array and prints the alternate elements of that array.

Problem Solution

1. Create an array and define its elements according to the size.
2. Using for loop, access the elements of the array, but instead of incrementing the iterator by 1, increase it by 2, so as to get alternate indexes of the array.

Program/Source Code

Here is source code of the C Program to print the alternate elements in an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  /*  * C Program to Print the Alternate Elements in an Array  */  #include <stdio.h> void main() {  int array[10]; int i; printf("enter the element of an array \n"); for (i = 0; i < 10; i++) scanf("%d", &array[i]);  printf("Alternate elements of a given array \n"); for (i = 0; i < 10; i += 2) printf( "%d\n", array[i]) ; }Program Explanation

1. Declare an array of some fixed capacity, 10.
2. Define all its elements using scanf() function under for loop.
3. Now, run a for loop with an iterator i, incrementing it by 2 each time so as to get alternate alternate indexes.
4. Alternate indexes will give alternate elements of the array.
5. Print the alternative elements as well.

Runtime Test Cases12 23 45 57 68 73 84 97 120 125 Alternate elements of a given array 12 45 68 84 120
Answered by samarthkrv
0

Answer:

#include <iostream>

using namespace std;

int main() {

   int arr[] = {1,2,3,4,5};

   int sum = 0 , ind = 0;

   int len = sizeof(arr)/sizeof(arr[0]);

   int i = 0;

       for(int i = 0; i < len; i++){

           if(i%2 == 0){

               sum = sum + arr[i];

           }

       }

       cout << sum;

}

Step-by-step explanation:

Similar questions