This Post Contains A C++ Program To Find Number Of Odd & Even Numbers From An Array With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else' & 'For-Loop' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program To Find Number Of Odd & Even Numbers From An Array.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int a[20], Val, Odd_No, Even_No, i;
clrscr();
/* Asking For The Input From User */
cout << " \n Enter Number Of Values : ";
cin >> Val;
cout << " \n Enter " << Val << " Values : ";
for( i=0; i<Val;i ++ )
{
cin >> a[i];
}
/* Printing The User Entered Values Onto The Screen/Console */
cout << " \n Entered Values Are : ";
for( i=0; i<Val; i++ )
{
cout << a[i] << " \t ";
}
/* Source Code For Computing No. Of Odd & Even Numbers */
Odd_No = Even_No = 0;
for(i=0; i<Val; i++)
{
if( a[i] % 2 == 0 )
{
Even_No++;
}
else
{
Odd_No++;
}
}
/* Printing The Output Onto The Screen/Console */
cout << " \n Total Number Of Odd Numbers : " << Odd_No;
cout << " \n Total Number Of Even Numbers : " << Even_No;
getch();
}
/* End Of Main Program */
Output :
Enter Number Of Values : 5
Enter 5 Values : 1 2 3 4 5
Entered Values Are : 1 2 3 4 5
Number Of Odd Numbers : 3
Number Of Even Numbers : 2
Algorithm :
1. Start.
2. Initialize necessary variables & one integer array.
3. Accept the dimension [Number Of Values] of array, i.e. 'Val' from user.
4. Accept the values for array.
4.1 for(i=0;i<Val;i++)
4.2 Accept the value & store it in array, i.e. a[i].
4.3 End of for loop.
5. Print the values of array.
5.1 for(i=0;i<Val;i++)
5.2 Print the value of array, i.e. a[i].
5.3 End of for loop.
6. Make 'Odd_No' & 'Even_No' variables zero.
7. Find odd or even number from array.
7.1 for(i=0;i<Val;i++)
7.2 If value, a[i] is completely divisible by 2, then
7.2.1 Increment Even_No variable by 1.
7.3 Else
7.3.1 Increment Odd_No variable by 1.
7.4 End of if...else.
7.5 End of for loop.
8. Print Number of odd numbers, Odd_No.
9. Print Number of even numbers, Even_No.
10. Exit.
This blog provides an overview about basic structure of C/C++ programs and their execution, which makes it an ideal guidance source for Programmers and Students. Based on practical approach this blog features a wide range of programs, which enables the learner to understand the techniques such as: Simple Programs, Loops, Arrays, Functions, Objects, Operator Overloading, Polymorphism, Files, Graphics, Strings, Sorting and Searching Methods, Stack, Queues, Link-Lists, Trees and Mini-Projects.
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2013
(77)
-
▼
February
(20)
- C++ Program To Find Number Of Odd & Even Numbers.
- C Program To Find & Print An Armstrong Number.
- C Program To Find & Print A Strong Number.
- C Program To Find & Print A Perfect Number.
- Commenting Style In 'C' & 'C++'.
- Good Coding Practices.
- C Program To Print Pyramid Of Numbers.
- C++ Program To Display Object Details Using Access...
- C Program To Find & Print Twin Prime Numbers.
- C Program To Find Average Of Odd & Even Numbers Up...
- C Program To Print Even Numbers Upto A User-Specif...
- C Program To Print Odd Numbers Upto A User-Specifi...
- C Program For Printing A Floyd's Triangle.
- C Program To Print Multiplication Tables.
- C Program For Printing Fibonacci Series.
- C++ Program For Defining A Class [Class Definition...
- C Program To Print Prime Numbers Upto A User-Speci...
- C Program To Find A Prime Number.
- C++ Program To Print Prime Numbers Upto A User-Spe...
- C Program To Find Odd Or Even Number.
-
▼
February
(20)
No comments:
Post a Comment