Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Problemas de Java

Descargar como doc, pdf o txt
Descargar como doc, pdf o txt
Está en la página 1de 25

PROBLEMAS DE JAVA-INSTRUCCIONES DE CONTROL

PROBLEMA 1 PROBLEMA 2 PROBLEMA 3 package javaapplication86; import java.util.Scanner; public class Problema3 { public static void main(String[] args) { Scanner ob=new Scanner(System.in);System.out.println("ingrese cunatos numeros requiere"); int suma=0;int n=ob.nextInt(); for(int i=1;i<=n;i++){ suma =suma+i;} System.out.println("total de numeros es: "+suma); } }

run:ingrese cunatos numeros requiere: 12 total de numeros es: 78 BUILD SUCCESSFUL (total time: 1 second)PROBLEMA 4

package javaapplication87; import java.util.Scanner; public class Problema4 { public static void main(String[] args) { Scanner ob=new Scanner(System.in);int m=ob.nextInt(); int suma=0; for(int i=1;i<=m;i++){ int j=(int)(Math.pow(i, 2));suma=suma+j;} System.out.println("suma de cuadrados es: "+suma); }}

run:n cuadrados: 7 suma de cuadrados es: 140 BUILD SUCCESSFUL (total time: 6 seconds)

PROBLEMA 5 package javaapplication88; import java.util.Scanner; public class Problema5 { public static void main(String[] args) { Scanner ob=new Scanner(System.in); System.out.println("ingrese lado:"); int lado=ob.nextInt();double total=0; int depreciar=(int)((2*Math.log(lado*Math.pow(10,10)))/Math.log(2)); for (int i=0;i<=depreciar;i++){ double j=(double)(Math.pow(lado,2)/Math.pow(2,i));total=total+j;} System.out.println("total: "+total);}}

run:ingrese lado:4 total: 32.0BUILD SUCCESSFUL (total time: 16 seconds)

PROBLEMA 6 package javaapplication89; import java.util.Scanner; public class Problema6 { public static void main(String[] args) { Scanner ob=new Scanner(System.in); double lado;double s;double dif; double p=0;double producto=1; for(int i=1;i<=3;i++){ System.out.print("lado "+i+": "); lado=ob.nextDouble();p=lado+p; }

s=0.5*p;System.out.println("semiperimtro: "+s); for(int j=1;j<=3;j++){ System.out.print("lado "+j+": "); lado=ob.nextDouble();dif=s-lado;producto=s*dif; } System.out.println("area: "+producto); }}}

run:lado 1: 3lado 2: 4lado 3: 6 semiperimtro: 6.5lado 1: 3lado 2: 4lado 3: 6 area: 3.25 BUILD SUCCESSFUL (total time: 16 seconds)

PROBLEMA 7

package javaapplication90; import java.util.Scanner; public class Problema7 { public static void main(String[] args) { Scanner ob=new Scanner(System.in);double lado;double hipo=0; for(int i=1;i<=2;i++){ System.out.print("lado "+i+": "); lado=ob.nextDouble();hipo=hipo+Math.pow(lado,2);} System.out.print("angulo que se opone a: "); double ladop=ob.nextDouble(); double m=Math.asin(ladop/Math.sqrt(hipo)); System.out.print("el angulo es : "+(m*180)/Math.PI+"y el otro"+(90-(m*180)/Math.PI)); }}

run:lado 1: 3lado 2: 4 angulo que se opone a: 4el angulo es : 53.13010235415598

y el otro36.86989764584402 BUILD SUCCESSFUL (total time: 7 seconds)

PROBLEMA 8 PROBLEMA 9 PROBLEMA 10 package javaapplication91; import java.util.Scanner; public class Problema10 { public static void main(String[] args) { Scanner ob=new Scanner(System.in);System.out.println("ingrese el numero de minutos "); int minutos=ob.nextInt(); double monto;if(minutos<=3){monto=(double)(0.5*minutos); System.out.println("Monto: "+monto); } if(minutos>3){ monto=1.5+(minutos-3)*0.1;System.out.println("Monto: "+monto); } }} run:ingrese el numero de minutos8 Monto: 2.0BUILD SUCCESSFUL (total time: 4 seconds) PROBLEMA 11package javaapplication92; import java.util.Scanner; public class Problema11 { public static void main(String[] args) { Scanner ob=new Scanner(System.in);System.out.println("ingrese el numero1"); int numero1=ob.nextInt();System.out.println("ingrese el numero2"); int numero2=ob.nextInt();if(numero1<numero2){ System.out.println("numero1<numero2");} if(numero1>numero2){ System.out.println("numero1>numero2");} if(numero1==numero2){ System.out.println("numero1=numero2");}}}

