Showing posts with label Programacion. Show all posts
Showing posts with label Programacion. Show all posts

Wednesday, March 6, 2019

Ejercicio con SWITCH en java.

Se teclea un numero entre 1-7 y hay que decir que dia de semana es, teniendo Lunes como 1 y domingo como 7. Si se teclea cualquier numero fuera de este rango de muestra un mensaje de "Error". 
public static void ejercicio(){
       
        System.out.println("Entra un numero entre 1-7 : ");
        int numero = teclado.nextInt();
        teclado.nextLine();
       
        switch(numero){
            case 1:
                System.out.println("Lunes");
                break;
               
            case 2:
                System.out.println("Martes");
                break;
               
            case 3:
                System.out.println("Miércoles");
                break;
               
            case 4:
                System.out.println("Jueves");
                break;
               
            case 5:
                System.out.println("Viernes");
                break;
               
            case 6:
                System.out.println("Sábado");
                break;
               
            case 7:
                System.out.println("Domingo");
                break;
           
            default:
                System.out.println("Error");
                break;
        }
       
    }

Dado una letra, decir si es un vocal o constante en java.

Codigo :

public static void ejercicio(){
        char c = teclado.next().charAt(0);
       
        if((c == 'a')|| (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')){
            System.out.println(c + " es un vocal.");
        }
        else {
            if(c >= 'a' && c <= 'z'){
                System.out.println(c + " es un constante.");
            }
            else System.out.println("ERROR");
        }
    }

Mostrar el maxim y el minim de tres numero.

Codigo :

public static void ejercicio(){
        System.out.println("Enter three numbers: ");
       
        int primero = teclado.nextInt();
        int segundo = teclado.nextInt();
        int tercero = teclado.nextInt();
       
        int maxim = 0;
        int minim = 0;
       
        if((primero > segundo) && (primero > tercero) && (segundo < tercero)){
            maxim = primero;
            minim = segundo;
        }
       
        if((segundo > primero) && (segundo > tercero) && (primero < tercero)){
            maxim = segundo;
            minim = primero;
        }
       
        if((tercero > primero) && (tercero > segundo) && (primero < segundo)){
            maxim = tercero;
            minim = primero;
        }
       
        if((primero > segundo) && (primero > tercero) && (segundo > tercero)){
            maxim = primero;
            minim = tercero;
        }
       
        if((segundo > primero) && (segundo > tercero) && (primero > tercero)){
            maxim = segundo;
            minim = tercero;
        }
       
        if((tercero > primero) && (tercero > segundo) && (primero > segundo)){
            maxim = tercero;
            minim = segundo;
        }
       
        System.out.println(maxim + " " + minim);
       
    }

Leer en java !

Para poder introducir/leer datos desde teclado es necesario añadir Scanner al principio de la clase.

Se puede poner cualquier nombre a este Scanner.

public class nombre_de_clase {
    /**
     * @param args the command line arguments
     */
    static Scanner teclado = new Scanner(System.in);
}

Decir si el primero es divisible entre segundo

Codigo :

public static void ejercicio(){
     
        System.out.println("Entra dos numero que quieres dividir: ");

       //Aqui leo las dos variables desde taclado ! 
                 Para ver como se puede añadir Scanner para leer en java consulta : Leer
        int primero = teclado.nextInt();
        int segundo = teclado.nextInt();
     
        if(primero % segundo == 0){
            System.out.println(primero + " es divisible por " + segundo);
        }
        else {
            System.out.println(primero + " no es divisible por " + segundo);
        }
    }

Wednesday, February 20, 2019

Numbers in an interval

Write a program that reads two numbers a and b, and prints all numbers between a and b.

Input: Input consists of two natural numbers a and b.

Output: Print a line with a, a+1,…, b−1, b. Separate the numbers with commas.

#include <iostream>
using namespace std;

int main () {
    
    int x, y;
    cin >> x >> y;
    bool primera=true;
    for (int i =x; i<=y; ++i) {
        if (primera) primera=false;
        else cout << ',';
        cout << i;
    }
    cout << endl;
}


First numbers

Write a program that reads a number n, and prints all numbers between 0 and n.

Input: Input consists of a natural number n.

Output: Print in order all natural numbers between 0 and n.

#include <iostream> 
using namespace std;

int main (){
    int n;
    cin >> n;
    int i = 0;
    while (i <= n) {
        cout << i << endl;
        ++i;
    }
}

Leap Years

Write a program that tells if a year is or is not a leap year.

A leap year has 366 days. After the Gregorian reform, the leap years are those multiple of four that do not end with two zeros, and also the years ending with two zeros such that, after removing these two zeros, are divisible by four. Thus, 1800 and 1900, although being multiples of four, were not leap years; by contrast, 2000 was a leap year.

Input: Input consists of a natural number between 1800 and 9999.

Output: If the year is a leap year, print “YES”; otherwise print “NO”.

#include <iostream>
using namespace std;

int main () {
    int a;
    cin >> a;
    if (a%100 == 0) {
        int n;
        n=a/100;
        if(n%4== 0) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    else if (a%4 ==0) cout << "YES" << endl;
    else cout << "NO" << endl;
}

Time decomposition

Write a program that, given a number of seconds n, prints the number of hours, minutes and seconds represented by n.

Input: Input consists of a natural number n.

Output: Print three natural numbers h, m, s such that 3600h+60m+s=n, with m<60 and s<60.

#include <iostream> 
using namespace std;
          
int main (){
    int n;
    cin >> n;
    
    int h, m, s;
    h = n/3600;
    s = n%60;
    m = (n-s-(3600*h))/60;
    
    cout << h <<' '<< m << ' ' << s << endl;
}

Tuesday, January 22, 2019

Add One Second

Write a program that adds one second to a clock time, given its hours, minutes and seconds.

Input : Input consists of three natural numbers h, m and s that represent a clock time, that is, such that h<24, m<60 and s<60.

Output : Print the new clock time defined by h, m and s plus one second in the format “HH:MM:SS”.

#include <iostream>
using namespace std;

int main () { 
    int h, m, s;
    cin >> h >> m >> s;
    
    if (s==59) { m = m+1, s=0;
    }
    else s = s+1;
    
    if (m==60) { m=0, h=h+1;
    }
    
    if (h==24) h=0;
    
    if (h<10) cout << "0" << h << ":";
    else cout << h << ":";
    
    if (m<10) cout << "0" << m << ":";
    else cout << m << ":";
    
    if (s<10) cout << "0" << s << endl;
    else cout << s << endl;
    
    
}

How many seconds are they?

Write a program that converts to seconds a given amount of years, days, hours, minutes and seconds.

Input : The input consists of five natural numbers that represent the years, days, hours, minutes and seconds, respectively.

Output : Write the total number of seconds represented by the input.

Observation : You may assume all the years have 365 days.

#include <iostream>
using namespace std;

int main () { 
    int a, d, h, m, s;
    cin >> a >> d >> h >> m >> s;
    a = a*365*24*3600;
    d= d*86400;
    h=h*3600;
    m=m*60;
    
    int segons;
    segons= a+d+h+m+s;
    cout << segons << endl;
}

Integer division and remainder of an integer number by a natural number

Write a program that reads an integer number a and a natural number b, with b > 0, and prints the integer division d and the remainder r of a divided by b.

Remember that, by definition, d i r must be the only integer numbers such that 0 ≤ r < b and d · b + r = a.

Input : Input consists of a and b, with b > 0.

Output : Print a line with the integer division and the remainder of a divided by b, separated by a space.

#include <iostream>
using namespace std;

int main () {
    int a, b;
    cin >> a >> b;
    
    int d, r;
    if (a%b < 0) {
        r= (a%b) +b ;
        d= a/b -1 ;
        cout << d << " " << r << endl;
    }
    
    else {
        d=a/b;
        r=a-b*d;
        cout << d << " " << r << endl;
    }
    
}

Intervals 3

Write a program that, given two intervals, tells if one is inside the other, and computes the interval corresponding to their intersection, or tells that it is empty.

Input : Input consists of four integer numbers a1, b1, a2, b2 that represent the intervals [a1,b1] and [a2,b2]. Assume a1≤ b1 and a2≤ b2.

Output : Print ‘=’ if the intervals are equal, ‘1’ if the first is inside the second (but they are not equal), ‘2’ if the second is inside the first (but they are not equal), or ‘?’ otherwise. Also, print “[]” if the intersection is empty, or “[x,y]” if this is their non-empty intersection.

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    
   // Print ‘=’ if the intervals are equal, 
   //‘1’ if the first is inside the second (but they are not equal), 
   //‘2’ if the second is inside the first (but they are not equal), 
    // or ‘?’ otherwise.
    
    if (a == c and b == d) cout << "= , "<< "[" << a << "," << b << "]" << endl;
    else {
        
        if ((a >= c and b < d) or (a > c and b <= d)) cout << "1 , "<< "[" << a << "," << b << "]" << endl;
        
        else {
            
            if ((a <= c and b > d) or (a < c and b >= d)) cout << "2 , "<< "[" << c << "," << d << "]" << endl;
            
            else {
                if (a == d) cout << "? , "<< "[" << a << "," << d << "]" << endl;
                else {
                    
                    if (b == c) cout << "? , "<< "[" << b << "," << c << "]" << endl;
                    
                    else {
                        
                        if (c>=a and c<=b and d>=b)  cout << '?' << ' ' << ',' << ' ' << '[' << c << ',' << b << ']' << endl;
                        
                        else {
                            
                            if (a>=c and a<=d and d<=b)  cout << '?' << ' ' << ',' << ' ' << '[' << a << ',' << d << ']' << endl;
                            
                            else {
                                
                                if (d>=a and d<=b and c<=a)  cout << '?' << ' ' << ',' << ' ' << '[' << a << ',' << d << ']' << endl;
                                
                                else  cout << "? , "<< "[]" << endl;
                            }
                        }
                    }
                }
            }
        }
    }
}

Intervals 2

Write a program that, given two intervals, tells if one is inside the other.

Input : Input consists of four integer numbers a1, b1, a2, b2 that represent the intervals [a1,b1] and [a2,b2]. Assume a1≤ b1 and a2≤ b2.

Output : Print ‘=’ if the intervals are equal, ‘1’ if the first is inside the second (but they are not equal), ‘2’ if the second is inside the first (but they are not equal), or ‘?’ otherwise.

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    
   // Print ‘=’ if the intervals are equal, 
   //‘1’ if the first is inside the second (but they are not equal), 
   //‘2’ if the second is inside the first (but they are not equal), 
    // or ‘?’ otherwise.
    
    if (a == c and b == d) cout << "=" << endl;
    else {
        
        if ((a >= c and b < d) or (a > c and b <= d)) cout << "1" << endl;
        
        else {
            
            if ((a <= c and b > d) or (a < c and b >= d))cout << "2" << endl;
            
            else 
                            cout << "?" << endl;
        }
    }
}

