Write a program that prompts the user for a collection of integers and displays the product of those integers. Your program should repeatedly prompt for, read data, ignore any negative numbers and terminate when a zero value is read.
when I enter number 0 the result is = 1 and when i enter negative number the result is = 1
Can someone tell me if my code is correct for ignore any negative numbers and terminate when a zero value is read.
#include <iostream>
using namespace std;
int main() {
int Num;
double product = 1.0;
int integerNumber=1;
int countNumber = 0;
cout << "Please enter the number of integer = ";
cin >> Num;
while (Num>countNumber)
{
cout << "Please enter the integers = ";
cin >> integerNumber;
if (integerNumber == 0) {
break;
}
else if (integerNumber >= 1) {
product = product * integerNumber;
countNumber = countNumber + 1;
}
else if (integerNumber <=-1)
{
integerNumber = integerNumber * -1;
}
}
cout << " the product number of all integers is = " << product << endl;
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire