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 6 years ago.
Improve this question
I know how to calculate a factorial using a loop. Below is the code for loop, but I am getting an error while doing it by recursion. Below are both the code samples. How can I fix this?
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
}
}
Factorial using recursion:
Is there something as where I am going wrong? When am I calculating it using recursion?
Factorial using loop:
public double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number * factorial_recursion(number - 1);
}
public double factorial_WhileLoop(int number)
{
double result = 1;
while (number != 1)
{
result = result * number;
}
return result;
}
Your call name is not equal to your method name:
factorial_Recursion is the method name.
factorial_recursion is the call.
This worked for me:
namespace Testing
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(factorial_Recursion(5));
Console.WriteLine("press any Key");
Console.ReadLine();
}
public static double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number*factorial_Recursion(number - 1);
}
Related
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 9 months ago.
Improve this question
Using the C# language, have the function StringChallenge(num) take the num
parameter being passed and return the number of hours and minutes the parameter *
converts to (ie. if num = 63 then the output should be 1:3). Separate the number *
of hours and minutes with a colon.
The question above. Problem is how to convert time with colon as return in integer function?
using System;
class MainClass {
public static int StringChallenge(int num) {
// code goes here
int hour=0;
int min=0;
if (num<60)
{
min = num;
}
if (num>60)
{
min = num % 60;
hour = num / 60;
}
return num ;
}
static void Main() {
// keep this function call here
Console.WriteLine(StringChallenge(Console.ReadLine()));
}
}
Let TimeSpan do the work for you:
int num = 60;
TimeSpan ts = TimeSpan.FromMinutes(num);
string formatted = ts.ToString(#"hh\:mm");
which outputs a string like
01:00
To change the format, different patterns can be used. Check out the documentation on this if necessary.
Use string interpolation
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
using System;
class MainClass {
public static string StringChallenge(int num) {
Console.WriteLine(num);
// code goes here
int hour=0;
int min=0 ;
if (num<60)
{
min = num;
}
if (num>60)
{
min = num % 60;
hour = num / 60;
}
return $"{hour} : {min}";
}
static void Main() {
// keep this function call here
Console.WriteLine(StringChallenge(int.Parse(Console.ReadLine())));
}
}
Here I have taken the method:
public static string StringChallenge(int minute)
{
return $"{minute / 60}:{minute % 60}";
}
from a 59 turns into "0:59"
from a 60 turns into "1:0"
from a 61 turns into"1:1"
In general case, I can see two problems here:
Large num, say num = 1600, we want 26:40, not 1 day 2:40
Negative numbers: we want -12:30, not -12:30
Code
// Note, that we should return string, not int
// d2 - formatting - to have leading zeroes, i.e. 5:01 instead of 5:1
public static string StringChallenge(int num) =>
$"{num / 60}:{Math.Abs(num % 60):d2}";
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 2 years ago.
Improve this question
I need to access an element from an array and I am getting this error right here Console.WriteLine(testChoice[0]);
Why is this happening? Here is my code:
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] m1 = { 100 };
int a = m1.ElementAtOrDefault(0);
Console.WriteLine("Type m1 underneath");
string test;
test = Console.ReadLine();
int testChoice;
testChoice = Convert.ToInt32(test);
for (int i = 0; i < 1; i++)
{
Console.WriteLine(testChoice[0]);
}
}
}
The variable which index you are trying to access, testChoice, is not an array. It is an int variable. To print it just get rid of the for loop and the index:
static void Main()
{
int[] m1 = { 100 };
int a = m1.ElementAtOrDefault(0);
Console.WriteLine("Type m1 underneath");
string test;
test = Console.ReadLine();
int testChoice;
testChoice = Convert.ToInt32(test);
Console.WriteLine(testChoice);
}
To access an array element by user input:
//class variable
private static int _testChoice;
static void Main()
{
int[] testChoices = { 100, 200, 300 };
//int a = m1.ElementAtOrDefault(0);
//Console.WriteLine("Type m1 underneath");
string indexInput;
Console.WriteLine("Please specify the index:");
indexInput = Console.ReadLine();
int index;
index = Convert.ToInt32(indexInput );
_testChoice = testChoices[index];
Console.WriteLine(_testChoice);
}
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 4 years ago.
Improve this question
I have a maybe simple problem. When i start program it show "Operator '==' cannot be applied to operands of type 'string' and 'int'. I dont know what to do with it. Thanks for every help.
Here is code:
class Program
{
static bool check(string input_number)
{
for (int i = 0; i < input_number.Length / 2; i++)
if (input_number[i] != input_number[input_number.Length - i - 1])
return false;
return true;
}
static void Main(string[] args)
{
var results = from i in Enumerable.Range(100, 900)
from j in Enumerable.Range(i, 1000 - i)
let k = i * j
where (check(k.ToString() == 1)
orderby k descending
select new { i, j, k };
var highestResult = results.FirstOrDefault();
if (highestResult == null)
Console.WriteLine("There are no palindromes!");
else
Console.WriteLine($"The highest palindrome is {highestResult.i} * {highestResult.j} = {highestResult.k}");
Console.ReadKey();
}
}
You are trying to compare strings (k.ToString() with numbers (1). In your case, I think, you need to do this: where check(k.ToString()) == true).
You can't compare a string and an int directly, due to C#'s strong typing. Instead, you should "convert" the int to a string, using ToString.
edit: or just not call ToString on k?
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 4 years ago.
Improve this question
using System;
namespace Zadacha
{
class Zadacha
{
static int Read(int x, int y)
{
Random rnd = new Random();
Console.WriteLine("Vuvedete minimalna velichina");
string MinValue = Console.ReadLine();
Console.WriteLine("Vuvedete maximalna velichina");
string MaxValue = Console.ReadLine();
int.TryParse(MinValue, out x);
int.TryParse(MaxValue, out y);
int value = rnd.Next(x, y);
Console.WriteLine("Proizvodnoto chislo e: " + value);
Console.ReadKey(true);
return value;
}
static void Main()
{
}
}
}
This is my code, program just starts and shutdowns after a second with no text in the console app. Everything seems fine and i do not know what is wrong. It is a uni task.
You don't do anything in your Main method - you need to call your Read(x,y) method at least, and/or Console.ReadLine(); in order for something to happen.
For example
static void Main()
{
Read(1,2);
}
Is this what you are trying to achieve? Console apps start from Main() so you'll have to put something in there for the program to work. I guess you don't actually want to feed Read() two int you want to get the users input?
using System;
namespace Zadacha
{
class Zadacha
{
static int Read()
{
int x = 0;
int y = 0;
Random rnd = new Random();
Console.WriteLine("Vuvedete minimalna velichina");
string MinValue = Console.ReadLine();
Console.WriteLine("Vuvedete maximalna velichina");
string MaxValue = Console.ReadLine();
int.TryParse(MinValue, out x);
int.TryParse(MaxValue, out y);
int value = rnd.Next(x, y);
Console.WriteLine("Proizvodnoto chislo e: " + value);
Console.ReadKey(true);
return value;
}
static void Main()
{
Read();
}
}
}
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 8 years ago.
Improve this question
How can I write a code in C# to find the sum of letters
If A=0;B=1,C=A+B,D=B+C,E=C+D.....
Example CD=1+2=3,
I have tried this way where input is string and output is sum of letter
using System;
public class Test
{
public static (int output1)
public static void Main(string input1)
{
// your code goes here
}
}
An answer without using dictionary list
class Program
{
static void Main(string[] args)
{
string test = "abcdef";
int sum = 0;
foreach (char c in test)
{
int letterNumber = char.ToUpper(c) - 64;
sum += rtnDegint(letterNumber);
}
Console.WriteLine(sum);
}
int rtnDegint(int n)
{
int first = 0, second = 1, next = 0, c;
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
}
return next;
}
}