Computer Science, asked by swarupsaha0721, 11 months ago

write a programe to find the sum of all the even numbers and odd numbers within 1to100​

Answers

Answered by manan948665
0

Answer:

write a programe to find the sum of all the even numbers and odd numbers within 1to100

Attachments:
Answered by vidit05gupta
1

Explanation:

  • Take 3 variables, one for loop for iteration, one for sum of odd numbers and one for sum of even numbers.
  • Start a loop from 0 to 100.
  • Inside the loop, check if the number is even or odd and add the numbers in the declared variables.
  • Finally, print the final values.

Program:

#include <iostream>

using namespace std;

int main()

{

   int sumEven = 0; // variable to sum even numbers

   int sumOdd = 0; // variable to sum odd numbers

   int i; // variable to iterate the loop

   for(i = 0; i <= 100; i++)

   {

       if(i % 2 == 0) // to check if number is even

           sumEven = sumEven + i;

       else

           sumOdd = sumOdd + i;

   }

   cout << "Sum of the even numbers = " << sumEven << endl;

   cout << "Sum of the odd numbers = " << sumOdd << endl;

   return 0;

}

#answerwithquality

#BAL

Attachments:
Similar questions