I want to draw a Line chart of the 3n+1 problem but I don't know what to do.
Here's my 3n+1 program
using System;
public class Program
{
public static void Main()
{
Console.Write("Enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());
while (n != 1)
{
if (0 == n % 2)
{
n = n / 2;
Console.WriteLine(n);
}
else
{
n = 3 * n + 1;
Console.WriteLine(n);
}
}
Console.WriteLine("End of program");
}
}
Help please :D
Thankyou
Related
I am absolutely new to C# and I am at total lost. What I need to do is enter numbers as many as possible and keep entering but when you enter the value "0" that is when you add all the entered numbers all up.
This is my program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Activity2
{
class Program
{
static void Main(string[] args)
{
int n, sum, x = 0;
do
{
Console.WriteLine("Enter a Number: ");
n = int.Parse(Console.ReadLine());
}
while (n != 0);
{
sum = n + x;
x = n;
n = sum;
Console.WriteLine("The sum is: " + n);
}
Console.ReadLine();
}
}
}
Some advices:
The while loop is a better practice than the do..while loop.
You should use the int.TryParse method for input validation.
You should calculate sum of numbers inside loop.
You can solve the problem with only two int variables: n for readed numbers and sum for numbers sum.
For example, you can solve your problem with the following code:
static void Main(string[] args)
{
int sum = 0;
while (true)
{
Console.WriteLine("Enter a Number: ");
int n;
if (int.TryParse(Console.ReadLine(), out n))
{
if (n == 0)
break;
sum += n;
}
}
Console.WriteLine("The sum is: " + sum);
}
you can simply do that with do ... while ... loop.
private static void Main(string[] args)
{
int n, sum = 0;
do
{
Console.WriteLine("Enter a number:");
n = Convert.ToInt32(Console.ReadLine());
sum += n;
} while (n != 0);
Console.WriteLine("Sum is:"+sum);
Console.ReadKey();
}
ConvertToInt32() is a method for converting a string to int32 (int).
You print the sum outside while loop.
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Activity2
{
class Program
{
static void Main(string[] args)
{
int n, sum, x = 0;
do
{
Console.WriteLine("Enter a Number: ");
n = int.Parse(Console.ReadLine());
}
while (n != 0);
{
sum = n + x;
x = n;
n = sum;
}
Console.WriteLine("The sum is: " + n);
Console.ReadLine();
}
}
}
I want to enter 10 numbers, and then show them in 1 line like this:
1,4,5,2,456,23,... and so on..
and it keeps writing them as I am entering them, and in the end when it's supposed to show all numbers in 1 line it shows only the last one.
I know it's possible with random numbers but when I enter them on my own I don't know how not to show them at all or keep them in 1 line and if it is even possible?
int a;
int x;
Console.WriteLine("a:");
a = int.Parse(Console.ReadLine());
x = 10;
for (int i = 0; i < x; i++)
{
a = int.Parse(Console.ReadLine());
}
Console.ReadKey();
you can use
Console.ReadKey(true)
it reads a key from console and does not show it.
you can use this to read word from console without showing it
public static string ReadHiddenFromConsole()
{
var word = new StringBuilder();
while (true)
{
var i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (i.Key == ConsoleKey.Backspace)
{
if (word.Length > 0)
{
word.Remove(word.Length - 1, 1);
Console.Write("\b \b");
}
}
else
{
word.Append(i.KeyChar);
Console.Write("*");
}
}
return word.ToString();
}
You can use this code:
static void Main(string[] args)
{
int length = 10;
int[] myNumbers = new int[length];
for (int i = 0; i < length; i++)
{
Console.Write("Enter number:" );
myNumbers[i] = Convert.ToInt32(Console.ReadLine());
Console.Clear();
}
Console.WriteLine("Your numbers: {0}", string.Join(",", myNumbers));
}
i dont know how to write them at the end all in 1 line?
Well you need to save them as they are entered:
int num;
var nums = new List<int>();
while (nums.Count < 10)
{
Console.Write("Enter: ");
if (int.TryParse(Console.ReadLine(), out num))
{
nums.Add(num);
Console.Clear();
}
}
Console.WriteLine(string.Join(", ", nums));
I'm a beginner in programming and I've started out with C# not long ago. My question is, how do I get back to the start of this small program if the user enters a smaller number to the second slot?
here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
Console.Write("Starting number? -> ");
int number1 = int.Parse(Console.ReadLine());
Console.Write("Ending number? -> ");
int number2 = int.Parse(Console.ReadLine());
if (number1 >= number2)
{
Console.WriteLine("Starting number has to be smaller than the ending number!");
}
var number = new Queue<int>();
number.Enqueue(9999);
int fizzorbuzz = number.Dequeue();
for (int i = number1; i < number2; i++)
{
number.Enqueue(i);
}
foreach (int i in number)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FIZZBUZZZ");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz!");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz!");
}
else
{
Console.WriteLine(i);
break;
}
}
}
}
}
I came up with a solution based on all the helpful code I got from you all.
This is what I had in mind, it works just like I wanted:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
int smallerNumber = 0;
int biggerNumber = 0;
bool running = true;
while (running)
{
Console.Write("Starting number? -> ");
smallerNumber = int.Parse(Console.ReadLine());
Console.Write("Ending number? -> ");
biggerNumber = int.Parse(Console.ReadLine());
if (smallerNumber >= biggerNumber)
{
Console.WriteLine("Starting number has to be smaller than the ending number!");
}
else
{
running = false;
}
}
var number = new Queue<int>();
number.Enqueue(9999);
int fizzorbuzz = number.Dequeue();
for (int i = smallerNumber; i < biggerNumber; i++)
{
number.Enqueue(i);
}
foreach (int i in number)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FIZZBUZZZ");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz!");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz!");
}
else
{
Console.WriteLine(i);
}
}
}
}
}
int number1;
int number2;
do{
Console.Write("Starting number? -> ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Ending number? -> ");
number2 = int.Parse(Console.ReadLine());
if (number1 >= number2)
{
Console.WriteLine("Starting number has to be smaller than the ending number!");
}
}
while (number1 >= number2);
In addition to a simple while loop, consider also this reusable scenario
static void Main(string[] args)
{
while(DoJob()) {}
}
static bool DoJob()
{
...
return true; // want restart
...
return false; // want exit
}
You can use something like do..while loop as shown below :-
int number1, number2;
do{
Console.Write("Starting number? -> ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Ending number? -> ");
number2 = int.Parse(Console.ReadLine());
}while (number1 <= number2);
{
Console.WriteLine("Starting number has to be smaller than the ending number!");
}
This is what I have so far:
namespace factorials
{
class Program
{
static void Main(string[] args)
{
int number;
do
{
Console.WriteLine("What non-negative integer do you want to factorial?");
while (!int.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter a whole number only");
calculate(ref number);
} while (number >= 0);
Console.WriteLine("Please enter a non-negative number");
}
static void calculate(ref int number)
{
int factorial;
int counter;
for (counter = number; counter <= number; counter++)
{
factorial = number * number;
Console.WriteLine("The factorial of {0} is {1}", number, factorial);
}
}
}
}
Right now it just gives me the square of the numbers, not the factorial of them. How do I make it repeat the number of times as the input so it results in a factorial?
Also I am not sure if it's necessary to limit the program to non-negative integers only but if I want to that part is just ending the program right there instead of looping back to the beginning.
The for loop doesn't make any sense at all! If you are looking for a factorial, then you have to multiply all the numbers from one to the given number. That would be:
int factorial = 1;
for (counter = 1; counter <= number; counter++)
{
factorial = factorial * counter;
}
Console.WriteLine("The factorial of {0} is {1}", number, factorial);
This would calculate the factorial of one number. You would have to repeat it for all the numbers you wanted.
Your loop assigns the square of the number to the result in a loop, and exits right away. You need to change it so that the result is repeatedly multiplied by numbers from 1 to N, inclusive.
Assign 1 to factorial, and multiply it by counter inthe loop:
factorial *= counter;
Don't forget to start your counter at 1.
I will give you hint.
Have a variable initialized to 1. Let's say it as Answer.
Run a loop from 1 to Number doing the operation of Answer = Answer * loopIterationVariable. After each iteration, increment loop iteration variable by 1.
static void Main(string[] args)
{
Console.WriteLine("Enter an integer number to factorise");
int ans = 1;
int a = int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
ans = ans * i;
}
Console.WriteLine(ans.ToString());
Console.ReadLine();
}
string str = Interaction.InputBox("Enter the number to find the factorial of: ");
double counter = double.Parse(str);
long factorial = 1;
while (counter > 1)
{
factorial *= (long)counter--;//factorial = factorial * counter - 1
}
MessageBox.Show("The factorial of " + str + " is " + String.Format("{0:N}", factorial));
I used a Microsoft.VisualBasic reference for Interaction.InputBox() method
Recursive Implementation as well as basic implementation is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication50
{
class Program
{
static void Main(string[] args)
{
NumberManipulator manipulator = new NumberManipulator();
Console.WriteLine("Please Enter Factorial Number:");
int a= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("---Basic Calling--");
Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a));
Console.WriteLine("--Recursively Calling--");
Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a));
Console.ReadLine();
}
}
class NumberManipulator
{
public int factorial(int num)
{
int result=1;
int b = 1;
do
{
result = result * b;
Console.WriteLine(result);
b++;
} while (num >= b);
return result;
}
public int recursively(int num)
{
if (num <= 1)
{
return 1;
}
else
{
return recursively(num - 1) * num;
}
}
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
userinputF = Int32.Parse(txtFaculteit.Text);
for (counter = 1; counter <= userinputF; counter++)
{
answer = answer *= counter;
}
}
catch (Exception exception)
{
MessageBox.Show("Please fill in a number " + exception.Message);
}
lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
}
}
}
int userinputF;
int counter;
int answer =1;
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
userinputF = Int32.Parse(txtFaculteit.Text);
for (counter = 1; counter <= userinputF; counter++)
{
answer = answer *= counter;
}
}
catch (Exception exception)
{
MessageBox.Show("Please fill in a number " + exception.Message);
}
lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
}
}
}
I am trying to write a program that will take the users input value and asks whether they want to calculate the value of the numbers 1 to n or the factorial of n!
This is what I have so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_2_Part_B
{
class Program
{
static void Main(string[] args)
{
var Fkeylow = "f";
var FkeyCap = "F";
var Skeylow="s";
var SkeyCap="S";
int n = 0;
long factorial = n;
Console.WriteLine("Input a value n");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Do you want to calculate factorial or sum");
Console.WriteLine("Enter F or S");
var A = Console.ReadLine();
if (A == Fkeylow)
Console.WriteLine();
if (A == FkeyCap)
Console.WriteLine();
var B=Console.ReadLine();
if (B == Skeylow)
Console.WriteLine();
if (B == SkeyCap)
Console.WriteLine();
Console.WriteLine("Press any key to close...");
Console.ReadLine();
}
}
}
My issue is with the syntax of the calculation to make the code execute the n*(n-1) while n>1.
static void Main(string[] args)
{
Console.WriteLine("Input a value n");
string number = Console.ReadLine(); // Read number
int n = Convert.ToInt32(number); // Converting to int
Console.WriteLine("Do you want to calculate factorial or sum? ");
Console.WriteLine("Enter F or S. ");
string choose = Console.ReadLine(); // F or S
int result = -1; // To view later
if (choose == "f" || choose == "F")
{
result = 1;
for (int i = n; i >= 1; i--) // Loop for calculating factorial
result *= i;
}
else if (choose == "s" || choose == "S")
{
result = 0;
for (int i = n; i >= 1; i--) // Loop for calculating sum
result += i;
}
Console.WriteLine(result); // Printing answer
Console.WriteLine("Press any key to close...");
Console.ReadLine();
}
Using recursion is another way of doing it:
static void Main(string[] args)
{
int n = 5;
Console.WriteLine(factorial(n));
Console.WriteLine(sum(n));
Console.Read();
}
public static int sum(int n)
{
if(n==0)
return 0;
return n+sum(n-1);
}
public static int factorial(int n)
{
if (n == 1)
return 1;
return n * factorial(n - 1);
}
Prints:
120 -->5!
15 -->1+2+3+4+5
Basic solution is here, tested,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication50
{
class Program
{
static void Main(string[] args)
{
NumberManipulator manipulator = new NumberManipulator();
Console.WriteLine("Factorial of six is :" + manipulator.factorial(16));
Console.ReadLine();
}
}
class NumberManipulator
{
public int factorial(int num)
{
int result=1;
int b = 1;
do
{
result = result * b;//fact has the value 1 as constant and fact into b will be save in fact to multiply again.
Console.WriteLine(result);
b++;
} while (num >= b);
return result;
}
}
}