I'm learning here some of C# still, but I have made a pyramid from asterisks with for loop:
using System;
namespace Nimi{
class Ohjelma{
static void Main(){
for(;;){
Console.Write("Anna korkeus: ");
string eka = Console.ReadLine();
int luku = int.Parse(eka);
//First, I made that if the number is 0 or lower, it will ask the number again.
//Hence the endless loop at start.
if(luku <= 0){
continue;
} else {
for (int i = 0; i < luku; i++ )
{
for (int k=i+1; k < luku; k++)
{
Console.Write(" ");
}
for (int j = 2*i+1; j > 0; j--)
{
Console.Write("*");
}
Console.WriteLine("");
}
break;
}
}
}
}
}
I'm just out of curiosity wanting to know how this would work with while-loop which I haven't been able to create still. The way I thought it would be:
using System;
namespace Nimi{
class Ohjelma{
static void Main(){
while(true){
// The While-loop version of endless loop. Not sure how different it is.
Console.Write("Anna korkeus: ");
string eka = Console.ReadLine();
int luku = int.Parse(eka);
if(luku <= 0){
continue;
} else {
int i = 0;
int j = i * 2 + 1;
int k = i+1;
while(i < luku)
{
while (j > 0){
while (k < luku){
Console.Write(" ");
k++;
}
Console.Write("*");
j--;
}
Console.WriteLine();
i++;
}
break;
}
}
}
}
}
Don't really work out. It only posts something like this (when value is 4:
*
What's the correct way to transfer from for-loop to while-loop to create a pyramid with asterisks?
This
using System;
namespace Nimi
{
class Ohjelma
{
static void Main()
{
for (; ; )
{
Console.Write("Anna korkeus: ");
string eka = Console.ReadLine();
int luku = int.Parse(eka);
//First, I made that if the number is 0 or lower, it will ask the number again.
//Hence the endless loop at start.
if (luku <= 0)
{
continue;
}
else
{
int i = 0;
while (i < luku)
{
int k = i + 1;
while (k < luku)
{
Console.Write(" ");
k++;
}
int j = 2 * i + 1;
while (j > 0)
{
Console.Write("*");
j--;
}
Console.WriteLine("");
i++;
}
break;
}
}
}
}
}
And remember: if your code is correctly formatted, it's easier to read. ^ED in Visual Studio to format everything.
(^ED means CTRL+E and then D (with the D you don't need the CTRL, it will work both ways)
for (int j = 2*i+1; j > 0; j--)
{
Console.Write("*");
}
becomes
{
int j = 2*i+1;
while (j > 0)
{
Console.Write("*");
j--;
}
}
Note that the "extra" curly braces preserve the locality of j, just like the for().
private static void pyramid()
{
int k = 10;
int j=0;
while(true)
{
int i = 0;
while(true)
{
if (i >= (k - j) && i <= (k + j))
{
Console.Write("*");
Console.Write("\t");
}
else
{
Console.Write("\t");
}
if (i > (j + k))
{
break;
}
i++;
}
Console.Write("\n");
if (j == (k - 1))
{
break;
}
j++;
}
}
Related
I have to create a program to print pyramid pattern (1 2 33 44 555 666...) and sum the numbers.
Here is my solution:
static void Main(string[] args)
{
int i, j;
i = 1;
int sum = 0;
while (i < 9)
{
for (j = 1; j <= i; j+=2)
{
Console.Write(i);
}
Console.Write("\n");
sum += i;
i++;
}
Console.WriteLine("Summary: " + sum);
Console.ReadLine();
}
And my output:
What am I doing wrong here (wrong summary)?
Here is an optimized and working version of your code:
int sum = 0;
for (int i = 1; i < 9; i++)
{
int current = 0;
for (int j = 1; j <= i; j += 2)
{
Console.Write(i);
current = 10 * current + i;
}
Console.WriteLine();
sum += current;
}
Console.WriteLine("Summary: " + sum);
The main issue is that you were only capturing the value of i (integer being printed) and using that to calculate the summary. As seen here, the current value is captured (for the entire line) within the nested loop and then added to the summary to give you the result you expect.
HTH
Only made small adjustments to your code.
static void Main(string[] args)
{
int i, j;
i = 1;
int sum = 0;
while (i <= 9)
{
for (j = 0; j <= i - 1; j++)
{
Console.Write(i);
sum += i * (int)Math.Pow(10, j);
}
Console.WriteLine();
i++;
}
Console.WriteLine("Sum: " + sum);
Console.ReadLine();
}
If you like to extend your code to let the user pick a number you can do this:
Instead of this line:
while (i <= 9)
You can do this:
Console.WriteLine("Please enter a number between 1 and 9:");
int maxValue = 1;
while (true)
{
if (!int.TryParse(Console.ReadLine(), out maxValue) || maxValue < 1 || maxValue > 9)
{
Console.WriteLine("Wrong input! Try again:");
continue;
}
break;
}
while (i <= maxValue)
Please find not optimized but quick solution
int i, j;
i = 1;
int sum = 0;
while (i < 9)
{
int current = 0;
for (j = 1; j <= i; j += 2)
{
current = 10 * current + i;
Console.Write(i);
}
Console.Write("\n");
sum += current;
i++;
}
Console.WriteLine("Summary: " + sum);
Console.ReadLine();
I've got two algorithms converting random 2d arrays (m х n or m х m) to 1d array. I'm wondering if there is a way to make them work als in the opposite direction and convert the result to 1d array saving the order of numbers. Here is the full code of my program and a picture to see how both of my algorithms work.enter image description here
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using static System.Math;
namespace MyProgram
{
public class Program
{
public static void Main(string[] args)
{
Console.Write("Enter number of rows:");
int n = int.Parse(Console.ReadLine());
Console.Write("Enter number of columns:");
int m = int.Parse(Console.ReadLine());
int[] arr1 = new int[n * m];
int[,] arr2 = new int[n, m];
int choice;
do
{
Console.WriteLine("Select option:");
Console.WriteLine("\t1: Diagonal");
Console.WriteLine("\t2: Spiral");
Console.WriteLine("\t3: Exit");
Console.Write("Your selection: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
SetArray(arr2);
PrintArray(arr2);
Diagonal(arr2, arr1);
PrintArray(arr1);
break;
}
case 2:
{
SetArray(arr2);
PrintArray(arr2);
Spiral(arr2, arr1);
PrintArray(arr1);
break;
}
}
Console.WriteLine();
Console.WriteLine();
} while (choice != 5);
}
static void Diagonal(int[,] array2, int[] array1)
{
int k = 0;
int row = 0;
int col = 0;
while (k < array1.Length)
{
array1[k] = array2[row, col];
if ((row + col) % 2 == 0)
{
if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; }
else
{
if (col == array2.GetLength(1) - 1) { row++; }
else { row--; col++; }
}
}
else
{
if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; }
else
{
if (row == array2.GetLength(0) - 1) { col++; }
else { row++; col--; }
}
}
k += 1;
}
}
private static void Spiral(int[,] array2, int[] array1)
{
int lengthX = array2.GetLength(0);
int lengthY = array2.GetLength(1);
int Product = lengthX * lengthY;
int CorrectY = 0;
int CorrectX = 0;
int Count = 0;
while (lengthX > 0 && lengthY > 0)
{
for (int j = CorrectY; j < lengthY && Count < Product; j++)
{
array1[Count] = array2[CorrectX, j];
Count++ ;
}
CorrectX++;
for (int i = CorrectX; i < lengthX && Count < Product; i++)
{
array1[Count] = array2[i, lengthY - 1];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthY-- ;
else break;
for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
{
array1[Count] = array2[lengthX - 1, j];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthX-- ;
else break;
for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
{
array1[Count] = array2[i, CorrectY];
Count++ ;
}
CorrectY++;
}
}
public static void SetArray(int[,] arr)
{
Random r = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = r.Next(11, 99);
}
}
}
public static void SetArray(int[] arr)
{
Random r = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(11, 99);
}
}
public static void PrintArray(int[] arr)
{
Console.Write("print 1d array:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
public static void PrintArray(int[,] arr)
{
Console.WriteLine("print 2d array:");
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Actually what you are asking is quite easy. Just change your algorithm methods to receive int rows, int cols and delegate like this
delegate void Apply(int row, int col, int index);
Then replace arr2.GetLength(0) with rows, arr2.GetLength(1) with cols, and array element assignments with delegate call.
Here is the updated Diagonal method (you can do the same with the other):
static void Diagonal(int rows, int cols, Apply action)
{
int k = 0;
int row = 0;
int col = 0;
int length = rows * cols;
while (k < length)
{
action(row, col, k);
if ((row + col) % 2 == 0)
{
if ((row == 0) && (col != cols - 1)) { col++; }
else
{
if (col == cols - 1) { row++; }
else { row--; col++; }
}
}
else
{
if ((col == 0) && (row != rows - 1)) { row++; }
else
{
if (row == rows - 1) { col++; }
else { row++; col--; }
}
}
k += 1;
}
}
and the usage:
SetArray(arr2);
PrintArray(arr2);
Diagonal(n, m, (r, c, i) => arr1[i] = arr2[r, c]);
PrintArray(arr1);
// Inverse
var arr3 = new int[n, m];
Diagonal(n, m, (r, c, i) => arr3[r, c] = arr1[i]);
PrintArray(arr3);
This seems to be working for me for backward Diagonal:
static void BackwardDiagonal(int[,] array2, int[] array1) {
int k = 0;
int row = 0;
int col = 0;
while (k < array1.Length) {
array2[row, col] = array1[k]; // just swap sides of the assignment...
if ((row + col) % 2 == 0) {
if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; } else {
if (col == array2.GetLength(1) - 1) { row++; } else { row--; col++; }
}
} else {
if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; } else {
if (row == array2.GetLength(0) - 1) { col++; } else { row++; col--; }
}
}
k += 1;
}
}
For backward Spiral:
private static void BackwardSpiral(int[,] array2, int[] array1)
{
int lengthX = array2.GetLength(0);
int lengthY = array2.GetLength(1);
int Product = lengthX * lengthY;
int CorrectY = 0;
int CorrectX = 0;
int Count = 0;
while (lengthX > 0 && lengthY > 0)
{
for (int j = CorrectY; j < lengthY && Count < Product; j++)
{
array2[CorrectX, j] = array1[Count]; // just swap sides of the assignment...
Count++ ;
}
CorrectX++;
for (int i = CorrectX; i < lengthX && Count < Product; i++)
{
array2[i, lengthY - 1] = array1[Count];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthY-- ;
else break;
for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
{
array2[lengthX - 1, j] = array1[Count];
Count++ ;
}
if (lengthY > 0 && lengthX > 0) lengthX-- ;
else break;
for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
{
array2[i, CorrectY] = array1[Count];
Count++ ;
}
CorrectY++;
}
}
I also added this to the switch statement to implement it:
case 4:
{
SetArray(arr2);
PrintArray(arr2);
Diagonal(arr2, arr1);
PrintArray(arr1);
int[,] arr3 = new int[n, m]; // new blank array to fill with arr1
BackwardDiagonal(arr3, arr1); // fill arr3 from backward Diagonal algorithm
PrintArray(arr3);
break;
}
case 5:
{
SetArray(arr2);
PrintArray(arr2);
Spiral(arr2, arr1);
PrintArray(arr1);
int[,] arr3 = new int[n, m]; // new blank array to fill with arr1
BackwardSpiral(arr3, arr1); // fill arr3 from backward Spiral algorithm
PrintArray(arr3);
break;
}
While you're at it, also make sure to have } while (choice != 3); at the end of your do loop so that you can exit the program!
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 trying to do a pyramid in C# but I can't get it to print properly.
I'm doing this for my school's C# class, can't really figure out how to get the spaces in the pyramid properly. I feel like I'm really doing something wrong here.
Instead of a triangle I'm getting something like this:
*
**
***
****
*****
******
*******
********
Here is my code:
using System;
namespace Pyramidi
{
class Ohjelma
{
static void Main()
{
int korkeusMax = 0;
int valit = 0;
do
{
Console.Write("Anna korkeus: ");
korkeusMax = Convert.ToInt32(Console.ReadLine());
if (korkeusMax > 0) {
break;
}
else {
continue;
}
}
while(true);
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
{
valit = (korkeusMax - korkeus) / 2;
for (int i = 0; i < valit; i++)
{
Console.Write(" ");
}
for (int leveys = 1; leveys <= korkeus; leveys++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Try this:
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
for (int i = 0; i < (korkeusMax - korkeus - 1); i++)
{
Console.Write(" ");
}
for (int i = 0; i < (korkeus * 2 + 1); i++)
{
Console.Write("*");
}
Console.WriteLine();
}
Alternatively, instead of loops, you can use new String('*', num). Try this:
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
Console.Write(new String(' ', korkeusMax - korkeus - 1));
Console.Write(new String('*', korkeus * 2 + 1));
Console.WriteLine();
}
You are doing the first half right, but what you want to do for the second half is another for loop that does the opposite.
So you probably want to copy your for loop and change the header from
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
to
for (int korkeus = kokeusMax; korkeus > 1; korkeus--)
or something along those lines
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";
}
}
}
}