Question

In: Computer Science

Write a program in C++ that prints out the even numbers between 1 and 21 using...

Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop.

Also, find the sum AND product of these numbers and display the resulting sum and product.

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()
{int sum=0; //variable to store sum of even numbers
long product=1;//variable to store product of even numbers
int i=1; //loop variable starts from 1
while(i<21) //run the loop till i=21
{if(i%2==0){ //if number is divisible by 2 i.e even number
cout <<i<< endl; //print even number
sum=sum+i; //calculate the sum
product=product*i; //calculate the product
i++; //increement the loop variable
}
else //if number is odd,increement the loop variable
i++;
}
  
cout <<"Sum of even numbers between 1 and 21 : "<<sum<< endl;
cout <<"Product of even numbers between 1 and 21 : "<<product<< endl;

return 0;
}

/********OUTPUT*********
2   
4   
6   
8   
10
12
14
16
18
20
Sum of even numbers between 1 and 21 : 110
Product of even numbers between 1 and 21 : 3715891200
********OUTPUT************/
/* Note:Please get back in case of any doubt,Thanks */


Related Solutions

Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a program that prints out all numbers, less than or equal to 10,000,000, that are...
Write a program that prints out all numbers, less than or equal to 10,000,000, that are evenly divisible by all numbers between 5 and 15 (inclusive). Do not use any libraries besides stdio.h. Need it in 10 minutes, please.
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the while loop. question2: Write a C++ program that prints all numbers between 10 to 1000 that are divisible by 5
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Write a program that prints the count of all prime numbers between A and B (inclusive),...
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7,...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Please explain answer so I can understand. 1. Write a C program that prints out its...
Please explain answer so I can understand. 1. Write a C program that prints out its command line arguments, one to a line. Discussion: A C program starts at a function called main. main has two arguments, conventionally named argc and argv. argc is of type int; it’s the argument count, an integer that contains the number of command line arguments (including the program name). argv is the argument vector; it’s an array of pointers to characters, In C this...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT