You are on page 1of 16

Ejercico desarrollado en java

1.

Reportar nmeros del 1 al 10.


o
Resolvemos el problema usando while

1
2
3
4
5
6
7
8
9
10
o

1
2
3
4
5
6
7
8
9
o

1
2
3
4
5
6
7
8
9
2.

public static void main (String args [])


{
int n=0;
while(n<10)
{
n++;
System.out.print(n+"n");

Resolvemos el problema usando do while


public static void main (String args [])
{

int n=0;
do{
n++;
System.out.print(n+" ");
}while(n<10);

Resolvemos el problema usando for


public static void main (String args [])
{
int n=0;
for(int i=1;i<=10;i++)
{
System.out.print(i+" ");
}
}

Reportar
20 25 30 35 70 75 80

1
2
3
4
5
6
7
8

los

siguiente

public class Ejemplo


{
public static void main(String[] args)
{
for (int i = 20; i < 80; i = i + 5)
System.out.print(i + " ");
}
}

serie:

3.

Reportar
100 98 96 94 56 54 52 50

1
2
3
4
5
6
7
8
9
4.

la

siguiente

serie:

public class Ejemplo


{
public static void main(String[] args)
{
for(int i=100;i>=50;i=i-2)
System.out.print(i+" ");
}
}

Ingresar N nmeros enteros y reportar la cantidad de pares y la cantidad de


impares.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
5.

import java.util.Scanner;
public class Ejemplo
{

public static void main(String[] args)


{
Scanner scan = new Scanner(System.in);
int n, x, cp = 0, ci = 0;
System.out.print("Ingrese la cantidad de numeros a revisar: ");
n = scan.nextInt();
for (int i = 1; i <= n; i++)
{
System.out.print(i + ") Ingrese un numero: ");
x = scan.nextInt();
if (x % 2 == 0)
cp++;
else
ci++;
}
System.out.println("La cantidad de pares son: " + cp);
System.out.println("La cantidad de impares son: " + ci);
}

Ingresar N nmeros y reportar la cantidad de positivos, negativos y ceros.

1
2
3
4
5
6
7
8
9

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n, x, cp = 0, cn = 0, c=0;
System.out.print("Ingrese la cantidad de numeros a revisar: ");
n = scan.nextInt();

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
6.

for (int i = 1; i <= n; i++)


{
System.out.print(i + ") Ingrese un numero: ");
x = scan.nextInt();
if (x==0)
c++;
else
{
if(x>0)
cp++;
else
cn++;

}
}
System.out.println("La cantidad de positivos son: " + cp);
System.out.println("La cantidad de negativos son: " + cn);
System.out.println("La cantidad de ceros son: " + c);

Ingresar el sexo de n personas y reportar el porcentaje de hombres y el


porcentaje de mujeres.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n, x, cv = 0, cm = 0;
double pv,pm;
System.out.print("Ingrese numero de personas : ");
n = scan.nextInt();
System.out.print("Ingrese sexo n"
+ "[1] Varon n"
+ "[2] Mujer n");
for (int i = 1; i <= n; i++)
{
System.out.print("Persona " + i + " : ");
x = scan.nextInt();
if (x==1)
cv++;
else
{
if(x==2)
cm++;
}
}
pv=(cv*100)/n;
pm=(cm*100)/n;
System.out.println("El porcentaje de varones es : " + pv + "%");

26
27
28
29
30
31
32
33
7.

System.out.println("El porcentaje de mujeres es : " + pm + "%");

Calcular
P = 1*2*3*4*5*6**50

1
2
3
4
5
6
7
8
9
10
11
8.

el

siguiente

producto:

public class Ejemplo


{

public static void main(String[] args)


{
double prodt=1;
for(int i=1;i<=50;i++)
prodt=prodt*i;
System.out.println("El producto es : " + prodt);
}

Calcular la sumatoria de los nmeros enteros del 1 al 100.

1
2
3
4
5
6
7
8
9
10
11
9.

public class Ejemplo


{
public static void main(String[] args)
{
int sum=0;
for(int i=1;i<=100;i++)
sum=sum+i;
System.out.println("La suma es : "+sum);
}
}

Calcular la suma de los cuadrados de los 15 primeros nmeros naturales.

1
2
3
4
5
6
7
8
9
10
10.

public class Ejemplo


{
public static void main(String[] args)
{
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 nmer
}
}

Se desea calcular independientemente la suma de los pares e impares


comprendidos entre 1 y 50.

1
2
3
4
5
6
7
8
9
10
11
12
11.

public class Ejemplo


{
public static void main(String[] args)
{
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);
}
}

Se desea calcular independientemente la suma de los impares y el producto de


todos los impares comprendidos entre 20 y 80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
12.

public class Ejemplo


{
public static void main(String[] args)
{
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);
}
}

Ingresar un nmero entero positivo y reportar su tabla de multiplicar.

1
2
3
4
5
6
7
8
9
10
11
12
13
13.

public class Ejemplo


{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n;
System.out.print("Ingresa un numero entero positivo : ");
n=sc.nextInt();
System.out.println("Tabla de multiplicar del "+n);
for(int i=1;i<=12;i++)
System.out.println(n+"x"+i+"="+n*i);
}
}

Calcular el factorial de un nmero entero mayor o igual que cero.

1
2
3

import java.util.Scanner;
public class Ejemplo

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
14.

public static void main(String[] args)


{

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);

Ingresar n nmeros. Se pide calcular el promedio de ellos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
15.

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
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);
}
}

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.

