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 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:
    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