Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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");
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • 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
    •