C# exercises and solutions-C# Loops: for loop


C# Loops: for loop exercises

Exercise 1: Write C# code to produce the output shown below:

*******

******

*****

****

***

**

*

Solution:

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

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j;
for(i=0;i<=6;i++){
  for(j=1;j<=7-i;j++) Console.Write("*");
Console.Write("\n");
}


Console.ReadLine();


   }
 }
}

Exercise 2: Write C# code to print the following pattern:

1******

12*****

123****

1234***

12345**

123456*

1234567

Solution:

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

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j,k;

   for (i = 1; i <= 7; i++)
    {
       for (j = 1; j <= i; ++j)
          Console.Write(j);

       for (k = 7 - i; k >= 1; k--)
          Console.Write("*");

 Console.Write("\n");
}


Console.ReadLine();


  }
 }
}




Comments

Jhong comment

 Jhong

Here is my answer on the exercise 1 for loop:

string asterisk = "*******";

for (int i = asterisk.Length; i > 0; i--)
{
string sub = asterisk.Substring(0, i);
Console.WriteLine(sub);
}

Console.ReadLine();


2017-01-25
jru comment

 jru

Here's how i wrote my first "for loop" exercise.

for (int x = 7; x > 0; x-=1)
{
for (int i = 0; i < x; i++)
{
Console.Write("*");
}
Console.WriteLine();

}
Console.ReadKey();


2017-01-15
vimal comment

 vimal

sdhkdfhkj


2016-08-08
Alithinos comment

 Alithinos

I did the first exercise kind of different:

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

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


for (int j = 7; j > 0; j--)
{
switch (j)
{
case 7:
Console.WriteLine("*******");
break;

case 6:
Console.WriteLine("******");
break;


case 5:
Console.WriteLine("*****");
break;

case 4:
Console.WriteLine("****");
break;

case 3:
Console.WriteLine("***");
break;

case 2:
Console.WriteLine("**");
break;

case 1:
Console.WriteLine("*");
break;

default:
break;
}

}





Console.ReadKey();


}


}
}


2016-03-18
Landonius comment

 Landonius

string first = "";
for (int j = 1; j <= 7; j++)
{
first = string.Concat(first, j.ToString());
Console.Write(first);
for (int k = j; k <7; k++)
{
Console.Write("*");
}
Console.WriteLine();
}


2016-02-03
Antonia comment

 Antonia

Ex. 1
==================================
char[] myArray = new char[] {'*','*','*','*','*','*','*'};

for (int i = 6; i > 0; i--)
{
myArray[i]=' ';
Console.WriteLine(myArray);
}
Console.ReadLine();
==================================

Ex.2
==================================
char[] myArray = new char[] {'1','*','*','*','*','*','*'};

for (int i = 0; i < 7; i++)
{
myArray[i]=char.Parse((i+1).ToString());
Console.WriteLine(myArray);
}
Console.ReadLine();
==================================


2015-07-27
shitss comment

 shitss

1******

12*****

123****

1234***

12345**

123456*

1234567

Solution:

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

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j,k;

for (i = 1; i <= 7; i++)
{
for (j = 1; j <= i; ++j)
Console.Write(j);

for (k = 7 - i; k >= 1; k--)
Console.Write("*");

Console.Write("\n");
}


Console.ReadLine();


}
}
}


2015-03-07
Timmy comment

 Timmy

Code for.....
*
* A *
* A * A *
* A * A * A *

for (int i = 0; i < 4; i++){
for (int x = 1; x <= i; x++) Console.Write("*A");
Console.WriteLine("*");}


2015-03-03
blitzen9x comment

 blitzen9x

Just another solution for Exercise 1:

for(int i=0; i<8; i++)
{
for(int j=7-i; j>0; j--)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();


2015-02-12
saroj comment

 saroj

program to find the average of given n numbers given by user


2015-01-29
Brett comment

 Brett

Exercise 2 solution is not correct.

I used

class Program
{
static void Main(string[] args)
{
StringBuilder numbers = new StringBuilder();
int i = 1;


for (int j = 6; j >= 0; j--)
{
numbers.Append(i.ToString());
Console.Write(numbers + String.Concat(Enumerable.Repeat("*", j)) + "\n");
i++;
Console.WriteLine();
}

Console.ReadKey();
}
}


2015-01-21
Simplicity comment

 Simplicity

for (var i = 7; i > 0; i--) {
Console.Write(String.Concat(Enumerable.Repeat("*", i)) + "\n\n");
}


2014-11-21
Mahesh comment

 Mahesh

Can u give me code for

*
* A *
* A * A *
* A * A * A *

maheshj1993@gmail.com


2014-08-18
Lambada comment

 Lambada

string s = "";

for (int i = 1; i <= 7; i++)
{
s += i;
Console.Write(s);
for (int j = i; j < 7; j++)
{
Console.Write("*");
}
Console.WriteLine();
Console.WriteLine();
}
}


2014-08-08
Qasim comment

 Qasim

public void Exercise10()
{
/*
* Exercise 2: Write C# code to print the following pattern:

1******

12*****

123****

1234***

12345**

123456*

1234567
*/

for (int a = 1; a <= 7; a++)
{
for (int d = 1; d <=a; d++)
{
Console.Write(d);
}

for (int b = 6; b >= a; b--)
{
Console.Write("*");
}
Console.WriteLine("\n");
}
}


2014-08-06
Gufran Ali comment

 Gufran Ali

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

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 8; i++)
{
for (int k=1,j=i,x=1; x < 8; x++)
{
if (k <= j) Console.Write(k++);
else Console.Write("*");

}
Console.WriteLine();
}



Console.ReadKey();
}
}
}


2014-08-05
Kurlander comment

 Kurlander

string z = "*********";
string c = "123456789";
string comb = "";
for (int i = 0; i < 10; i++)
{
comb = c.Substring(0, i) + z.Substring(i, z.Length - i);
Console.WriteLine(comb);
}
Console.ReadLine();


2014-06-06



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.