...

View Full Version : How to stop the loop when user enter negative number?

calestia
12-21-2011, 06:13 AM
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");

abduraooft
12-21-2011, 06:45 AM
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");