martes, 23 de abril de 2013

Mostrar el total de cifras de un numero C#


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

namespace NCifras
{
    class Program
    {
        static void Main(string[] args)
        {
            long numero;
            int c=0;

            Console.WriteLine(" Inserta cantidad: ");
            numero = long.Parse(Console.ReadLine());

            while(numero>0.0)
            {
                numero = numero / 10;
                c++;

            }

            Console.WriteLine("total de "+c+" digitos");
            Console.ReadLine();
        }
    }
}

numero de cifras JAVA



package cifras;
import java.util.*;



public class Cifras {

   
    public static void main(String[] args) {
       
        int Contador = 0;
        long Numero;
        Scanner objeto=new Scanner(System.in);
   
        System.out.println("Introduce un numero: ");
        Numero=objeto.nextInt();

          while (Numero>0.0){
            Numero = Numero/10;
           
                Contador++;
               }

           System.out.println( "Total :" + Contador + " dígitos");
       
    }
}

Introducir un numero y mostrar el total de cifras JAVA



package cifras;
import java.util.*;



public class Cifras {

   
    public static void main(String[] args) {
       
        int Contador = 0;
        long Numero;
        Scanner objeto=new Scanner(System.in);
   
        System.out.println("Introduce un numero: ");
        Numero=objeto.nextInt();

          while (Numero>0.0){
            Numero = Numero/10;
           
                Contador++;
               }

           System.out.println( "Total :" + Contador + " dígitos");
       
    }
}

Mostrar abecedario en C# while


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

namespace Abecedario1
{
    class Program
    {
        static void Main(string[] args)
        {
            char letra='A';

            while (letra <= 'Z')
            {
                Console.Write("-"+letra);
           
                letra++;

            }

            Console.ReadLine();

        }
    }
}

lunes, 15 de abril de 2013

C# Demonstrate the fail-soft array


class FSDemo {
  static void Main() {
    FailSoftArray fs = new FailSoftArray(5, -1);
    int x;

    // Show quiet failures.
    Console.WriteLine("Fail quietly.");
    for(int i=0; i < (fs.Length * 2); i++)
      fs.Put(i, i*10);

    for(int i=0; i < (fs.Length * 2); i++) {
      x = fs.Get(i);
      if(x != -1) Console.Write(x + " ");
    }
    Console.WriteLine("");

    // Now, handle failures.
    Console.WriteLine("\nFail with error reports.");
    for(int i=0; i < (fs.Length * 2); i++)
      if(!fs.Put(i, i*10))
        Console.WriteLine("Index " + i + " out-of-bounds");

    for(int i=0; i < (fs.Length * 2); i++) {
      x = fs.Get(i);
      if(x != -1) Console.Write(x + " ");
      else
        Console.WriteLine("Index " + i + " out-of-bounds");
    }
  }
}

C# Public vs private access.


using System;

class MyClass {
  private int alpha; // private access explicitly specified
  int beta;          // private access by default
  public int gamma;  // public access

  /* Methods to access alpha and beta.  It is OK for a member
     of a class to access a private member of the same class. */

  public void SetAlpha(int a) {
    alpha = a;
  }

  public int GetAlpha() {
    return alpha;
  }

  public void SetBeta(int a) {
    beta = a;
  }

  public int GetBeta() {
    return beta;
  }
}
 
class AccessDemo {
  static void Main() {
    MyClass ob = new MyClass();
 
    // Access to alpha and beta is allowed only through methods.
    ob.SetAlpha(-99);
    ob.SetBeta(19);
    Console.WriteLine("ob.alpha is " + ob.GetAlpha());
    Console.WriteLine("ob.beta is " + ob.GetBeta());

    // You cannot access alpha or beta like this:
//  ob.alpha = 10; // Wrong! alpha is private!
//  ob.beta = 9;   // Wrong! beta is private!

    // It is OK to directly access gamma because it is public.
    ob.gamma = 99;
   }
}

Program C# Compute your weight on the moon.


using System;

class Moon {  
  static void Main() {  
    double earthweight; // weight on earth
    double moonweight;  // weight on moon  
 
    earthweight = 165.0;  
 
    moonweight = earthweight * 0.17;
 
    Console.WriteLine(earthweight + " earth-pounds is equivalent to " +
                      moonweight + " moon-pounds.");  
 
  }  
}

Numeros primos C#


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

namespace numerosprimos
{
    class Program
    {
        static void Main(string[] args)
        {
            int numero = 2;
            for (numero = 2; numero < 100; numero++)
            {
                Console.WriteLine("Numeros primos del 1 al 100"+numero);
            }

            Console.ReadLine();

        }
    }
}

Tabla de la verdad C#


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

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

            //encuentra los numeros primos entre 2 y 100
            int i, j;
            bool esPrimo;

            for (i = 2; i < 100;i++ )
            {
                esPrimo = true;
                //prueba si un numero uniformemente es divisible

               for(j=2;j<=i/j;j++)
               
             
                if((i%j) == 0)
               
                esPrimo=false;
                if(esPrimo)

                    Console.WriteLine(i+" es primo");
               
                Console.ReadLine();
            }

        }
    }
}

Tablero Ajedrez C#


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

namespace TableroAjedres
{
    class Program
    {
        static void Main(string[] args)
        {
            int falfil, calfil; // posición inicial del alfil
            int fila, columna;  // posición actual del alfil

            Console.WriteLine("Posición del alfil:");
            Console.Write("  fila    ");
            falfil = Convert.ToInt32(Console.ReadLine());
            Console.Write("  columna ");
            calfil = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(); // dejar una línea en blanco

            // Pintar el tablero de ajedrez
            for (fila = 1; fila <= 8; fila++)
            {
                for (columna = 1; columna <= 8; columna++)
                {
                    if ((fila + columna == falfil + calfil) ||
                       (fila - columna == falfil - calfil))
                        Console.Write("* ");
                    else if ((fila + columna) % 2 == 0)
                        Console.Write("B ");
                    else
                        Console.Write("N ");
                }
                Console.WriteLine(); // cambiar de fila

            }
            Console.ReadLine();
        }
    }
}