You are on page 1of 6

Lab: Loop Statements

1. Sum of Odd Numbers


Write a program that prints the next n odd numbers (starting from 1) and on the last row
prints the sum of them

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

namespace Lab2._1
{
internal class Program
{
static void Main(string[] args)
{
int i = 1;
int sum = 0;
Console.Write("Input n: ");
int n = Convert.ToInt32(Console.ReadLine());
int count = 0;
while (count < n)
{
Console.Write(" {0} ", i);
i += 2;
sum = sum + i;
count++;
}
Console.WriteLine();
Console.WriteLine("Sum: {0}", sum);
Console.ReadKey();
}
}
}
2. Multiplication Table
You will receive an integer as an input from the console. Print the 10 times table for this
integer. See the examples below for more information.

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

namespace Lab3._2
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Input n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" the 10 times table for this integer");
int i = 1;
while ( i <= 10)
{
Console.WriteLine("{0} * {1} = {2}", n, i, n * i);
i++;
}
Console.ReadKey();
}
}
}
3. Multiplication Table 2.0
Rewrite you program so it can receive the multiplier from the console. Print the table from
the given multiplier to 10. If the given multiplier is more than 10 - print only one row with the
integer, the given multiplier and the product. See the examples below for more information.

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

namespace Lab3._3
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Input n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input times: ");
int times = Convert.ToInt32(Console.ReadLine());
if (times <= 10)
{
Console.WriteLine("--------------");
Console.WriteLine("Result:");
int i = 11;
while (times <= 10)
{
Console.WriteLine("{0} * {1} = {2}", n, times, n * times);
times++;
}
}
else
{
Console.WriteLine("--------------");
Console.Write("Result: ");
Console.WriteLine("{0} * {1} = {2}", n, times, n * times);
}
Console.ReadKey();
}
}
4. Even Number
Take as an input an even number and print its absolute value. If the number is odd, print
"Please write an even number." and continue reading numbers.

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

namespace Lab3._4
{
internal class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Input n: ");
int n = int.Parse(Console.ReadLine());

if (n % 2 == 0)
{
Console.WriteLine("This is an even number {0} ", Math.Abs(n));
break;
}
else
{
Console.WriteLine("Please input an even number:");
}
}
Console.ReadKey();
}
}
}
5. Display the pattern like right angle triangle using an asterisk

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3._5
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Input height: ");
int height = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
int i = 0;

while (i <= height)


{
int j = 0;
while (j <= i)
{
j++;
Console.Write("*");
}
Console.Write("\n");
i++;
}
Console.ReadKey();
}
}
}
6. Display the pattern like right angle triangle with a number

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

namespace Lab3._6
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Input height: ");
int height = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
int i = 0;
while (i < height)
{
int j = 0;
while (j <= i)
{
j++;
Console.Write(j);
}
Console.Write("\n");
i++;
}
Console.ReadKey();
}
}
}

You might also like