Divide all elements of an array with a given number in C++

Hello, guys, today blog is to divide all the elements of an array with a given number in C++. Each element is to divide with a given provided number and updated in an array after dividing by a provided number. Let’s start today’s code.

How to divide all elements of an array with a specific number in C++

This is a collection of items stored in a contiguous memory location. Each element has a different memory location in a continuous order. Hence it is easy to find the location from the base address. For example, an array contains five numbers at different locations. So it is easy to apply operation on different numbers of elements of an array.

C++ Code:

#include <iostream>

using namespace std;

int main()
{
    int n, number,A[100],i;
    cout << "Enter the number of element of an array(max 100):";
    cin>>n;
    cout<<"Enter the elements of an array:";
    for(i=0;i<n;i++)
        cin>>A[i];
    cout<<"Enter the number you want to divide all the elements of an array:";
    cin>>number;
    for(i=0;i<n;i++)
        A[i]=A[i]/number;
    cout<<"Enter final array after dividing all elements with "<<number<<" :\n";
    for(i=0;i<n;i++)
        cout<<A[i]<<" ";
    return 0;
}

First, we provide values like the number of elements and array. Then a number to divide all elements of an array using a for a loop. Then we are going to display the final.

Output:

Enter the number of element of an array(max 100):5
Enter the elements of an array:10 20 30 40 50
Enter the number you want to divide all the elements of an array:5
Enter final array after dividing all elements with 5 :
2 4 6 8 10

This example we are creating an array of five elements and a number for dividing them. Then the final output is display.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *