sexta-feira, 25 de setembro de 2015

Divisores de números em vetor II


Exceto os números primos, todos os demais possuem múltiplos e divisores.
Um número é divisor de outro quando o resto da divisão for igual a 0. Portanto,
12 é divisível por 1, 2, 3, 4, 6 e 12.
36 é divisível por 1, 2, 3, 4, 6, 9, 12, 18 e 36.
48 é divisível por 1, 2, 3, 4, 6, 8, 12, 24 e 48.

Observações importantes:

1 - O menor divisor natural de um número é sempre o número 1.
2 - O maior divisor de um número é o próprio número.
3 - O zero não é divisor de nenhum número.
4 - Os divisores de um número formam um conjunto finito.

Alguns números têm apenas dois divisores: o 1 e ele mesmo.
Esses números são chamados de primos.
Mais informações sobre o assunto acesse o seguinte link abaixo:

http://www.mundoeducacao.com/matematica/multiplos-divisores.htm

De posse destas informações criei este código, onde na verdade já estava 97 por cento criado, apenas editei algumas linhas para modificar o outro post e transformá-lo neste aqui,
onde considero mais avançado, neste as impressões dos divisores são marcados no próprio vetor,
em côr bem destacada, na tabela.
Veja o vídeo abaixo:




Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 100
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "Color 10" );
     for ( lin = 2; lin <= 24 ; lin++ )
         for ( col = 2; col <= 78 ; col++ ) {
              gotoxy ( col , lin );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int Divisores ( char num [ 4 ] ) {
     unsigned int i;
     for ( i = 0; i < strlen ( num ) ; i++ ) {
         if ( num [ i ] < '0' || num [ i ] > '9' )
              return 0;
     }
     return 1;
}
int main ( ) {
     system ( "cls" );
     Janela5 ( );
     system ( "title DIVISORES DE NÚMEROS EM VETOR II" );
     int A [ 100 ], i, j;
     char num [ 3 ];
     textcolor ( LIGHTRED );
     gotoxy ( 26 , 3 );
     printf ( "DIVISORES DE NÚMEROS EM VETOR II" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 21 , 5 );
     printf ( "Tabela com os " );
     textcolor ( BLACK );
     printf ( "100 " );
     textcolor ( LIGHTBLUE );
     printf ( "primeiros números naturais" );
     gotoxy ( 17 , 7 );
     textcolor ( LIGHTBLUE );
     for ( i = 1; i <= tam ; i++ ) {
         A [ i ] = i;
         if ( i == 11 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 21 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 31 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 41 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 51 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 61 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 71 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 81 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 91 ) {
              gotoxy ( 17 , 16 );
         }
         printf ( " %3d " , A [ i ] );
     }
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 19 , 18 );
         printf ( "Digite um número entre " );
         textcolor ( BLACK );
         printf ( "2 " );
         textcolor ( LIGHTBLUE );
         printf ( "e " );
         textcolor ( BLACK );
         printf ( "100 " );
         textcolor ( LIGHTBLUE );
         gotoxy ( 19 , 19 );
         printf ( "e mostraremos seus divisores: " );
         textcolor ( BLACK );
         gets ( num );
         fflush ( stdin );
         j = atoi ( num );
         gotoxy ( 19 , 21 );
         textcolor ( LIGHTBLUE );
         if ( j < 2 || j > 100 ) {
              printf ( "\aNúmeros acima de " );
              textcolor ( BLACK );
              printf ( "2 " );
              textcolor ( LIGHTBLUE );
              printf ( "e menores que " );
              textcolor ( BLACK );
              printf ( "100 " );
              Sleep ( 2800 );
              gotoxy ( 19 , 21 );
              clreol ( );
              gotoxy ( 48 , 19 );
              clreol ( );
         }
         if ( Divisores ( num ) == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 19 , 21 );
              printf ( "Você digitou " );
              textcolor ( BLACK );
              printf ( "%s" , num );
              textcolor ( LIGHTBLUE );
              gotoxy ( 19 , 22 );
              printf ( "\aIsso não é um número válido" );
              Sleep ( 2800 );
              gotoxy ( 19 , 21 );
              clreol ( );
              gotoxy ( 19 , 22 );
              clreol ( );
         }
         textcolor ( LIGHTRED );
     } while ( ( j < 2 ) || ( j > 100 ) );
     gotoxy ( 17 , 7 );
     for ( i = 1; i <= tam ; i++ ) {
         if ( i == 11 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 21 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 31 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 41 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 51 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 61 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 71 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 81 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 91 ) {
              gotoxy ( 17 , 16 );
         }
         textcolor ( LIGHTRED );
         if ( j % i == 0 ) {
              textcolor ( LIGHTRED );
              printf ( " %3d " , A [ i ] );
         } else {
              textcolor ( BLACK );
              printf ( " %3d " , A [ i ] );
         }
     }
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
}


Me siga no facebook:

https://www.facebook.com/profile.php?id=100004632880855

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.