Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-21-2011, 06:13 AM   PM User | #1
calestia
New to the CF scene

 
Join Date: Dec 2011
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
calestia is an unknown quantity at this point
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");
calestia is offline   Reply With Quote
Old 12-21-2011, 06:45 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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");
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
calestia (12-21-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:55 PM.


Advertisement
Log in to turn off these ads.