Write number from 0 to 10000 in c++ language

Asked

Viewed 37 times

-4

I need help to write numbers in full from 0 to 10000. I could only write up to 1500, as you can see below:

#include <stdio. h>

int main () {

float a; 
printf ("Digite um numero de 1 a 1500:\n");
scanf ("%f", &a); 
                             
if (a>1500) 
   printf ("O numero deve ser menor do que 1500\n");

else 
     {             
     if (a>=1000)
        {
        printf ("mil ");            
        a=a-1000;      
        }                         
         
     if (a>=100)            
        if (a>=900)
           {                       
           printf ("novecentos ");               
           a=a-900;               
           }
                 
        else if (a>=800)               
           {
           printf ("oitocentos ");               
           a=a-800;               
           }   
           
        else if (a>=700)               
           {               
           printf ("setecentos ");               
           a=a-700;               
           }
           
        else if (a>=600)               
           {               
           printf ("seiscentos ");                
           a=a-600;              
           }
                                
        else if (a>=500)
           {               
           printf ("quinhentos ");                 
           a=a-500;               
           }
           
        else if (a>=400)
           {               
           printf ("quatrocentos ");               
           a=a-400;               
           }   
           
        else if (a>=300)
           {               
           printf ("trezentos ");               
           a=a-300;               
           }
           
        else if (a>=200)
           {               
           printf ("duzentos ");               
           a=a-200;               
           }
                                                       
        else if (a>=100)
           {               
           printf ("cem ");               
           a=a-100;               
           }      
     
      if (a>=10)
        
        if (a>=90)
           {                       
           printf ("noventa ");               
           a=a-90;               
           }
                 
        else if (a>=80)               
           {      
           printf ("oitenta ");               
           a=a-80;               
           }   
           
        else if (a>=70)               
           {               
           printf ("setenta ");               
           a=a-70;                        
           }
           
        else if (a>=60)               
           {               
           printf ("sessenta ");                
           a=a-60;              
           }
                                
        else if (a>=50)
           {               
           printf ("cinquenta ");                
           a=a-50;               
           }
           
        else if (a>=40)
           {               
           printf ("quarenta ");               
           a=a-40;
           }   
           
        else if (a>=30)
           {               
           printf ("trinta ");               
           a=a-30;               
           }
           
        else if (a>=20)
           {               
           printf ("vinte ");               
           a=a-20;               
           }
                                                       
        else if (a>=10)
           {               
           printf ("dez ");               
           a=a-10;               
           }                   
           
      if (a>=1)
        
        if (a>=9)
           {                       
           printf ("nove ");               
           a=a-9;               
           }
                 
        else if (a>=8)               
           {      
           printf ("oito ");               
           a=a-8;               
           }   
           
        else if (a>=7)               
           {               
           printf ("sete ");               
           a=a-7;                        
           }
           
        else if (a>=6)               
           {               
           printf ("seis ");                
           a=a-6;              
           }
                                
        else if (a>=5)
           {               
           printf ("cinco ");                
           a=a-5;               
           }
           
        else if (a>=4)
           {               
           printf ("quatro ");               
           a=a-4;
           }   
           
        else if (a>=3)
           {               
           printf ("tres ");               
           a=a-3;               
           }
           
        else if (a>=2)
           {               
           printf ("dois ");               
           a=a-2;               
           }
                                                       
        else if (a>=1)
           {               
           printf ("um ");               
           a=a-1;               
           }     
        
       if (a>0,00)
                                   
          if (a>=0,90)
             {                       
             printf ("noventa ");               
             a=a-0,90;               
             }
          
          else if (a>=0,80)
             {                       
             printf ("oitenta ");               
             a=a-0,80;               
             }
          
          else if (a>=0,70)
             {                       
             printf ("setenta ");               
             a=a-0,70;               
             }
             
          else if (a>=0,60)
             {                       
             printf ("sessenta ");               
             a=a-0,60;               
             }
          
          else if (a>=0,50)
             {                       
             printf ("cinquenta ");               
             a=a-0,50;               
             }
          
          else if (a>=0,40)
             {                       
             printf ("quarenta ");               
             a=a-0,40;               
             }
          
          else if (a>=0,30)
             {                       
             printf ("trinta ");               
             a=a-0,30;               
             }
          
          else if (a>=0,20)
             {                       
             printf ("vinte ");               
             a=a-0,20;               
             }
          
          else if (a>=0,10)
             {                       
             printf ("dez ");               
             a=a-0,10;               
             }
                      
           
           
        }
     
fflush (stdin);
getchar ();
return 0;

}

  • Welcome to Stackoverflow. What have you tried? Once you answer, you can see your question and help yourself. If you can edit your question to include your attempt it would be very nice.

  • I found something in [tag:C] here on [en.so] see How to write a number in full in C Language?

1 answer

0

You’re using the wrong technique to create numbers in full.

You should first map the parts that make up a generic number in base 10 to the full text. This is done statically (vectors).

Only then do you take the number and break it into its constituent parts, in the case of base 10.

For example:

6523 = 6000 + 500 + 20 + 3 = 6 (thousands) + 5 (hundreds) + 20 (tens) + 3 units.

The mapping is trivial:

char unidades_str[] = {zero, um, dois, três, quatro, cinco, seis, sete, oito, nove};
char dezenas_str[] = {dez, vinte, trinta, quarenta, cinquenta, sessenta, setenta, oitenta, noventa};
//etc.

Now, take a number, for example: 23 (input) -> twenty-three (output)

Your show goes like this:

23 % 10 = 3 -> resto 3 (unidades)
23 / 10 = 2
2 % 10 = 2 -> resto 2 (dezenas)
2 / 10 = 0 -> pare!

See another number: 431

431 % 10 = 1 -> resto 1 (unidades)
430 / 10 = 43
43  % 10 = 3 -> resto 3 (dezenas)
43 / 10 = 4
4 % 10 = 4 -> resto 4 (centenas) 
4 / 10 = 0 -> pare!

In C:

int numero = get_number();  //tem que ser feita essa função
int itens = 0;

unidades = numero % 10;
itens++;
numero = numero / 10;
if(numero == 0) goto fim;
dezenas = numero % 10;
itens++;
numero = numero / 10;
if(numero == 0) goto fim;
centenas = numero % 10;
itens++;
numero = numero / 10;
if(numero == 0) goto fim;
//etc.
fim:
   switch(itens) {
       case 0: printf("Não é um número válido\n"); break;
       case 1: printf("%s\n", unidades_str[unidades]); break;
       case 2: printf("%s e %s\n", 
                          dezenas_str[dezenas],
                          unidades_str[unidades]); break;
       case 3: printf("%s e %s e %s\n",
                          centenas_str[centenas], 
                          dezenas_str[dezenas],
                          unidades_str[unidades]); break;
       //etc.
   }

Of course here only have the main ideas, but I think it is enough for you to finish your lesson!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.