sexta-feira, 11 de janeiro de 2013

Programa Números Pares e Ímpares


Este Programa imprime uma Matriz de 100 Elementos, que conta de 1 a 100, e mostra a soma destes elementos, depois imprime a quantidade de Pares e de Ímpares contidos; Mostra ainda a porcentagem de pares e de ímpares. Na tela seguinte imprime todos os números pares, a soma destes números pares, o maior e o menor número par.
 Na tela última tela imprime todos os números ímpares, a soma destes números ímpares, o maior e o menor número ímpar.
Excelente código para auxiliar os iniciantes em Linguagem C.

Veja algumas imagens do programa abaixo:



Veja abaixo este excelente código:

#include <conio.h>
#include <stdio.h>
int pr [10] [10] =
{{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 },
{ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 },
{ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 },
{ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 },
{ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70 },
{ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 },
{ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 },
{ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 }};
int inicioIntervalo = pr [0] [0], fimIntervalo = pr [9] [9];
int par [100];int impar [100];
void Porcentagem_Pares_Impares(){
int f, g, i = 0, j = 0, ip, Pr;
for (g = 0; g < 10; g++){
for (f = 0; f < 10; f++){
if (pr [g] [f] % 2 == 0) par [i++] = pr [g] [f];
else
impar [j++] = pr [g] [f];
}
}
for (f = 0; f < i; f++){
Pr = par [i] / pr [g] [f] * 100;
}
for (f = 0; f < i; f++){
ip = impar [j] / pr [g] [f] * 100;
}
Pr = f * 100 / 100;
ip = j * 100 / 100;
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,11);printf("A porcentagem dos numeros pares e: ");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(45,11);printf(" %d ", Pr);
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,13);printf("A porcentagem dos numeros impares e: ");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(45,13);printf(" %d \n",  ip);
getche();
}
void Numeros_Pares_Impares(){
system ("cls");
textcolor(LIGHTRED);gotoxy(28,3);printf("PROGRAMA NUMEROS PARES E IMPARES");
textcolor(LIGHTGRAY);gotoxy(24,5);printf("IMPRIMINDO A QUANTIDADE DE PARES E IMPARES");
int f, g, i = 0, j = 0, ip, Pr;
for (g = 0; g < 10; g++){
for (f = 0; f < 10; f++){
if ( pr [g] [f] % 2 == 0) par [i++] = pr [g] [f];
else
impar [j++] = pr [g] [f];
}
}
for (f = 0; f < i; f++){
Pr = par [i] / pr [g] [f] * 100;
}
for (f = 0; f < i; f++){
ip = impar [j] / pr [g] [f] * 100;
}
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,7);printf("O numero de elementos Pares e: ");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(43,7);printf("%d", f);
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,9);printf("O numero de elementos Impares e: ");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(43,9);printf("%d", j);
Sleep(800);
Porcentagem_Pares_Impares();
}
void Exibe_Matriz(){
int a = 0, i = 0, j = 0, result = 0;
system ("title PROGRAMA NUMEROS PARES E IMPARES");
textcolor(LIGHTRED);gotoxy(28,3);printf("PROGRAMA NUMEROS PARES E IMPARES");
textcolor(LIGHTBLUE);gotoxy(30,5);printf("IMPRIMINDO A MATRIZ ORIGINAL");
textcolor(YELLOW);printf("\n");
for(i = 0; i < 10; i++){

gotoxy(23, i + 8);
for(j = 0; j < 10; j++){
par [a] = pr [i] [j];
printf(" %d ", pr [i] [j] );
result += par  [a];
}
}
textcolor(LIGHTBLUE);gotoxy(24,20);printf("A SOMA DOS ELEMENTOS DA MATRIZ E: ");
textcolor(LIGHTMAGENTA);gotoxy(58,20);printf(" %d ", result);
textcolor(LIGHTGRAY);gotoxy(30,23);printf("PRESSIONE QUALQUER TECLA");
getche();
}
void Imprime_Pares_Maior_Menor(){
system ("cls");
int f, g, i = 0, j = 0, m = 0, soma_pares = 0;
textcolor(LIGHTRED);gotoxy(28,3);printf("PROGRAMA NUMEROS PARES E IMPARES");
textcolor(LIGHTBLUE);gotoxy(32,5);printf("IMPRIMINDO OS NUMEROS PARES");
textcolor(DARKGRAY);gotoxy(34,7);printf("Numeros pares da Matriz ");
gotoxy(2,9);
for (g = 0; g < 10; g++){
for (f = 0; f < 10; f++){
if (pr [g] [f] % 2 == 0) par [i++] = pr [g] [f];
else
impar [j++] = pr [g] [f];
}
}
for (f = 0; f < i; f++){
textcolor(LIGHTMAGENTA);printf(" %d ", par [f]);
}
Sleep(800);
textcolor(LIGHTBLUE);gotoxy(8,13);printf ( "Esta e a soma dos Pares da Matriz: " );
gotoxy(41,13);textcolor(LIGHTMAGENTA);Sleep(800);
for ( f = 0; f < i; f++ ){
soma_pares += par [f];
}
printf ( "  %d ",  soma_pares);
f = 0;
m = par [f];
g = par [f];
for ( f = 0; f < i; f++ ){
if ( par [f] > m ){
m = par [f];
}
if ( par [f] < g ){
g = par [f];
}
}
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,15);printf("O maior Numero par e:");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,15);printf(" %d ", m);
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,17);printf("O menor Numero par e:");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,17);printf(" %d ", g);
getche();
}
void Imprime_Ipares_Maior_Menor(){
system ("cls");
int f, g, i = 0, j = 0, m = 0, soma_pares = 0;
textcolor(LIGHTRED);gotoxy(28,3);printf("PROGRAMA NUMEROS PARES E IMPARES");
textcolor(LIGHTBLUE);gotoxy(32,5);printf("IMPRIMINDO OS NUMEROS IMPARES");
textcolor(DARKGRAY);gotoxy(34,7);printf("Numeros Impares da Matriz ");
gotoxy(2,9);
for (g = 0; g < 10; g++){
for (f = 0; f < 10; f++){
if (pr [g] [f] % 2 == 0) par [i++] = pr [g] [f];
else
impar [j++] = pr [g] [f];
}
}
for (f = 0; f < i; f++){
textcolor(LIGHTMAGENTA);printf(" %d ", impar [f]);
}
Sleep(800);
textcolor(LIGHTBLUE);gotoxy(8,13);printf ( "Esta e a soma dos Impares da Matriz: " );
gotoxy(44,13);textcolor(LIGHTMAGENTA);Sleep(800);
for ( f = 0; f < i; f++ ){
soma_pares += impar [f];
}
printf ( "  %d ",  soma_pares);
f = 0;
m = impar [f];
g = impar [f];
for ( f = 0; f < i; f++ ){
if ( impar [f] > m ){
m = impar [f];
}
if ( impar [f] < g ){
g = impar [f];
}
}
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,15);printf("O maior Numero Impar e:");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,15);printf(" %d ", m);
Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,17);printf("O menor Numero Impar e:");
Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,17);printf(" %d ", g);
textcolor(LIGHTRED);gotoxy(33,20);printf ("O ROCCO AGRADECE");Sleep(1800);exit(0);
getche();
}
int main(){
Exibe_Matriz();
Numeros_Pares_Impares();
Imprime_Pares_Maior_Menor();
Imprime_Ipares_Maior_Menor();
}