run:ingrese el numero145 ingrese el numero276

numero1<numero2BUILD SUCCESSFUL (total time: 10 seconds)

PROBLEMA 12 PROBLEMA 13 package javaapplication93; import java.util.Scanner; public class Problema13 { double a,b,c;void lectura(double aa,double bb,double cc){ aa=a;bb=b;cc=c;Scanner ob=new Scanner(System.in);System.out.println("a*x*x+b*x+c"); System.out.println("ingrese a"); a=ob.nextDouble(); System.out.println("ingrese b"); b=ob.nextDouble(); System.out.println("ingrese c"); c=ob.nextDouble();} void evaluar(double a1,double b1,double c1){ a1=a;b1=b;c1=c; double disc=0.25*(b1*b1-4*a1*c1); double com=-b1/(2*a1);if(disc<0){ System.out.println("raices complejas");} if(disc == 0){ double raiz=com+Math.sqrt(disc); System.out.println("las raices son "+raiz); } if(disc>0){ double raiz1=com+Math.sqrt(disc); double raiz2=com-Math.sqrt(disc); System.out.println("las raices son ("+raiz1+" , "+raiz2+")"); }}

public static void main(String[] args) { Problema13 obj=new Problema13(); double aa=0,bb=0,cc=0; obj.lectura(aa, bb, cc); obj.evaluar(aa, bb, cc); }} run:a*x*x+b*x+cingrese a2ingrese b5ingrese c-3 las raices son (2.25 , -4.75) BUILD SUCCESSFUL (total time: 32 seconds) PROBLEMA 14 PROBLEMA 15 PROBLEMA 16

package javaapplication94; import java.util.Scanner; public class Problema16 { double lectura(double d){ Scanner obj=new Scanner(System.in); System.out.print("ingrese valor: "); d=obj.nextDouble();return d;} double funcion(double d){ double f=(d-3)/(d*d-25); System.out.println("f= "+f); return f;} public static void main(String[] args) { Problema16 prueba=new Problema16();double d=0; double dd=prueba.lectura(d);prueba.funcion(dd);}}

run:ingrese valor: 4f= -0.1111111111111111 BUILD SUCCESSFUL (total time: 1 second)

PROBLEMA 17

package javaapplication95; import java.util.Scanner; public class Problema17 { double x;void lectura(double xx){ xx=x; Scanner obj=new Scanner(System.in); System.out.print("Escribe valor: "); x=obj.nextDouble();} void evaluar(double xx){ xx=x;double f; if(xx <= 0){ f=xx+Math.sin(xx); System.out.println("f: "+f);} else if(xx <= 8){ f=2*xx-Math.log(xx); System.out.println("f: "+f);}else { f=Math.log10(xx-8); System.out.println("f: "+f);}} public static void main(String[] args) { Problema17 prueba=new Problema17(); double x = 0; prueba.lectura(x); prueba.evaluar(x); }} run:Escribe valor: 3f: 4.90138771133189 BUILD SUCCESSFUL (total time: 7 seconds) PROBLEMA 18

package javaapplication96; import java.util.Scanner; public class Problema18 { double lado1,lado2,lado3; void lectura(double l1,double l2,double l3){ l1=lado1;l2=lado2;l3=lado3; Scanner obj=new Scanner(System.in); System.out.print("Escriba lado1: "); lado1=obj.nextDouble(); System.out.print("Escriba lado2: "); lado2=obj.nextDouble(); System.out.print("Escriba lado3: "); lado3=obj.nextDouble();} void existencia(double L1,double L2,double L3) {L1=lado1;L2=lado2;L3=lado3; if(Math.abs(L3-L2)<L1&&L1<L3+L2){ if(Math.abs(L3-L1)<L2&&L2<L1+L3){ if(Math.abs(L1-L2)<L3&&L3<L1+L2){ System.out.println("El triangulo si existe");}}} else{System.out.println("El triangulo no existe");}} void tipotri(double L1,double L2,double L3){ L1=lado1;L2=lado2;L3=lado3; if(L1==L2&&L2==L3){ System.out.println("Equlatero");} if(L1 != L2 && L2 != L3 && L1!=L3) { System.out.println("Escaleno");} else if(L1 != L2 && L2 == L3 || L1 != L3 && L3 == L2 || L2 != L3 && L3 == L1){ System.out.println("isosceles");} else{System.out.println("isosceles");}} public static void main(String[] args) { Problema18 prueba=new Problema18();

double l1=0,l2=0,l3=0; prueba.lectura(l1, l2, l3); prueba.existencia(l1, l2, l3); prueba.tipotri(l1, l2, l3); }}

