
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Code to Decrease Even Numbers in an Array
Suppose, we are given an array 'arr' of size n that contains positive integer numbers. We have to find the even numbers and decrease them by 1. We print out the array after this process.
So, if the input is like n = 7, arr = {10, 9, 7, 6, 4, 8, 3}, then the output will be 9 9 7 5 3 7 3.
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < n, update (increase i by 1), do: if arr[i] mod 2 is same as 0, then: (decrease arr[i] by 1) print(arr[i]) print a new line
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, int arr[]) { for (int i = 0; i < n; i++){ if (arr[i] % 2 == 0) arr[i]--; cout<< arr[i] << " "; } cout<< endl; } int main() { int n = 7, arr[] = {10, 9, 7, 6, 4, 8, 3}; solve(n, arr); return 0; }
Input
7, {10, 9, 7, 6, 4, 8, 3}
Output
9 9 7 5 3 7 3
Advertisements