write a programe to find the sum of all the even numbers and odd numbers within 1to100
Answers
Answered by
0
Answer:
write a programe to find the sum of all the even numbers and odd numbers within 1to100
Attachments:
Answered by
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
Computer Science,
5 months ago
Computer Science,
5 months ago
Social Sciences,
5 months ago
Math,
10 months ago
Science,
10 months ago
Psychology,
1 year ago
Physics,
1 year ago
Math,
1 year ago