Buscar

Gabarito Aula 08 Algoritmos

Prévia do material em texto

Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 14 - Soluc¸a˜o 01
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int num , soma = 0, n_neg = 0;
7
8 printf("Digite um numero: ");
9 scanf("%d", &num);
10
11 while(num != 0)
12 {
13 soma = soma + num;
14
15 if(num < 0) n_neg += 1;
16
17 printf("Digite outro numero: ");
18 scanf("%d", &num);
19 }
20
21 printf("Soma dos numeros = %d \n", soma);
22 printf("Quantidade de elementos negativos =
%d \n", n_neg);
23
24 system("PAUSE");
25 return 0;
26 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 14 - Soluc¸a˜o 02
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int num , soma = 0, n_neg = 0;
7
8 printf("Digite um numero: ");
9 scanf("%d", &num);
10
11 for( ; num != 0; )
12 {
13 soma = soma + num;
14
15 n_neg = num < 0 ? ++ n_neg : n_neg;
16
17 printf("Digite outro numero: ");
18 scanf("%d", &num);
19 }
20
21 printf("Soma dos numeros = %d \n", soma);
22 printf("Quantidade de elementos negativos =
%d \n", n_neg);
23
24 system("PAUSE");
25 return 0;
26 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 15
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int num;
7
8 printf("Digite um numero: ");
9 scanf("%d", &num);
10
11 while(num != 0)
12 {
13 printf("%dx%d = %d \n", num , num ,
num*num);
14 printf("Digite outro numero: ");
15 scanf("%d", &num);
16 }
17
18 system("PAUSE");
19 return 0;
20 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 16
1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5 int n, k, num1 , num2 , soma = 0, crescente =1;
6 /* crescente = 0: seque^ncia n~ao crescente
7 crescente = 1: seque^ncia crescente */
8 printf("Numero de elementos da sequencia:");
9 scanf("%d", &n);
10 printf("Digite um numero: ");
11 scanf("%d", &num1);
12
13 soma = num1%2 ? soma : soma + num1;
14 for(k = 1; k < n; k += 1)
15 {
16 printf("Digite outro numero: ");
17 scanf("%d", &num2);
18 soma = num2%2 ? soma : soma + num2;
19 if(num1 < num2 && crescente == 1)
20 num1 = num2;
21 else
22 crescente = 0;
23 }
24 if(crescente == 1)
25 printf("Crescente. Soma = %d \n", soma);
26 else
27 printf("Nao crescente. Soma=%d \n",soma);
28 system("PAUSE");
29 return 0;
30 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 17
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int n, dig , p = 0;
7
8 printf("Digite um int. positivo: ");
9 scanf("%d", &n);
10
11 dig = n%10;
12 while(n != 0)
13 {
14 if(dig %2==0 && dig != 0)
15 {
16 p = p*10 + dig;
17 }
18 n = n/10;
19 dig = n%10;
20 }
21 printf("p = %d \n", p);
22 system("PAUSE");
23 return 0;
24 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 18
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int n, dig , pot = 1, i = 0;
7 printf("Digite um int. positivo: ");
8 scanf("%d", &n);
9
10 dig = n%10;
11 while(n != 0)
12 {
13 if(dig%2 != 0)
14 {
15 i = i + dig*pot;
16 pot = pot *10;
17 }
18 n = n/10;
19 dig = n%10;
20 }
21 printf("i = %d \n", i);
22 system("PAUSE");
23 return 0;
24 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 19
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int k = 1;
7 double x, eps , termo , exp = 1.0;
8
9 printf("Expoente e precis~ao: ");
10 scanf("%lf %lf", &x, &eps);
11
12 termo = x>= eps ? x : 0.0;
13
14 while(termo >= eps)
15 {
16 exp = exp + termo;
17 k++;
18 termo = termo *(x/k);
19 }
20
21 printf("exp(%lf) = %lf \n", x, exp);
22 system("PAUSE");
23 return 0;
24 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 20
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int n, aux , reverso;
7 printf("Digite um numero natural:");
8 scanf("%d", &n);
9
10 aux = n; reverso = 0;
11
12 while(aux != 0)
13 {
14 reverso = reverso *10 + aux %10;
15 aux = aux /10;
16 }
17
18 if(reverso == n)
19 printf("%d eh palindromo \n", n);
20 else
21 printf("%d nao palindromo \n", n);
22 system("PAUSE");
23 return 0;
24 }
Algoritmos e Programac¸a˜o
Computacional
Gabarito
Exerc´ıcio 21
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int num , soma , div;
7
8 printf("Inteiro positivo: ");
9 scanf("%d", &num);
10
11 soma = 0;
12
13 for(div = 1; div <= num -1; div ++)
14 if(num%div == 0)
15 soma = soma + div;
16
17 if(num == soma)
18 printf("numero perfeito \n");
19 else
20 printf("numero nao perfeito \n");
21
22 system("PAUSE");
23 return 0;
24 }

Outros materiais

Materiais relacionados

Perguntas relacionadas

Materiais recentes

Perguntas Recentes