Wednesday 15 January 2014

C program to find sum of odd numbers and even numbers from 1 to N

21:49
/* Write a C program to find the sum of odd numbers and even numbers from 1 to N */


#include
#include

void main ()
{
                 int i, N, oddsum=0, evensum=0;
                clrscr();
                           printf("enter the value for N \n");
                           scanf("%d", &N);

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

printf("Sum of all odd numbers is = "%d\n ", oddsum);

printf("sum of all even numbers is = "%d\n", evensum);

}


/* sample output */


enter the value of N
10
sum of all odd numbers in = 25
sum of all even numbers is = 30

0 comments:

Post a Comment