1
2
3
4
5
6

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
int n,i,x,sp=0,si=0,cp=0,ci=0;

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
16.

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");

Ingresar n nmeros y reportar el promedio de los positivos y el promedio de los


negativos.

1
2
3
4
5
6
7
8
9

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n,i,x,sp=0,sn=0,cp=0,cn=0;
double pp,pn;

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
17.

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");

Ingresar n nmeros, Calcular el mayor y el menor de ellos.

1
2
3
4
5
6
7
8
9
10

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
int n,i;
double x,maximo,minimo;
Scanner in = new Scanner(System.in);
do{
System.out.print("Valor de n : ");

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
18.

Calcular
la
suma
de
los
S=1 1/2 + 1/3 1/4 + 1/5 1/6 + 1/n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
19.

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);

trminos

de

la

serie:

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n,i;
double s=0;
do{
System.out.print("Valor de n : ");
n=sc.nextInt();
}while(n<=0);

for(i=1;i<=n;i++)
{
if(i%2==0) s=s-1.0/i;
else s=s+1.0/i;
}
System.out.println("La sumatoria es : "+s);

Realizar un programa que escriba los n trminos de la serie de Fibonacci


1, 1, 2, 3, 5, 8, 13, 21,

1
2
3
4

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args) {

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
20.

Leer nmeros hasta que el ltimo nmero ingresado sea -99 (este no se toma en
cuenta para el clculo) y reportar el mayor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
21.

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();

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n,i=0;
double x,mayor;
mayor=-1e30;
do{
System.out.print("Ingrese numero (-99 para finalizar) : ");
x=sc.nextDouble();
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");
}
}

Calcular
S=
1
+

x^2/2!

la
x^3/3!

Se debe ingresar x real y n entero positivo

x^4/4!

sumatoria:
+
x^n/n!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
22.

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n,i;
double p=1,x,f=1,s=1;
System.out.print("Ingrese valor de x : ");
x=sc.nextDouble();
do{
System.out.print("Valor de n : ");
n=sc.nextInt();
}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);
}
}

Programa para ingresar un nmero entero positivo y reportar todos sus divisores.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
23.

import java.util.Scanner;
public class Ejemplo
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num;
do{
System.out.print("Ingrese numero :");
num=in.nextInt();
}while(num<=0);
int d;
System.out.println("Los divisores del numero son :");
for(d=1;d<=num;d++)
if(num%d==0) System.out.print(d+" ");
System.out.println();
}
}

Ingresar un nmero entero y reportar si es primo. Un nmero es primo cuando es


divisible por si mismo y la unidad.

1
2

import java.util.Scanner;
public class Ejemplo

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
24.

public static void main(String args[])


{
Scanner in = new Scanner(System.in);
int num;
do{
System.out.print("Ingrese numero :");
num=in.nextInt();
}while(num<=0);
int n;
int d=1;
do{
d=d+1;
}while( num%d!=0 && d*d<=num);
if(d*d>num) n=1;
else n=0;
if(n==1)
System.out.println("El numero es primo");
else
System.out.println("El numero no es primo");
}

