In: Computer Science
Write a C++ code that keeps reading integer numbers (x) until a negative number is entered. Every time x is entered, the program does the following: calculates and prints the product of odd numbers. calculates and prints the count of numbers that ends with 19. Example, 19, 3219, 50619,....etc calculates and prints the percentage of the numbers that contain 3 digits exactly, such as, 301, 500, 206,...etc.
Code :
#include <iostream>
using namespace std;
int main()
{
int n;
int c=0; // counter to count no of integres inputted by iser
int end19=0; // counter to count no of integers that ends with
19
int digits3=0; // counter to count no of integers that contains 3
digits
int prod=1; // varaible to calculate product of odd numbers
while (1)
{
cout << "Enter an integer: ";
cin >> n;
cout << n<<"\n";
c=c+1;
if ( n <0) // If no id negative then we will come out of
loop
break ;
if (n%2!=0) // If the no is odd
prod=prod*n; // It will calculate the product
if (n > 99 && n <1000) // If the no has exactly 3
digits
++digits3 ; // It will increment the counter digits3
if (n%100 ==19) // If the number ends with 19
++end19; // It will increment the counter end19
cout << "Product of odd integere: " << prod
<<"\n";
cout <<"percentage of numbers that ends with 19: "<<
(end19*100.0)/c <<"%\n";
cout <<"percentage of numbers that contains 3 digits:
"<< (digits3*100.0)/c <<"%\n";
}
return 0;
}
I am adding the screenshot of some outputs: