153k views
0 votes
Write a function that asks the user for a positive integer with a loop. C

A) getPositiveInteger()
B) inputInteger()
C) requestPositiveInteger()
D) loopPositiveInteger()

User Shadros
by
7.8k points

1 Answer

6 votes

Final answer:

The correct answer is C) requestPositiveInteger(). It accurately describes the purpose of the function.

Step-by-step explanation:

The correct answer is C) requestPositiveInteger(). This function name accurately describes its purpose, which is to ask the user for a positive integer. Here is an example of how you can implement this function in C:

#include <stdio.h>

int requestPositiveInteger() {
int num;

do {
printf("Enter a positive integer: ");
scanf("%d", &num);
} while (num <= 0);

return num;
}

In this example, the function repeatedly prompts the user to enter an integer until a positive value is provided, by using a do-while loop. The function then returns the positive integer entered by the user.

User Zahed
by
7.9k points