Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

best

problema numero primo e produto

Recommended Posts

Opa galera o problema aki eh o seguinte, nesse exercicio eu tinha q achar os numeros primos nesse intervalo ai embaixo e no final achar o produto deles.. eu fiz isso MAS o produto ta dando um numero negativo.... oq fazer para corrigi isso?

 

int i=0, j, primop=1, p=0;
	
printf("\n Os numeros primos sao: ");	
	
	for(i=92; i<1478; i++)
{
	for(j=1; j<=i; j++)
  {
	  if(!(i%j))
	  {
		 p++;
	  }  
  }
	  if(p==2)
	  {  printf("\n %d", i);
		 primop=primop*i;
	  }
	p=0; 
}		
	printf("\n Produto dos primos: %d", primop);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Falar que não adianta não ajuda a identificar o erro...

 

#include <stdio.h>
int main (int argc, char *argv[])
{
int idx1,idx2;
double produto=1;
for(idx1=2;idx1<30;idx1++) {
	for (idx2 = 2; idx2 < idx1 && idx1%idx2; idx2++);
	if (idx2 == idx1)
		produto*=idx1;
}
printf("produto:%lf\n",produto);
return 0;
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Só se for pra você...

 

#include <stdio.h>
int 
main (int argc, char *argv[])
{
int idx1,idx2;
double produto=1;
for(idx1=2;idx1<10;idx1++) {
	for (idx2 = 2; idx2 < idx1 && idx1%idx2; idx2++);
	if (idx2 == idx1)
		produto*=idx1;
}
printf("produto:%lf\n",produto);
return 0;
}

 

 

Produto dos primos no intervalo [2;10): 2*3*5*7 = 210

 

 

GNU gdb (GDB; openSUSE 11.1) 6.8.50.20081120-cvs

Copyright © 2008 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "show copying"

and "show warranty" for details.

This GDB was configured as "i586-suse-linux".

For bug reporting instructions, please see:

<http://bugs.opensuse.org/>...

(gdb) l 1

1 #include <stdio.h>

2 int

3 main (int argc, char *argv[])

4 {

5 int idx1,idx2;

6 double produto=1;

7 for(idx1=2;idx1<10;idx1++) {

8 for (idx2 = 2; idx2 < idx1 && idx1%idx2; idx2++);

9 if (idx2 == idx1)

10 produto*=idx1;

(gdb)

11 }

12 printf("produto:%lf\n",produto);

13 return 0;

14 }

(gdb) b 7

Breakpoint 1 at 0x804843a: file primos.c, line 7.

(gdb) b 10

Breakpoint 2 at 0x8048471: file primos.c, line 10.

(gdb) r

Starting program: /home/isis/Desktop/a.out

 

Breakpoint 1, main (argc=<value optimized out>, argv=<value optimized out>) at primos.c:7

7 for(idx1=2;idx1<10;idx1++) {

(gdb) display idx1

1: idx1 = -1208176652

(gdb) display produto

2: produto = 1

(gdb) c

Continuing.

 

Breakpoint 2, main (argc=<value optimized out>, argv=<value optimized out>) at primos.c:10

10 produto*=idx1;

2: produto = 1

1: idx1 = 2

(gdb) c

Continuing.

 

Breakpoint 2, main (argc=<value optimized out>, argv=<value optimized out>) at primos.c:10

10 produto*=idx1;

2: produto = 2

1: idx1 = 3

(gdb) c

Continuing.

 

Breakpoint 2, main (argc=<value optimized out>, argv=<value optimized out>) at primos.c:10

10 produto*=idx1;

2: produto = 6

1: idx1 = 5

(gdb) c

Continuing.

 

Breakpoint 2, main (argc=<value optimized out>, argv=<value optimized out>) at primos.c:10

10 produto*=idx1;

2: produto = 30

1: idx1 = 7

(gdb) c

Continuing.

produto:210.000000

 

Program exited normally.

Compartilhar este post


Link para o post
Compartilhar em outros sites

De novo: leia sobre overflow. Meu código está fazendo o cálculo certo.

O 'inf' quer dizer 'infinity'.

 

Não precisa fazer um escândalo porque aparece número negativo. Troque o tipo. Se double não aguenta, use long double. Existem tópicos sobre isso.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Best

 

fica difícil ajudar se você não faz a sua parte...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.