Visualg algorithm over prime numbers in a repeat structure

Asked

Viewed 1,170 times

0

I’m new to programming and I need help in a college exercise, I need an algorithm in Visualg that shows the prime numbers between an n1 number and an N2 number, using a repetition structure, where n1 and N2 are informed by the user. The algorithm I made is this (but I don’t know what’s left to work):

Var
// Seção de Declarações das variáveis 
c, i, n1, n2, p: inteiro

Inicio
// Seção de Comandos, procedimento, funções, operadores, etc... 
escreva ("Digite um número incial: ")
leia(n1)
escreva ("Digite um número final: ")
leia (n2)
para c de n1 ate n2 faca
     p<-0
     para i de n1 ate c faca
          se(cmod i=0) entao
                  p<-p+1
          fimse
     fimpara
     se (p=2) entao
        escreva (c)
     fimse
fimpara
Fimalgoritmo

1 answer

1


There’s no point in you doing:

para c de n1 ate n2 faca
     ...
     para i de n1 ate c faca

Another thing is that cmod is the name of a variable and c modyou are utilizing the rest of the division operator.

Try:

Var
    c, i, n1, n2, p: inteiro
Inicio
    escreva ("Digite um número inicial: ")
    leia(n1)
    escreva ("Digite um número final: ")
    leia (n2)
    para c de n1 ate n2 faca
          p<-0
          para i de 1 ate c faca
               se(c mod i = 0) entao
                       p<-p+1
               fimse
          fimpara
          se (p=2) entao
             escreva (c)
          fimse
    fimpara
Fimalgoritmo
  • I tested it here and it worked, that’s what I needed, I wasn’t able to find the error, I just altered it a little by putting some spaces to help in the display and understanding when the algorithm runs. Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.