what are even numbers?
Indeed, even numbers can be separated equitably into gatherings of two. The number four can be isolated into two gatherings of two.
Indeed, even numbers can be separated equitably into gatherings of two. The number four can be isolated into two gatherings of two.
what are odd numbers?
Odd numbers can NOT be separated equally into gatherings of two. The number five can be separated into two gatherings of two and one gathering of one.
Indeed, even numbers dependably end with a digit of 0, 2, 4, 6 or 8.
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 are even numbers.
Odd numbers dependably end with a digit of 1, 3, 5, 7, or 9.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 are odd numbers.
/*a cpp program to comput even and odd numbers * Whole the odd and even numbers, individually, from 1 to a given upperbound. * Also compute the absolute difference. */ #include <iostream> // Needed to use IO functions using namespace std; int main() { int sumOdd = 0; // For gathering odd numbers, init to 0 int sumEven = 0; // For gathering even numbers, init to 0 int upperbound; // Total from 1 to this upperbound int absDiff; // The total distinction between the two aggregates //Provoke client for an upperbound cout << "Enter the upperbound: "; cin >> upperbound; // Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound int number = 1; while (number <= upperbound) { if (number % 2 == 0) { // Even number sumEven += number; // Add number into sumEven } else { // Odd number sumOdd += number; // Add number into sumOdd } ++number; // increment number by 1 } // Compute the difference between the two sums if (sumOdd > sumEven) { absDiff = sumOdd - sumEven; } else { absDiff = sumEven - sumOdd; } // Print results cout << "The sum of odd numbers is " << sumOdd << endl; cout << "The sum of even numbers is " << sumEven << endl; cout << "The absolute difference is " << absDiff << endl; return 0; }
0 comments:
Post a Comment