Está en la página 1de 32

UNIVERSIDAD DE LA SFUERZAS ARMADAS ESPE

Deber 1 S_2

ING. Fausto Meneses Becerra Mgs. I. S.


Fundamentos de Programación
NRC:

Integrantes:
Pérez Ronquillo Kevin Alexander
Rogel Malla Steven Alexis
Sotalin Males Francisco Sebastian
Yanacallo Casagualpa Kevin Omar
Yela Cabezas Bryan Gustavo

GRUPO: 3
TRIANGULOS DE PASCAL
VERSION 1
CODIGO FUENTE
/*Entradas: Altura del triangulo
Salidas: Triangulo de Pascal:
Proposito: Generar y desplegar el Triangulo de Pascal, usando sentencias de control.
*/
package triangulopascalv1;
import java.util.Scanner;
/**
*
* @author kevin
*/
public class TRIANGULOPASCALV1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int alt, m, n, fm, fn, fmn, i, combinacion;
Scanner sc = new Scanner(System.in);
for (;;) { // Ciclo infinito
System.out.print("Ingresar la altura del triangulo: ");
alt = sc.nextInt();
if (alt < 1 || alt > 20)
break;
System.out.print("-----VERSION 1-----\n");
System.out.println("TRIANGULO DE PASCAL DE ALTURA " + alt + ":");
for (m = 0; m < alt; m ++) {
for(int espacio=(alt-m-1);espacio>0;espacio--)
System.out.print(" ");
for (n = 0; n <= m; n ++) {
fm = 1;
for (i = 2; i <= m; i ++)
fm *= i;
fn = 1;
for (i = 2; i <= n; i ++)
fn *= i;
fmn = 1;
for (i = 2; i <= m - n; i ++)
fmn *= i;
combinacion = fm / fn / fmn;
System.out.print(combinacion + " ");
}
System.out.println();
System.out.println();
}
}
}
}
RESULTADOS

VERSION 2
CODIGO FUENTE
/*
TrianguloPascalV2.java
Entradas: Altura del triangulo
Salidas: Triangulo de Pascal:
Proposito: Generar y desplegar el Triangulo de Pascal, usando funciones o metodos.

*/
package triangulopascalv2;

import java.util.Scanner;

/**
*
* * @author kevin
*/
public class TRIANGULOPASCALV2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int alt;
Scanner sc = new Scanner(System.in);
for (;;) { // Ciclo infinito
System.out.print("Ingresar la altura del triangulo: ");
alt = sc.nextInt();
if (alt < 1 || alt > 20)
break;
System.out.print("-----VERSION 2-----\n");
desplegarTrinagulo(alt);
}
}

static void desplegarTrinagulo(int altura) {


int m, n, cmb;
System.out.println("TRIANGULO DE PASCAL DE ALTURA " + altura + ":");
for (m = 0; m < altura; m ++) {
for(int espacio=(altura-m-1);espacio>0;espacio--)
System.out.print(" ");
for (n = 0; n <= m; n ++) {
cmb = combinacion(m, n);
System.out.print(cmb + " ");
}
System.out.println();
System.out.println();
}
}

private static int combinacion(int m, int n) {


return (int) (factorial(m) / factorial(n) / factorial(m - n));
}

private static long factorial(int m) {


long f = 1;
int i;
for (i = 2; i <= m; i ++)
f *= i;
return f;
}
}
RESULTADOS
VERSION 3
CODIGO FUENTE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package triangulopascalv3;

import java.util.Scanner;

/**
*
* @author kevin
*/
public class TRIANGULOPASCAL_V3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int alt;
Scanner sc = new Scanner(System.in);
CTrianguloPascal tp = new CTrianguloPascal();
for (;;) { // Ciclo infinito
System.out.print("Ingresar la altura del triangulo: ");
alt = sc.nextInt();
if (alt < 1 || alt > 20)
break;
System.out.print("-----VERSION 3-----\n");
tp.desplegarTrinagulo(alt);
}

