Question

In: Computer Science

2.c++ if and loop statement Write a program that will count the number of even number...

2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers.

Sample outputs:

Enter starting number:3 Enter starting number:4

Enter ending number:10 Enter ending number:10

odd numbers Even number

3 4

5 6

7 8

9 10

number of even numbers=4 number of even numbers=4

sum=24 sum=28

average=6 average=7

Even number odd number

4 5

6 7

8 9

10

Number of even number=4 Number of even number=3

sum=28 sum=21

average=7 average=7

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main() {

int num1,num2,count=0,count_odd=0,count_even=0,even_sum=0,odd_sum=0;

cout<<"Enter starting number : ";

cin>>num1;

cout<<"Enter ending number : ";

cin>>num2;

if(num1%2==0)

cout<<"Even numbers odd numbers\n";

else

cout<<"Odd numbers even numbers\n";

for(int i=num1;i<=num2;i++)

{

if(i%2==0)

{

count_even++;

even_sum+=i;

cout<<i<<" ";

count++;

}

else

{

count_odd++;

odd_sum+=i;

cout<<i<<" ";

count++;

}

if(count==2)

{

count=0;

cout<<endl;

}

}

if(num1%2==0)

{

cout<<"\nNumber of even numbers="<<count_even<<" Number of odd numbers="<<count_odd<<endl;

cout<<"sum="<<even_sum<<" sum="<<odd_sum<<endl;

cout<<"average="<<even_sum/count_even<<" average="<<odd_sum/count_odd;

}

else

{

cout<<"\nNumber of odd numbers="<<count_odd<<" Number of even numbers="<<count_even<<endl;

cout<<"sum="<<odd_sum<<" sum="<<even_sum<<endl;

cout<<"average="<<odd_sum/count_odd<<" average="<<even_sum/count_even;

}

}


Related Solutions

2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
Lab-3C   Pre-test Loop Write a MIPS program to count the number of students who failed in...
Lab-3C   Pre-test Loop Write a MIPS program to count the number of students who failed in a course. The final grades are given in an array. The fail grade is 60. Example: Array and Output      .data NoOfStudents: 10 Values:   60 70 80 50 90 80 55 90 80 70 Output: 2 students failed      .data NoOfStudents: 12 Values:   60 70 80 50 90 80 55 90 80 70 55 80 Output: 3 students failed
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples...
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples of 2 and does the loop until count is less than 50.
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
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.
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write a program that prompts the user for an even number from 2 to 100 until...
Write a program that prompts the user for an even number from 2 to 100 until the number 90 is encountered. Not including the 90, calculate the minimum value. In case you know what this means: DO NOT USE LISTS! We will look into the use of lists later. This has to be done in the python program. Here's what I have so far: inp = 0 min = 0 while inp != 90:     inp = int(input("Please enter an even...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT