63.3k views
3 votes
Write code that prompts the user to enter a positive nonzero number and validates the input

User Mirodil
by
5.6k points

2 Answers

6 votes


\huge \boxed{\sf Code}

#include <stdio.h>

int main() {

// Declare the variables

int a;

// Ask user to input a value greater than 0

do{

printf("Enter a positive non-zero number: ");

scanf("%d", &a);

}while(a <= 0);

// Return an integer value

return 0;

}


\Large \boxed{\sf Explanation}

Declare the variable as an integer type.

Prompt the user to input a positive non-zero number which is a number greater than 0.

Use the do-while loop, and if the variable is less or equals 0, then keep asking the user for input until it is greater than 0.

Return an integer value.

User Joseph Holsten
by
6.3k points
4 votes
Hello, the following should work. If it doesnt, let me know.


Module Main()

Declare Integer n

Display "Please enter a positive non zero positive number"

Input n

While n < = 0

Display " Invalid Input, Please enter a non zero positive number"

Input n

End While

END

Hope this helps.
User Eck
by
5.3k points