private static class CTrianguloPascal {

public CTrianguloPascal() {
}

private void desplegarTrinagulo(int altura) {


int m, n, cmb;
System.out.println("TRIANGULO DE PASCAL DE ALTURA " + altura + ":");
for (m = 0; m < altura; m ++) {
for(int espacio=(altura-m-1);espacio>0;espacio--)
System.out.print(" ");
for (n = 0; n <= m; n ++) {
cmb = combinacion(m, n);
System.out.print(cmb + " ");
}
System.out.println();
System.out.println();
}
}

private int combinacion(int m, int n) {


return (int) (factorial(m) / factorial(n) / factorial(m - n));
}

private long factorial(int x) {


long f = 1;
int i;
for (i = 2; i <= x; i ++)
f *= i;
return f;
}
}

}
RESULTADO
VERSION 4
CODIGO FUENTE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package triangulopascalv4;

import java.util.Scanner;

/**
*
* @author kevin
*/
public class TrianguloPascalV4 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int alt;
Scanner sc = new Scanner(System.in);
CTrianguloPascal tp = new CTrianguloPascal();
for (;;) { // Ciclo infinito
System.out.print("Ingresar la altura del triangulo: ");
alt = sc.nextInt();
if (alt < 1 || alt > 20)
break;
tp.desplegarTrinagulo(alt);
}
}
}
CTrianguloPascal
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package triangulopascalv4;

/**
*
* @author kevin
*/
class CTrianguloPascal {

void desplegarTrinagulo(int altura) {


int m, n, cmb;
System.out.println("TRIANGULO DE PASCAL DE ALTURA " + altura + ":");
for (m = 0; m < altura; m ++) {
for(int espacio=(altura-m-1);espacio>0;espacio--)
System.out.print(" ");
for (n = 0; n <= m; n ++) {
cmb = combinacion(m, n);
System.out.print(cmb + " ");
}
System.out.println();
System.out.println();
}
}

private int combinacion(int m, int n) {


return (int) (factorial(m) / factorial(n) / factorial(m - n));
}

private long factorial(int x) {


long y = 1;
for (int i = 2; i <= x; i ++)
y *= i;
return y;
}
}
RESULTADOS
VERSION 5
CODIGO FUENTE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package triangulopascalv5;

import java.util.Scanner;
import libcomun.CTrianPasc;

/**
*
*/
public class TRIANGULOPASCALlV5 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int alt;
CTrianPasc tp = new CTrianPasc();
for (;;) { // Ciclo infinito
alt = tp.LeerAltura();
if (alt < 1 || alt > 15)
break;
tp.desplegarTrinagulo(alt);
}
}
}
RESULTADOS

Ingresar por consola los coeficientes de una ecuación cuadrática y desplegar las soluciones. El
proceso debe repetirse hasta que se intente calcular raíces imaginarias.

VERSION 1:

/*
EcuacionCuadraticaV1
Ingrese por consola los coeficientes de una ecuacion cuadratica y desplegar las soluciones
El proceso debe repetirse hasta que se intente calcular raices imaginarias.
Usando funciones
*/

package ecuacioncuadraticav1;
import java.util.Scanner;

/**
*
* @author User
*/
public class EcuacionCuadraticaV1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
double a,b,c;
double x1,x2;
System.out.println("Ecuación cuadratica de la forma ax^2 + bx + c = 0");
for (;;){
System.out.println("Ingrese los valores de los coeficienes:");
System.out.print("a: ");
a = sc.nextDouble();
System.out.print("b: ");
b = sc.nextDouble();
System.out.print("c: ");
c = sc.nextDouble();
double numero = (b*b)-(4*a*c);
if (numero > 0){
x1 = (-b + Math.sqrt(numero)) / (2*a);
x2 = (-b - Math.sqrt(numero)) / (2*a);
System.out.println("La ecuación tiene dos soluciones:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
else if (numero == 0){
x1 = (-b) / (2*a);
System.out.println("La ecuación tiene una solución:");
System.out.println("x1 = " + x1);
}
else {
System.out.println("La ecuacion tiene solucion imaginaria");
break;
}
}
}
}
VERSION 2:
/*
EcuacionCuadraticaV2
Ingrese por consola los coeficientes de una ecuacion cuadratica y desplegar las soluciones
El proceso debe repetirse hasta que se intente calcular raices imaginarias.
Usando funciones o metodos
*/

