Showing posts with label even and odd numbers. Show all posts
Showing posts with label even and odd numbers. Show all posts

Friday, June 20, 2014

Odd and Even Numbers Generator in C++


One of the basic programming problem that is always given to students in programming is how to determine if the given number is an odd or even number. To check if the given number is even number is very simple a number should not have an remainder on the other hard and odd number has a remainder.

For example of odd numbers are 1,3,5,7,9,11 and even numbers are 2,4,6,8,10,12,14. In this example I will show you how to write a program to list down the numbers that are odd and even numbers. Our program will ask the user to enter 10 numbers and then our program will display the numbers that belongs to the even and odd numbers. In this sample program I am using C++ as my programming language. I hope you will find my work useful in your learning how to programming in C++.

Thank you very much.


Sample Output of Our Program


Program Listing

#include <iostream>
#include <stdlib.h>

using namespace std;


  main() {

      int b[10],mod=0;
      int *mypointer;

          cout << "\t<===================================>";
          cout << "\n\t    Odd and Even Numbers Generator";
          cout << "\n\t<===================================>";
          cout << "\n\n";

           mypointer = b;

           for (int z=0; z < 10; z+=1) {
               cout << "Enter Value No. " << z+1 << ": ";
               cin >> mypointer[z];
           }
           cout << "\n \t Odd Numbers";
          cout << "\n\n";
           for (int z=0; z < 10; z+=1) {
               mod = mypointer[z] % 2;
             if (mod != 0) {
                 cout << " " << mypointer[z] << " ";
             }
           }
           cout << "\n\n";
           cout << "\n \t Even Numbers";
           cout << "\n\n";
          for (int z=0; z < 10; z+=1) {
              mod = mypointer[z] % 2;
             if (mod == 0) {
                 cout << " " << mypointer[z] << " ";
             }
          }
         cout << "\n\n";
         system("pause");
       }