Home > Net >  When the input is bigger than 9999, I need the output to be "entered number is invalid"
When the input is bigger than 9999, I need the output to be "entered number is invalid"

Time:10-04

I have a problem with my output when the input is bigger than 9999. I need the out to be "entered number is invalid".

#include <stdio.h> 

main() 
{   
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);      
{
    

    if(num<0 || num>9999)
    {  printf("entered number is not valid\n");}
    
    
    //four digits
    if (num >= 9000)       
    {
    printf("nine thousand ");
    num -= 9000;}
    
    else if (num >= 8000)       
    {
    printf("eight thousand ");
    num -= 8000;}
    
    else if (num >= 7000)       
    {
    printf("seven thousand ");
    num -= 7000;}
    
    else if (num >= 6000)       
    {
    printf("six thousand ");
    num -= 6000;}
    
    else if (num >= 5000)       
    {
    printf("five thousand ");
    num -= 5000;}
    
    else if (num >= 4000)       
    {
    printf("four thousand ");
    num -= 4000;}
    
    else if (num >= 3000)       
    {
    printf("three thousand ");
    num -= 3000;}
    
    else if (num >= 2000)       
    {
    printf("two thousand ");
    num -= 2000;}
    
    
    else if (num >= 1000)       
    {
    printf("one thousand ");
    num -= 1000;}
     
     //three digits
    if (num >= 900)   
    { printf("nine hundred ");
    num -= 900; }
    
    if (num >= 800)   
    { printf("eight hundred ");
    num -= 800;}
    
    if (num >= 700)   
    { printf("seven hundred ");
    num -= 700;}
    
    if (num >= 600)   
    { printf("six hundred ");
    num -= 600;}
    
    if (num >= 500)   
    { printf("five hundred ");
    num -= 500;}
    
     if (num >= 400)   
    { printf("four hundred ");
    num -= 400;}
     
     if (num >= 300)   
    { printf("three hundred ");
    num -= 300;}
    
     if (num >= 200)   
    { printf("twohundred ");
    num -= 200;}
     
    if (num >= 100)   
    { printf("one hundred ");
    num -= 100;}
    
    //two digits
    
    if (num >= 90)   
    { printf("ninety ");
    num -= 90;}
    
    
    if (num >= 80)
    { printf("eighty ");
    num -= 80;}
    
    if (num >= 70)
    { printf("seventy ");
    num -= 70;}
    
    if (num >= 60)
    { printf("sixty ");
    num -= 60;}
    
    if (num >= 50)
    { printf("fifty ");
    num -= 50;}
    
    if (num >= 40)
    { printf("fourty ");
    num -= 40;}
    
    if (num >= 30)
    { printf("thirty ");
    num -= 30;}
    
    if (num >= 20)
    { printf("twenty ");
    num -= 20;}
    
//10-19 numbers
    if (num >= 19)
    { printf("nineteen ");
    num -= 19;}
    
    if (num >= 18)
    { printf("eighteen ");
    num -= 18;}
    
    if (num >= 17)
    { printf("seventeen ");
    num -= 17;}

    if (num >=16 )
    { printf("sixteen ");
    num -= 16;}
    
    if (num >=15 )
    { printf("fifteen ");
    num -= 15;}
    
    if (num >=14 )
    { printf("fourteen ");
    num -= 14;}
    
    if (num >=13 )
    { printf("thirteen ");
    num -= 13;}
    
    if (num >=12 )
    { printf("twelve ");
    num -= 12;}
    
    if (num >=11 )
    { printf("eleven ");
    num -= 11;}
    
    if (num >=10 )
    { printf("ten ");
    num -= 10;}
// one digit
    if (num >=9 )
    { printf("nine ");
    num -= 9 ;}
    
    if (num >= 8)   
    {
    printf("eight ");
    num -= 8;}
    
    if (num >= 7)
    { printf("seven ");
    num -= 7;}
    
    if (num >=6 )
    { printf("six ");
    num -= 6;}
    
    if (num >= 5)
    { printf("five ");
    num -= 5;}
    
    if (num >=4 )
    { printf("four");
    num -= 4;}
    
    if (num >= 3)
    { printf("three ");
    num -= 3;}
    
    if (num >=2 )
    { printf("two ");
    num -= 2;}
    
    if (num >= 1)
    { printf("one ");
    num -=1 ;}
    
    
 
}
}
                               
        

//Input: 99999

Output: entered number is not valid nine thousand nine hundred eight hundred seven hundred six hundred five hundred four hundred three hundred twohundred one hundred ninety eighty seventy sixty fifty fourty thirty twenty nineteen eighteen seventeen sixteen fifteen fourteen thirteen twelve eleven ten nine eight seven six five fourthree two one

Target output: entered number is not valid

CodePudding user response:

  1. Use the correct main definition.
  2. Always check the scanf return value
  3. You need to skip printing if scanf was not cusscessfull or number is < 0 or > 9999.
int main(void) 
{   
    int num;

    printf("Enter a number: ");
    if(scanf("%d", &num) != 1 || num<0 || num>9999)
    {
        printf("entered number is not valid\n"); 
        return 1;
    }
    /* .... */
  •  Tags:  
  • c
  • Related