I need help with checking IF the position in array must save ' ' or '*' for later in program to print out a pyramid.
static void Main(string[] args)
{
Console.WriteLine("Enter size: ");
int s = int.Parse(Console.ReadLine());
char[,] pyramid = new char[s, s * 2 - 1];
FillArray(pyramid);
Out(pyramid);
Console.ReadLine();
}
static void FillArray(char[,] t)
{
for (int i = 0; i < t.GetLength(0); i++)
{
for (int j = 0; j < t.GetLength(1); j++)
{
if (j == t.GetLength(1) / 2 || j == t.GetLength(1) / 2 + i || j == t.GetLength(1) / 2 - i)
{
t[i, j] = '*';
}
else t[i, j] = ' ';
}
}
}
static void Out(char[,] t)
{
for (int i = 0; i < t.GetLength(0); i++)
{
for (int j = 0; j < t.GetLength(1); j++)
{
Console.Write(t[i, j]);
}
Console.WriteLine();
}
}
At the moment I get out something like this:
The code should be like this
int i, j, n;
Console.WriteLine("Enter size: ");
n = int.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
for (j = 1; j <= n - i; j++)
Console.Write(" ");
for (j = 1; j <= 2 * i - 1; j++)
Console.Write("*");
Console.Write("\n");
}
Related
Hello. I have this task to sum the numbers as shown. Tried everything I can, but still not the right answer. Can I have some guidance?
static void Main(string[] args)
{
string input = Console.ReadLine();
int n = (int)char.GetNumericValue(input[0]);
int m = (int)char.GetNumericValue(input[2]);
int[,] matrix = new int[n, m];
int sum = 0;
//fill matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i, j] = (j * 3 + 1) + i * 3;
}
}
for (int i = 0; i < matrix.GetLength(0) - 1; i+=1)
{
for (int j = 0; j < matrix.GetLength(1) - i; j+=1)
{
if (i % 2 == 0)
{
sum += matrix[i, j + i] + matrix[i + 1, j + 1];
}
}
}
Console.WriteLine(sum);
}
I think you would've a easier time hard coding the input (and naming them as "columns" and "rows" instead, much more readable).
What is the expected output? Not sure I'm following this sum. I'm guessing, 297? If so:
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
if(j == 5) Console.WriteLine();
if (matrix[i, j] % 2 != 0)
{
if (i == 0 || i == matrix.GetLength(0) - 1
|| j == 0 || j == matrix.GetLength(0))
{
sum += (matrix[i, j]);
}
else
{
sum += (matrix[i, j] * 2);
}
}
}
}
Matrix entered the result is:
1 5 48
7 11 3
P.S User can enter any number not like above.
It should be in ascending order: 1 3 5 7 11 48
I used loop but it does not work properly
using System;
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n, sum = 0;
int count = 0;
Console.Write("The MxN matrix\n");
Console.Write("Enter the number of rows and columns of the matrix :\n");
Console.Write("Rows (M): ");
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Columns (N): ");
n = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[m, n];
Console.Write("Enter elements in the first matrix :\n");
/* Entering matrix elements */
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write("element - [{0}],[{1}] : ", i, j);
arr[i, j] = Convert.ToInt32(Console.ReadLine());
/* Calculating odd numbers of the matrix */
if (arr[i, j] % 2 != 0)
{
sum += arr[i, j];
}
}
}
Console.Write("\nThe matrix is:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", arr[i, j]);
Console.Write("\n");
}
Console.Write("\nThe sum of odd numbers is: {0}", sum);
/* Sorting Matrix in ascending order*/
for (i = 0; i < m; i++)
{
for (j = i+1; j < n; j++)
{
for (int j1 = 0; j1 < n; j1++ )
{
if (arr[i, j] > arr[i, j1]){
int temp = arr[i, j];
arr[i, j] = arr[i, j1];
arr[i, j1] = temp;
}
}
}
}
Console.Write("\nAscending order: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", arr[i, j]);
}
Console.Read();
}
}
}
Here you are.
using System;
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n, sum = 0; int count = 0; Console.Write("The MxN matrix\n"); Console.Write("Enter the number of rows and columns of the matrix :\n"); Console.Write("Rows (M): "); m = Convert.ToInt32(Console.ReadLine()); Console.Write("Columns (N): "); n = Convert.ToInt32(Console.ReadLine()); int[,] arr = new int[m, n]; Console.Write("Enter elements in the first matrix :\n");
/* Entering matrix elements */
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
{
Console.Write("element - [{0}],[{1}] : ", i, j);
arr[i, j] = Convert.ToInt32(Console.ReadLine());
/* Calculating odd numbers of the matrix */
if (arr[i, j] % 2 != 0)
{
sum += arr[i, j];
}
}
}
Console.Write("\nThe matrix is:\n");
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
Console.Write("{0} ", arr[i, j]);
Console.Write("\n");
}
Console.Write("\nThe sum of odd numbers is: {0}", sum);
/* Sorting Matrix in ascending order*/
for (i = 0; i < arr.Length - 1; i++)
{
for (j = i + 1; j < arr.Length; j++)
{
int row1 = i % m;
int col1 = i / m;
int row2 = j % m;
int col2 = j / m;
if (arr[row1, col1] > arr[row2, col2])
{
int temp = arr[row1, col1];
arr[row1, col1] = arr[row2, col2];
arr[row2, col2] = temp;
}
}
}
Console.Write("\nAscending order: ");
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
Console.Write("{0} ", arr[i, j]);
}
Console.Read();
}
}
}
class Triangle
{
static void Main(string[] args)
{
int i,j,k,odd=1,size,s=0;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces=size-1;
for (i = 1; i <= size; i++)
{
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
s = s + 1;
}
else
{
s = s - 1;
}
Console.Write(s);
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}
}
This is the code and it gives the following result:
1
232
34543
4567654
56789875
But I need the result like this:
1
121
12321
1234321
---------
Any help would be greatly appreciated. Thank you.
Add
s = 0;
at the right line in your code.
class Triangle
{
static void Main(string[] args)
{
int i,j,k,odd=1,size;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces=size-1;
int s = 0;
for (i = 1; i <= size; i++)
{
int g = 0;
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
Console.Write(j);
g = j;
}
else
{
//for (int n = j-1; n >= i; n--)
//{
// Console.Write(n - 1);
//}
Console.Write(--g);
}
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}
}
}
Thank you All!... The above code is working....
You should try this...
This is a simple example with for loops
class Triangle
{
static void Main(string[] args)
{
int size;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < size; i++)
{
for (int j = size ; j > i; j--)
{
Console.Write(" ");
}
for (int x = 1; x <= i; x++)
{
Console.Write(x);
}
for (int j = i-1; j > 0; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
Please write as below
static void Main(string[] args)
{
int i, j, k, odd = 1, size, s = 0;
Console.Write("Enter the Size:");
size = Convert.ToInt32(Console.ReadLine());
int nofSpaces = size - 1;
for (i = 1; i <= size; i++)
{
for (k = 1; k <= nofSpaces; k++)
{
Console.Write(" ");
}
s = 0;
for (j = 1; j <= odd; j++)
{
if (i >= j)
{
s = s + 1;
}
else
{
s = s - 1;
}
Console.Write(s);
}
Console.Write("\n");
odd = odd + 2;
nofSpaces = nofSpaces - 1;
}
Console.ReadKey();
}
So I'm attempting to print a multiplication table in C# however I can't quite figure out how to get what I need.
So far my program outputs the following:
1 2 3
2 4 6
3 6 9
However, I need it to output this:
0 1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
I've tried a lot of different ways to get the second output however I can't quite figure it out. I'm not necessarily asking for an answer but if someone could point me in the right direction it would be much appreciated.
This is the code I have as of now:
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.Write(i * j + "\t");
}
Console.Write("\n");
}
Console.ReadLine();
}
for (int i = 0; i <= 3; i++)
{
Console.Write(i + "\t");
for (int j = 1; j <= 3; j++)
{
if (i>0) Console.Write(i * j + "\t");
else Console.Write(j + "\t");
}
Console.Write("\n");
}
int tbl= int.Parse(Console.ReadLine());
int j = int.Parse(Console.ReadLine());
for (int i=1; i<=10; i++)
{
for (int j=1;j<=10; j++)
{
Console.WriteLine("{0}*{1}={2}", i, j, (i * j));`enter code here`
}
}
Console.ReadLine();
You should skip both 0's.
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
Console.Write((i == 0? j : (j == 0? i : i*j)) + "\t");
}
Console.Write("\n");
}
You could try one of this three solutions.
Solution 1 (without if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
Console.Write("{0}\t", i);
for (int j = 1; j <= 3; j++)
{
Console.Write("{0}\t", i * j);
}
Console.WriteLine();
}
Console.ReadLine();
}
Solution 2 (With if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (i == 0)
{
Console.Write("{0}\t", i);
}
else
{
Console.Write("{0}\t", i * j);
}
}
Console.WriteLine();
}
Console.ReadLine();
}
Solution 3 (With short-hand if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.Write("{0}\t", (i == 0) ? i : i * j);
}
Console.WriteLine();
}
Console.ReadLine();
}
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if (i == 0)
{
Console.Write(j);
}
else
{
if(j == 0)
{
Console.Write(i);
}
else
{
Console.Write(i * j);
}
}
}
Console.Write("\n");
}
Console.WriteLine("Enter A Number");
int j = Convert.ToInt32(Console.ReadLine());
for (int i = 0 ; i <= 10; i++) {
Console.WriteLine("{1} X {0} = {2}",i,j,i*j);
Console.ReadLine();
}
public class Program
{
//int num;
public static void Main(string[] args)
{
int num,num1;
Console.WriteLine("enter a any number num");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter any second number num1");
num1 = Convert.ToInt32(Console.ReadLine());
for(int i = num;i <= num1; i++)
{
for (int j = 1; j <= 10; j++)
{
Console.Write(i *j+ "\t");
}
}
Console.Write("\n");
}
}
using System;
/*
* Write a console-based application that displays a multiplication table of the product of
* every integer from 1 through 10 multiplied by every integer from 1 through 10. Save the
* file as DisplayMultiplicationTable.cs.
*/
namespace MultiplicationTable
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t\t\t\t\t\t\tMultiplication Table");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------");
const int END = 11;
for(int x = 1; x < END; x++)
{
for(int y = 1; y < END; y++)
{
int value = x * y;
Console.Write("{0} * {1} = {2}\t", y, x, value);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output
Output of Code
I am attempting to complete the above code in a GUI. So far I have come up with the following code; but the output is not like the above output.
My Code for the GUI is as follows:
using System;
using System.Windows.Forms;
namespace DisplayMultiplicationTableGUI
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void ShowTableButton_Click(object sender, EventArgs e)
{
int a;
int b;
const int STOP = 11;
for(a = 1; a < STOP; a++)
{
for(b = 1; b < STOP; b++)
{
int value = a * b;
multiplicationTableLabel.Text += String.Format("{0} * {1} = {2} ", b, a, value);
}
multiplicationTableLabel.Text += "\n";
}
}
}
}
Hello everyone
I try to solve asterisk tree problem
and found my code is not work correctly and can be improved.
This is output that expected
input : 5
*
* * *
* * * * *
* * *
*
input : 4
* * * *
* *
* * * *
and this is my code
static void Main(string[] args)
{
Console.Write("input:");
char input = Console.ReadKey().KeyChar;
if (char.IsDigit(input))
{
int couter = (int)char.GetNumericValue(input);
Console.WriteLine();
if (couter % 2 != 0)
{
for (int i = 1; i <= couter; i++)
{
for (int j = 3; j > i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write(" *");
}
Console.WriteLine();
}
for (int i = couter - 1; i >= 3; i--)
{
for (int j = 1; j <= i; j++)
{
if (j <= couter - i)
{
Console.Write(" ");
}
else
{
Console.Write("* ");
}
}
Console.WriteLine();
}
}
else
{
for (int i = couter; i > 3; i--)
{
for (int j = 1; j <= i; j++)
{
if (couter - i >= j)
{
Console.Write(" ");
}
else
{
Console.Write("* ");
}
}
Console.WriteLine();
}
for (int i = couter - 1; i <= couter; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("* ");
}
Console.WriteLine();
}
}
}
}
Please could you help me to solve this problem.
Lately, I think I'm poor at algorithms and a little complex problem. Is anybody know useful link or how I can improve this skill, please let me know.
Thanks,
Check this page for input 5 (diamond) : http://www.dreamincode.net/forums/topic/126715-diamond-asterisk/
I've translated it to C# - now it displays diamonds with size that you set in variable 'rows':
int rows = 5;
StringBuilder sb = new StringBuilder();
// top part
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
sb.Append(' ');
for (int k = 1; k <= 2 * i - 1; k++)
sb.Append('*');
sb.AppendLine();
}
//bottom part
for (int n = rows - 1; n > 0; n--)
{
for (int l = 1; l <= rows - n; l++)
sb.Append(' ');
for (int m = 1; m <= 2 * n - 1; m++)
sb.Append('*');
sb.AppendLine();
}
Console.Write(sb.ToString());
I was initially reluctant to post it because it definitely smells like homework...
Anyway, here's a piece of working code:
static void Main(string[] args)
{
Console.Write("input:");
char input = Console.ReadKey().KeyChar;
if (char.IsDigit(input))
{
int couter = (int)char.GetNumericValue(input);
Console.WriteLine();
if (couter % 2 != 0)
PrintDiamond(couter);
else
PrintHourGlass(couter);
}
Console.ReadLine();
}
private static void PrintDiamond(int couter)
{
bool moreAsterisks = true;
for (int row = 0; row < couter; row++)
{
int nAsterisks = moreAsterisks ? (2 * row) + 1 : 2 * (couter - row - 1) + 1;
int nSpaces = (couter - nAsterisks) / 2;
if (row == (couter - 1) / 2)
moreAsterisks = false;
for (int i = 0; i < nSpaces; i++)
Console.Write(" ");
for (int i = 0; i < nAsterisks; i++)
Console.Write("*");
for (int i = 0; i < nSpaces; i++)
Console.Write(" ");
Console.WriteLine();
}
}
private static void PrintHourGlass(int couter)
{
bool moreAsterisks = false;
for (int row = 0; row < couter - 1; row++)
{
int nAsterisks = moreAsterisks ? couter - 2 * (couter - row - 2) : couter - (2 * row);
int nSpaces = (couter - nAsterisks) / 2;
if (row == (couter - 2) / 2)
moreAsterisks = true;
for (int i = 0; i < nSpaces; i++)
Console.Write(" ");
for (int i = 0; i < nAsterisks; i++)
Console.Write("*");
for (int i = 0; i < nSpaces; i++)
Console.Write(" ");
Console.WriteLine();
}
}
P.S.:
it works with any number, not just 4-5...
Here's a minified LINQ solution for your problem:
class Program
{
static void Main(string[] args)
{
Console.Write("input: ");
string line = Console.ReadLine();
int n;
if (!int.TryParse(line, out n))
{
Console.WriteLine("Enter a valid integer number.");
return;
}
for (int i = 0; i < n; i++)
{
int l = Math.Abs(n - i * 2 - 1) + 1;
if (n % 2 != 0) l = n - l + 1;
Console.Write(Enumerable.Repeat(" ", n - l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
Console.WriteLine(Enumerable.Repeat("* ", l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
}
}
}
It's pretty simple; inside the loop, first line calculates length of the i-th diamond row if input is even, second one corrects the calculation for odd input. The remaining two lines print i-th row using some LINQ tricks. If you don't like showing off with LINQ, you can replace thoose lines with regular for loops (which is probably going to be faster). Then the code would look like:
class Program
{
static void Main(string[] args)
{
Console.Write("input: ");
string line = Console.ReadLine();
int n;
if (!int.TryParse(line, out n))
{
Console.WriteLine("Enter a valid integer number.");
return;
}
for (int i = 0; i < n; i++)
{
int l = Math.Abs(n - i * 2 - 1) + 1;
if (n % 2 != 0) l = n - l + 1;
//Console.Write(Enumerable.Repeat(" ", n - l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
//Console.WriteLine(Enumerable.Repeat("* ", l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
for (int c = 0; c < n - l; c++) Console.Write(" ");
for (int c = 0; c < l; c++) Console.Write("* ");
Console.WriteLine();
}
}
}