Está en la página 1de 25

UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRION FACULTAD DE INGENIERA ESCUELA DE FORMACION PROFESIONAL DE SISTEMAS Y COMPUTACIN

LABORATORIO DOMICILIARIO No. 1 LENGUAJE DE PROGRAAMACION II (JAVA 2SE NETBEANS-ECLIPSE) 1. Se desea calcular independientemente la suma de los pares e impares comprendidos

entre 1 y 50 (incluidos los extremos).


\\NETBEANS-WINDOWS

package t01; private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i,sp=0,si=0; for(i=1;i<=50;i++) { if(i%2==0) { sp=sp+i; } else { si=si+i; } } txtResultado.setText("\n SUMA DE PARES :"+ sp ); txtResultado.append("\n SUMA DE IMPARES :"+ si ); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0);

\\ECLIPSE
package ta1; public class ta1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i,sp=0,si=0; for(i=1;i<=50;i++) if(i%2==0) sp=sp+i; else si=si+i; System.out.println("La suma de pares es :" +sp); System.out.println("La suma de impares es:" +si); }

2. Calcular y visualizar la suma y el producto de los nmeros impares comprendidos entre 20 y 80. \\ JAVA 2SE

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i,si=0; double pi=1; for(i=20;i<=80;i++) if(i%2!=0) { si=si+i; pi=pi*i; } txtResultado.setText("\n La suma es : "+si); txtResultado.append("\n El producto es : "+pi); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

\\ ECLIPSE /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ta2; /** * * @author laura */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int i,si=0; double pi=1; for(i=20;i<=80;i++) if(i%2!=0) { si=si+i; pi=pi*i; } System.out.println("La suma es : "+si); System.out.println("El producto es : "+pi); }

3. Leer n nmeros enteros y obtener el promedio de los positivos y el promedio de los negativos. \\ JAVA 2SE

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {


Scanner in = new Scanner(System.in); int n,i,x,sp=0,sn=0,cp=0,cn=0; double pp,pn; do{ n=Integer.parseInt(txtNumero.getText()); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++) { x=Integer.parseInt(txtResultado.getText()); if(x>0) { cp++; sp=sp+x; } else if(x<0) { cn++; sn=sn+x; } } if(cp>0) { pp=(double)sp/cp;

txtResultado.setText("El Promedio de positivos es : "+pp); } else txtResultado.setText("No se Ingresaron Positivos"); if(cn>0) { pn=(double)sn/cn; txtResultado.setText("El Promedio de Negativos es : "+pn); } else txtResultado.setText("No se Ingresaron Negativos");

} private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); \\ECLIPSE package ta3; import java.util.Scanner; public class ta3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n,i,x,sp=0,sn=0,cp=0,cn=0; double pp,pn; do{System.out.print("Valor de n : "); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextInt(); if(x>0) { cp++; sp=sp+x; } else if(x<0) { cn++; sn=sn+x;

} } if(cp>0) { pp=(double)sp/cp; System.out.println("El Promedio de positivos es : "+pp); } else System.out.println("No se Ingresaron Positivos"); if(cn>0) { pn=(double)sn/cn; System.out.println("El Promedio de Negativos es : "+pn); } else System.out.println("No se Ingresaron Negativos"); } }

4. Calcular la suma de los cuadrados de los nmeros desde el 1 hasta el 15. \\JAVA 2SE

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: {int i,sc=0; for(i=1;i<=15;i++) sc=sc+i*i; txtResultado.setText("La suma de los"); txtResultado.append("\n cuadrados de los primeros"); txtResultado.append("\n 15 nmeros es : "+sc); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); }

\\ECLIPSE package ta4; public class ta4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub {int i,sc=0; for(i=1;i<=15;i++) sc=sc+i*i; System.out.println("La suma de los cuadrados de los primeros 15 nmeros es : "+sc); } } }

5. Se ingresan n nmeros. Se pide calcular el promedio de ellos \\ JAVA 2SE

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtResultado.setText(""); txtNumero.setText(""); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int rpta; rpta=JOptionPane.showConfirmDialog(this,"Esta seguro de Salir?", "Si" ,0); if(rpta==0) System.exit(0); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int n,i; double x,s=0,p; do { n= Integer.parseInt(txtNumero.getText()); } while(n<=0); for(i=1;i<=n;i++) { x=in.nextDouble(); s=s+x; } p=s/n; txtResultado.setText("\n El Promedio es : "+p);

\\ ECLIPSE
package ta5; import java.util.Scanner; public class ta5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n,i; double x,s=0,p; do { System.out.print("Valor de n : "); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++)

{ System.out.print("Ingrese numero : "); x=in.nextDouble(); s=s+x; } p=s/n; System.out.println("El Promedio es : "+p); } }

6. Ingresar n nmeros enteros, visualizar la suma de los nmeros pares de la lista, cuantos pares existen y cul es la media de los nmeros impares. \\JAVA 2SE

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtResultado.setText(""); txtNumero1.setText("");

