1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

odd and even integers

Discussion in 'C' started by harim, Apr 27, 2011.

  1. harim

    harim New Member

    Joined:
    Apr 18, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    how can i get the sum of odd and even integers by using only one while loop?
     
  2. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    your question is not clear please make ot clearer...what do u mean the sum of odd and integer you mean all odd numbers are added together and all even numbers added together...plus from where you need to read the number??
    however i can help by a small stuff if that what you mean:

    Code:
    void main()
    {
       int a[5];
       int evensum=0;
       int oddsum=0;
       a[0]=7;
       a[1]=1;
       a[2]= 3;
       a[3]= 2;
       a[4]= 6;
    
    
    for(int i=0; i<6; i++)
    {
         if(a[i]%2==0)//means its even
             { 
                 evensum += a[i]; //then add this to the int evensum
              }
          else
              {
                   oddsum += a[i];
               }
    }
    
    hope that was your point
     

Share This Page