jueves, 13 de junio de 2013

ELEVAR UN NUMERO A LA POTENCIA SIN USAR EL MAT.POW

en la siguiente aplicación tu le da a teclear un numero cualquiera y te lo elevara al cuadrado . Lo diferente de esta aplicación es que para elevar un numero se tiene que usar la propiedad Math.Pow, en este caso nos saltamos eso y se hiso mediante un ciclo for. A continuación el código:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Elevar_a_la_potencia_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int Potencia(int num)
        {
            int res = num;

            if (num == 0)
                return 1;
            else
                for (int i = 1; i <= num; i++)
                {
                     res = num * num;
                }
            return res;
        }

        private void butCalcular_Click(object sender, EventArgs e)
        {
            try
            {
                int valor = int.Parse(txtNumero.Text);

                int resultado = Convert.ToInt32(Potencia(valor));

                txtResultado.Text = Convert.ToString(resultado);
            }
            catch (Exception)
            {
                MessageBox.Show("Digite solo numeros enteros", "Datos no valodos", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtNumero.Text = "";
            txtResultado.Text = "";
        }

        private void butCerrar_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}


Esta aplicación es similar a la otra solo que en vez de que lo eleve al cuadrado el usuario también teclea a que potencia quiere que se eleve el numero. A continuación el código:

consola:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace elevar_un_numero_sin_pow
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Console.Title = "ELEVAR NUMEROS A LA POTENCIA N";
                Console.Write("INTRODUCE UN NUMERO: --->  ");
                int n = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("\n");


                Console.Write("ESCRIBE A QUE POTENCIA SE VA A ELEVAR: --->  ");
                int pot = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("\n");

                int res = n;

                for (int i = 1; i < pot; i++)
                {
                    res = (res * n);
                }

                // elevar = Convert.ToInt32(Math.Pow(n, pot));


                //(a,b)^c = (a^c , b^c)

                Console.WriteLine("El resultado es: >>>  " + res);
            }
            catch (Exception)
            {
                Console.WriteLine("Digita solo numeros enteros");
            }


            Console.ReadLine();
        }
    }
}


visual:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace elevar_un_numero_sin_pow__visual_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int Potencia(int num, int pot)
        {
            int res = num;

            if (num == 0)
                return 1;
            else
                for (int i = 1; i < pot; i++)
                {
                    res = res * num;
                }
            return res;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(textBox1.Text);
            int pot = Convert.ToInt32(textBox2.Text);
            int resultado = Convert.ToInt32(Potencia(n,pot));
            label1.Text = Convert.ToString(resultado);
        }
    }
}


1 comentario: