terça-feira, 25 de agosto de 2015

Divisores de números em vetor

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 umas três linhas para modificar o último poste e transformá-lo neste aqui.

Veja abaixo imagens do programa em execução:






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 <= 31 ; lin++ )
         for ( col = 2; col <= 78 ; col++ ) {
              gotoxy ( col , lin );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int continuando ( ) {
     int nr;
     do {
         system ( "Color 90" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 28 , 3 );
         printf ( "DIVISORES DE NÚMEROS EM VETOR" );
         textcolor ( BROWN );
         gotoxy ( 25 , 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( GREEN );
         gotoxy ( 52 , 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 34 , 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23 , 11 );
         printf ( "Digite " );
         textcolor ( LIGHTRED );
         printf ( " 0 " );
         textcolor ( LIGHTBLUE );
         printf ( " para sair ou " );
         textcolor ( LIGHTRED );
         printf ( " 1 " );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar " );
         textcolor ( LIGHTRED );
         gotoxy ( 24 , 13 );
         scanf ( "%d" , &nr );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36 , 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( BLACK );
         gotoxy ( 36 , 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int Gera_Num_Sem_Repetidos ( int *A ) {
     int aut, i, j, result = 0, t = 100;
     for ( i = 0; i < 100 ; i++ ) {
         do {
              do {
                   t = rand ( ) % 100;
                   if ( t == 0 ) {
                        t = 100;
                   }
                   break;
              } while ( 1 );
              aut = 0;
              for ( j = 0; j < 100 ; j++ )
                   if ( t == A [ j ] )
                        aut = 1;
              if ( aut == 0 ) {
                   A [ i ] = t;
              }
         } while ( aut == 1 );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 28 , 3 );
     printf ( "DIVISORES DE NÚMEROS EM VETOR" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 21 , 5 );
     printf ( "Abaixo os 100 números gerados sem repetições\n" );
     textcolor ( BLACK );
     gotoxy ( 17 , 7 );
     for ( i = 0; i < 100 ; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 17 , 16 );
         }
         printf ( " %3d " , A [ i ] );
         result += A [ i ];
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 18 );
     Sleep ( 500 );
     printf ( "Total de números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d" , i );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 19 );
     Sleep ( 500 );
     printf ( "Soma dos números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d" , result );
     Sleep ( 500 );
     return ( 0 );
}
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 ( ) {
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     system ( "title DIVISORES DE NÚMEROS EM VETOR" );
     int *A, d, j, vezes = 0;
     char num [ 3 ];
     A = ( int* ) malloc ( tam * sizeof(int) );
     vezes = Gera_Num_Sem_Repetidos ( A );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 21 );
         printf ( "Digite um número entre 2 e 100 para sabermos" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 22 );
         printf ( "quais são seus divisores no vetor: " );
         textcolor ( LIGHTRED );
         gotoxy ( 20 , 36 );
         gets ( num );
         fflush ( stdin );
         j = atoi ( num );
         gotoxy ( 20 , 24 );
         textcolor ( LIGHTBLUE );
         if ( j < 2 || j > 100 ) {
              printf ( "\aSó são aceitos Números Acima de 2 e menor que 100" );
              getche ( );
              Sleep ( 1800 );
              gotoxy ( 15 , 24 );
              clreol ( );
              gotoxy ( 20 , 25 );
              clreol ( );
              gotoxy ( 53 , 22 );
              clreol ( );
         }
         if ( Divisores ( num ) == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 20 , 25 );
              printf ( "Você digitou " );
              textcolor ( LIGHTRED );
              printf ( "%s" , num );
              textcolor ( LIGHTBLUE );
              gotoxy ( 20 , 26 );
              printf ( "\aIsso não é um número válido !" );
              textcolor ( YELLOW );
              gotoxy ( 31 , 28 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 15 , 25 );
              clreol ( );
              gotoxy ( 20 , 26 );
              clreol ( );
              gotoxy ( 32 , 22 );
              clreol ( );
              gotoxy ( 31 , 28 );
              clreol ( );
         }
         textcolor ( LIGHTRED );
     } while ( ( j < 2 ) || ( j > 100 ) );
     for ( d = 1; d <= j ; d++ ) {
         if ( j % d == 0 )
              printf ( " %3d" , d );
     }
     textcolor ( YELLOW );
     gotoxy ( 36 , 30 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
}

Nenhum comentário:

Postar um comentário

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