Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
var input = Convert.ToInt32(Console.ReadLine());
for (var i = 1; i <= input; i++)
{
for (var tr = 1; tr <= input; tr++)
{
for (var ch = 1; ch <= input; ch++)
{
if (ch <= i) Console.Write("+");
else Console.Write(" ");
}
}
Console.WriteLine();
}
The program outputs triangles. The code uses three loops. How to make it so that there are only two loops and the program works the same as before?
Notice in the pattern of triangles that on the first line you print 1 + then print n-1 blanks repeatedly. On the second line you print two + then print n-2 blanks repeatedly.
To decide if you need to print a plus or a blank you can use modulo arithmetics. if c % n < lineno print + else print a blank.
You print n * n characters in each line.
That's not the complete code but a good boost to implement the solution.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Hi everyone I need to generate all strings from aa0000 to zz9999; where the first two position are only chars from a to z and the last four positions are from 0000 to 9999.
I tried everything I could but I can't find a way to do this.
Thanks in advance!
You can try nested loops, e.g.
Code:
public static IEnumerable<string> Generator() {
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
for (int i = 0; i <= 9999; ++i)
yield return $"{a}{b}{i:d4}";
}
...
foreach (string s in Generator()) {
//TODO: Put relevant code here
}
Demo:
Console.WriteLine(string.Join(Environment.NewLine, Generator()
.Skip(1_000_000 - 5) // skip some items
.Take(10))); // then take some items
Console.WriteLine();
Console.Write(Generator().Count()); // how many items do we have?
Outcome:
dv9995
dv9996
dv9997
dv9998
dv9999
dw0000
dw0001
dw0002
dw0003
dw0004
6760000
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner C# learner. I am trying to learn through an app called SoloLearn.
In the app, it wants me to replace 3 and its multiples in a series of numbers with "*".
For example; the input is n number, let's say 7, the output should go "12 * 45 * 7" (without spaces)
I tried this, it worked but these two "ifs" at the end make my eyes bleed.
using System;
using System.Collections.Generic;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
//your code goes here
for (int x = 1; x <= number; x++)
{
if (x%3==0)
Console.Write("*");
if (x%3==0)
continue;
{
Console.Write(x);
}
}
}
}
}
I played around it a little but can't seem to find any way around it.
How would you shorten these only with the content in this code given?
You don't need a continue statement there. Instead of two ifs you could use an if and an else, e.g.:
for (int x = 1; x <= number; x++)
{
if (x % 3 == 0)
{
Console.Write("*");
}
else
{
Console.Write(x);
}
}
For fun:
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
var seq = Enumerable.Range(1, number).Select(x=>x%3==0?"*":x.ToString());
Console.WriteLine(string.Join("", seq));
}
And I could, of course, shorten it further to a single line, but that just seems excessive. There's probably also a way to get an implicit ToString() conversion.
The slightly shorter version of the for loop is this:
for (int x = 1; x <= number; x++)
{
Console.Write(x % 3 == 0 ? "*" : $"{x}");
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a user input, lets say 3 digit number. I want to split the numbers and add them or multiply - it doesn't matter.
This is what I tried to do:
Console.WriteLine("enter a number: ");
int userInput = Convert.ToInt32(Console.ReadLine());
string temp = userInput.ToString();
int x = 0;
foreach(char c in temp)
{
x += c;
Console.WriteLine(x);
}
Console.ReadLine();
I'm not allowed to use %
Any suggestion?!
If you want to calculate sum of digits of a number, you can read characters of the string and convert them to numbers and then use them:
string number = Console.ReadLine();
var sum = number.ToArray().Sum(x => int.Parse(x.ToString()));
Console.WriteLine(sum);
Don't forget to add using System.Linq;
Also as an option without using Linq:
string number = Console.ReadLine();
int sum = 0;
foreach (char c in number)
{
sum += int.Parse(c.ToString());
}
Console.WriteLine(sum);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I try to do, is for example if I write five numbers 1,2,3,4,5 after this it should print 5,4,3,2,1
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length; i++)
{
}
If you need just to print them in a reverse order, but you want to keep them stored as you inserted them, change the second loop like this:
for (int i=numbers.Length-1; i >= 0; i--){
Console.WriteLine(numbers[i]);
}
You can print the array with a reverse loop as in the previous answer, or you can do that in a single line:
Console.WriteLine(string.Join(",", numbers.Reverse()));