Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How to stop the loop when user enter negative number?

1. ## How to stop the loop when user enter negative number?

here's the question :

Write a program that reads a positive -digit integer and prints the word equivalent for it.

For example if the user enter 6, your program should print You have entered six. The program should execute repeatedly until the user enters a negative digit.

Use user defined function in this program.

and this is my coding :

#include <stdio.h>
void digit(int);

void main(){
int number,i=0;
while ( i==0 ){
printf("Enter positive number in range [1-9]: ");
scanf("%d", &number);
if (number >= 0 && number <=9)
digit(number);
else if ( !(number >= 0 && number <=9) )
printf("Sorry invalid number: Try again\n");
}
}

void digit(int number){
if (number == 1)
printf("\nYou entered the number One\n");
else if (number == 2)
printf("\nYou entered the number Two\n");
else if (number == 3)
printf("\nYou entered the number Three\n");
else if (number == 4)
printf("\nYou entered the number Four\n");
else if (number == 5)
printf("\nYou entered the number Five\n");
else if (number == 6)
printf("\nYou entered the number Six\n");
else if (number == 7)
printf("\nYou entered the number Seven\n");
else if (number == 8)
printf("\nYou entered the number Eight\n");
else if (number == 9)
printf("\nYou entered the number Nine\n");

2. Code:
```if(number< 0){
printf("Exiting....");
break;
}
else if (number >= 0 && number <=9)
digit(number);
else if ( !(number >= 0 && number <=9) )
printf("Sorry invalid number: Try again\n");```

3. ## Users who have thanked abduraooft for this post:

calestia (12-21-2011)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•