Monday, May 2, 2016

Sum Of Odd and Even Numbers From 1 to N

Write a C program to calculate the sum of odd numbers and sum of even numbers from 1 to N.


#include <stdio.h>
#include <stdlib.h>

int main () {
    int i , num , oddsum=0 , evensum=0 ;
    printf("Enter Integer Number :\n");
    scanf("%d",&num);

    for (i=0;i<=num;i++){
            if (i%2 == 0) {
                evensum=evensum+i;
            }
            else {
                oddsum=oddsum+i;
            }

    }
    printf("Sum Of All Odd Number is %d\nSum Of All Even Number is %d\n",oddsum,evensum);
    return 0;

}




Share this

0 Comment to "Sum Of Odd and Even Numbers From 1 to N"

Post a Comment