function for even and odd

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masqwerty16
    New Member
    • May 2015
    • 8

    function for even and odd

    i have here a code that can distinguish between odd and even numbers but my problem is how can i add for even numbers and odd numbers. here is my code...



    //odd and even functions

    #include <iostream.h>

    void even (int a,int b,int c, int d,int e);
    void odd (int a,int b, int c,int d,int e);
    void addeven (int a,int b,int c,int d, int e);
    void addodd (int a, int b,int c, int d,int e);

    void even (int a,int b,int c,int d,int e)

    {

    if ((a%2)==0) cout << a << " it is even.\n";
    if ((b%2)==0) cout << b << " it is even.\n";
    if ((c%2)==0) cout << c << " it is even.\n";
    if ((d%2)==0) cout << d << " it is even.\n";
    if ((e%2)==0) cout << e << " it is even.\n";

    }

    void odd (int a,int b, int c,int d,int e)

    {
    if ((a%2)!=0) cout << a << " it is odd.\n";
    if ((b%2)!=0) cout << b << " it is odd.\n";
    if ((c%2)!=0) cout << c << " it is odd.\n";
    if ((d%2)!=0) cout << d << " it is odd.\n";
    if ((e%2)!=0) cout << e << " it is odd.\n";
    }

    void addeven (int a,int b,int c,int d, int e)

    {
    cout << "Addition for Even Numbers!" << endl;
    cout << "Result: " << endl;
    }

    void addodd (int a, int b,int c, int d,int e)

    {
    cout << "Addition for Odd Numbers!" << endl;
    cout << "Result: " << endl;
    }

    main ()

    {
    int a, b, c, d, e;

    cout << "Enter five numbers: ";
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >> e;

    even (a,b,c,d,e);
    odd (a,b,c,d,e);
    addeven (a,b,c,d,e);
    addodd (a,b,c,d,e);
    }
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    You should use <iostream> instead of <iostream.h>
    You should also use an array, it's better (unless it's a school assignment...)

    C++
    Code:
    #include <iostream>
    using namespace std;
    
    void validate(int* list,int evenadd,int oddadd){
     for(int i=0;i<=sizeof(list);i++){
      if(list[i]%2==0){
       cout<<"Num"<<i+1<<"("<<list[i]<<") is even"<<endl;
       cout<<list[i]<<"+"<<evenadd<<"= "<<list[i]+evenadd<<endl;
      }
      else if(list[i]%3==0){
       cout<<"Num"<<i+1<<"("<<list[i]<<") is odd"<<endl;
       cout<<list[i]<<"+"<<oddadd<<"= "<<list[i]+oddadd<<endl;
      }
      else{
       cout<<"Num"<<i+1<<"("<<list[i]<<") is prime"<<endl;
      }
     }
    }
    
    int main(){
     int max=5;
     int list[max];
     
     for(int i=0;i<max;i++){
      cout<<"Please enter value for num"<<i+1<<": ";
      cin>>list[i];
     }
     validate(list,1,2);
     system("PAUSE");
     return 0;
    }


    C:
    Code:
    #include <stdio.h>
    
    void validate(int* list,int evenadd,int oddadd){
     int i=0;
     while(i<sizeof(list)){
      if(list[i]%2==0){
       printf("Num%d(%d) is even...\n",i+1,list[i]);
       printf("%d + %d is: %d\n",list[i],evenadd,list[i]+evenadd);
      }
      else if(list[i]%3==0){
       printf("Num%d(%d) is odd...\n",i+1,list[i]);
       printf("%d + %d is: %d\n",list[i],oddadd,list[i]+oddadd);
      }
      else{
       printf("Num%d(%d) is prime...\n",i+1,list[i]);
      }
      i++;
     }
    }
    
    int main(){
     int max=5;
     int list[max];
     int i=0;
     while(i<max){
      printf("Please enter value for num%d: ",i+1);
      scanf("%d",&list[i]);
      i++;
     }
     validate(list,1,2);
     system("PAUSE");
     return 0;
    }


    Hope that helps!
    Last edited by computerfox; May 25 '15, 03:31 PM. Reason: Added citations

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What do you want addeven() and addodd() to do?
      Your function prototype shows that each of these functions take 5 arguments.

      Comment

      Working...