Mostrar Mensajes

Esta sección te permite ver todos los posts escritos por este usuario. Ten en cuenta que sólo puedes ver los posts escritos en zonas a las que tienes acceso en este momento.


Temas - Dimitar Stefanov

Páginas: [1] 2 3 4 5 6 ... 14
1
Ejercicio resuelto de C#:

Citar
Multiplication sign
Description

Write a program that shows the sign (+, - or 0) of the product of three real numbers, without calculating it.

    Use a sequence of if operators.

El código:

Código: [Seleccionar]
using System;

namespace MultiplicationSign {
class Program {
static void Main() {

double a = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());

double producto = a * b * c;

if(producto < 0) {
Console.WriteLine("-");
}else if(producto > 0) {
Console.WriteLine("+");
}else {
Console.WriteLine("0");
}
}
}
}

Saludos.

2
Ejercicio resuelto:

Citar
Sort 3 Numbers
Description

Write a program that enters 3 real numbers and prints them sorted in descending order.

    Use nested if statements.
    Don’t use arrays and the built-in sorting functionality.

El código:

Código: [Seleccionar]
using System;

namespace Sort3Numbers {
class Program {
static void Main() {

int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int bigger;
int smaller;
int secondNumber;

if(a > b && a > c) {
bigger = a;
}else if(b > a && b > c) {
bigger = b;
}else {
bigger = c;
}

if(a < b && a < c) {
smaller = a;
}else if(b < a && b < c) {
smaller = b;
}else {
smaller = c;
}

if(a > smaller && a < bigger) {
secondNumber = a;
}else if(b > smaller && b < bigger) {
secondNumber = b;
}else {
secondNumber = c;
}

Console.WriteLine("{0} {1} {2}", bigger, secondNumber, smaller);

}
}
}

Saludos.

3
Ejercicio resuelto.

Citar
Play card
Description

Classical play cards use the following signs to designate the card face: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K and A. Write a program that enters a string and prints "yes CONTENT_OF_STRING" if it is a valid card sign or "no CONTENT_OF_STRING" otherwise.

El código:

Código: [Seleccionar]
using System;

namespace PlayCard {
class Program {
static void Main() {

string card = Console.ReadLine();

switch(card) {
case "2":
Console.WriteLine("yes {0}", card);
break;
case "3":
Console.WriteLine("yes {0}", card);
break;
case "4":
Console.WriteLine("yes {0}", card);
break;
case "5":
Console.WriteLine("yes {0}", card);
break;
case "6":
Console.WriteLine("yes {0}", card);
break;
case "7":
Console.WriteLine("yes {0}", card);
break;
case "8":
Console.WriteLine("yes {0}", card);
break;
case "9":
Console.WriteLine("yes {0}", card);
break;
case "10":
Console.WriteLine("yes {0}", card);
break;
case "J":
case "j":
Console.WriteLine("yes {0}", card);
break;
case "Q":
case "q":
Console.WriteLine("yes {0}", card);
break;
case "K":
case "k":
Console.WriteLine("yes {0}", card);
break;
case "A":
case "a":
Console.WriteLine("yes {0}", card);
break;
default:
Console.WriteLine("no {0}", card);
break;
}

}
}
}

Saludos

4
Ejercicio resuelto:

Citar
Exchange numbers
Description

Write a program that reads two double values from the console A and B, stores them in variables and exchanges their values if the first one is greater than the second one. Use an if-statement. As a result print the values of the variables A and B, separated by a space.

El código:

Código: [Seleccionar]
using System;

namespace ExchangeNumbers {
class Program {
static void Main() {

double a = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double aux;

if(a > b) {
aux = a;
a = b;
b = aux;
}

Console.WriteLine("{0} {1}", a, b);
}
}
}

Saludos

5
1er Ejercicio resuelto en C#:

Citar
Quadratic Equation
Description
Write a program that reads the coefficients a, b and c of a quadratic equation ax2 + bx + c = 0 and solves it (prints its real roots).

El código:

Código: [Seleccionar]
using System;

namespace QuadraticEquation {
class Program {
static void Main() {
double a = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());

double raizPositiva = (-b+(Math.Sqrt(Math.Pow(b,2)-4*a*c)))/(2*a);
double raizNegativa = (-b-(Math.Sqrt(Math.Pow(b,2)-4*a*c)))/(2*a);

if(raizNegativa.ToString() == "NaN" || raizPositiva.ToString() == "NaN") {
Console.WriteLine("no real roots");
}else if(raizPositiva != raizNegativa) {
Console.WriteLine("{0:0.00}",raizNegativa);
Console.WriteLine("{0:0.00}",raizPositiva);
}else {
Console.WriteLine("{0:0.00}",raizPositiva);
}

}
}
}

