C# - What's with this weird 0 at the end? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm a newbie.
I have a problem with C#, I don't know why program prints me a "0" at the end of the first row :( Plz halp.
namespace Tabliczka_mnożenia
{
class Program
{
static void NumbersInRow(int x)
{
Console.Write(" ");
for (int i = x; i <= 10; i++)
{
Console.Write(i);
Console.Write(" ");
}
}
static void NumbersInColumn(int y)
{
for (int i = y; i <= 10; i++)
{
Console.WriteLine(i);
Console.Write(" ");
}
}
static void Main(string[] args)
{
int x = 1;
int y = 0;
Console.Write(" ");
NumbersInRow(x);
NumbersInColumn(y);
}
}
https://i.stack.imgur.com/ljsLW.jpg

The 0 is printed intentionally from the NumbersInColumn function. You have just forgotten to print a line break after the NumbersInRow function, like this:
static void NumbersInRow(int x)
{
Console.Write(" ");
for (int i = x; i <= 10; i++)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine("");
}

0 is printed by NumbersInColumn method.
WriteLine methods writes 0(y=0) and moves cursor to a new line.

In your case y starts with 0. You are then calling console.writeline() which writes whatever you want and then goes to the next line.
You should return a new line before starting to iterate through the rows.

Related

I dont get the correct answer of the factorial of the 2nd time I enter a number [closed]

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 1 year ago.
Improve this question
I don't get the correct answer of the factorial the second time I enter a number.
using System;
namespace Outcome2
{
class Program
{
static void Main(string[] args)
{
int t, FactR = 1;
do
{
Console.Write("Enter no.: ");
t = Convert.ToInt32(Console.ReadLine());
if (t < 0)
Console.WriteLine("Factorial of Negative number's can't be found ");
else if (t <= 1)
Console.WriteLine("{0}! = {1}", t, FactR);
else
{
for (int counter = t; counter >= 2; counter--)
{
FactR = FactR * counter;
}
Console.WriteLine("{0}! = {1}", t, FactR);
}
Console.WriteLine("Do you wish to quit? Y/N");
} while (Console.ReadKey().KeyChar != 'Y');
}
}
}
its probably because you don't reset the FactRvariable you can do it like this.
do{
if (t < 0)
Console.WriteLine("Factorial of Negative number's can't be found ");
else if (t <= 1)
Console.WriteLine("{0}! = {1}", t, FactR);
else
{
FactR = 1;
for (int counter = t; counter >= 2; counter--)
{
FactR = FactR * counter;
}
Console.WriteLine("{0}! = {1}", t, FactR);
}
Console.WriteLine("Do you wish to quit? Y/N");
} while (Console.ReadKey().KeyChar != 'Y');

C# program that takes an integer as input and then displays a rectangle using that integer [closed]

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 2 years ago.
Improve this question
I am a new learner of C# programming language. I came to know about StackOverflow that I can get the answers to my questions by experienced programmers.
I have made this program that takes an integer as input and makes a rectangle of a given number of rows and columns using that integer. And it's working! but I want a simpler way of doing that.
And sorry in advance if it is hard to read😅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rectangle
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Your Number: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter no. of rows: ");
int rows = Int32.Parse(Console.ReadLine());
Console.Write("Enter no. of colums: ");
int cols = Int32.Parse(Console.ReadLine());
for (int i = 0; i < rows; i++)
{
if(i == 0 || i == (rows - 1)) //In the first and last row, it will print all the column elements
{
for (int j = 0; j < cols; j++)
{
Console.Write(n);
}
Console.WriteLine();
}
else //In the mid section, only first and last column elements will be printed
{
for (int z = 0; z < cols; z++)
{
if (z == 0 || z == (cols - 1)) //first and last column elements printed
{
Console.Write(n);
}
else
{
//in the mid-column, there should be no. of spaces equal to the no. of digits to form a perfect rectangle
for (int l = 0; l < Convert.ToString(n).Length; l++)
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}
}
Console.ReadLine();
}
}
}
Heres your simplified version, enjoy
using System;
namespace Rectangle{
class Program{
static void Main(string[] args){
Console.Write("Enter Your Number: ");
string n=int.Parse(Console.ReadLine())+"",s=new String(' ',n.Length);
Console.Write("Enter no. of rows: ");
int r = int.Parse(Console.ReadLine());
Console.Write("Enter no. of colums: ");
int c = int.Parse(Console.ReadLine());
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
Console.Write(j==0||j==c-1||i==0||i==r-1?n:s);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Or a single loop one, if you like golf
using System;
namespace Rectangle{
class Program{
static void Main(string[] args){
Console.Write("Enter Your Number: ");
string n=int.Parse(Console.ReadLine())+"",s=new String(' ',n.Length);
Console.Write("Enter no. of rows: ");
int i=0,c,r=int.Parse(Console.ReadLine());
Console.Write("Enter no. of colums: ");
c=int.Parse(Console.ReadLine());
for(;i<r*c;i++)Console.Write(i%r==0||i%r==r-1||i/r==0||i/r==c-1?n+(i%r==r-1?"\n":""):s);
Console.ReadLine();
}
}
}
Or this neat thing :)
namespace System{using i=Console;class Program{static void Main(){i.Write("number: ");
string n=int.
Parse(i. ReadLine
())+"",s =new String
(' ',n. Length);i.
Write( "rows: ");
int j=0, c,r=int.
Parse(i. ReadLine
());i. Write(
"columns: " );c=int.
Parse(i. ReadLine
());for (;j<r*c;
j++)i.Write(j%r==0||j%r==r-1||j/r==0||j/r==c-1?n+(j%r==r-1?"\n":""):s);i.ReadLine();}}}

Trouble suming totals [closed]

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 5 years ago.
Improve this question
Have trouble adding a summary for columns number, square and cube for totals. Any pointers where I should be looking at? Below is copy of my code.
static void Main(string[] args)
{
int number;
int total = 0;
Console.WriteLine("number\t" + "square\t" + "cube");
Console.WriteLine("-----------------------------");
for (int i = 0; i <= 20; i += 2)
{
number = i;
int k = 0;
do
{
Console.Write(number + "\t");
number *= i;
total += number;
k++;
} while (k < 3);
Console.WriteLine("Total is",total);
Console.WriteLine();
}
Console.WriteLine("---------------------------------------");
If I understand what you want correctly, one way to do this is to keep track of the running totals for each power (1, 2, and 3) in an array, and then display those values at the end.
The array would have 3 indexes, and each time we increase the 'power' that we're raising our number to, we add that value to the corresponding index in the array.
For example:
static void Main(string[] args)
{
// This array will hold three items:
// - totals[0] = numberTotal
// - totals[1] = squareTotal
// - totals[2] = cubeTotal
var totals = new int[3];
Console.WriteLine("number\t" + "square\t" + "cube");
Console.WriteLine("-----------------------------");
for (int number = 0; number <= 20; number += 2)
{
// Grab a copy of 'number' so we don't modify the loop variable
var thisNumber = number;
for(int powerIndex = 0; powerIndex < 3; powerIndex++)
{
// Write this number to screen
Console.Write($"{0:n0}\t", thisNumber);
// Add this number to the current number in 'power' index
totals[powerIndex] += thisNumber;
// Power up
thisNumber *= number;
}
Console.WriteLine();
}
Console.WriteLine("-----------------------------");
Console.WriteLine("{0:n0}\t{1:n0}\t{2:n0}\t", totals[0], totals[1], totals[2]);
// Alternatively, if you're using C#6.0, you could write:
Console.WriteLine($"{totals[0]:n0}\t{totals[1]:n0}\t{totals[2]:n0}\t");
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
Output:
There are two errors in your code:
First one is about Console.WriteLine. To get it working you should pass parameters as for example shown here (it's not the only way to do it but it's the simplest)
Console.WriteLine("Total is" + total);
Secon one is more about algorithm. Let's check when you are adding number to total. If you look closer you can see that you are not adding same number, which you displayed, but your adding number * i ! Thats big mistake but to fix it just swap that two lines like that:
Console.Write(number + "\t");
total += number;
number *= i;
k++;
I belive that fixes every issue, hope it helps :-)
Full code:
using System;
namespace Sum
{
public class Program
{
public static void Main(string[] args)
{
int number;
Console.WriteLine("number\t" + "square\t" + "cube");
Console.WriteLine("-----------------------------");
for (int i = 0; i <= 20; i += 2)
{
number = i;
int total = 0;
int k = 0;
do
{
Console.Write(number + "\t");
total += number;
number *= i;
k++;
} while (k < 3);
Console.WriteLine("Total is "+total);
Console.WriteLine();
}
Console.WriteLine("---------------------------------------");
}
}
}

How does this code work exactly? Help me how to approah the code?

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 6;
for (; ; )
{
Console.Write(i++ + " ");
if (i <= 10)
i += 1;
else
break;
}
Console.ReadLine();
}
}
}
Output is :- 6 8 10
I am new to programming language, I wondering how it works?
As I have to write output for i++...
so it works like since its i++, it will print 6 1st?
6+1=i then increment with i++ that gives 8 at 2nd
8+1=i then increment with i++ that gives 10 at 3rd?
I don't know I'm very confused, can anyone help me is my approach to the answer right?
Its quite simple:
int i = 6;
for (; ; ) //-> This is an infinite loop
{
Console.Write(i++ + " ");//-> This prints i then increments so you get 6 first
if (i <= 10) //->This conditions fails when i = 10 and then else part executes
i += 1; //->Here i gets incremented again hence you get 6 then 8 then 10
else
break;
}
This is not good code.
When you use a "for" like that, your loop will run forever and only the "break" command will stop it.
Here is better code that does the same:
static void Main(string[] args)
{
for (int i = 6; i <= 10; i+=2)
{
Console.Write(i + " ");
}
Console.ReadLine();
}
Read more about For loops

