Write a program that calculates N!*K! / (K-N)! for given N and K (1
Blog Archives
[C#]Loops – 4
Write a program that calculates N!/K! for given N and K (1
[C#]Conditional Statements – 8
Write a program that, depending on the user’s choice inputs int, double or string variable. If the variable is integer or double, increases it with 1. If the variable is string, appends “*” at its end. The program must show the value of that variable as a console output. Use switch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; class PlusOneOrAsterix { static void Main() { Console.WriteLine("Type a string or a number: "); string inputVar = Console.ReadLine(); int iVar; double dVar; bool itSint; bool itSdouble; if (itSint = int.TryParse(inputVar, out iVar)) { iVar++; Console.WriteLine(iVar); } else if (itSdouble = double.TryParse(inputVar, out dVar)) { dVar++; Console.WriteLine(dVar); } else { Console.WriteLine(inputVar + "*"); } } } |
[C#]Conditional Statements – 10
Write a program that applies bonus scores to given scores in the range [1..9]. The program reads a digit as an input. If the digit is between 1 and 3, the program multiplies it by 10; if it is between 4 and 6, multiplies it by 100; if it is between 7 and 9, multiplies it by 1000. If it is zero or if the value is not a digit, the program must report an error. Use a switch statement and at the end print the calculated new value in the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; using System.Linq; class ApplyBonus { static void Main() { Console.WriteLine("Enter the score (1-9) to see it with bonus:"); ushort n = ushort.Parse(Console.ReadLine()); switch (n) { case 1: case 2: case 3: n *= 10; Console.WriteLine(n); break; case 4: case 5: case 6: n *= 100; Console.WriteLine(n); break; case 7: case 8: case 9: n *= 1000; Console.WriteLine(n); break; default: Console.WriteLine("ERROR please enter a valid score (1-9)"); break; } } } |
[C#]Conditional Statements – 9
We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8 1+1-2=0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Threading; using System.Linq; using System.Collections.Generic; class CheckSubSumZero { static void Main() { int sum = 0; List<int> ListOf5 = new List<int>(); for (int i = 0; i < 5; i++) { int n = int.Parse(Console.ReadLine()); ListOf5.Add(n); } if (ListOf5.Sum() == 0) { Console.WriteLine("{0} + {1} + {2} + {3} + {4} = 0", ListOf5[0], ListOf5[1], ListOf5[2], ListOf5[3], ListOf5[4]); } for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 5; j++) { if (ListOf5[i] + ListOf5[j] == sum) { Console.WriteLine("{0} + {1} = 0", ListOf5[i], ListOf5[j]); } for (int k = j + 1; k < 5; k++) { if (ListOf5[i] + ListOf5[j] + ListOf5[k] == 0) { Console.WriteLine("{0} + {1} + {2} = 0", ListOf5[i], ListOf5[j], ListOf5[k]); } for (int m = k + 1; m < 4; m++) { if (ListOf5[i] + ListOf5[j] + ListOf5[k] + ListOf5[m] == 0) { Console.WriteLine("{0} + {1} + {2} + {3} = 0", ListOf5[i], ListOf5[j], ListOf5[k], ListOf5[m]); } } } } } } } |