Saludos.

6
Ejercicio:

Citar
Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer(read from the console).

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
long num;
Console.Write("Introduce un número entero positivo desde 0 a 67108863: ");
num = long.Parse(Console.ReadLine());
Console.WriteLine("El número introducido es: "+num+" y en formato binario es: "+Convert.ToString(num, 2));
long posicion3 = 1 << 3;
posicion3 = num & posicion3;
posicion3 = posicion3 >> 3;

long posicion4 = 1 << 4;
posicion4 = num & posicion4;
posicion4 = posicion4 >> 4;

long posicion5 = 1 << 5;
posicion5 = num & posicion5;
posicion5 = posicion5 >> 5;

long posicion24 = 1 << 24;
posicion24 = num & posicion24;
posicion24 = posicion24 >> 24;

long posicion25 = 1 << 25;
posicion25 = num & posicion25;
posicion25 = posicion25 >> 25;

long posicion26 = 1 << 26;
posicion26 = num & posicion26;
posicion26 = posicion26 >> 26;

if(posicion3 != posicion24) {
if(posicion3 == 0) {
posicion3 = ~(1 << 24);
num = num & posicion3;
}else {
posicion3 = 1 << 24;
num = num | posicion3;
}
if(posicion24 == 0) {
posicion24 = ~(1 << 3);
num = num & posicion24;
}else {
posicion24 = 1 << 3;
num = num | posicion24;
}
}

if(posicion4 != posicion25) {
if(posicion4 == 0) {
posicion4 = ~(1 << 25);
num = num & posicion4;
}else {
posicion4 = 1 << 25;
num = num | posicion4;
}
if(posicion25 == 0) {
posicion25 = ~(1 << 4);
num = num & posicion25;
}else {
posicion25 = 1 << 4;
num = num | posicion25;
}
}

if(posicion5 != posicion26) {
if(posicion5 == 0) {
posicion5 = ~(1 << 26);
num = num & posicion5;
}else {
posicion5 = 1 << 26;
num = num | posicion5;
}
if(posicion26 == 0) {
posicion26 = ~(1 << 5);
num = num & posicion26;
}else {
posicion26 = 1 << 5;
num = num | posicion26;
}
}

Console.WriteLine("El número cambiado es: "+num+" y en forma binaria es: "+Convert.ToString(num, 2));
}
}

Saludos.

7
Ejercicio:

Citar
We are given an integer number N (read from the console), a bit value v (read from the console as well) (v = 0 or 1) and a position P (read from the console). Write a sequence of operators (a few lines of C# code) that modifies N to hold the value v at the position P from the binary representation of N while preserving all other bits in N. Print the resulting number on the console.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
long num;
Console.Write("Introduzca un número entero positivo: ");
num = Convert.ToInt64(Console.ReadLine());
Console.WriteLine("El número en representación binaria es: "+Convert.ToString(num, 2));
Console.Write("Qué posición quieres cambiar?: ");
byte posicion;
posicion = Convert.ToByte(Console.ReadLine());
byte valor;
Console.Write("Qué valor quieres ponerle (1 ó 0)?: ");
valor = Convert.ToByte(Console.ReadLine());

if(valor == 0) {
long mask = ~(1 << posicion);
Console.WriteLine(Convert.ToString(mask, 2));
num = num & mask;
}else{
long mask = 1 << posicion;
Console.WriteLine(Convert.ToString(mask, 2));
num = num | mask;
}

Console.WriteLine("El nuevo número (representado en binario) es: "+Convert.ToString(num, 2)+" y representado en decimal es: "+num);
}
}

Saludos.

8
Ejercicio:

Citar
Write a program that reads from the console two integer numbers P and N and prints on the console the value of P's N-th bit.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
long num;
int position;
Console.Write("Introduce un número entero: ");
num = int.Parse(Console.ReadLine());
Console.WriteLine(Convert.ToString(num, 2));
Console.Write("Introduce un número entero entre 0 y 55: ");
position = int.Parse(Console.ReadLine());
long mask = 1 << position;
Console.WriteLine(Convert.ToString(mask, 2));
long nAnMask = num & mask;
long bit = nAnMask >> position;
Console.WriteLine(bit);
}
}

Saludos.

9
Ejercicio:

Citar
Using bitwise operators, write a program that uses an expression to find the value of the bit at index 3 of an unsigned integer read from the console.

    The bits are counted from right to left, starting from bit 0.
    The result of the expression should be either 1 or 0. Print it on the console.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
