Skip to content

Instantly share code, notes, and snippets.

@Vijay-Kumavat
Last active September 4, 2021 10:28
Show Gist options
  • Save Vijay-Kumavat/15754ef3e7761bf2fb0584e3cba5d8e4 to your computer and use it in GitHub Desktop.
Save Vijay-Kumavat/15754ef3e7761bf2fb0584e3cba5d8e4 to your computer and use it in GitHub Desktop.
C# Programs
/****************
-> Given two integers, write a method that swaps them using temporary variable.
Expected input and output
SwapTwoNumbers(87, 45) → "Before: a = 87, b = 45; After: a = 45, b = 87"
SwapTwoNumbers(-13, 2) → "Before: a = -13, b = 2; After: a = 2, b = -13"
*****************/
using System;
public class HelloWorld {
public static void Main() {
string result;
do{
Console.Write("Enter the first number : ");
double FirstValue = Double.Parse(Console.ReadLine());
Console.Write("Enter the second number : ");
double SecondValue = Double.Parse(Console.ReadLine());
// Console.Write("Enter the third number : ");
// double ThirdValue = Double.Parse(Console.ReadLine());
Console.WriteLine("The answer is : {0}",ModuloOperations(FirstValue,SecondValue));
Console.WriteLine("Are you check again? YES : NO");
result = Console.ReadLine();
if(result.ToUpper() != "YES")
{
Console.WriteLine("Thank you, visit again!!!");
}
}while(result.ToUpper() == "YES");
}
public static (double, double) ModuloOperations(double FirstValue, double SecondValue)
{
double temp = FirstValue;
FirstValue = SecondValue;
SecondValue = temp;
return (FirstValue, SecondValue);
}
}
/****************
-> Given two integers, write a method that returns their multiplication if they are both divisible by 2 or 3,
otherwise returns thier sum.
*****************/
using System;
public class HelloWorld {
public static void Main() {
string result;
do{
Console.Write("Enter the first number : ");
double FirstValue = Double.Parse(Console.ReadLine());
Console.Write("Enter the second number : ");
double SecondValue = Double.Parse(Console.ReadLine());
// Console.Write("Enter the third number : ");
// double ThirdValue = Double.Parse(Console.ReadLine());
Console.WriteLine("The answer is : {0}",Function(FirstValue, SecondValue));
Console.WriteLine("Are you check again? YES : NO");
result = Console.ReadLine();
if(result.ToUpper() != "YES")
{
Console.WriteLine("Thank you, visit again!!!");
}
}while(result.ToUpper() == "YES");
}
public static double Function(double FirstValue, double SecondValue)
{
return (FirstValue % 2 == 0 && SecondValue % 2 == 0 || FirstValue % 3 == 0 && SecondValue % 3 == 0) ? FirstValue*SecondValue : FirstValue+SecondValue;
}
}
/****************
-> Given a 3 characters long string, write a method that checks if it consists only of uppercase letters.
*****************/
using System;
public class HelloWorld {
public static void Main() {
string result;
do{
Console.Write("Enter the first number : ");
string FirstValue = Console.ReadLine();
// Console.Write("Enter the second number : ");
// double SecondValue = Double.Parse(Console.ReadLine());
// Console.Write("Enter the third number : ");
// double ThirdValue = Double.Parse(Console.ReadLine());
Console.WriteLine("The answer is : {0}",Function(FirstValue));
Console.WriteLine("Are you check again? YES : NO");
result = Console.ReadLine();
if(result.ToUpper() != "YES")
{
Console.WriteLine("Thank you, visit again!!!");
}
}while(result.ToUpper() == "YES");
}
public static string Function(string FirstValue)
{
return (FirstValue == FirstValue.ToUpper()) ? "True" : "False";
}
}
/****************
-> Given an array of 3 integers, write a method that checks if multiplication or sum of
two first numbers is greater than third one.
*****************/
using System;
public class HelloWorld {
public static void Main() {
string result;
do{
int[] arr = new int[3]{-5, -8, 50};
// Console.Write("Enter the first number : ");
// string FirstValue = Console.ReadLine();
// Console.Write("Enter the second number : ");
// double SecondValue = Double.Parse(Console.ReadLine());
// Console.Write("Enter the third number : ");
// double ThirdValue = Double.Parse(Console.ReadLine());
Console.WriteLine("The answer is : {0}",Function(arr));
Console.WriteLine("Are you check again? YES : NO");
result = Console.ReadLine();
if(result.ToUpper() != "YES")
{
Console.WriteLine("Thank you, visit again!!!");
}
}while(result.ToUpper() == "YES");
}
public static string Function(int[] arr)
{
return (arr[0]+arr[1]>arr[2] || arr[0]*arr[1]>arr[2]) ? "True" : "False";
}
}
/*****************
-> Write a method that prints 10 by 10 multiplication table. Remember about readibility (spaces in the right place).
Expected input and output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
*****************/
using System;
class HelloWorld {
static void Main() {
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
Console.Write($"{i*j} ");
if(j==10)
{
Console.WriteLine();
}
}
}
}
}
/*****************
-> Given an integer n, write a method that returns sum of series 1 + (½)² + (⅓)² + ... + (1⁄n)². Do not use library function!
Expected input and output
FractionsSum(3) → 1.36111111111111
FractionsSum(5) → 1.46361111111111
*****************/
using System;
public class HelloWorld {
public static void Main(string[] args) {
Console.WriteLine("The answer is : {0}",Fractions(3));
}
public static double Fractions(int n)
{
double sum=0.0;
for(double j=1;j<=n;j++)
{
sum+=(1/(double)(j*j));
}
return sum;
}
}
/******************************************************************************
-> Given an array of integers, write a method that returns the biggest number in this array.
*******************************************************************************/
using System;
public class HelloWorld {
public void Main() {
Console.WriteLine("The biggestNumber is : {0}",biggestNumber(new int[]{-9, -2, -7, -8, -4}));
}
public int biggestNumber(int[] arr)
{
int biggestNum = arr[0];
for(int i=0;i<arr.Length-1;i++)
{
if(arr[i]>biggestNum)
{
biggestNum = arr[i];
}
}
return biggestNum;
}
}
/******************************************************************************
Input: Enter first number : 23
Enter second number: 45
Enter third number : 87
// check by using this method
// (a>b)?((a>c)?a:c):(b>c?b:c);
Output: Largest number is 87
*******************************************************************************/
using System;
class HelloWorld {
static void Main() {
int a=23,b=45,c=87;
Console.WriteLine("The Bigger number is : {0}",a>b?(a>c?a:c):(b>c?b:c));
}
}
/******************************************************************************
Input : I Love@#$ you
Output : Love
Input : There are many trees
Output : There
******************************************************************************/
using System;
using System.Text.RegularExpressions;
using System.Linq;
class MainClass {
public static string LongestWord(string sen) {
Regex rgx = new Regex(@"[^\w\s]");
sen = rgx.Replace(sen,"");
string[] words = sen.Split(' ');
return words.OrderByDescending(x => x.Length).First();
}
static void Main() {
Console.WriteLine(LongestWord(Console.ReadLine()));
}
}
/******************************************************************************
An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly steps,
for every step it was noted if it was an uphill, , or a downhill, step. Hikes always start and end at sea level,
and each step up or down represents a unit change in altitude. We define the following terms:
A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending
with a step down to sea level. A valley is a sequence of consecutive steps below sea level, starting with a step down
from sea level and ending with a step up to sea level. Given the sequence of up and down steps during a hike,
find and print the number of valleys walked through.
Example
The hiker first enters a valley units deep. Then they climb out and up onto a mountain units high.
Finally, the hiker returns to sea level and ends the hike.
Function Description
Complete the countingValleys function in the editor below.
countingValleys has the following parameter(s):
int steps: the number of steps on the hike
string path: a string describing the path
Returns
int: the number of valleys traversed
Input Format
The first line contains an integer , the number of steps in the hike.
The second line contains a single string , of characters that describe the path.
Sample Input
steps : 8
path : UDDDUDUU
If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:
_/\ _
\ /
\/\/
The hiker enters and leaves one valley.
******************************************************************************/
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
public static int countingValleys(int steps, string path)
{
int lvl =0, v=0;
char[] c = path.ToCharArray();
for(int i=0;i<c.Length;i++)
{
if(c[i]=='U')
++lvl;
if(c[i]=='D')
--lvl;
if(c[i]=='U' && lvl==0)
++v;
}
return v;
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int steps = Convert.ToInt32(Console.ReadLine().Trim());
string path = Console.ReadLine();
int result = Result.countingValleys(steps, path);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment