198k views
1 vote
Re-type the code and fix any errors. The code should convert non-positive numbers to 1.

if (userNum > 0)
cout << "Positive." << endl;
elsecout << "Not positive, converting to 1." << endl;
userNum = 1;
cout << "Final: " << userNum << endl;
answer:
#include
using namespace std;
int main() {
int userNum;
cin >> userNum;
/* Your solution goes here */
return 0;
}

User Amarillion
by
5.3k points

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int userNum;

cout<<"Enter a number"<<endl;

cin >> userNum;

if (userNum > 0){

cout << "Positive." << endl;

}

else{

cout << "Not positive, converting to 1." << endl;

userNum=1;

}

cout<<"Final: "<<userNum<<endl;

return 0;

}

Step-by-step explanation:

The code as implemented above will prompt the user to enter a number, read and save the number in a variable (userNum)

Using if/else statements, if the number is greater than 0 (i.e positive),it outputs Positive

else it assigns a new value of 1 to the number and outputs the final value