Write a program that inputs values into one-dimensional array and finds out the total number of odd and even values entered into one-dimensional array.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variables i, odd and sum.
- Step 3: Initialized i, odd and even variable equal to zero.
- Step 4: Take inputs from user.
- Step 5: Divide array elements one by one with 2 until i < = 9.
- Step 6: If element divided by 2 is equal to 0 then Even ++.
- Step 7: If element divided by 2 is not equal to 0 then odd ++.
- Step 8: Print values
- Step 9: End program
Code:
Write a program that inputs values into a one-dimensional array and finds out the total number of odd and even values entered into a one-dimensional array.
#include<iostream>
using namespace std;
main()
{
int arr[10], i, odd, even;
i = odd = even = 0;
while(i<=9)
{
cout<<"Enter value into element "<<i<<"?";
cin>>arr[i];
(arr[i]%2==0)? even++ : odd++;
i++;
}
cout<<"\nEven Values : "<<even;
cout<<"\nOdd Values : "<<odd;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well๐ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: ๐https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: ๐https://myustaadg.com/category/programming-exercises/arrays-exercises/