LIMPIAR VARIABLES
Publicado por MIGUEL ALBERTO TRUJILLO GONZALEZ (1 intervención) el 14/08/2019 19:26:38
Estoy empezando en el asunto de programación, tengo el siguiente código, que la primera ocasión que ingresas los datos, hace bien el calculo y al ingresar por segunda ocasión, el resultado es erroneo. Por favor alguien me puede ayudar?
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EvidenciaU2
{
class Program
{
static void Main(string[] args)
{
int opc = 0; //OPCIONES DEL MENU PRINCIPAL
int num = 0;//NÚMERO DEL CUAL SE HARA EL CALCULO
int contar = 0;
int i = 0;
double res = 1;
do
{
Console.WriteLine("Selecciona una de las siguientes opciones"
+ "\n1.- Calcular si el número que ingresa es PRIMO"
+ "\n2.- Calcular el FACTORIAL de un número"
+ "\n3.- Salir del programa");
opc = int.Parse(Console.ReadLine());
Console.Clear();
switch (opc)
{
case 1:
Console.WriteLine("Ingrese un número entero para calcular si es PRIMO");
num = int.Parse(Console.ReadLine());
Console.Clear();
if (num == 1)
{
Console.WriteLine("El número " + num + " no es considerado matematicamente primo, pues, para serlo,"
+ "\ndeberá de tener como 2 divisores que serán la unidad (1) y el mismo."
+ "\nEn el caso especifico del número 1 solo tiene un divisor");
Console.ReadKey();
}else if(num > 1)
{
for (i=1; i<=num; i++)
{
if (num % i == 0)
{
contar = contar + 1;
}
}
if (contar > 2)
{
Console.WriteLine("El número " + num + " no se encuentra en el grupo de los PRIMOS");
}
else
{
Console.WriteLine("El número " + num + " es PRIMO");
}
}
break;
case 2:
Console.WriteLine("Ingrese un número entero para calcular su FACTORIAL");
num = int.Parse(Console.ReadLine());
for (i=1; i<=num ; i++)
{
res = res * i;
}
Console.WriteLine("El FACTORIAL del número " + num + " es: " + res);
Console.WriteLine("");
break;
case 3:
Console.WriteLine("El programa se cerrará, gracias!");
Console.WriteLine("");
break;
default:
Console.WriteLine("La opción ingresada es incorrecta, por favor, intente nuevamente");
break;
}
}
while (opc != 3);
Console.ReadKey();
}
}
}
Valora esta pregunta


0