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
Besides a .txt, where else i can insert data using C# ?
string file = GenerarPath(Environment.SpecialFolder.MyDocuments, "Pascal Triangle.txt");
var tw = new StreamWriter(file);
Console.WriteLine("\t\t Pascal Triangle in C#");
Console.WriteLine("\t________________________________________");
Console.Write("\nNumber of Rows: ");
int number = int.Parse(Console.ReadLine());
int x = number * 2;
for (int i = 0; i <= number; ++i, x -= 2)
{
for (int s = 0; s <= x; ++s)
tw.Write(" ");
for (int k = 0; k <= i; ++k)
//Console.Write(String.Format("{0,4:D}", formula(i, k)));
tw.Write(String.Format("{0,4:D}",pascal(i,k)));//formula(i, k)));
tw.WriteLine();
}
tw.Close();
Console.WriteLine("Finished");
Console.ReadKey();
Thank you
Almost anywhere. like databases (Sqlserver, access), word files, txt files as you have done.
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 1 year ago.
Improve this question
I'm new to the programming world and I'm learning C#. I have an algorithm given to me in expression language and I need to do it with C# .NET Framework console application, but I'm not very good at it. Can you help?
Here's the algorithm I'm talking about;
Start
int toplam, carpım, karetoplam
toplam=0
carpım=1
Read N
6.Cycle I=1, N, 1
if(N%2==1)
toplam=toplam+I
carpım=carpım*I
else
karetoplam=karetoplam+(I*I)
if over
cycle over
Cw toplam
Cw carpım
Cw karetoplam
Done
I only understood the title of your post not the text ;)
Code is untested. Could be optimized to 1 for-loop in 1 method.
Product of oddnumber from 1 to n?
long GetProductsOfOddNumbers(int n) {
var product = 1L;
for (var i = 1; i <= n; i++) {
if (i % 2 == 0) continue;
product *= i;
}
return product;
}
Sum of squares of even numbers from 1 to n?
long GetSumOfSquaresOfEvenNumbers(int n) {
var sum = 0L;
for (var i = 1; i <= n; i++) {
if (i % 2 != 0) continue;
sum += (i * i);
}
return 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 1 year ago.
Improve this question
I'm trying to solve a task from HackerRank, but I'm not able to solve it. Any help is appreciated!
public static void plusMinus(List<int> arr)
{
float noOfPositive = 0;
float noOfNegative = 0;
float noOfZero = 0;
float length = arr.Count;
for(var i = 0; i < arr.Count; i++){
if(i > 0){
noOfPositive += 1;
}else if(i == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
Console.WriteLine($"{noOfPositive/length}\n{noOfNegative/length}\n{noOfZero/length}");
input:
6
-4 3 -9 0 4 1
My Result:
0.8333333
0
0.1666667
Expected:
0.500000
0.333333
0.166667
You are checking the value of i not arr[i].
So your code should be:
for(var i = 0; i < arr.Count; i++){
if(arr[i] > 0){
noOfPositive += 1;
}else if(arr[i] == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
And your expected values are wrong, you are putting in 7 items and expecting half of them to be positive so 3.5 items are positive?
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 5 years ago.
Improve this question
Program generates 50 random numbers (between 1 to 10) and tells the amount of values that are smaller than 5 and bigger than 5 (C#)
Try this:
int n = 50
IList<int> randomNumbers = new List<int>(n);
Random ran = new Random(1);
for (int i = 0; i < n; i++)
{
randomNumbers.Add(ran.Next(1, 10));
}
int lessThan5Count = randomNumbers.Count(c => c < 5);
int greaterThan5Count = randomNumbers.Count(c => c > 5);
You could do it like that:
Random r = new Random();
int n = 50;
int smaller_than_5 = 0;
int bigger_than_5 = 0;
double[] d = new double[n];
for (int i = 0; i < n; i++)
{
d[i] = 1 + r.NextDouble() * 9;
if (d[i] < 5) smaller_than_5++;
else if (d[i] > 5) bigger_than_5++;
}
Hope this helps ...
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
String Array contains values like {'2','4','8'}
if 8 is present, want to add 999 to it,if string array contains 999 want to add 8 to it.
If both 8 and 999 are present in array no need to add any value to array.
Have a look at the following:
public string[] Add8or999(string[] source)
{
string[] output = source;
if (source.Contains("8") && source.Contains("999"))
{
// what to do here?
}
else if (source.Contains("8"))
{
output = new string[source.Length + 1];
for (int i = 0; i < source.Length; i++)
{
output[i] = source[i];
}
output[source.Length] = "999";
}
else if (source.Contains("999"))
{
output = new string[source.Length + 1];
for (int i = 0; i < source.Length; i++)
{
output[i] = source[i];
}
output[source.Length] = "8";
}
return output;
}
Basic Usage:
string[] s = Add8or999(new string[] {"8", "9", "10"});
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
I try Start the console and after i write 5 numbers is do exsepsion what wrong in code
what the console do is after I write 5 number for example 7,4,9,5,1 ---> 1,5,9,4,7
this what I wrote
static void Main(string[] args)
{
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 / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
Console.Read();
You missed two things. First - in order to swap two array items, you need to store item somewhere. Otherwise you just replace one item with other. Second - arrays have indexes from 0 to Length - 1. So you need to subtract 1 from second item index:
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}