Question

In: Computer Science

write in c plus plus Write a program that computes the sum of the odd numbers...

write in c plus plus

Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10

Solutions

Expert Solution

SOURCE CODE:

#include <iostream>
using namespace std;

int main() {
   int a,b; //declaring two integer variables a and b..
  
   cout<<"Enter the value of a= ";
   cin>>a; //taking input from user, the value of a..
   cout<<endl; //new line..
  
   cout<<"Enter the value of b= ";
   cin>>b; //taking input from user, the value of b..
   cout<<endl; //new line..
  
   int evenSum=0, oddSum=0;
   for(int i=a; i<=b; i++) //for loop is iterated from i=a to i=b with incrementation of 1 each time to get all numbers between a and b..
   {
   if(i%2==0) //if its remainder with 2 is 0.. it means it is even..hence taking all even numbers between a and b here..
   {
   evenSum=evenSum+i; //adding all even numbers and storing it back in evenSum..
   }
   else{ //if a number is not even it would be odd..hence taking all odd numbers between a and b here..
  
   oddSum=oddSum+i; ////adding all odd numbers and storing it back in oddSum..
   }
   }
  
   cout<<"Sum of the even numbers between "<<a<<" and "<<b<<" is "<<evenSum<<endl; //printing the sum of even numbers..
  
   cout<<"Sum of the odd numbers between "<<a<<" and "<<b<<" is "<<oddSum<<endl; //printing the sum of odd numbers..
  
   return 0;
}

OUTPUT:

NOTE: Since, Output of the test case was not shown and nothing was mentioned about whether we have to include a and b.

So, I have given the code which includes the value of a and b while calculating sum of odd and even numbers between them.

If you want the code which excludes the value of a and b while calculating sum of odd and even numbers between them. Please feel free to comment I will do the respective changes according to you.

Please verify in the comment section if this is what you want.

I have mentioned comments wherever necessary in the code. Please copy the entire code and save it with ".cpp" extension, then compile and run.

Hope it Helps..!!

Thanks..


Related Solutions

Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT