Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What am i doing wrong here? (simple C code to calculate powered numbers)

0 votes
asked Apr 20, 2018 by Oswaldo Páez (120 points)
#include <stdio.h>

int main()
{
    int potencia;
    int contador = 0;
    int num;
    int total = 0;
    printf("introduzca un número:\n");
    scanf("%i",&num);
    printf("introduzca potencia entera positiva:\n");
    scanf("%i",&potencia);
    if(potencia == contador){
        printf("potencia 0 de %i es 1\n",num);
    }
    else
    {
        do{
            total = num;
            total = total*total;
            contador = contador++;
        }while(contador < potencia);
            printf("potencia %i de %i es: %i \n",potencia,num,total);
        }
    }

    return 0;
}

4 Answers

0 votes
answered Apr 20, 2018 by anonymous

            contador = contador++; 
 

0 votes
answered Apr 20, 2018 by Bird Man (250 points)
Donde falla?

contador++;

o contador = contador+1;
0 votes
answered Apr 23, 2018 by Lorck Sider (140 points)

1°: ao Inves de colocar contador = contador++, tente colocar apenas contador++ ou contador = contador +1.

2°: Tente colocar apenas o while ao inves do Do While, pq caso a potencia for igual à 1, o resultado vai sair num ao quadrado.

tente assim:

 #include <stdio.h>

int main()
{
    int potencia;
    int contador = 0;
    int num;
    int total = 0;
    printf("introduzca un número:\n");
    scanf("%i",&num);
    printf("introduzca potencia entera positiva:\n");
    scanf("%i",&potencia);
    if(potencia == contador){
        printf("potencia 0 de %i es 1\n",num);
    }
    else
    {
        total=num;
          
        for (contador=1;contador<potencia;contador++){

            total = num;
            total = total*total;
            contador = contador++; 

}
            printf("potencia %i de %i es: %i \n",potencia,num,total);
        }
    

    return 0;
}

0 votes
answered Apr 25, 2018 by dasardharam (350 points)
#include <stdio.h>

int main()
{
    int potencia;
    int contador = 0;
    int num;
    int total = 0;
    printf("introduzca un número:\n");
    scanf("%i",&num);
    printf("introduzca potencia entera positiva:\n");
    scanf("%i",&potencia);
    if(potencia == num){
        printf("potencia 0 de %i es 1\n",num);
    }
    else
    {
        do
        {
            total = num;
            total = total*total;
            contador = contador++;
        }
        while(contador < potencia);
        {
            printf("potencia %i de %i es: %i \n",potencia,num,total);
        }
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...