txtNumero2.setText(""); txtNumero3.setText(""); txtNumero4.setText(""); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int rpta; rpta=JOptionPane.showConfirmDialog(this,"Esta seguro de Salir?", "Si" ,0); if(rpta==0) System.exit(0); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int N1,N2,N3,N4,i,n,x,sp=0,si=0,cp=0,ci=0; double mi; do { N1= Integer.parseInt(txtNumero1.getText()); } N2= Integer.parseInt(txtNumero2.getText()); } N3= Integer.parseInt(txtNumero3.getText()); } N4= Integer.parseInt(txtNumero4.getText()); } while(n<=0); for(i=1;i<=n;i++) { if(x%2==0) { cp++; sp=sp+x; } else { ci++; si=si+x; } } if(cp>0) { txtResultado.setText("\n La suma de los numeros pares es : "+sp); txtResultado.setText("\n La cantidad de numeros pares es : "+cp); } else txtResultado.setText("\n No se Ingresaron numeros pares"); if(ci>0) { mi=(double)si/ci; txtResultado.setText("\n La media de los impares es : "+mi); } else txtResultado.setText("\No se Ingresaron numeros impares"); } }

\\ECLIPSE package ta6; import java.util.Scanner; public class ta6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in =new Scanner(System.in); int n,i,x,sp=0,si=0,cp=0,ci=0; double mi; do { System.out.print("Valor de n : "); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextInt(); if(x%2==0) { cp++; sp=sp+x; } else { ci++; si=si+x; } } if(cp>0) { System.out.println("La suma de los numeros pares es : "+sp); System.out.println("La cantidad de numeros pares es : "+cp); } else System.out.println("No se Ingresaron numeros pares"); if(ci>0) { mi=(double)si/ci; System.out.println("La media de los impares es : "+mi); } else System.out.println("No se Ingresaron numeros impares"); } }

7. Desarrolle un programa que determine en un conjunto de nmeros naturales.


o o o

Cuantos son menores de 15 Cuantos son mayores de 50 Cuantos estn comprendidos entre 25 y 45.

\\ JAVA 2SE

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Scanner in = new Scanner(System.in); int n,i,c1=0,c2=0,c3=0; double x; do{txtResultado.setText("Valor de n : "); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++) { txtResultado.setText("Ingrese numero : "); x=in.nextDouble(); if(x<15) c1++; if(x>50) c2++; if(x>25 && x<45) c3++; } txtResultado.setText("La cantidad de numeros menores que 15 es : "+c1); txtResultado.append("La cantidad de numeros mayores de 50 es : "+c2); txtResultado.append("La cantidad de numeros compredios entre 25 y 45 es : "+c3); }

\\ ECLIPSE
package ta7; import java.util.Scanner; public class ta7 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n,i,c1=0,c2=0,c3=0; double x; do{System.out.print("Valor de n : "); n=in.nextInt(); } while(n<=0); for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextDouble(); if(x<15) c1++; if(x>50) c2++; if(x>25 && x<45) c3++; } System.out.println("La cantidad de numeros menores que 15 es : "+c1); System.out.println("La cantidad de numeros mayores de 50 es : "+c2); System.out.println("La cantidad de numeros compredios entre 25 y 45 es : "+c3); } }

8. Calcular el factorial de un numero n>=0 \\ JAVA 2SE

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtResultado.setText(""); txtNumero.setText(""); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int rpta; rpta=JOptionPane.showConfirmDialog(this,"Esta seguro de Salir?", "Si" ,0); if(rpta==0) System.exit(0); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int n,i; double f=1; do { n= Integer.parseInt(txtNumero.getText()); } } while(n<0); for(i=1;i<=n;i++) f=f*i; txtResultado.setText("\El factorial es : "+f); } }

\\ ECLIPSE
package ta8; import java.util.Scanner; public class ta8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n,i; double f=1; do { System.out.print("Ingrese numero positivo o cero : "); n=in.nextInt(); } while(n<0); for(i=1;i<=n;i++) f=f*i; System.out.println("El factorial es : "+f); } }

9. Ingresar un valor de x y un valor n positivo reportar la potencia de x elevado a la n. \\ JAVA 2SE

package ta9; import

java.util.Scanner;

private void btnHallarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Scanner in = new Scanner(System.in); int n,i; double x,p=1; txtResultado.setText("Valor de x : "); x=in.nextDouble(); do { txtResultado.append("Valor de n : "); n=in.nextInt(); } while(n<0); for(i=1;i<=n;i++) p=p*x; txtResultado.append("La potencia es : "+p); } \\ ECLIPSE package ta9; import java.util.Scanner; public class ta9 {

public static void main(String[] args) { // TODO code application logic here Scanner in = new Scanner(System.in); int n,i; double x,p=1; System.out.print("Valor de x : "); x=in.nextDouble();

do { System.out.print("Valor de n : "); n=in.nextInt(); } while(n<0); for(i=1;i<=n;i++) p=p*x; System.out.println("La potencia es : "+p); }

10. Imprimir las 10 primeras potencias de 4.

\\ JAVA 2SE
private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtResultado.setText(""); txtNumero.setText(""); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int rpta; rpta=JOptionPane.showConfirmDialog(this,"Esta seguro de Salir?", "Si" ,0); if(rpta==0) System.exit(0); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i; double p=1; n= Integer.parseInt(txtNumero.getText()); } for(i=1;i<=10;i++) {

p=p*4; } txtResultado.setText("\4+ " elevado a la "+i+" es "+p); } } \\ ECLIPSE


package ta10;

public class ta10 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i; double p=1; System.out.println("Las 10 primeras potencias de 4 son"); for(i=1;i<=10;i++) { p=p*4; } System.out.println(4+" elevado a la "+i+" es "+p); } }

