[go: up one dir, main page]

0% found this document useful (0 votes)
49 views12 pages

4 IniciarProgramaC# For

The document contains 15 code examples that use for loops to perform different tasks in C#. The examples demonstrate using for loops to: 1) Read in successive numbers or text from the user 2) Calculate sums, averages, maximums, and minimums of sets of numbers 3) Count positive, negative, and zero values in sets of numbers 4) Iterate over ranges of numbers to perform calculations 5) Output successive even or ordered numbers

Uploaded by

Stephania Paucar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views12 pages

4 IniciarProgramaC# For

The document contains 15 code examples that use for loops to perform different tasks in C#. The examples demonstrate using for loops to: 1) Read in successive numbers or text from the user 2) Calculate sums, averages, maximums, and minimums of sets of numbers 3) Count positive, negative, and zero values in sets of numbers 4) Iterate over ranges of numbers to perform calculations 5) Output successive even or ordered numbers

Uploaded by

Stephania Paucar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Ingrese 5 numeros sucesivos, alicando for.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 1; i <= 5; i++)
{
Console.WriteLine("Ingrese Numero: {0} ", i);
}
Console.ReadLine();
}
}
}

2. Ingrese 5 textos “Erick”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 1; i <= 5; i++)
{
Console.WriteLine("{0} Erick ", i);
}
Console.ReadLine();
}
}
}
3. Ingrese n numeros, de forma sucesiva.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i,n;
Console.Write("Ingese Valor :");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
Console.WriteLine("Numero Ingresado : {0} ", i);
}
Console.ReadLine();
}
}
}

4. Ingrese n textos “Leon”, aplicando for.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i,n;
Console.Write("Ingese Valor :");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
Console.WriteLine("{0} Leon ", i);
}
Console.ReadLine();
}
}
}
5. Aplicando for halla la suma de n numeros.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i, n, suma, suma1;
suma1 = 0;
Console.Write("ingrese valor:");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
Console.Write("{0}º Numero :", i);
suma = int.Parse(Console.ReadLine());
suma1 = suma + suma1;
}
Console.WriteLine("Resultado de la Suma : {0}", suma1);
Console.ReadLine();
}
}
}

6. Hallar las 7 notasde un alumno y sacar promedio


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int nota, suma, i;
double prom;
suma = 0;
for (i = 1; i <= 7; i++)
{
Console.Write("ingrese nota {0}:", i);
nota = int.Parse(Console.ReadLine());
suma = suma + nota;
}
prom = suma / 7.0; Console.WriteLine("el promedio es : {0}", prom);
Console.ReadLine();
}
}
}
7. Leer 10 números y hallar el mayor
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

int n, i, max;
max = 0;

for (i = 1; i <= 10; i++)


{
Console.Write("ingrese nro {0}:", i);
n = int.Parse(Console.ReadLine());
if (i == 1)
max = n;
if (n > max)
max = n;

}
Console.WriteLine("Numero mayor: {0}", max);
Console.ReadLine();

}
}
}

8. Leer 10 números y hallar el menor


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

int n, i, mim;
mim = 0;

for (i = 1; i <= 10; i++)


{
Console.Write("ingrese nro {0}:", i);
n = int.Parse(Console.ReadLine());
if (i == 1)
mim = n;
if (n < mim)
mim = n;

}
Console.WriteLine("Numero menor: {0}", mim);
Console.ReadLine();

}
}
}
9. Leer 10 números y mostrar cuantos son positivos y cuántos son negativos y mostrar cuantos ceros hay.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

int n,i,cp,cn,cc;
cp=0;
cn=0;
cc=0;

for (i = 1; i <= 10; i++)


{
Console.Write("ingrese nro {0}:", i);
n = int.Parse(Console.ReadLine());
if (n < 0)
cn = cn + 1;
else if (n > 0)
cp = cp + 1;
else
cc = cc + 1;
}
Console.WriteLine("Cantidad de positivo: {0}", cp);
Console.WriteLine("cantidad de negativo: {0}", cn);
Console.WriteLine("cantidad de ceros : {0}", cc);
Console.ReadLine();

}
}
}

10. Dado un rango numerico entero.inicial y numerico entero. Final, obtener la cantidad de
numeros positivos y negativos que existen en el rango.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int ni, nf, i, cp = 0, cn = 0;
Console.Write("Numero Inicial : ");
ni = int.Parse(Console.ReadLine());
Console.Write("Numero Final : ");
nf = int.Parse(Console.ReadLine());
for (i = ni; i <= nf; i++)
{
Console.WriteLine("{0}", i);
if (i < 0)
cn = cn + 1;
else if (i > 0)
cp = cp + 1;
}
Console.WriteLine("Cantidad de Positivos : {0}", cp);
Console.WriteLine("Cantidad de negativos : {0}", cn);
Console.ReadLine();
}
}
}
11. Ingresar dos numeros “primer numero” y “segundo numero”, mostrando sucesivamente hasta
el “segundo numero”

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indice, indice2, a, b;
Console.Write("Ingrese el primer numero : ");
indice = int.Parse(Console.ReadLine());
Console.Write("Ingrese el segundo numero : ");
indice2 = int.Parse(Console.ReadLine());
for (int i = indice; i <= indice2; i++)
Console.WriteLine("{0}", i);
Console.ReadLine();
}
}
}