run:Escriba lado1: 3 Escriba lado2: 4 Escriba lado3: 4 El triangulo si existe isosceles BUILD SUCCESSFUL (total time: 4 seconds)

PROBLEMA 19 package javaapplication97; import java.util.Scanner; public class Problema19 { int d,m,a;void lectura(int dd,int mm,int aa){ dd=d;mm=m;aa=a; Scanner obj=new Scanner(System.in); System.out.print("Escriba dia: "); d=obj.nextInt(); System.out.print("Escriba mes: "); m=obj.nextInt(); System.out.print("Escriba ao: "); a=obj.nextInt();} void estacion(int dd,int mm,int aa){ dd=d;mm=m;aa=a; int cambio=100*mm+dd; {if(cambio>=1221){

System.out.println("verano");} else if(cambio>=923){ System.out.println("primavera");} else if(cambio>=622){ System.out.println("invierno");} else if(cambio>=321){ System.out.println("otoo");} else if(cambio>0){ System.out.println("verano");}}} public static void main(String[] args) { Problema19 prueba=new Problema19(); int d=0,m=0,a=0; prueba.lectura(d, m, a); prueba.estacion(d, m, a);}} run:Escriba dia: 25 Escriba mes: 6 Escriba ao: 1980 inviernoBUILD SUCCESSFUL (total time: 11 seconds)

PROBLEMA 20

package javaapplication98; import java.util.*; public class Problema20 { int m,a;void lectura(int mm,int aa){ mm=m;aa=a;Scanner obj=new Scanner(System.in); System.out.print("Escriba mes: "); m=obj.nextInt();System.out.print("Escriba ao: "); a=obj.nextInt();} void evaluar(int mm,int aa){ mm=m;aa=a;

if (aa%400 ==0){ System.out.println("El ao es bisiesto");} else if (aa%100==0){ System.out.println("El ao es no bisiesto");} else if(aa%4==0){ System.out.println("El ao es bisiesto");} else{ System.out.println("El ao no es bisiesto");} System.out.println("El numero de diasndel mes: "); if(mm==1){ System.out.println(31);} if(mm==2){ if(aa%400 ==0||aa%4==0){ System.out.println(29);} else if(aa%100==0){ System.out.println(28);} else{System.out.println(28);}} if(mm==3){System.out.println(31);} if(mm==4){System.out.println(30);} if(mm==5){System.out.println(31);} if(mm==6){System.out.println(30);} if(mm==7){System.out.println(31);} if(mm==8){System.out.println(31);} if(mm==9){System.out.println(30);} if(mm==10){System.out.println(31);} if(mm==11){System.out.println(30);} if(mm==12){System.out.println(31);}} public static void main(String[] args) { Problema20 ob=new Problema20(); int m=0;int a=0;ob.lectura(m, a);

ob.evaluar(m, a);

}}

run: Escriba mes: 10 Escriba ao: 2010 El ao no es bisiesto El numero de das del mes:31 BUILD SUCCESSFUL (total time: 10 seconds)