Intervals 1

Write a program that, given two intervals, computes the interval corresponding to their intersection, or tells that it is empty.

Input : Input consists of four integer numbers a1, b1, a2, b2 that represent the intervals [a1,b1] and [a2,b2]. Assume a1≤ b1 and a2≤ b2.

Output : Print “[]” if their intersection is empty, or “[x,y]” if this is their non-empty intersection.

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    
    if (c>b and c>a) cout << "[" << "]" << endl;
    else if (c < a and d < a) cout << "[]" << endl;
    
    else if (a >= c) {
        if (b >= d) {
            cout << "[" << a << "," << d << "]" << endl;
        }
        else {
            cout << "[" << a << "," << b << "]" << endl;
        }
    }
    else {
        if (b >= d) cout << "[" << c << "," << d << "]" << endl;
        else cout << "[" << c << "," << b << "]" << endl;
    }
}

Classification of characters

Write a program that reads a letter, and that tells if it is an uppercase letter or a lowercase letter, and that also tells if it is a vowel or a consonant. Here, assume that the vowels are ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’, and their corresponding uppercase letters.

Input : Input consists of a letter.

Output : Tell if the letter is uppercase or lowercase, and also tell if it is a vowel or a consonant.

#include <iostream> 
using namespace std;

int main (){
    char c;
    cin >> c;
    if (c >= 'a' and c <= 'z' )cout << "lowercase" << endl; 
    else cout << "uppercase" << endl; 
    
    if ((c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u') or (c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U')) cout << "vowel" << endl;
    else cout << "consonant" << endl;
}

