0% found this document useful (0 votes)
43 views4 pages

Naturalnumbers: Using Class Static Void Int

The document contains code examples demonstrating different ways to declare, initialize, and iterate through arrays in C#. It includes examples using for loops, foreach loops, and while loops to print out array elements. Arrays of integers, strings, and other data types are demonstrated. Various ways to initialize array elements are shown such as hardcoding values or generating values in a for loop.

Uploaded by

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

Naturalnumbers: Using Class Static Void Int

The document contains code examples demonstrating different ways to declare, initialize, and iterate through arrays in C#. It includes examples using for loops, foreach loops, and while loops to print out array elements. Arrays of integers, strings, and other data types are demonstrated. Various ways to initialize array elements are shown such as hardcoding values or generating values in a for loop.

Uploaded by

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

using System;

class NaturalNumbers
{
static void Main()
{
int i;
Console.Write("\n\n");
Console.Write("Display the first 10 natural numbers:\n");
Console.Write("---------------------------------------");
Console.Write("\n\n");

Console.WriteLine("The first 10 natural numbers are:");

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


{
Console.Write("{0}", i);
}
Console.Write("\n\n");
}
}

using System;

public class Program


{
public static void Main()
{
for (int i = 0; i < 10; i++)
{
if (i == 5) break;
Console.WriteLine("Valeu of i: {0}", i);
}
}
}

Declaring an array
using System;

namespace example
{
class exemplearray
{
public static void Main()
{
int[] intArray;
intArray = new int[5];
intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 30;
intArray[3] = 40;
intArray[4] = 50;
}
}
}
using System;

namespace example
{
class exemplearray
{
public static void Main()
{
int[] intArray;
intArray = new int[5];
intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 30;
intArray[3] = 40;
intArray[4] = 50;

Console.Write("For loop:");
for (int i = 0; i < intArray.Length; i++)
Console.Write(" " + intArray[i]);
}
}
}

using System;

namespace example
{
class exemplearray
{
public static void Main()
{
int[] intArray;
intArray = new int[5];
intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 30;
intArray[3] = 40;
intArray[4] = 50;

Console.WriteLine("");
Console.Write("For-each loop:");

foreach (int i in intArray)


Console.Write(" " + i);
}
}
}
using System;

namespace example
{
class exemplearray
{
public static void Main()
{
int[] intArray;
intArray = new int[5];
intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 30;
intArray[3] = 40;
intArray[4] = 50;

Console.WriteLine("");
Console.Write("while loop:");

int j = 0;
while( j<intArray.Length)
{
Console.Write(" " + intArray[j]);
j++;
}
}
}
}

using System;

namespace example
{
class exemplearray
{
public static void Main()
{
string[] weekDays;
weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

foreach (string day in weekDays)


Console.Write(day + " ");
}
}
}

using System;

namespace example
{
class exemplearray
{
public static void Main()
{
string[] weekDays;
weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat" };

Console.Write("For loop:");
for (string day in weekDays)
Console.Write(day + " ");
}
}
}

using System;

namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int[] n = new int[10];
int i, j;

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


{
n[i] = i + 100;
}

for (j = 0; j < 10; j++)


{
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
}
}
}

using System;

namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int[] n = new int[10];
int i, j;

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


{
n[i] = i + 100;
}

foreach(int j in n)
{
int i = j - 100;
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
}
}
}

You might also like