package ecuacioncuadraticav2;
import java.util.Scanner;

/**
*
* @author User
*/
public class EcuacionCuadraticaV2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
double a,b,c;
System.out.println("Ecuación cuadratica de la forma ax^2 + bx + c = 0");
for (;;){
System.out.println("Ingrese los valores de los coeficienes:");
System.out.print("a: ");
a = sc.nextDouble();
System.out.print("b: ");
b = sc.nextDouble();
System.out.print("c: ");
c = sc.nextDouble();
double numero = (b*b)-(4*a*c);
if (numero > 0){
Detmayorcero(a,b,numero);
}
else if (numero == 0){
Detigualcero(a,b);
}
else {
System.out.println("La ecuacion tiene solucion imaginaria");
break;
}
}
}
static void Detmayorcero(double a, double b, double numero){
double x1,x2;
x1 = (-b + Math.sqrt(numero)) / (2*a);
x2 = (-b - Math.sqrt(numero)) / (2*a);
System.out.println("La ecuación tiene dos soluciones:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
static void Detigualcero(double a, double b){
double x1;
x1 = (-b) / (2*a);
System.out.println("La ecuación tiene una solución:");
System.out.println("x1 = " + x1);
}
}
VERSION 3:
/*
EcuacionCuadraticaV2
Ingrese por consola los coeficientes de una ecuacion cuadratica y desplegar las soluciones
El proceso debe repetirse hasta que se intente calcular raices imaginarias.
Usando funciones o metodos
*/

package ecuacioncuadraticav3;
import java.util.Scanner;

/**
*
* @author User
*/
public class EcuacionCuadraticaV3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
CEcuacionCuadratica tp= new CEcuacionCuadratica();
double a,b,c;
System.out.println("Ecuación cuadratica de la forma ax^2 + bx + c = 0");
for (;;){
System.out.println("Ingrese los valores de los coeficienes:");
System.out.print("a: ");
a = sc.nextDouble();
System.out.print("b: ");
b = sc.nextDouble();
System.out.print("c: ");
c = sc.nextDouble();
double numero = (b*b)-(4*a*c);
if (numero > 0){
tp.Detmayorcero(a,b,numero);
}
else if (numero == 0){
tp.Detigualcero(a,b);
}
else {
System.out.println("La ecuacion tiene solucion imaginaria");
break;
}
}
}

private static class CEcuacionCuadratica {

public CEcuacionCuadratica() {
}

private void Detmayorcero(double a, double b, double numero) {


double x1,x2;
x1 = (-b + Math.sqrt(numero)) / (2*a);
x2 = (-b - Math.sqrt(numero)) / (2*a);
System.out.println("La ecuación tiene dos soluciones:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}

private void Detigualcero(double a, double b) {


double x1;
x1 = (-b) / (2*a);
System.out.println("La ecuación tiene una solución:");
System.out.println("x1 = " + x1);
}
}
}

VERSION 4:

EcuacionCuadraticaV4.java

/*
EcuacionCuadraticaV2
Ingrese por consola los coeficientes de una ecuacion cuadratica y desplegar
las soluciones. El proceso debe repetirse hasta que se intente calcular
raices imaginarias.Usando funciones o metodos
*/

package ecuacioncuadraticav4;

import java.util.Scanner;

/**
*
* @author User
*/
public class EcuacionCuadraticaV4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
CEcuacionCuadratica tp= new CEcuacionCuadratica();
double a,b,c;
System.out.println("Ecuación cuadratica de la forma ax^2 + bx + c = 0");
for (;;){
System.out.println("Ingrese los valores de los coeficienes:");
System.out.print("a: ");
a = sc.nextDouble();
System.out.print("b: ");
b = sc.nextDouble();
System.out.print("c: ");
c = sc.nextDouble();
double numero = (b*b)-(4*a*c);
if (numero > 0){
tp.Detmayorcero(a,b,numero);
}
else if (numero == 0){
tp.Detigualcero(a,b);
}
else {
System.out.println("La ecuacion tiene solucion imaginaria");
break;
}
}
}
}
CEcuacionCuadratica.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ecuacioncuadraticav4;