Uppercase and lowercase letters

Write a program that reads a letter and prints it in lowercase if it was uppercase, or prints it in uppercase if it was lowercase.
Input : Input consists of a letter.
Output : Print a line with the given letter in lowercase if it was uppercase, or in uppercase if it was lowercase.
#include <iostream>
using namespace std;

int main () {
    char c, nueva;
    cin >> c;
    if (c>='a' and c<='z') nueva= c-32;
    else nueva = c+32;
    cout << nueva << endl;
}

Sum of the minimum and the maximum of three integers

Write a program that reads three numbers and prints the sum of their minimum and maximum values. 
Input : Input consists of three integer numbers.
Output : Print a line with the sum of the maximum and the minimum of the three numbers.
#include <iostream>
using namespace std;

int main() {
    int l, m, n;
    cin >> l >> n >> m;
    int max, min;
    if (n < l and n < m) min = n;
    else if (m < n and m < l) min = m;
    else min = l;
    
    
    if (n > l and n > m) max = n;
    else if (m > n and m > l) max = m;
    else max = l;
    
    cout << max+min << endl;
}

Minimum of three different integers

Write a program that reads three numbers and prints their minimum.

Input: Input consists of three different integer numbers.

Output : Print a line with the minimum of the three numbers.

#include <iostream>
using namespace std;

int main() {
    int l, m, n;
    cin >> l >> n >> m;
    if (n < l and n < m) cout << n << endl;
    else if (m < n and m < l) cout << m << endl;
    else cout << l << endl;
}

Minimum of two integers


Write a program that reads two numbers and prints their minimum.

Input: Input consists of two integer numbers.

Output: Print a line with the minimum of the two numbers.

Solution :


#include <iostream>
using namespace std;

int main() {
    int n,m;
    cin >> n >> m;
    if (n < m) cout << n << endl;
    else cout << m << endl;
}

Modelo OSI

Open System Interconnection , Interconexión de Sistemas Abiertos, es un modelo para estudiar las categorías en que se pueden dividir los pr...