Loop Constructs
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace CSharp_Shell
{
public class Program
{
public static void Main()
{
/*
//Print Table
int number, result;
Console.Write("Enter a number : ");
number = int.Parse(Console.ReadLine());
//
for (int i = 1; i <= 10; i++)
{
result = number * i;
Console.WriteLine("{0} × {1} = {2}", number, i, result);
}
*/
//
/*
//1 22 333 4444 55555
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
*/
//
/*
//While loop
int number;
char input = 'Y';
while (input == 'Y' || input == 'y')
{
Console.Write("Enter a number : ");
number = Convert.ToInt32(Console.ReadLine());
//
Console.WriteLine("{0}^2 = {1}", number, number * number);
Console.Write("Continue : ");
input = Convert.ToChar(Console.ReadLine());
}
*/
//
//
//
/*
//Pattern Program
int number = 1;
//
Console.Write("Enter a number : ");
try
{
number = int.Parse(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
//
//
for (int i = 1; i <= number; i++)
{
for (int j = i; j < number; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write(k);
}
for (int l = i - 1; l >= 1; l--)
{
Console.Write(l);
}
Console.WriteLine();
}
*/
//
//
//
/*
//Pattern
int number;
//
Console.Write("Enter a Number : ");
number = int.Parse(Console.ReadLine());
//
//This loop first half
for (int i = 1; i <= number; i++)
{
//This loop print spaces in descending order
for (int j = i; j < number; j++)
{
Console.Write(" ");
}
//This loop print in 1234 order ++
for (int k = 1; k <= i; k++)
{
Console.Write(k);
}
//This loop print in 4321 order each time --
for (int l = i - 1; l >= 1; l--)
{
Console.Write(l);
}
Console.WriteLine();
}
// 1
// 121
//12321
//
int x = number;
int p = number - 2;
//This loop print second half upside down triangle
for (int i = 1; i < number; i++)
{
//This loop print spaces in accending order
for (int j = 1; j <= i; j++)
{
Console.Write(" ");
}
//1234 order
for (int k = 1; k < x; k++)
{
Console.Write(k);
}
x--;
//321 order
for (int z = p; z >= 1; z--)
{
Console.Write(z);
}
p--;
Console.WriteLine();
}
// 121
// 1
*/
}
}
}
Comments
Post a Comment