-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.S.P0037.c
More file actions
88 lines (75 loc) · 2 KB
/
C.S.P0037.c
File metadata and controls
88 lines (75 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
* Function 1
* Display a screen to ask users to input a positive natural number
*/
void getUserInput(int *userInput) {
short isValid;
printf("Check Square Number Program\n");
while (1) {
printf("Enter a positive integer n = ");
isValid = scanf("%d", &(*userInput));
fflush(stdin);
// check input validity
// if user didn't enter a positive integer (natural number)
// then throw an alert and force them to enter again
if (isValid != 1 || *userInput < 0)
printf("Invalid input. Making sure that you entered a positive integer.\n");
else
break;
}
}
/*
* Function 2
* Check if the inputted number is square number
* If it is, then return its square root of 2
* otherwise return -1
*/
int isSquareNum(int userInput) {
int squareRoot = sqrt(userInput);
if (pow(squareRoot, 2) == userInput)
return squareRoot;
else
return -1;
}
/*
* Function 3
* Output the result to the screen
*/
void showResult(int userInput) {
if (isSquareNum(userInput) == -1)
printf("%d is not a square number\n", userInput);
else
printf("%d is a square number\n", userInput);
}
/*
* Function 4
* Ask user either to rerun this program or not
*/
void askContinue() {
char userChoice;
while (1) {
printf("Press any key then enter to continue.");
userChoice = getchar();
fflush(stdin);
if (userChoice != 0) {
fflush(stdin);
printf("\n");
main();
}
else
printf("Invalid input. Please try again\n");
}
}
int main() {
int userInput;
// prompt user to enter a positive integer, while check its validity
getUserInput(&userInput);
// show the result, either user input is a square number or not
showResult(userInput);
// to ask user to rerun program or not
askContinue();
return (EXIT_SUCCESS);
}