Ingresar un nmero entero positivo y reportar si es perfecto. Un nmero es


perfecto si es igual a la suma de divisores menores que l. Por ejemplo 6 es perfecto
pues es igual 1 + 2 + 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int num;
do{
System.out.print("Ingrese numero :");
num=in.nextInt();
}while(num<=0);
int d,sum=0;
for(d=1;d<num;d++)
if(num%d==0) sum=sum +d;
if(sum==num)
System.out.print("El numero es Perfecto!!");
else
System.out.print("El numero NO es Perfecto!!");
System.out.println();
}
}

25.

Ingresar un nmero y reportar todos sus factores primos. Por ejemplo si


ingresamos 12 debe reportar 2x2x3. Si ingresamos 25 se debe reportar 55.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
26.

import java.util.Scanner;
public class Ejemplo
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num;
do{
System.out.print("Ingrese numero :");
num=in.nextInt();
}while(num<=0);
int d=2;
System.out.print("Factores primos : ");
while(num>1)
{
while(num % d !=0) d=d+1;
System.out.print(d+" ");
num=num/d;
}
System.out.println("");
}
}

Ingresar 2 nmeros enteros positivos y reportar su mximo comn divisor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import java.util.Scanner;
public class Ejemplo
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int x,y;
do{
System.out.print("Ingrese primer numero :");
x=in.nextInt();
}while(x<0);
do{
System.out.print("Ingrese el segundo numero : ");
y=in.nextInt();
}while(y<0);
int d=2,p=1,a,b;
a=x;
b=y;
while(d<=a && d<=b)
if(a%d==0 && b%d==0)
{
p=p*d;
a=a/d;
b=b/d;
}
else

23
24
25
26
27
28
29
30
31
27.

d++;
System.out.println("El m.c.d de "+x+" y "+y+" es : "+p);

}
}

Ingresar un nmero entero positivo y reportar su suma de dgitos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
28.

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);


int num, sum=0, digit;
do
{
System.out.print("Ingrese un numero : ");
num = sc.nextInt();
}while(num<0);
while(num>0)
{
digit=num%10;
sum=sum+digit;
num=num/10;
}
System.out.println("La suma de sus digitos es : "+sum);

Ingresar un numero entero positivo y reportar si es capica

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, invert=0,aux, digit;
System.out.print("Ingresa numero : ");
num = sc.nextInt();
aux=num;
while(aux>0)
{
digit=aux%10;
invert=invert*10 + digit;
aux=aux/10;
}
if(num==invert)

16
17
18
19
20
21
22
23
29.

System.out.println("El numero es Capicua!!");


else

System.out.println("El numero NO es Capicua!!");

Ingresar un numero entero en base 10 y reportar el numero en base b ( entre 2 y


9)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

import java.util.Scanner;
public class Ejemplo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int num,base;
String result=" ";
System.out.print("Ingrese el numero en base 10 : ");
num= sc.nextInt();
do{
System.out.print("Ingrese la base : ");
base= sc.nextInt();
}while(base>=9);
if(num<base)
result= num + result;
while(num>=base)
{
result= num%base + result;
num=num/base;
if(num<base)
result= num + result;
}
System.out.println("El numero en base "+base+" es : "+result);
}
}

30 Hacer un programa para ingresar la edad y el sexo de una persona e imprima si es


hombre o mujer mayor o menor de edad.
import java.util.Scanner;
1

2
3
4
5
6

public class Ejemplo5 {


public static void main(String []args)
{
Scanner in=new Scanner(System.in);

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

}
}

char sexo;
int edad;
System.out.print("Cuantos aos tiene:");
edad=in.nextInt();
in.nextLine();
System.out.println("Cual es su sexo(M/F):");
sexo=in.nextLine().charAt(0);
if(edad>=18)
if(sexo=='M')
System.out.print("Hombre mayor de edad");
else
System.out.print("Mujer mayor de edad");
else
if(sexo=='M')
System.out.print("Hombre menor de edad");
else
System.out.print("Mujer menor de edad");

You might also like