1+ class Program
2+ {
3+
4+ // Variables Globales
5+ static string variableGlobal = "Esta es una variable global" ;
6+ static void Main ( String [ ] args )
7+ {
8+ HolaMundo ( ) ;
9+ SaludoPersonalizado ( "Emilio" ) ;
10+
11+ Console . WriteLine ( "Bienvenido a la calculador de IMC" ) ;
12+ Console . WriteLine ( "Ingresa tu peso en kg:" ) ;
13+ decimal peso = decimal . Parse ( Console . ReadLine ( ) ) ;
14+ Console . WriteLine ( "Ingresa tu altura en mts" ) ;
15+ decimal altura = decimal . Parse ( Console . ReadLine ( ) ) ;
16+ decimal imc = CalcularIMC ( peso , altura ) ;
17+ imc = Math . Round ( imc , 2 ) ;
18+ Console . WriteLine ( $ "Tu IMC es de { imc } ") ;
19+
20+ double random = GenerarNumeroAleatorio ( ) ;
21+ Console . WriteLine ( $ "El número generado de manera aleatoria es { random } ") ;
22+
23+ double resultado = MultiplicacionConNumeroAleatorio ( 3.15 ) ;
24+ Console . WriteLine ( $ "El resultado de la multiplicación es { resultado } ") ;
25+
26+ //Funciones creadas dentro del lenguaje
27+ int year = DateTime . Now . Year ;
28+ bool isLeapYeay = DateTime . IsLeapYear ( year ) ; // La función IsLeapYear() existe dentro del lenguaje
29+ if ( isLeapYeay )
30+ Console . WriteLine ( $ "El año { year } es un año bisiesto") ;
31+ else
32+ Console . WriteLine ( $ "El año { year } no es un año bisiesto") ;
33+
34+ Console . WriteLine ( variableGlobal ) ;
35+ Variables ( ) ;
36+
37+ int count = Extra ( "Fizz" , "Buzz" ) ;
38+ Console . WriteLine ( $ "Los números se imprmieron { count } veces") ;
39+ }
40+
41+ // FUNCIONES
42+ // 1.- Función sin parámetros ni retorno de datos
43+ static void HolaMundo ( )
44+ {
45+ Console . WriteLine ( "Hola Mundo" ) ;
46+ }
47+
48+
49+ // 2.- Función con parametros y sin retorno de datos
50+
51+ static void SaludoPersonalizado ( string nombre )
52+ {
53+ Console . WriteLine ( $ "Hola, { nombre } ") ;
54+ }
55+
56+
57+
58+ // 3.- Función con parámetros y retorno de datos
59+
60+ static decimal CalcularIMC ( decimal peso , decimal altura )
61+ {
62+ decimal imc = peso / ( altura * altura ) ;
63+
64+ return imc ;
65+ }
66+
67+
68+ // 4.- Función sin parámetros y con retorno de datos
69+
70+ static double GenerarNumeroAleatorio ( )
71+ {
72+ double random = new Random ( ) . NextDouble ( ) ;
73+ random = random * 50 ;
74+ random = Math . Round ( random , 2 ) ;
75+
76+ return random ;
77+ }
78+
79+
80+
81+ // Funciones dentro de funciones
82+
83+ static double MultiplicacionConNumeroAleatorio ( double num )
84+ {
85+ int random = GenerarRandom ( ) ;
86+ double resultado = num * random ;
87+
88+ int GenerarRandom ( )
89+ {
90+ int random = new Random ( ) . Next ( ) ;
91+ return random ;
92+ }
93+ return resultado ;
94+ }
95+
96+ static void Variables ( )
97+ {
98+ variableGlobal = "Al ser global puedo utilizarla aquí" ;
99+ string variableLocal = "Esta variable solo existe dentro de esta función" ;
100+ Console . WriteLine ( variableGlobal ) ;
101+ Console . WriteLine ( variableLocal ) ;
102+ }
103+
104+ //Ejercicio Extra
105+ static int Extra ( string str1 , string str2 )
106+ {
107+ int count = 0 ;
108+ for ( int i = 1 ; i <= 100 ; i ++ )
109+ {
110+ if ( i % 3 == 0 && i % 5 == 0 )
111+ Console . WriteLine ( str1 + str2 ) ;
112+ else if ( i % 3 == 0 )
113+ Console . WriteLine ( str1 ) ;
114+ else if ( i % 5 == 0 )
115+ Console . WriteLine ( str2 ) ;
116+ else
117+ {
118+ Console . WriteLine ( i ) ;
119+ count ++ ;
120+ }
121+ }
122+ return count ;
123+ }
124+
125+
126+ }
0 commit comments