C++ Code To Print The Sum Of Even And Odd Numbers Between Any Two Numbers::
=================================================================#include <stdio.h>
int main()
{
int firstnum, secondnum, sumOfEvenNumbers = 0, sumOfOddNumbers = 0,i;
do
{
printf("Enter first number: ");
scanf("%d", &firstnum);
printf("Enter second number: ");
scanf("%d", &secondnum);
if(firstnum >= secondnum)
printf("Error: first number must be less than the second number\n");
} while(firstnum >= secondnum);
for( i = firstnum; i <= secondnum; i++)
{
if(i % 2 == 0)
sumOfEvenNumbers += i;
else
sumOfOddNumbers += i;
}
printf("sum of even numbers from %d to %d = %d\n", firstnum, secondnum, sumOfEvenNumbers);
printf("sum of odd numbers from %d to %d = %d\n", firstnum, secondnum, sumOfOddNumbers);
return 0;
}
No comments:
Post a Comment