//Console.WriteLine(Convert.ToString(35,2));
//Console.WriteLine(Convert.ToString(~35,2));
int num;
Console.Write("Introduce un número entero: ");
num = Convert.ToInt16(Console.ReadLine());
//Console.WriteLine(Convert.ToString(num, 2));
int mask = 1 << 3;
int nAnMask = num & mask;
int bit = nAnMask >> 3;
Console.WriteLine(bit);
}
}

Saludos.

10
Ejercicio:

Citar
Write a program that reads a pair of coordinates x and y and uses an expression to checks for given point (x, y) if it is within the circle K({1, 1}, 1.5) and out of the rectangle R(top=1, left=-1, width=6, height=2).

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
float x;
float y;
float radio = 1.5f;
Console.Write("Introduce la X: ");
x = float.Parse(Console.ReadLine());
Console.Write("Introduce la Y: ");
y = float.Parse(Console.ReadLine());

Console.WriteLine();

if(Math.Sqrt((Math.Pow((x-1),2))+(Math.Pow((y-1),2)))<=radio){
Console.Write("Circulo: Entra, ");
}else {
Console.Write("Circulo: NO entra, ");
}

if(x < -1 || x > 5 && y < -1 || y > 1) {
Console.Write("Rectángulo: NO entra");
}else {
Console.Write("Rectángulo: Entra");
}

Console.WriteLine();
}
}

Saludos.

11
Ejercicio:

Citar
Write an expression that calculates trapezoid's area by given sides a and b and height h. The three values should be read from the console in the order shown below. All three value will be floating-point numbers.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
float ladoA;
float ladoB;
float altura;
Console.Write("Introduce el lado A: ");
ladoA = float.Parse(Console.ReadLine());
Console.Write("Introduce el lado B: ");
ladoB = float.Parse(Console.ReadLine());
Console.Write("Introduce la altura: ");
altura = float.Parse(Console.ReadLine());
float area = (float)(((ladoA+ladoB)*altura)/2.0);
Console.WriteLine("El área del trapecio es: "+area.ToString("f7"));
}
}

Saludos.

12
Ejercicio:

Citar
Write a program that reads an integer N (which will always be less than 100 or equal) and uses an expression to check if given N is prime (i.e. it is divisible without remainder only to itself and 1).

    Note: You should check if the number is positive

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main() {
int num = -1;
while(num < 0 || num > 100) {
Console.Write("Introducir un número entero de 0 a 100: ");
num = Convert.ToInt16(Console.ReadLine());
}
int result = 0;
for(int i = 2;i<=Math.Sqrt(num);i++) {
if(num%i == 0) {
result = result+1;
}
}

if(num==0 || num==1) {
Console.WriteLine("False");
}else if(num==2) {
Console.WriteLine("True");
}else {
if(result>0) {
Console.WriteLine("False");
}else {
Console.WriteLine("True");
}
}
}
}

Saludos.

13
Ejercicio:

Citar
Write a program that reads the coordinates of a point x and y and using an expression checks if given point (x, y) is inside a circle K({0, 0}, 2) - the center has coordinates (0, 0) and the circle has radius 2. The program should then print "yes DISTANCE" if the point is inside the circle or "no DISTANCE" if the point is outside the circle.

    In place of DISTANCE print the distance from the beginning of the coordinate system - (0, 0) - to the given point.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main(){
float x;
float y;
float radio = 2.0f;
Console.Write("Introduce la X: ");
x = float.Parse(Console.ReadLine());
Console.Write("Introduce la Y: ");
y = float.Parse(Console.ReadLine());

if(Math.Sqrt((Math.Pow((x-0),2))+(Math.Pow((y-0),2)))<=radio){
Console.WriteLine("Entra, distancia: "+ (Math.Sqrt(Math.Pow((x-0), 2) + Math.Pow((y-0), 2))).ToString("f2"));
}else {
Console.WriteLine("NO entra, distancia: " + (Math.Sqrt(Math.Pow((x-0), 2) + Math.Pow((y-0), 2))).ToString("f2"));
}
}
}

Saludos.

14
Ejercicio:

Citar
Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:

    Calculates the sum of the digits (in our example 2 + 0 + 1 + 1 = 4) and prints it on the console.
    Prints on the console the number in reversed order: dcba (in our example 1102) and prints the reversed number.
    Puts the last digit in the first position: dabc (in our example 1201) and prints the result.
    Exchanges the second and the third digits: acbd (in our example 2101) and prints the result.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