Find numbers that don't divide by 3 or 7 [closed]

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 9 years ago.
Improve this question
I need help with a program.
I need to write a program that prints on the console numbers from 1 to N that can't divide in the same time by 3 or 7. I need to parse N so the user inputs a value for it. Here is my code for now, can you tell me what is wrong?
Console.Write("Enter a number: ");
int n = int.Parse(Console.ReadLine());
for (n = 1; n <= 99999; n++) {
//n % (3 * 7) == 0
I thought out how I will check it but I can't think out how to make the other part. I think there is something wrong with my loop too. Can you give me some hints where I am mistaken and what I can do? Thank you!
You're getting N from the user then immediately overwriting it. Use a different variable for your loop:
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
// do stuff
You can use Modulus %
Console.Write("Enter a number: ");
string input = Console.ReadLine();
int n;
if (int.TryParse(input, out n))
{
for (int i = 1; i < n; i++)
{
if(i % 3 != 0 || i% 7!= 0) Console.WriteLine(i);
}
}
Your loop is wrong because your condition is wrong.It should be i < n, you are getting an input but then you overwriting it's value by n = 1;
Console.Write("Enter a number: ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
if (i % 21 != 0) { Console.Write(i + " "); }
}
Example: http://ideone.com/ThASen

Categories

Resources