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
Multiples of 2 and 3: write a c++ program Using a while loop, write a program...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
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.
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 program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
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.
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