Nivel medio

Anuncio
Ing. Iver Claros Ascui
Nivel medio
Ing. Iver Claros Ascui
1.- MUESTRA LA CANTIDAD DE DIGITOS DE UN NUMERO
import java.io.*;
public class prog1
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
int n,x;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
System.out.println();
x=0;
while (n>0)
{
n=n/10;
x=x+1;
}
System.out.println ("LA CANTIDAD DE DIGITOS ES: "+ x);
}
}
Ing. Iver Claros Ascui
2.- MUESTRA LOS DIGITOS QUE COMPONEN UN NUMERO
import java.io.*;
public class prog2
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
int n,x;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
System.out.println("LOS DIGITOS SON: ");
x=0;
while (n>0)
{
x=n%10;
n=n/10;
System.out.println(x);
}
}
}
Ing. Iver Claros Ascui
3.- VERIFICA SI UN NUMERO ES PAR
import java.io.*;
public class prog3
{
public static void main (String[] arg) throws Exception
{
BufferedReader Jorge=new BufferedReader(new InputStreamReader(System.in));
int n,x;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(Jorge.readLine());
System.out.print("EL NUMERO INTRODUCIDO: ");
x=n%2;
if (x==0)
{
System.out.println ("ES PAR");
}
else
{
System.out.println ("ES IMPAR");
}
}
}
Ing. Iver Claros Ascui
4.- MUESTRA SI UN NUMERO ES PRIMO
import java.io.*;
public class prog4
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=2;
y=0;
while (x<n)
{
if (n%x==0)
{
y=1;
}
x=x+1;
}
if (y==0)
{
System.out.println("ES PRIMO");
}
else
{
System.out.println("NO ES PRIMO");
}
}
}
Ing. Iver Claros Ascui
5.- MUESTRA EL PRIMER DIGITO DE LA DERECHA
import java.io.*;
public class prog5
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=n%10;
System.out.println ("EL PRIMER DIGITO DE LA DERECHA ES: "+x);
}
}
Ing. Iver Claros Ascui
6.- MUESTRA EL PRIMER DIGITO DE LA IZQUIERDA
import java.io.*;
public class prog6
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int n,x;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=0;
while (n>9)
{
n=n/10;
}
System.out.println ("EL PRIMER DIGITO DE LA IZQUIERDA ES: "+n);
System.out.println();
}
}
Ing. Iver Claros Ascui
7.- MUESTRA EL TAMAÑO DE LA CADENA
import java.io.*;
public class prog7
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
String cad;
int x;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine ();
x=cad.length();// length toma en cuenta los espacios en blanco como caracteres
System.out.println();
System.out.print ("LA CADENA TIENE "+x+" CARACTERES, ");
System.out.print ("INCLUYENDO ESPACIOS EN BLANCO");
}
}
Ing. Iver Claros Ascui
8.- INDICA CUANTOS ESPACIOS EN BLANCO HAY EN LA CADENA
import java.io.*;
public class prog8
{
public static void main(String[] arg)throws Exception
{
BufferedReader tecla =new BufferedReader (new InputStreamReader (System.in));
String cad;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
x=0;
y=0;
System.out.println();
while (x<cad.length())
{
if (cad.charAt(x)==' ')
{
y=y+1;
}
x=x+1;
}
System.out.println("HAY "+y+" ESPACIOS");
}
}
Ing. Iver Claros Ascui
9.- MUESTRA LA CANTIDAD DE VOCALES
import java.io.*;
public class prog9
{
public static void main(String[] arg)throws Exception
{
BufferedReader tecla= new BufferedReader (new InputStreamReader(System.in));
String cad;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA (en minuscula)");
cad=tecla.readLine();
x=0;
y=0;
System.out.println();
while (x<cad.length())
{
if((cad.charAt(x)=='a')||(cad.charAt(x)=='e')||(cad.charAt(x)=='i')||
(cad.charAt(x)=='o')||(cad.charAt(x)=='u'))
{
y=y+1;
}
x=x+1;
}
System.out.println("LA CANTIDAD DE VOCALES ES: "+y);
}
}
Ing. Iver Claros Ascui
10.- MUESTRA LA CANTIDAD DE CONSONANTES
import java.io.*;
public class prog10
{
public static void main(String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
String cad;
int x,y;
x=0;
y=0;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
System.out.println();
while (x<cad.length())
{
if ((cad.charAt(x)!='a')&&(cad.charAt(x)!='e')&&
(cad.charAt(x)!='i')&&(cad.charAt(x)!='o')&&
(cad.charAt(x)!='u')&&(cad.charAt(x)!=' '))
{
y=y+1;
}
x=x+1;
}
System.out.println("LA CANTIDAD DE CONSONANTES ES: "+y);
}
}
Ing. Iver Claros Ascui
11.- VERIFICA SI UN NUMERO ES MULTIPLO DE 10
import java.io.*;
public class prog11
{
public static void main(String[] arg)throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
int n;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
System.out.println();
if (n%10==0)
{
System.out.println("Si es multiplo de 10");
}
else
{
System.out.println("No es multiplo de 10");
}
}
}
Ing. Iver Claros Ascui
12.- MUESTRA LA SUMA DE LOS DIGITOS DE UN NUMERO
import java.io.*;
public class prog12
{
public static void main(String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=0;
y=0;
System.out.println();
while (n>0)
{
x=n%10;
y=y+x;
n=n/10;
}
System.out.println("LA SUMA ES: "+y);
}
}
Ing. Iver Claros Ascui
13.- MUESTRA EL SEGUNDO DIGITO DE LA IZQ DE UN NUMERO
import java.io.*;
public class prog13
{
public static void main(String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=0;
System.out.println();
while (n>99)
{
n=n/10;
}
x=n%10;
System.out.print("EL SEGUNDO DIGITO DE LA IZQUIERDA DEL NUMERO ES: ");
System.out.println(x);
}
}
Ing. Iver Claros Ascui
14.- VERIFICA SI EL PRIMER NUMERO DE LA DERECHA ES MULTIPLO DE 3
import java.io.*;
public class prog14
{
public static void main (String[] arg)throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
int n,x;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=n%10;
if (x%3==0)
{
System.out.println("El primer digito de la derecha, ”+x+”, es multiplo de 3");
}
else
{
System.out.println("El primer digito de la derecha, ”+x+”, no es multiplo de 3");
}
}
}
Ing. Iver Claros Ascui
15.- Muetra la cantidad de dígitos pares de un número
import java.io.*;
public class prog15
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=0;
y=0;
while(n>0)
{
x=n%10;
if(x%2==0)
{
y=y+1;
}
n=n/10;
}
System.out.println("HAY "+y+" DIDITOS PARES");
}
}
Ing. Iver Claros Ascui
16.- ******
Ing. Iver Claros Ascui
17.- SUMA LOS DOS PRIMEROS DIGITOS DE LA IZQUIERDA
import java.io.*;
public class prog17
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
System.out.println();
while (n>99)
{
n=n/10;
}
x=n%10;
n=n/10;
y=x+n;
System.out.println("La suma de los primeros dos dígitos de la izquierda es: "+y);
}
}
Ing. Iver Claros Ascui
18.- VERIFICA SI LA SUMA DE LOS DÍGITOS PARES ES UN NÚMERO MULTIPLO DE 3.
import java.io.*;
public class prog18
{
public static void main(String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int n,x,y;
System.out.println("INGRESE UN NUMERO");
n=Integer.parseInt(tecla.readLine());
x=0;
y=0;
while (n>0)
{
y=n%10;
if (y%2==0)
{
x=y+x;
}
n=n/10;
}
System.out.println();
if (x%3==0)
{
System.out.println("La suma de los digitos pares es "+x+" y es multiplo de 3");
}
else
{
System.out.println("La suma de los digitos pares es "+x+" y no es multiplo de 3");
}
}
}
Ing. Iver Claros Ascui
19.- OBTIENE EL MAYOR DE DOS NUMEROS
import java.io.*;
public class prog19
{
public static void main (String[]arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int x,y;
System.out.println("INGRESE UN NUMERO");
x=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
y=Integer.parseInt(tecla.readLine());
System.out.println();
if (x>y)
{
System.out.println("El Mayor es: "+x);
}
else
{
System.out.println("El Mayor es: "+y);
}
}
}
Ing. Iver Claros Ascui
20.- OBTIENE EL MAYOR DE TRES NUMEROS INTRODUCIDOS
import java.io.*;
public class prog20
{
public static void main (String[]arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int x,y,z;
System.out.println("INGRESE UN NUMERO");
x=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
y=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
z=Integer.parseInt(tecla.readLine());
if (x>y)
{
if (x>z)
{
System.out.println("El Mayor es: " +x);
}
else
{
System.out.println("El Mayor es: " +z);
}
}
else{
if (y>z)
{
System.out.println("El Mayor es: " +y);
}
else{
System.out.println("El Mayor es: " +z);
}
}
}
}
Ing. Iver Claros Ascui
21.- OBTIENE EL MAYOR DE 4 NUMEROS INTRODUCIDOS
import java.io.*;
public class prog21
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
int w,x,y,z;
System.out.println("INGRESE UN NUMERO");
w=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
x=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
y=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN NUMERO");
z=Integer.parseInt(tecla.readLine());
System.out.println();
if (w>x)
{
if (w>y)
{
if (w>z)
{
System.out.println("El Mayor es: "+w);
}
else
{
System.out.println("El Mayor es: "+z);
}
}
else
{
if (y>z)
{
System.out.println("El Mayor es: "+y);
}
else
{
System.out.println("El Mayor es: "+z);
}
}
}
else
{
if (x>y)
{
Ing. Iver Claros Ascui
if (x>z)
{
System.out.println("El Mayor es: "+x);
}
else
{
System.out.println("El Mayor es: "+z);
}
}
else
{
if (y>z)
{
System.out.println("El Mayor es: "+y);
}
else
{
System.out.println("El Mayor es: "+z);
}
}
}
}
}
Ing. Iver Claros Ascui
22.- MUESTRA LA CANTIDAD DE PALABRAS DE UNA CADENA
import java.io.*;
public class prog22
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader (System.in));
String cad;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
x=0;
y=1;
while (x<cad.length()-1)
{
if ((cad.charAt(x)==' ')&&(cad.charAt(x+1)!=' '))
{
y=y+1;
}
x=x+1;
}
System.out.println("LA CANTIDAD DE PALABRAS ES: ");
System.out.println(y);
}
}
Ing. Iver Claros Ascui
23.- MUESTRA LA CANTIDAD DE CARACTERES QUE TIENE CADA PALABRA DE UNA
CADENA
import java.io.*;
public class prog23
{
public static void main (String[]arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
String cad;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
System.out.println();
x=0;
y=0;
while (x<cad.length())
{
if (cad.charAt(x)!=' ')
{
y=y+1;
}
else
{
System.out.println(y);
System.out.println();
y=0;
}
x=x+1;
}
System.out.println(y);
}
}
Ing. Iver Claros Ascui
24.- MUESTRA LAS PALABRAS CON LA CANTIDAD DE CARACTERES QUE TIENEN
import java.io.*;
public class prog24
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
String cad;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
System.out.println();
x=0;
y=0;
while (x<cad.length())
{
if (cad.charAt(x)!=' ')
{
System.out.print(cad.charAt(x));
y=y+1;
}
else
{
System.out.println(" -> "+ y);
System.out.println();
y=0;
}
x=x+1;
}
System.out.println(" -> "+ y);
System.out.println();
}
}
Ing. Iver Claros Ascui
25.- MUESTRA LA ULTIMA PALABRA DE UNA CADENA
import java.io.*;
public class prog25
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,cad2;
cad2="";
int x;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
cad=cad.trim();//trim.- elimina los espacios en blanco al principio y al final
x=cad.length()-1;
while (x>0)
{
if (cad.charAt(x)!=' ')
{
cad2=cad.charAt(x)+cad2;
}
else
{
x=0;
}
x=x-1;
}
System.out.println("LA ULTIMA PALABRA DEL TEXTO ES: "+cad2);
}
}
Ing. Iver Claros Ascui
26.- MUESTRA LA PRIMERA PALABRA DE UNA CADENA
import java.io.*;
public class prog26
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,cad2;
int x;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
cad=cad.trim();//trim.- elimina los espacios en blanco al principio y al final
x=0;
cad2="";
while (x<cad.length())
{
if (cad.charAt(x)!=' ')
{
cad2=cad2+cad.charAt(x);
}
else
{
x=cad.length();
}
x=x+1;
}
System.out.println("LA PRIMERA PALABRA DEL TEXTO ES: "+cad2);
}
}
Ing. Iver Claros Ascui
27.- MUESTRA LA CADENA AL REVES
ej. hola como estas ; resultato-> estas como hola
import java.io.*;
public class prog27
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,cad2,cad3;
int x;
cad2="";
cad3="";
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=" "+tecla.readLine();
x=cad.length()-1;
while (x>=0)
{
if (cad.charAt(x)!=' ')
{
cad2=cad.charAt(x)+cad2;
}
else
{
cad3=cad3+cad2+" ";
cad2="";
}
x=x-1;
}
System.out.println("EL TEXTO AL REVES SERIA : "+cad3);
}
}
Ing. Iver Claros Ascui
28.- INVIERTE LOS CARACTERES DE LAS PALABRAS DE UNA CADENA
ej. hola como estas ; resultado-> aloh omoc satse
import java.io.*;
public class prog28
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,cad2,cad3;
int x;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine()+" ";
x=0;
cad2="";
cad3="";
while (x<cad.length())
{
if (cad.charAt(x)!=' ')
{
cad2=cad.charAt(x)+cad2;
}
else
{
cad3=cad3+" "+cad2;
cad2="";
}
x=x+1;
}
System.out.println("EL TEXTO INVERTIDO ES:"+cad3);
}
}
Ing. Iver Claros Ascui
29.- REEMPLAZA UNA LETRA EN UNA POSICION DESEADA
import java.io.*;
public class prog29
{
public static void main (String[] arg) throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,ele,result;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA");
cad=tecla.readLine();
System.out.println("INGRESE UN NUMERO");
y=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UNA LETRA CUALQUIERA");
ele=tecla.readLine();
result=cad.substring(0,y-1)+ele.charAt(0)+cad.substring(y,cad.length());
System.out.println("EL NUEVO TEXTO SERIA");
System.out.println (result);
}
}
Ing. Iver Claros Ascui
30.- INSERTA UN CARACTER EN UNA CADENA SEGUN LA POSICION QUE SE LE
INDIQUE
import java.io.*;
public class prog30
{
public static void main (String[] arg)throws Exception
{
BufferedReader tecla=new BufferedReader (new InputStreamReader(System.in));
String cad,ele,result;
int x,y;
System.out.println("INGRESE UN TEXTO CUALQUIERA:");
cad=tecla.readLine();
System.out.println("INGRESE UN NUMERO:");
y=Integer.parseInt(tecla.readLine());
System.out.println("INGRESE UN CARACTER");
ele=tecla.readLine();
result="";
x=0;
while (x<cad.length())
{
if(x!=y-1)
{
result=result+cad.charAt(x);
}
else
{
result=result+ele+cad.charAt(x);
}
x=x+1;
}
System.out.println(result);
}
}
Ing. Iver Claros Ascui
31.- MOSTRAR LA SEGUNDA PALABRA DE UNA CADENA
import java.io.*;
public class prog31
{
public static void main(String[] arg)throws Exception
{
BufferedReader tecla=new BufferedReader(new InputStreamReader(System.in));
String cad,cad2;
int y,x;
System.out.println ("Introdusca la cadena");
cad=tecla.readLine();
cad2="";
x=0;
y=0;
while (x<cad.length())
{
if((cad.charAt(x)!=' ')&&(y==1))
{
cad2=cad2+cad.charAt(x);
}
else
{
if((cad.charAt(x)==' ')||(y==0))
{
if((cad.charAt(x)==' ')&&(cad.charAt(x+1)!=' '))
{
y=y+1;
}
}
}
x=x+1;
}
System.out.println("La segunda palabra es : "+cad2);
}
}
Descargar