/**
*
* @author User
*/
class CEcuacionCuadratica {
void Detmayorcero(double a, double b, double numero) {
double x1,x2;
x1 = (-b + Math.sqrt(numero)) / (2*a);
x2 = (-b - Math.sqrt(numero)) / (2*a);
System.out.println("La ecuación tiene dos soluciones:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}

void Detigualcero(double a, double b) {


double x1;
x1 = (-b) / (2*a);
System.out.println("La ecuación tiene una solución:");
System.out.println("x1 = " + x1);
}

VERSION 5:

LibComun.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package libcomun;

/**
*
* @author User
*/
public class LibComun {

CEcacionCuadratica.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package libcomun;

import java.util.Scanner;

/**
*
* @author User
*/
public class CEcuacionCuadratica {
public double Leera(){
double a;
Scanner sc = new Scanner(System.in);
System.out.print("a: ");
a = sc.nextDouble();
return a;
}
public double Leerb(){
double b;
Scanner sc = new Scanner(System.in);
System.out.print("b: ");
b = sc.nextDouble();
return b;
}
public double Leerc(){
double c;
Scanner sc = new Scanner(System.in);
System.out.print("c: ");
c = sc.nextDouble();
return c;
}
public double LeerNumero(double a, double b ,double c){
Scanner sc = new Scanner(System.in);
double numero = (b*b)-(4*a*c);
return numero;
}
public void Detmayorcero(double a, double b, double numero) {
double x1,x2;
x1 = (-b + Math.sqrt(numero)) / (2*a);
x2 = (-b - Math.sqrt(numero)) / (2*a);
System.out.println("La ecuación tiene dos soluciones:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}

public void Detigualcero(double a, double b) {


double x1;
x1 = (-b) / (2*a);
System.out.println("La ecuación tiene una solución:");
System.out.println("x1 = " + x1);
}
}
EcuacionCuadraticaV5.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ecuacioncuadraticav5;
import libcomun.CEcuacionCuadratica;

/**
*
* @author User
*/
public class EcuacionCuadraticaV5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CEcuacionCuadratica tp= new CEcuacionCuadratica();
double a ,b ,c;
System.out.println("Ecuación cuadratica de la forma ax^2 + bx + c = 0");
for (;;){
double numero;
System.out.println("Ingrese los valores de los coeficienes:");
a = tp.Leera();
b = tp.Leerb();
c = tp.Leerc();
numero = tp.LeerNumero(a,b,c);
if (numero > 0){
tp.Detmayorcero(a,b,numero);
}
else if (numero == 0){
tp.Detigualcero(a,b);
}
else {
System.out.println("La ecuacion tiene solucion imaginaria");
break;
}
}
}
}
Literal B:

Ingresar por consola un valor entero y desplegar los números primos <= que el número ingresado.
El proceso se repite hasta cuando se ingrese un valor fuera del rango [1,1000].

VERSION 1:

/*
NumPrimosV1
Ingresar por consola un valor entero y desplegar los números primos <= que
el número ingresado. El proceso se repite hasta cuando se ingrese un valor
fuera del rango [1,1000].
*/
package numprimosv1;

import java.util.Scanner;

/**
*
* @author User
*/
public class NumPrimosV1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int num, m;
System.out.println("Numeros primos menores a un numero dado");
for(;;){
System.out.print("Numero: ");
num = sc.nextInt();
if (num<1){
break;
}
if (num>1000){
break;
}
for(int i=2;i<num+1;i++){
for(int j=2;j<i+1;j++){
m=i%j;
if(m==0 && j!=i){
break;
}
if(m==0 && j==i){
System.out.println(i);
break;
}
}
}
}
}
}
VERSION 2:

/*
NumPrimosV1
Ingresar por consola un valor entero y desplegar los números primos <= que
el número ingresado. El proceso se repite hasta cuando se ingrese un valor
fuera del rango [1,1000].
*/

package numprimosv2;

import java.util.Scanner;

/**
*
* @author User
*/
public class NumPrimosV2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int num;
System.out.println("Numeros primos menores a un numero dado");
for(;;){
System.out.print("Numero: ");
num = sc.nextInt();
if (num<1){
break;
}
if (num>1000){
break;
}
CalcPrimos(num);
}
}
static void CalcPrimos(int num){
int m;
for(int i=2;i<num+1;i++){
for(int j=2;j<i+1;j++){
m=i%j;
if(m==0 && j!=i){
break;
}
if(m==0 && j==i){
System.out.println(i);
break;
}
}
}
}
}