int num;
Console.Write("Introduzca un número entero de 4 dígitos: ");
num = int.Parse(Console.ReadLine());
int primerNumero = num%10;
num = num / 10;
int segundoNumero = num%10;
num = num/10;
int tercerNumero = num%10;
num = num/10;
int cuartoNumero = num%10;
Console.WriteLine("Suma: "+(primerNumero+segundoNumero+tercerNumero+cuartoNumero));
Console.WriteLine("Invertidos: "+primerNumero.ToString()+segundoNumero.ToString()+tercerNumero.ToString()+cuartoNumero.ToString());
Console.WriteLine("El último dígito en primer lugar: " + primerNumero.ToString()
+cuartoNumero.ToString() + tercerNumero.ToString() + segundoNumero.ToString());
Console.WriteLine("Invertir el lugar del segundo y el tercer dígito: "+cuartoNumero.ToString()+segundoNumero.ToString()+tercerNumero.ToString()+primerNumero.ToString());
}
}

Saludos.

15
Ejercicio:

Citar
Write a program that reads an integer N from the console and prints true if the third digit of N is 7, or "false THIRD_DIGIT", where you should print the third digits of N.

    The counting is done from right to left, meaning 123456's third digit is 4.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
Console.Write("Entra un número entero: ");
int num01;
num01 = Convert.ToInt32(Console.ReadLine());
int num02 = num01/100;
//Console.WriteLine(num01+"/100="+num02);
int num03 = num02%10;
//Console.WriteLine(num02+"%10="+num03);
if(num03 == 7)
{
Console.WriteLine("True");
} else
{
Console.WriteLine("False "+num03);
}
}
}

Saludos.

16
Ejercicio:

Citar
Write an expression that calculates rectangle’s area and perimeter by given width and height. The width and height should be read from the console.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
Console.Write("Entra la anchura del rectángulo: ");
float anchura;
anchura = float.Parse(Console.ReadLine());
Console.Write("Entra la altura del rectángulo:");
float altura;
altura = float.Parse(Console.ReadLine());
float area = altura*anchura;
float perimetro = (altura*2)+(anchura*2);
Console.WriteLine("El área del rectángulo es: "+area.ToString("f2")+
" y el perémetro es: "+ perimetro.ToString("F2"));
}
}

Saludos.

17
Ejercicio:

Citar
Write a program that does the following:

    Reads an integer number from the console.
    Stores in a variable if the number can be divided by 7 and 5 without remainder.
    Prints on the console "true NUMBER" if the number is divisible without remainder by 7 and 5. Otherwise prints "false NUMBER". In place of NUMBER print the value of the input number.

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
Console.WriteLine("Entra un número entero: ");
int num;
num = Convert.ToInt16(Console.ReadLine());
if(num % 5 == 0 || num % 7 == 0){
Console.WriteLine("El número: " + num + " es divisble por 5 ó 7");
}else{
Console.WriteLine("El número: " + num + " NO es divisble por 5 ó 7");
}
}
}

Saludos.

18
Ejercicio:

Citar
The gravitational field of the Moon is approximately 17% of that on the Earth.

    Write a program that calculates the weight of a man on the moon by a given weight W(as a floating-point number) on the Earth.
    The weight W should be read from the console.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
sbyte a = 31;
while(a < -30 || a > 30)
{
Console.WriteLine("Introduzca un número entero entre -30 y 30: ");
a = Convert.ToSByte(Console.ReadLine());
}
if(a % 2 == 0){
Console.WriteLine("El número: " + a + " es par");
}else{
Console.WriteLine("El número: " + a + " es impar");
}
}
}

Saludos.

19
Ejercicio:

Citar
Write a program that reads an integer from the console, uses an expression to check if given integer is odd or even, and prints "even NUMBER" or "odd NUMBER", where you should print the input number's value instead of NUMBER.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
sbyte a = 31;
while(a < -30 || a > 30)
{
Console.WriteLine("Introduzca un número entero entre -30 y 30: ");
a = Convert.ToSByte(Console.ReadLine());
}
if(a % 2 == 0){
Console.WriteLine("El número: " + a + " es par");
}else{
Console.WriteLine("El número: " + a + " es impar");
}
}
}

Saludos.

20
Probado con Visual Studio.

Código: [Seleccionar]
using System;

    class Program
    {
        static void Main()
        {
        double a = 3.56;
        double b = 3.89;
        double c = 7.45;

        Console.WriteLine("a == b: "+((a+b)-c < 0.000001));
        Console.WriteLine(a==b); // it does not work
    }
    }

Páginas: [1] 2 3 4 5 6 ... 14

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".