12. Ingresar un numero, automáticamente que muestre los siguientes numero sucesivos pares

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indice;
Console.Write("Ingrese cantidad :");
indice = int.Parse(Console.ReadLine());
for (int i = 1; i <= indice; i++)
if (i % 2 == 0)
Console.WriteLine(i);
Console.ReadLine();

}
}
}
13. Dado un rango de números enteros, obtener la cantidad de números enteros que contiene

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i, ni, nf, can;
Console.Write("numero Inicial : ");
ni = int.Parse(Console.ReadLine());
Console.Write("Numero Final : ");
nf = int.Parse(Console.ReadLine());
ni = ni + 1;
nf = nf - 1;
for (i = ni; i <= nf; i++)
Console.WriteLine("{0}", i);
can = nf - ni + 1;
Console.WriteLine("Cantidad : {0}", can);
Console.ReadLine();
}
}
}

14. Hallar la suma y el producto de n numeros que son pares

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, i, s = 0, p = 1;
Console.Write("Ingrese Cantidad : ");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
Console.WriteLine(i * 2);
s = s + i * 2;
p = p * i * 2;
}
Console.WriteLine("suma : {0}", s);
Console.WriteLine("Producto : {0}", p);
Console.ReadLine();
}
}
}
15. Ingresar un número y mostrar sucesivamente ordenado sucesivamente hasta llegar al número
indicado.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indice,o=1;
Console.Write("Ingrese cantidad :");
indice = int.Parse(Console.ReadLine());
for (int i = 1; i <=indice ; i++)
Console.WriteLine(o+"/"+(i));
Console.ReadLine();
}
}
}

16. Ingresar un número y mostrar los números sucesivos indicados al número ingresado

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indice,o=1;
Console.Write("Ingrese cantidad :");
indice = int.Parse(Console.ReadLine());

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


Console.WriteLine(i+ "+" + (i+1) +"/"+(i+2));
Console.ReadLine();
}
}
}
17. Hallar la tabla de multiplicar hasta el 12, ingresando un numero “x”

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indice;
Console.Write("Ingrese un numero :");
indice = int.Parse(Console.ReadLine());

for (int i = 0; i <= 12; i++)


Console.WriteLine("{0}*{1}={2}",i,indice,i*indice);
Console.ReadLine();
}
}
}

18. Hallar la tabla de multiplicar del 10 * 12, siendo sucesivamente del 1 hasta el 10

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//int indice;
//Console.Write("Ingrese cantidad :");
//indice = int.Parse(Console.ReadLine());

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


for (int j = 1; j <= 12;j++ )
Console.WriteLine("{0}*{1}={2}", i, j, i * j);
Console.ReadLine();
}
}
}
19. Mostrar los números del 1 al 20, en excepción de los múltiplos de tres

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 20; i++)
{
if (i % 3 == 0) continue;
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
20. Una persona debe realizar un muestreo con 50 personas para determinar el promedio de peso de los
niños jóvenes adultos y viejos que existen en su zona habitacional. Se determinan las categorías con base
en la siguiente tabla.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, ni, jo, ad, vi, edad;
double peso, pn, pj, pa, pv, ppn, ppa,
ppj, ppv;
ni = 0;
jo = 0;
ad = 0;
vi = 0;
pn = 0;
pj = 0;
pa = 0;
pv = 0;
for (i = 1; i <= 5; i++)
{
Console.Write("ingrese edad:");
edad = int.Parse(Console.ReadLine());
Console.Write("ingrese peso:");
peso = double.Parse(Console.ReadLine());
if (edad >= 0 && edad <= 12)
{
pn = pn + peso;
ni = ni + 1;
}
else if (edad >= 13 && edad <= 29) Cambiar
{
pj = pj + peso; Nº
jo = jo + 1;
}
else if (edad >= 30 && edad <= 59)
{
pa = pa + peso;
ad = ad + 1;
}
else if (edad >= 60 && edad < 100)
{
pv = pv + peso;
vi = vi + 1;
}
}
if (ni > 0)
{
ppn = pn / ni;
Console.WriteLine("x peso niños:{0}", ppn);
}
if (jo > 0)
{
ppj = pj / jo;
Console.WriteLine("x peso jovenes: {0}", ppj);
}
if (ad > 0)
{
ppa = pa / ad;
Console.WriteLine("x peso adulto;{0}", ppa);
}
if (vi > 0)
{
ppv = pv / vi;
Console.WriteLine("x peso de viejo: {0}", ppv);
}
Console.ReadLine();
}
}
}
21. Aplicar un FOR dentro de otro FOR

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int cf;
Console.Write("Cantidad de Facturas: ");
cf = int.Parse(Console.ReadLine());
for( int i=1;i<=cf;i++)
{
Console.WriteLine("");
Console.WriteLine("Factura Número{0}",i);
Console.WriteLine("Detalle de Facturas");
for (int j = 1; j <= 3; j++)
{
Console.WriteLine("Linea de detalle(0)", j);
}
}
Console.ReadLine();
}
}
}

You might also like