odd and even integers

Discussion in 'C' started by harim, Apr 18, 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. nansijs

    nansijs New Member

    Joined:
    Apr 17, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.cprogrammingexpert.com/
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int n,even=0,odd=0;
    clrscr();
    printf("Enter the numbers(0 to exit)");
    while(1)
       {
        scanf("%d",&n);
        if(n==0)
            break;
        if(n%2==0)
    	even+=n;
        else
    	 odd+=n;
       }
    printf("Sum of odd numbers = %d\n",odd);
    printf("Sum of even numbers = %d\n",even);
    getch();
    return 0;
    }
    
    source : cprogrammingexpert.com
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice