If I wanted to modify a console app that currently prints:
1
12
123
1234
to print
1
121
12321
1234321
Which loop should i modify?
Below is the code from the original console app.
int altura; string space = ""; int cont2 = 0;
Console.Write("Dar altura: ");
altura = int.Parse(Console.ReadLine());
for (int i = 1; i <= altura; i++)
{
space = "";
for (int j = 1; j <= i; j++)
{
space = space + Convert.ToString(j);
}
Console.WriteLine(space);
}
Console.ReadLine();
Thank you in advance.
Try this
int altura; string space = "";
int cont2 = 0;
Console.Write("Dar altura: ");
altura = int.Parse(Console.ReadLine());
for (int i = 1; i <= altura; i++)
{
space = "";
for (int j = 1; j <= i; j++)
{
space = space + Convert.ToString(j);
}
for (int k = i - 1; k >= 1 ; k--)
{
space = space + Convert.ToString(k);
}
Console.WriteLine(space);
}
Console.ReadKey();
int altura; string space = ""; int cont2 = 0;
Console.Write("Dar altura: ");
altura = int.Parse(Console.ReadLine());
for (int i = 1; i <= altura; i++)
{
var stack = new System.Collections.Generic.Stack<int>();
space = "";
for (int j = 1; j <= i; j++)
{
space = space + Convert.ToString(j);
stack.Push(j);
}
stack.Pop();
while (stack.Count > 0)
{
space = space + Convert.ToString(stack.Pop())
}
Console.WriteLine(space);
}
Console.ReadLine();
Or for fun:
int altura;
Console.Write("Dar altura: ");
altura = int.Parse(Console.ReadLine());
var lines = Enumerable.Range(1, altura).Select(i =>
{
var line = Enumerable.Range(1, i).ToArray();
var reverse = line.Reverse().Skip(1).ToArray();
return String.Join("", line.Concat(reverse).Select(c => c.ToString()).ToArray())
});
foreach(string line in lines)
{
Console.Writeline(line);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int rows=5;
for(int i=1;i<=rows;i++){
for(int j=1;j<=i;j++){
Console.Write(" "+j);
}
for(int k=i-1;k>=1;k--){
Console.Write(" "+k);
}
Console.WriteLine();
}
}
}
}
Related
I am new in this fascinating world of programming. I have done this array, but when I type a non integer it crashes. I have tried many ways like int.Parse(console.readLine)), tryparse(text, out int) and ConvertTo32 ,However it continues saying that "Input string was not in correct format." Thanks
using System;
namespace BubbleSort
{
class Program
{
public static void HelpME(int[] a, int t)
{
for (int j = 0; j <= a.Length - 2; j++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
}
static void Main(string[] args)
{
int[] num = { 1, 2, 3, 4, 5 };
int[] a = new int[5];
for (int x = 0; x < 5; x++)
{
Console.WriteLine($"Input enter {num[0 + x]} of five");
a[0 + x] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
{
HelpME(num, 5);
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
{
Console.Write(aray + " ");
}
Console.ReadLine();
}
}
}
you should validate the user unput by using int.TryParse method. If the entered string can be converted to int then only it should be inserted into the array, otherwise the program should ignore that value.
static void Main(string[] args)
{
int[] num = { 1, 2, 3, 4, 5 };
int[] a = new int[5];
for (int x = 0; x < 5; x++)
{
Console.WriteLine($"Input enter {num[0 + x]} of five");
int temp = 0;
string input = Console.ReadLine();
if(int.TryParse(input, out temp))
{
a[0 + x] = Convert.ToInt32(input);
}
}
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
{
HelpME(num, 5);
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
{
Console.Write(aray + " ");
}
Console.ReadLine();
}
I am trying to write a program that sums very big numbers (I am trying to solve a problem on projecteuler.net), so I cannot parse them into number types. So I was wondering if it is possible to sum such numbers using only strings or something like that?
Try using BigInteger instead. It effictively lets you use integers of arbitrary size.
kindly use BigInteger found in System.Numerics and use `
Add(BigInteger, BigInteger)
` please follow the below link for better understanding.
add two bigintegers
BigInteger Represents an arbitrarily large signed integer. microsoft documentation.
class Program
{
static void Main(string[] args)
{
string inputString = Console.ReadLine();
string[] linePart = inputString.Split(' ', ',', '\n'); // split as , and " " and new line
/* Console.WriteLine(linePart[1]);*/
string num0 = linePart[0];
string num1 = linePart[1];
int val0 = num0.Length;
int iv0 = val0;
int val1 = num1.Length;
int iv1 = val1;
/* Console.WriteLine(val0);
Console.WriteLine(val1)*/;
int arraySize;
if (val0 > val1)
{
arraySize = val0;
}
else { arraySize = val1; }
int[] arr0 = new int[arraySize];
int[] arr1 = new int[arraySize];
for (int i =0; i <iv0; i++)
{
arr0[i] = num0[val0-1]-48;
val0--;
}
for (int i = 0; i < iv1; i++)
{
arr1[i] = num1[val1-1]-48;
val1--;
}
/* for (int i = 0; i < arraySize; i++)
{
Console.Write(arr0[i]);
Console.Write(" ");
Console.Write(arr1[i]);
Console.WriteLine();
}*/
int tamp=0;
int rem=0;
int[] ans = new int[arraySize+1];
int ansloop = arraySize;
for(int i = 0; i <= arraySize; i++)
{
if (i != arraySize)
{
tamp = arr0[i] + arr1[i];
tamp = tamp + rem;
if (tamp >= 10)
{
tamp = tamp % 10;
rem = 1;
}
else { rem = 0; }
ans[ansloop] = tamp;
ansloop--;
}
else {
ans[ansloop] = rem;
}
}
for (int i = 0; i <= arraySize; i++)
{
Console.Write(ans[i]+" ");
}
}
}
}
I'm novice in C# and cause I request help me to implement this:
*
*
***
*
***
*****
*
***
*****
*******
*
***
*****
*******
*********
I just had this code:
class Program
{
static void Main(string[] args)
{
AnotherTriangle ob = new AnotherTriangle();
ob.CreateTriangle();
Console.ReadLine();
}
}
class AnotherTriangle
{
int n;
string result = "*";
public void CreateTriangle()
{
flag1:
Console.Write("Please enter number of triangles of your tree: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine();
if (n <= 0)
{
Console.WriteLine("Wrong data type\n"); goto flag1;
}
string s = "*".PadLeft(n);
for (int i = 0; i < n; i++)
{
Console.WriteLine(s);
s = s.Substring(1) + "**";
for (int j = 0; j < n; j++)
{
Console.WriteLine(s);
}
}
}
}
At now I have only "tower", not equilateral triangle. Please help.
Console.WriteLine("Please enter the number of triangles in your tree: ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
string branch = new String('*', j);
Console.WriteLine(branch.PadLeft(n + 3) + "*" + branch);
}
}
A working example.
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";
}
}
}
}