Iteración – Ejemplo 6

Enunciado

Informar las potencias de 2 a partir de 20 hasta la primera mayor a 2 000 000 000. Versión con repeat.

Código

Python

def main():
    a = 1
    i = 0

    while True:
        print(" 2 ^", i, " = ", a )
        i += 1 
        a *= 2
        if a>2E9: break

    print(" 2 ^", i, " = ", a )
    
    input( "Presionar/Press Enter to exist " );
main()

C++

#include <iostream>
using namespace std ;
#include <conio.h>

int main()
{
   int i ;
   float a ;

   a = 1 ;
   i = 0 ;
   do
   {
   	cout << 2 << "^" << i << " = " << a << endl ;
   	i++ ;
      	a *= 2 ;
   }
   while ( a < 2E9 ) ;
   cout << 2 << "^" << i << " = " << a ;
	
   cout << endl << "Presionar/Press enter to exit " ;
   getch() ;
   return 0 ;
}

Pascal

Program Problema8_3a ;
var
    a : real ;
    i : integer ;
	
begin
    a := 1 ; i := 0 ;
    repeat
        writeln ( 2 ,' ^ ' , i:3 , ' = ' , a:12:0 ) ;
        i := i+1 ;
	a := a*2
    until a >= 2E9;
    writeln ( 2 ,' ^ ' , i:3 , ' = ' , a:12:0 ) ;
    
    writeln( 'Presionar/Press Enter to exit' ) ;
    readln ;
end.

Diagramas