Write a program to input an array of 10 elements from user and count the number of odd numbers and display it.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and varaiable odd.
- Step 3: Initialaize odd = 0;
- Step 4: Take inputs from user.
- Step 5: Check condition if(arr[i]% == 1) add value in odd,
- Step 6: Print odd
- Step 7: End Progarm
Code:
Write a program to input an array of 10 elements from the user and count the number of odd numbers and display it.
#include<iostream>
using namespace std;
main()
{
int ar[10], odd = 0;
for(int i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
if (ar[i]%2 == 1)
odd = odd + 1;
}
cout<<"Number of odd values in array are : "<<odd<<endl;
}
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/