PROBLEMA 21 package javaapplication99; import java.util.Scanner; public class Problema21 { int d,m,a; void lectura(int dd,int mm,int aa){ dd=d;mm=m;aa=a; Scanner obj=new Scanner(System.in); System.out.print("Escriba dia: "); d=obj.nextInt(); System.out.print("Escriba mes: "); m=obj.nextInt(); System.out.print("Escriba ao: "); a=obj.nextInt();} void evaluar(int dd,int mm,int aa){ dd=d;mm=m;aa=a;if (aa%400 ==0){ System.out.println("El ao es bisiesto");} else if (aa%100==0){ System.out.println("El ao es correcto");} else if(aa%4==0){

System.out.println("El ao es bisiesto");} else{System.out.println("El ao no es correcto");} if(mm==1){ if(dd<=31); else System.out.println("dia incorrecto"); if(mm==2){ if (aa%400 ==0){ if(dd<=29); else System.out.println("dia incorrecto");} else if (aa%100==0){ System.out.println("El ao es no bisiesto");}

else if(aa%4==0){ if(dd<=29); else System.out.println("dia incorrecto");} else{System.out.println("El ao no es bisiesto");}} if(mm==3){if(dd<=31); else System.out.println("dia incorrecto");} if(mm==4){if(dd<=30); else System.out.println("dia incorrecto");} if(mm==5){if(dd<=31); else System.out.println("dia incorrecto");} if(mm==6){if(dd<=30); else System.out.println("dia incorrecto");} if(mm==7){if(dd<=31); else System.out.println("dia incorrecto");} if(mm==8){if(dd<=31); else System.out.println("dia incorrecto");} if(mm==9){if(dd<=30); else System.out.println("dia incorrecto");} if(mm==10){if(dd<=31);

else System.out.println("dia incorrecto");} if(mm==11){if(dd<=30); else System.out.println("dia incorrecto");} if(mm==12){if(dd<=31); else System.out.println("dia incorrecto");}} } public static void main(String[] args) { Problema21 ob=new Problema21(); int d=0;int m=0;int a=0;ob.lectura(d,m, a); ob.evaluar(d,m, a);}} run:Escriba dia: 23 Escriba mes: 9 Escriba ao: 2004 El ao es bisiesto BUILD SUCCESSFUL (total time: 14 seconds) PROBLEMA 22 PROBLEMA 23

package javaapplication100; import java.util.Scanner; public class Problema23 { int a,b;void lectura(int aa,int bb){ aa=a;bb=b;Scanner obj=new Scanner(System.in); System.out.println("ingrese primer numero"); a=obj.nextInt(); System.out.println("ingrese segundo numero"); b=obj.nextInt();} void calcula(int aa,int bb){ System.out.println("::::::"); aa=a;bb=b;int i;for(i=1;i<aa;i++){ if(aa%i==0){{if(bb%i==0){

System.out.println(": "+i);}}}}} public static void main(String[] args) { Problema23 ob=new Problema23(); int a=0;int b=0;ob.lectura(a, b); ob.calcula(a, b); }}

run:ingrese primer numero6 ingrese segundo numero18 :::::: run:Escriba dia: 23Escriba mes: 9 Escriba ao: 2004 El ao es bisiesto

: 1: 2: 3 BUILD SUCCESSFUL (total time: 4 seconds)

PROBLEMA 24 package javaapplication101; import java.util.Scanner; public class Problema24 { int a;void lectura(int aa){ aa=a;Scanner obj=new Scanner(System.in); System.out.println("ingrese numero"); a=obj.nextInt();}

void factorizar(int aa){ aa=a;int i=2;while (aa>1){ i=i+1;if(aa%i==0){aa=aa/i;

System.out.println(":"+i);i=1;} } } public static void main(String[] args) { Problema24 obj=new Problema24(); int a=0;obj.lectura(a); obj.factorizar(a);

}}

run:

ingrese numero45 :3 :3 :5

BUILD SUCCESSFUL (total time: 4 seconds) PROBLEMA 25 package javaapplication102; import java.util.Scanner; public class Problema25 { int a;void lectura(int aa){ aa=a; Scanner obj=new Scanner(System.in); System.out.println("ingrese numero"); a=obj.nextInt();} void primo(int aa){ aa=a; int contador=1; for(int i=2;i<aa;i++){ if(aa%i==0) {

aa=aa/i;contador=contador +1;}} if(contador>1){ System.out.println("no es primo"); } else if(contador==1){ System.out.println("es primo");}} public static void main(String[] args) { Problema25 obj=new Problema25(); int a=0;obj.lectura(a);obj.primo(a); }}

run: ingrese numero86 no es primo BUILD SUCCESSFUL (total time: 5 seconds)

run:

es primoBUILD SUCCESSFUL (total time: 2 seconds) PROBLEMA 26 package javaapplication103; import java.util.Scanner; public class Problema26 { int a,b;void lectura(int aa,int bb){ aa=a;bb=b; Scanner obj=new Scanner(System.in); System.out.println("la forma de division es a/b"); System.out.print("ingrese a:"); a=obj.nextInt(); System.out.print("ingrese b:"); b=obj.nextInt();}

void factorizar(int num1,int num2){ num1=a;num2=b;int min; if(num1 < num2){ min = num1;} else{min = num2;} int factor=0;int a1,b1; for(int i=1;i<=min;i++){ if(num1 % i == 0 && num2 % i == 0){ factor=i ;}}a1=num1/factor; b1=num2/factor; System.out.println(a+"/"+b+"="+a1+"/"+b1);} public static void main(String[] args) { Problema26 obj=new Problema26(); int a=0;int b=0; obj.lectura(a, b);obj.factorizar(a, b); }} run: la forma de division es a/b ingrese a:28i ngrese b:64 28/64=7/16

