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;
}
}
}
Related
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
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
int[] a = new int[m];
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1s in the Entered Number : "+count);
Console.ReadLine();
}
here get each value into the array, and check each value equal to one are not. But i need this task without using an array. Could you please help us.
Just check the input when it's entered, without storing it:
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
if(Console.ReadLine() == "1")
count++;
}
Console.WriteLine("Number of 1's in the Entered Number : "+count);
Console.ReadLine();
}
You can simply keep a count where you add it to the array
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
count += Console.ReadLine() == "1" ? 1 : 0;
}
Console.WriteLine("Number of 1's in the Entered Number : "+count);
Console.ReadLine();
You could use LINQ and remove the for loop as well as the array.
Console.WriteLine("Enter the Limit : ");
int m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
int count =
Enumerable
.Range(0, m)
.Select(n => Console.ReadLine())
.Where(x => x == "1")
.Count();
Console.WriteLine("Number of 1's in the Entered Number : " + count);
Console.ReadLine();
I would advice using more meaningful variable names and adding input validation Int32.TryParse instead Convert.ToInt32.
Just do the if (o == 1) check in the first for and forget about the second one.
You can use List instead of array.
code is here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberofOnes
{
public class Program
{
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
List<int> a = new List<int>();
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a.Add( Convert.ToInt32(Console.ReadLine()));
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1's in the Entered Number : " + count);
Console.ReadLine();
}
}
}
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'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!");
}
Can that be done with no while loops?
static void Main(string[] args)
{
Console.WriteLine("Please enter a number");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" #" + Fibonacci(number));
}
public static int Fibonacci(int number)
{
if (number <= 1)
{
return 1;
}
else
{
return Fibonacci(number - 2) + Fibonacci(number - 1);
}
}
I can't even add a Console.WriteLine in the body of base case since it gets executed [number] number of times; Not sure how to do this without loops...
static void Main(string[] args)
{
Console.WriteLine("Please enter a number");
int number = Convert.ToInt32(Console.ReadLine());
Fibonacci(0, 1, 1, number);
}
public static void Fibonacci(int a, int b, int counter, int number)
{
Console.WriteLine(a);
if (counter < number) Fibonacci(b, a+b, counter+1, number);
}
public static int Fibonatchi(int position) {
if(position == 0) {
return 1;
}
if(position == 1) {
return 1;
} else {
return Fibonatchi(position - 2) + Fibonatchi(position - 1);
}
}
I didn't find a way to do it closest way is it to combine both loops + recursion
static void Main(string[] args)
{
Console.WriteLine("Please enter a number");
int number = Convert.ToInt32(Console.ReadLine());
for(int counter=0;counter<number;counter++)
Console.WriteLine(" \n" + Fibonacci(counter) );
}
public static int Fibonacci(int number)
{
if (number == 0)
return 0;
else if(number ==1)
return 1;
else
{
return Fibonacci(number - 2) + Fibonacci(number - 1);
}
}
namespace Algorithms
{
class Program
{
static void Main(string[] args)
{
string fibResult = "";
fibResult = FibCal(10);
Console.WriteLine(fibResult);
Console.ReadLine();
}
public static string FibCal(int n)
{
string series = "";
int k, f1, f2 , f = 0;
f1 = f2 = 1;
if (n < 2)
return n.ToString();
else
for (k = 0; k < n; k++)
{
f = f1 + f2;
f2 = f1;
f1 = f;
series += f.ToString() + ",";
}
return series;
}
}
}
Hope this helps
Using recursion in this fashion is a very bad idea. It will cause memory problems very quickly. I know you want to avoid using while/for loops, but an array is really the best way to go.
Using LINQ
public static void fibSeriesEx3()
{
List<int> lst = new List<int> { 0, 1 };
for (int i = 0; i <= 10; i++)
{
int num = lst.Skip(i).Sum();
lst.Add(num);
foreach (int number in lst)
Console.Write(number + " ");
Console.WriteLine();
}
}
That's a way to do it by returning a value into the main.
public static void Main() {
Console.WriteLine("Introduce the number");
int num = Convert.ToInt32(Console.ReadLine());
int num1 = 1, num2 = 1, counter = num-2;
//Take 2 out to match the list as first 2 numbers doesn't count in the function.
Console.WriteLine(Fibo(num1, num2, counter));
}
public static int Fibo(int num1, int num2, int counter) {
int temp = num1;
if (counter <= 0)
return num2;
else
return Fibo(num1 = num2, num2 += temp, counter-1);
}
public static class Golden
{
public static IEnumerable<long> Fibonacci()
{
var a = 0L;
var b = 1L;
var s = 0L;
yield return a;
while (a < long.MaxValue - b)
{
yield return b;
s = a + b;
a = b;
b = s;
}
}
public static IEnumerable<long> FibonacciR()
{
IEnumerable<long> Fibo(long a, long b)
{
yield return a;
if (a < long.MaxValue - b)
{
foreach (var v in Fibo(b, a + b))
{
yield return v;
}
}
}
return Fibo(0, 1);
}
}
I realize this may be an old thread, but oh well I think this kinda question is good in its nature.
Using while loop/or recursive way of doing is not the optimal way of doing as it takes a O(2^n) times. A better way to do this is using what is already in memory like below. This should take at most O(n) time.
Cheers!
static double fibDynamic(int n)
{
double[] array = new double[n];
array[0] = array[1] = 1;
for(int i = 2; i < n; i++)
{
array[i] = array[i - 1] + array[i - 2];
}
return array[n-1];
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
int n, i = 0, c;
Console.WriteLine("Enter the number of terms:");
n = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Fibonacci series\n");
for (c = 1; c <= n; c++)
{
int result = FibonacciFunction(i);
Console.Write(result + " " );
i++;
}
Console.WriteLine();
return 0;
}
public static int FibonacciFunction(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return (FibonacciFunction(n - 1) + FibonacciFunction(n - 2));
}
}
}
}
Simple and easy solution :
static void Main(string[] args)
{
int number;
Console.WriteLine("enter number");
number = int.Parse(Console.ReadLine());
Console.WriteLine(Recursive(number));
Console.ReadLine();
}
public static int Recursive(int number)
{
if (number <= 2)
{
return 1;
}
else
{
return Recursive(number - 1) + Recursive(number - 2);
}
}
Using one line code:
public static int Fibonacci(int i)
{
return i <= 2 ? 1 : Fibonacci(i - 1) + Fibonacci(i - 2);
}