11. Ingresar n nmeros, Calcular el mximo y el mnimo de ellos. \\ JAVA 2SE

package ta11; import java.util.Scanner; private void btnHallarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int n,i; double x,maximo,minimo; Scanner in = new Scanner(System.in); do { TxtResultado.append ("Valor de n : "); n=in.nextInt(); } while(n<=0); maximo=-1e30; minimo=1e30; for(i=1;i<=n;i++) { TxtResultado.append ("Ingrese numero : "); x=in.nextDouble(); if(x>maximo) maximo=x; if(x<minimo) minimo=x; } TxtResultado.setText("El maximo es : "+maximo); TxtResultado.append("El minimo es : "+minimo); } } \\ ECLIPSE package ta11; import java.util.Scanner; public class ta11 { /** * @param args */

public static void main(String[] args) { // TODO Auto-generated method stub int n,i; double x,maximo,minimo; Scanner in = new Scanner(System.in); do { System.out.print("Valor de n : "); n=in.nextInt(); } while(n<=0); maximo=-1e30; minimo=1e30; for(i=1;i<=n;i++) { System.out.print("Ingrese numero : "); x=in.nextDouble(); if(x>maximo) maximo=x; if(x<minimo) minimo=x; } System.out.println("El maximo es : "+maximo); System.out.println("El minimo es : "+minimo); } }

12. Realizar un programa que escriba los n trminos de la serie de Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, \\ JAVA 2SE

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtResultado.setText(""); } private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int rpta; rpta=JOptionPane.showConfirmDialog(this,"Esta seguro de Salir?", "Si" ,0); if(rpta==0) System.exit(0); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int Nact, Nant, Nsig, cont; double suma; //leer numero int N=Integer.parseInt(txtNumero.getText()); // Inacializar variables locales cont =1; //Contador de terminos sumados suma=0; // Acumulador de la suma Nact=1; // segundo terminos de la serie Nant =1; // primer terminos de la serie // Imprimir titulo txtResultado.setText(N + " Terminos de la serie Fibonacci : \n"); \\ ECLIPSE package ta12; import java.util.Scanner; public class ta12 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n,i; double p=1,s=0,t; do { System.out.print("Numero de terminos : "); n=in.nextInt(); } while(n<=2); for(i=1;i<=n;i++) { t=p+s;

System.out.print(t+" "); p=s; s=t; } System.out.println(); } }

13. Leer Nmeros (el ultimo numero es -99) y obtener el mayor. \\ JAVA 2SE
package ta13; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author laura */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n,i=0; double x,mayor; mayor=-1e30; do { System.out.print("Ingrese numero (-99 para finalizar) : "); x=Double.parseDouble(br.readLine()); if(x!=-99) { i++; if(x>mayor) mayor=x; } } while(x!=-99); if(i>0) System.out.println("El mayor es : "+mayor); else System.out.println("No se ingresaron numeros"); } } \\ ECLIPSE

package ta13; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ta13 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n,i=0; double x,mayor; mayor=-1e30; do { System.out.print("Ingrese numero (-99 para finalizar) : "); x=Double.parseDouble(br.readLine()); if(x!=-99) { i++; if(x>mayor) mayor=x; } } while(x!=-99); if(i>0) System.out.println("El mayor es : "+mayor); else System.out.println("No se ingresaron numeros"); } }

14. Calcular la s = 1 + x + x^2/2! + x^3/3! Se debe ingresar x real y n entero positivo \\ JAVA 2SE
package ta14; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** * * @author laura */ public class Main {

x^4/4!

sumatoria + x^n/n!

/** * @param args the command line arguments */ public static void main(String[] args)throws IOException { // TODO code application logic here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n,i; double p=1,x,f=1,s=1; System.out.print("Ingrese valor de x : "); x=Double.parseDouble(br.readLine()); do { System.out.print("Valor de n : "); n=Integer.parseInt(br.readLine()); } while(n<0); for(i=1;i<=n;i++) { f=f*i; p=p*x; s=s+p/f; } System.out.println("La sumatoria es : "+s); } }

\\ECLIPSE
package ta14; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class ta14 { /** * @param args */ public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n,i; double p=1,x,f=1,s=1; System.out.print("Ingrese valor de x : "); x=Double.parseDouble(br.readLine());

do { System.out.print("Valor de n : "); n=Integer.parseInt(br.readLine()); } while(n<0); for(i=1;i<=n;i++) { f=f*i; p=p*x; s=s+p/f; } System.out.println("La sumatoria es : "+s); } }

También podría gustarte