BUILD SUCCESSFUL (total time: 11 seconds)

PROBLEMA 27

package javaapplication104; import java.util.Scanner; public class Problema27 { int a;void lectura(int aa){

aa=a;Scanner obj=new Scanner(System.in); System.out.println("ingrese numero"); a=obj.nextInt();} void cambio(int aa){ String numcadena; String numCadena= Integer.toString(aa); int s=numCadena.length(); System.out.println("El numero de cifras es: "+s);} public static void main(String[] args) { Problema27 ob=new Problema27(); int n=0;ob.lectura(n);

}}

run: ingrese numero12345 El numero de cifras es: 5

BUILD SUCCESSFUL (total time: 17 seconds)

PROBLEMA 28 package javaapplication85; import java.util.Scanner; public class Problema28 { public static void main(String[] args) {// TODO code application logic here Scanner lector = new Scanner (System.in); int cifra, numero; System.out.print("Ingrese una cifra: "); cifra = lector.nextInt(); for (numero=0; cifra>0; cifra/=10)numero = numero * 10 + cifra % 10;

System.out.printf("Nmero: %d\n", numero); }}

run: Ingrese una cifra: 324556 Nmero: 655423 BUILD SUCCESSFUL (total time: 5 seconds)

PROBLEMA 29 package javaapplication105; import java.util.Scanner; public class Problema29 { int a;void lectura(int aa){ aa=a; Scanner obj=new Scanner(System.in); System.out.print("ingrese numero: "); a=obj.nextInt();} void numperfecto(int aa){ aa=a;int suma=0; for(int i=2;i<=aa;i++){ if(aa%i==0){

// System.out.println(aa/i); suma=suma+aa/i;}} System.out.println("suma de divisores: "+suma); if(suma==aa){System.out.println("El numero es perfecto");} else System.out.println("El numero no es perfecto");} public static void main(String[] args) { Problema29 obj=new Problema29(); int a=0;obj.lectura(a); obj.numperfecto(a);

}} run:ingrese numero: 28 suma de divisores: 28 El numero es perfectoBUILD SUCCESSFUL (total time: 46 seconds) PROBLEMA 30 PROBLEMA 31 PROBLEMA 32 package javaapplication106; import java.util.Scanner;public class Problema32 { int a,b;void lectura(int aa,int bb){ aa=a;bb=b; Scanner obj=new Scanner(System.in); System.out.println("ingrese primer numero"); a=obj.nextInt(); System.out.println("ingrese segundo numero"); b=obj.nextInt();} void MCD(int aa,int bb){

aa=a;bb=b;while( aa != bb ) { if( aa < bb ) {bb = bb - aa;} else {aa = aa - bb; }} System.out.print("El MCD es: "+aa); System.out.println();} public static void main(String[] args) { Problema32 obj=new Problema32(); int a=0;int b=0;obj.lectura(a, b);obj.MCD(a, b); }} run: ingrese primer numero48 ingrese segundo numero54 El MCD es: 6

BUILD SUCCESSFUL (total time: 15 seconds)

PROBLEMA 33 package javaapplication107; import java.util.Scanner; public class Problema33 { int a,b;void lectura(int aa,int bb){ aa=a;bb=b; Scanner obj=new Scanner(System.in); System.out.println("ingrese primer numero"); a=obj.nextInt(); System.out.println("ingrese segundo numero"); b=obj.nextInt();

}void MCm(int num1,int num2){ num1=a;num2=b;int min; if(num1 < num2){ min = num1;} else{ min = num2;}int mcd=0;for(int i=1;i<=min;i++){ if(num1 % i == 0 && num2 % i == 0){ mcd = i;}} //cuando se imprime afuera del for me sale el ultimo valor //que se evaluo y sale el mayor int mcm=(min*num2)/mcd;System.out.println( MCM= mcm); } public static void main(String[] args) { Problema33 obj=new Problema33(); int a=0;int b=0;obj.lectura(a, b);

obj.MCm(a, b); }}

run:ingrese primer numero16 ingrese segundo numero15 MCM= 225 BUILD SUCCESSFUL (total time: 5 seconds)

También podría gustarte