VERSION 3:

/*
NumPrimosV1
Ingresar por consola un valor entero y desplegar los números primos <= que
el número ingresado. El proceso se repite hasta cuando se ingrese un valor
fuera del rango [1,1000].
*/

package numprimosv3;

import java.util.Scanner;

/**
*
* @author User
*/
public class NumPrimosV3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
CNumPrimos tp = new CNumPrimos();
int num;
System.out.println("Numeros primos menores a un numero dado");
for(;;){
System.out.print("Numero: ");
num = sc.nextInt();
if (num<1){
break;
}
if (num>1000){
break;
}
CNumPrimos.CalcPrimos(num);
}
}

private static class CNumPrimos {

public CNumPrimos() {
}

static void CalcPrimos(int num){


int m;
for(int i=2;i<num+1;i++){
for(int j=2;j<i+1;j++){
m=i%j;
if(m==0 && j!=i){
break;
}
if(m==0 && j==i){
System.out.println(i);
break;
}
}
}
}
}
}

VERSION 4:

NumPrimosV4.java

/*
NumPrimosV1
Ingresar por consola un valor entero y desplegar los números primos <= que
el número ingresado. El proceso se repite hasta cuando se ingrese un valor
fuera del rango [1,1000].
*/

package numprimosv4;

import java.util.Scanner;

/**
*
* @author User
*/
public class NumPrimosV4 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
CNumPrimos tp = new CNumPrimos();
int num;
System.out.println("Numeros primos menores a un numero dado");
for(;;){
System.out.print("Numero: ");
num = sc.nextInt();
if (num<1){
break;
}
if (num>1000){
break;
}
CNumPrimos.CalcPrimos(num);
}
}
}
CNumPrimos.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package numprimosv4;

/**
*
* @author User
*/
class CNumPrimos {
static void CalcPrimos(int num){
int m;
for(int i=2;i<num+1;i++){
for(int j=2;j<i+1;j++){
m=i%j;
if(m==0 && j!=i){
break;
}
if(m==0 && j==i){
System.out.println(i);
break;
}
}
}
}
}

VERSION 5:

LibComun.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package libcomun;

/**
*
* @author User
*/
public class LibComun {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}

CNumPrimos.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package libcomun;

import java.util.Scanner;

/**
*
* @author User
*/
public class CNumPrimos {
public int LeerNum(){
int num;
Scanner sc = new Scanner(System.in);
System.out.print("Numero: ");
num = sc.nextInt();
return num;
}
public void CalcPrimos(int num){
int m;
for(int i=2;i<num+1;i++){
for(int j=2;j<i+1;j++){
m=i%j;
if(m==0 && j!=i){
break;
}
if(m==0 && j==i){
System.out.println(i);
break;
}
}
}
}
}
NumPrimosV5.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package numprimosv5;

import java.util.Scanner;
import libcomun.CNumPrimos;

/**
*
* @author User
*/
public class NumPrimosV5 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// TODO code application logic here
Scanner sc = new Scanner(System.in);
CNumPrimos tp = new CNumPrimos();
int num;
System.out.println("Numeros primos menores a un numero dado");
for(;;){
num = tp.LeerNum();
if (num<1){
break;
}
if (num>1000){
break;
}
tp.CalcPrimos(num);
}
}

También podría gustarte