0% found this document useful (0 votes)
19 views

C# Basic Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C# Basic Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise 1: Add two numbers

Given three numbers, write a method that adds two first ones and multiplies them
by a third one. Input will be gotten from users through Main method
Expected input and output
AddAndMultiply(2, 4, 5) → 30

Exercise 2: Celsius to Fahrenheit


Given a temperature in Celsius degrees, write a method that converts it to
Fahrenheit degrees. Remember that temperature below -271.15°C (absolute zero)
does not exist! Input will be gotten from users through Main method
Expected input and output
CtoF(0) → "T = 32F"
CtoF(100) → "T = 212F"
CtoF(-300) → "Temperature below absolute zero!"

Exercise 3: Elementary operations


Given two integers, write a method that returns results of their elementary
arithmetic operations: addition, substraction, multiplication, division. Remember
that you can't divide any number by 0! Input will be gotten from users through
Main method.
Expected input and output
ElementaryOperations(3, 8) → 11, -5, 24, 0.375

Exercise 4: The cube of


Given a number, write a method that returns its cube. Input will be gotten from
users through Main method.
Expected input and output
TheCubeOf(2) → 8
TheCubeOf(-5.5) → -166.375

Exercise 5: Swap two numbers


Given two integers, write a method that swaps them using temporary variable.
Input will be gotten from users through Main method.
Expected input and output
SwapTwoNumbers(87, 45) → a = 45, b = 87
SwapTwoNumbers(-13, 2) → a = 2, b = -13

Exercise 6: Absolute value


Given an integer, write a method that returns its absolute value. Input will be
gotten from users through Main method.
Expected input and output
AbsoluteValue(6832) → 6832
AbsoluteValue(-392) → 392

Exercise 7: Divisible by 2 or 3
Given two integers, write a method that returns their multiplication if they are both
divisible by 2 or 3, otherwise returns their sum. Input will be gotten from users
through Main method.
Expected input and output
DivisibleBy2Or3(15, 30) → 450
DivisibleBy2Or3(2, 90) → 180
DivisibleBy2Or3(7, 12) → 19

Exercise 8: If number is even


Given an integer, write a method that checks if it is even. Input will be gotten from
users through Main method.
Expected input and output
IfNumberIsEven(721) → false
IfNumberIsEven(1248) → true

Exercise 9: If year is leap


Given a year as integer, write a method that checks if year is leap. Input will be
gotten from users through Main method.
Expected input and output
IfYearIsLeap(2016) → true
IfYearIsLeap(2018) → false

Exercise 10: Return even numbers


Write a method that returns a string of even numbers greater than 0 and less than
100.
Expected input and output
ReturnEvenNumbers() → "2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90
92 94 96 98"

Exercise 11: Sort array ascending


Given an array of integers, write a method that returns sorted array in ascending
order.
Expected input and output
SortArrayAscending([9, 5, 7, 2, 1, 8]) → [1, 2, 5, 7, 8, 9]
Exercise 12: Sum and average
Given two integers n and m (n <= m), write a method that returns sum of all
integers and average from range [n, m]. Input will be gotten from users through
Main method.
Expected input and output
SumAndAverage(11, 66) → "Sum: 2156, Average: 38.5"
SumAndAverage(-10, 0) → "Sum: -55, Average: -5"

Exercise 13: Length of string


Given a string, write a method that returns its length. Do not use library methods!
Input will be gotten from users through Main method.
Expected input and output
LengthOfAString("computer") → 8
LengthOfAString("ice cream") → 9

Exercise 14: String in reverse order


Given a string, write a method that returns that string in reverse order. Input will be
gotten from users through Main method.
Expected input and output
StringInReverseOrder("qwerty") → "ytrewq"
StringInReverseOrder("oe93 kr") → "rk 39eo"

Exercise 15: Negative or positive


Given a number, write a method that returns number to the power of 2 if negative
or square root if positive or zero. Input will be gotten from users through Main
method.
Hint: Use Math.Pow() and Math.Sqrt()
Expected input and output
NegativeOrPositive(-2) → 4
NegativeOrPositive(6.25) → 2.5

Exercise 16: Replace x with y


Write a method that replaces every letter 'y' in the string with 'x'. Assume that
string contains only lower case letters. Input will be gotten from users through
Main method.
Hint: Use functions from String library (Contains, Replace, Join…)
Expected input and output
ReplaceXWithY("yellow") → "xellow"
ReplaceXWithY("mushroom") → "mushroom"

Exercise 17: Greater number


Given two numbers, write a method that returns greater one. Input will be gotten
from users through Main method.
Hint: Use Math.Max()
Expected input and output
GreaterNumber(2.1, 3) → 3
GreaterNumber(-5, 0) → 0
GreaterNumber(-111.22,111.222) → 111.222

You might also like