Write a code Find the sum of odd numbers and sum of even numbers in a given range - C PROGRAMMING LANGUAGE @

C PROGRAMMING LANGUAGE @

LEARN TODAY LEAD TOMORROW

Saturday, July 15, 2017

Write a code Find the sum of odd numbers and sum of even numbers in a given range

//Write a code Find the sum of odd numbers and sum of even numbers in a given range

                                                                     

Algorithm:
procedure()
sum=0;
for i=1 to n;
sum=sum+(i*i*i);
end procedure;
source code:
#include<stdio.h>
int main()
{
int i,n,sum=0,sum1=0;
printf("Enter the number: ");
scanf("%d",&n); //assign the value of n is 5
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sum=sum+i;
}
else
sum1=sum1+i;
}
printf("Sum of even number is %d.\n",sum);
printf("Sum of odd number is %d.\n",sum1);
}
output:
Sum of even number is 6
Sum of odd number is 9
                                                                       




No comments:

Post a Comment