In: Computer Science
ks) Write a program that prompts a person to enter a positive integer value. If the value
cannot be converted into an integer or if the value is less than 1, keep prompting the user to
enter a value until they enter a valid integer
value. Determine if the value is odd or even and
display the result on the screen. Pause the screen until the user presses a key to continue.(c programming begineers method
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
#include <stdio.h>
#include <conio.h>
int main()
{
// declare a number as integer
int num;
while(1)
{
// prompt the user
printf("Enter a positive number: ");
// read the number entered by user
scanf("%d",&num);
// check if the number is less than 1
if(num<1)
printf("Invalid. Please try again..\n");
else
// here the number is more tham 1. so, stop repeating. break
break;
}
// check if num is even or odd
// if the number is odd, we will have remainder when divided by 2
is ZERO
// ie., num%2==0==> EVEN else, num is ODD
if(num%2==0)
printf("%d is Even Number.",num);
else
printf("%d is Odd Number.",num);
printf("\nPlease press any key to continue..");
getch();
return 0;
}
=================
SCREENSHOT: