c# lessons and homeworks
Would you like to react to this message? Create an account in a few clicks or log in to continue.

if constructions

Go down

if constructions Empty if constructions

Писане by iodan Пон Апр 27, 2015 10:23 pm

1.      Да се напише if-конструкция, която проверява стойността на две целочислени променливи и разменя техните стойности, ако
стойността на първата променлива е по-голяма от втората.
2.      Напишете програма, която показва знака (+ или -) от произведението на три реални числа, без да го пресмята. Използвайте
последователност от if оператори.
3.      Напишете програма, която намира най-голямото по стойност число, измежду три дадени числа.
4.      Сортирайте 3 реални числа в намаляващ ред. Използвайте вложени if оператори.
5.      Напишете програма, която за дадена цифра (0-9), зададена като вход, извежда името на цифрата на български език.
6.      Напишете програма, която при въвеждане на коефициентите (a, b и c) на квадратно уравнение: ax2+bx+c, изчислява и извежда
неговите реални корени (ако има такива). Квадратните уравнения могат да имат 0, 1 или 2 реални корена.
7.      Напишете програма, която намира най-голямото по стойност число измежду дадени 5 числа.
8.      Напишете програма, която по избор на потребителя прочита от конзолата променлива от тип int, double или string. Ако
променливата е int или double, трябва да се увеличи с 1. Ако променливата е string, трябва да се прибави накрая символа "*".
Отпечатайте получения резултат на конзолата. Използвайте switch конструкция.
9.      Дадени са пет цели числа. Напишете програма, която намира онези подмножества от тях, които имат сума 0. Примери:
-  Ако са дадени числата {3, -2, 1, 1, 8}, сумата на -2, 1 и 1 е 0.
-  Ако са дадени числата {3, 1, -7, 35, 22}, няма подмножества със сума 0.
10.   Напишете програма, която прилага бонус точки към дадени точки в интервала [1..9] чрез прилагане на следните правила:
-  Ако точките са между 1 и 3, програмата ги умножава по 10.
-  Ако точките са между 4 и 6, ги умножава по 100.
-  Ако точките са между 7 и 9, ги умножава по 1000.
-  Ако точките са 0 или повече от 9, се отпечатва съобщение за грешка.
11.   * Напишете програма, която преобразува дадено число в интервала [0..999] в текст, съответстващ на българското произношение на числото. Примери:
-  0 → "Нула"
-  12 → "Дванадесет"
-  98 → "Деветдесет и осем"
-  273 → "Двеста седемдесет и три"
-  400 → "Четиристотин"
-  501 → "Петстотин и едно"
-  711 → "Седемстотин и единадесет"
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.1

Писане by iodan Пон Апр 27, 2015 10:33 pm

//1.      Да се напише if-конструкция, която проверява стойността на две целочислени
//променливи и разменя техните стойности, ако стойността на първата променлива е
//по-голяма от втората.


1.    Write an if-statement that takes two integer variables and exchanges their values if the first one is greater than the second one.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
     {
       static void Main(string[] args)
       {
         
           int firstNumber = int.Parse(Console.ReadLine());
           int secondNumber = int.Parse(Console.ReadLine());


           if (firstNumber > secondNumber)
            {
               Console.WriteLine(firstNumber+" "+secondNumber);
           }
           else
           {
               Console.WriteLine(secondNumber+" "+firstNumber);
           }

        }
   }
}


Последната промяна е направена от iodan на Сря Апр 29, 2015 8:04 pm; мнението е било променяно общо 3 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.2

Писане by iodan Пон Апр 27, 2015 10:39 pm

//2.      Напишете програма, която показва знака (+ или -) от произведението на три реални числа, без да го пресмята. Използвайте последователност от if оператори.

2. Write a program that shows the sign (+ or -) of the product of three real numbers, without calculating it. Use a sequence of if operators.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
    {
       static void Main(string[] args)
       {
           int firstVariable = int.Parse(Console.ReadLine());
           int secondVariable = int.Parse(Console.ReadLine());
           int thirdVariable = int.Parse(Console.ReadLine());
           int sum = firstVariable + secondVariable + thirdVariable;

           if (sum > 0)
           {
               Console.WriteLine("+");
           }
           else if (sum == 0)
           {
               Console.WriteLine("The sum is zero");
           }
           else
           {
               Console.WriteLine("-");
           }
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:29 pm; мнението е било променяно общо 3 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.3

Писане by iodan Пон Апр 27, 2015 10:43 pm

//3.      Напишете програма, която намира най-голямото по стойност число, измежду три дадени числа.

3. Write a program that finds the biggest of three integers, using nested if statements.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
           int a = int.Parse(Console.ReadLine());
           int b = int.Parse(Console.ReadLine());
           int c = int.Parse(Console.ReadLine());

           if (a > b && b > c)
           {
               Console.WriteLine(a);
           }
           else if (a < b && b > c)
           {
               Console.WriteLine(b);
           }
           else Console.WriteLine(c);
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:31 pm; мнението е било променяно общо 3 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.4

Писане by iodan Пон Апр 27, 2015 11:09 pm

//4.      Сортирайте 3 реални числа в намаляващ ред. Използвайте вложени if оператори.


4. Sort 3 real numbers in descending order. Use nested if statements.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
     {
       static void Main(string[] args)
       {
           double a = double.Parse(Console.ReadLine());
           double b = double.Parse(Console.ReadLine());
           double c = double.Parse(Console.ReadLine());

           if (a>b&&b>c)
           {
               Console.WriteLine("{0} {1} {2}",  a, b, c);
           }
           else if (a>=c&&c>=b)
           {
               Console.WriteLine("{0} {1} {2}", a, c, b);
           }
           else if (b>=a&&a>=c)
           {
               Console.WriteLine("{0} {1} {2}", b, a, c);
           }
           else if (b>=c&&c>=a)
           {
               Console.WriteLine("{0} {1} {2}", b, c, a);
           }
           else if (c>=a&&a>=b)
           {
               Console.WriteLine("{0} {1} {2}", c, a, b);
           }
           else if (c>=b&&b>=a)
           {
               Console.WriteLine("{0} {1} {2}", c, b, a);
           }
       
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:32 pm; мнението е било променяно общо 3 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.5

Писане by iodan Пон Апр 27, 2015 11:13 pm

//5.      Напишете програма, която за дадена цифра (0-9), зададена като вход, извежда името на цифрата на български език.

5.    Write a program that asks for a digit (0-9), and depending on the input, shows the digit as a word (in Bulgarian or English, you choоse). Use a switch statement

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
int n = int.Parse(Console.ReadLine());

           switch (n)
          {
               case 1: Console.WriteLine("едно"); break;
               case 2: Console.WriteLine("две"); break;
               case 3: Console.WriteLine("три"); break;
               case 4: Console.WriteLine("четири"); break;
               case 5: Console.WriteLine("пет"); break;
               case 6: Console.WriteLine("шест"); break;
               case 7: Console.WriteLine("седем"); break;
               case 8: Console.WriteLine("осем"); break;
               case 9: Console.WriteLine("девет"); break;
               case 0: Console.WriteLine("нула"); break;
               default: Console.WriteLine("invalid number"); break;
           }
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:35 pm; мнението е било променяно общо 4 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.6

Писане by iodan Пон Апр 27, 2015 11:23 pm

//6.      Напишете програма, която при въвеждане на коефициентите (a, b и c) на квадратно уравнение: ax2+bx+c, изчислява и извежда неговите реални корени (ако има такива). Квадратните уравнения могат да имат 0, 1 или 2 реални корена.

6. Write a program that gets the coefficients a, b and c of a quadratic equation: ax2 + bx + c, calculates and prints its real roots (if they exist). Quadratic equations may have 0, 1 or 2 real roots.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.Write("a= ");
           string str = Console.ReadLine();
           double a = double.Parse(str);
           Console.Write("b= ");
           str = Console.ReadLine();
           double b = double.Parse(str);
           Console.Write("c= ");
           str = Console.ReadLine();
           double c = double.Parse(str);

           if (a == 0) //условието за а!=0
           {
               Console.WriteLine("\"a\" is invalid");
               return;
           }

           double D = (b * b) - (4 * a * c);// изчисляване на дискриминантата
           if (D < 0) // ако D<0
           {
               Console.WriteLine("No real roots");
           }
           else if (D == 0)//ако D=0
           {
               Console.WriteLine("One real root");
               double x = -b / (2 * a);
               Console.WriteLine("x= " + x);
           }
           else// ако D>0
           {
               Console.WriteLine("Two real roots");
               double x1 = (-b + Math.Sqrt(D)) / (2 * a);
               Console.WriteLine("x1= " + x1);
               double x2 = (-b - Math.Sqrt(D)) / (2 * a);
               Console.WriteLine("x2= " + x2);
           }
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:36 pm; мнението е било променяно общо 2 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.7

Писане by iodan Пон Апр 27, 2015 11:35 pm

//7.      Напишете програма, която намира най-голямото по стойност число измежду дадени 5 числа.


7. Write a program that finds the greatest of given 5 numbers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
           int a = int.Parse(Console.ReadLine());
           int b = int.Parse(Console.ReadLine());
           int c = int.Parse(Console.ReadLine());
           int d = int.Parse(Console.ReadLine());
           int e = int.Parse(Console.ReadLine());

           if (a > b && a > c && a > d && a > e)
           {
               Console.WriteLine(a);
           }
           else if (a < b && b > c && b > d && b > e)
           {
               Console.WriteLine(b);
           }
           else if (a < c && b < c && c > d && c > e)
           {
               Console.WriteLine(c);
           }
           else if (a < d && b < d && c < d && d > e)
           {
               Console.WriteLine(d);
           }
           else Console.WriteLine(e);

       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:37 pm; мнението е било променяно общо 2 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.8

Писане by iodan Пон Апр 27, 2015 11:43 pm

//8.      Напишете програма, която по избор на потребителя прочита от конзолата променлива от тип int, double или string. Ако променливата е int или double, трябва да се увеличи с 1. Ако променливата е string, трябва да се прибави накрая символа "*". Отпечатайте получения резултат на конзолата. Използвайте switch конструкция.


8. Write a program that, depending on the user’s choice, inputs int, double or string variable. If the variable is int or double, the program increases it by 1. If the variable is a string, the program appends "*" at the end. Print the result at the console. Use switch statement.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {

           Console.WriteLine("Enter your choise:\r\n1 - int type\r\n2 - double type\r\n3 - string type");
           int n = int.Parse(Console.ReadLine());
           int a;
           double b;
           string c;

           if (n > 1 || n < 3)
           {
               switch (n)
               {
                   case 1: Console.WriteLine("enter a number: ");
                       a = int.Parse(Console.ReadLine());
                       Console.WriteLine(a + 1);
                       break;
                   case 2: Console.WriteLine("enter a number");
                       b = double.Parse(Console.ReadLine());
                       Console.WriteLine(b + 1);
                       break;
                   case 3: Console.WriteLine("enter your choise");
                       c = Console.ReadLine();
                       Console.WriteLine(c + "*");
                       break;
                   default: Console.WriteLine("invalid choise"); break;

               }
           }

       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:40 pm; мнението е било променяно общо 2 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.9

Писане by iodan Пон Апр 27, 2015 11:49 pm

//9.      Дадени са пет цели числа. Напишете програма, която намира онези подмножества от тях, които имат сума 0. Примери:
           //-  Ако са дадени числата {3, -2, 1, 1, 8}, сумата на -2, 1 и 1 е 0.
           //-  Ако са дадени числата {3, 1, -7, 35, 22}, няма подмножества със сума 0.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace only_body
{
   class Program
   {
       static void Main(string[] args)
       {

       }
   }
}
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.10

Писане by iodan Пон Апр 27, 2015 11:51 pm

           //10.   Напишете програма, която прилага бонус точки към дадени точки в интервала [1..9] чрез прилагане на следните правила:
           -  Ако точките са между 1 и 3, програмата ги умножава по 10.
           -  Ако точките са между 4 и 6, ги умножава по 100.
           -  Ако точките са между 7 и 9, ги умножава по 1000.
           -  Ако точките са 0 или повече от 9, се отпечатва съобщение за грешка.


10. Write a program that applies bonus points to given scores in the range [1…9] by the following rules:
- If the score is between 1 and 3, the program multiplies it by 10.
- If the score is between 4 and 6, the program multiplies it by 100.
- If the score is between 7 and 9, the program multiplies it by 1000.
- If the score is 0 or more than 9, the program prints an error message.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
           int n = int.Parse(Console.ReadLine());
           switch (n)
           {
               case 1:
               case 2:
               case 3:
                   Console.WriteLine("n= {0}", n * 10); break;
               case 4:
               case 5:
               case 6:
                   Console.WriteLine("n= {0}", n * 100); break;
               case 7:
               case 8:
               case 9:
                   Console.WriteLine("n= {0}", n * 1000); break;
               default:
                   Console.WriteLine("ERROR"); break;
           }
       }
   }
}


Последната промяна е направена от iodan на Вто Апр 28, 2015 3:41 pm; мнението е било променяно общо 2 пъти
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty зад.11*

Писане by iodan Пон Апр 27, 2015 11:59 pm

           //11.   * Напишете програма, която преобразува дадено число в интервала [0..999] в текст, съответстващ на българското произношение на числото.

11. * Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace if_else
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Въведете число между 0 и 999: ");
           int n = int.Parse(Console.ReadLine());

           int ones = n % 10;
           int tens = (n / 10) % 10;
           int hundrets = n / 100;


           if (n < 1000 && n >= 0)
           {
               if (ones == 0 && tens == 0 && hundrets == 0)
               {
                   Console.WriteLine("нула");
               }
               if (hundrets != 0)
               {
                   switch (hundrets)
                   {
                       case 1: Console.Write("сто "); break;
                       case 2: Console.Write("двеста "); break;
                       case 3: Console.Write("триста "); break;
                       case 4: Console.Write("четиристотин "); break;
                       case 5: Console.Write("петстотин "); break;
                       case 6: Console.Write("шестстотин "); break;
                       case 7: Console.Write("седемстотин "); break;
                       case 8: Console.Write("осемстотин "); break;
                       case 9: Console.Write("деветстотин "); break;
                       case 0:
                           break;
                           Console.WriteLine();

                   }

               }
               if (hundrets != 0 && tens == 0 && ones != 0)
               {
                   Console.Write("и ");
               }

               if (tens != 0)
               {
                   switch (tens)
                   {
                       case 1:
                           if (tens == 1)
                           {
                               if (hundrets != 0)
                               {
                                   Console.Write("и ");
                               }
                               switch (ones)
                               {
                                   case 1: Console.Write("единадесет"); break;
                                   case 2: Console.Write("дванадесет"); break;
                                   case 3: Console.Write("тринадесет"); break;
                                   case 4: Console.Write("четиринадесет"); break;
                                   case 5: Console.Write("петнадесет"); break;
                                   case 6: Console.Write("шестнадесет"); break;
                                   case 7: Console.Write("седемнадесет"); break;
                                   case 8: Console.Write("осемнадесет"); break;
                                   case 9: Console.Write("деветнадесет"); break;
                                   case 0: Console.Write("десет"); break;

                               }
                           }; break;
                       case 2: Console.Write("двадесет"); break;
                       case 3: Console.Write("тридесет"); break;
                       case 4: Console.Write("четиридесет"); break;
                       case 5: Console.Write("петдесет"); break;
                       case 6: Console.Write("шестдесет"); break;
                       case 7: Console.Write("седемдесет"); break;
                       case 8: Console.Write("осемдесет"); break;
                       case 9: Console.Write("деветдесет"); break;
                       case 0: ; break;

                   }
               }
               if (tens != 0 && tens != 1 && ones != 0)
               {
                   Console.Write(" и ");
               }
               if (ones > 0 && tens != 1)
               {
                   switch (ones)
                   {
                       case 1: Console.Write("едно"); break;
                       case 2: Console.Write("две"); break;
                       case 3: Console.Write("три"); break;
                       case 4: Console.Write("четири"); break;
                       case 5: Console.Write("пет"); break;
                       case 6: Console.Write("шест"); break;
                       case 7: Console.Write("седем"); break;
                       case 8: Console.Write("осем"); break;
                       case 9: Console.Write("девет"); break;
                       case 0:
                           ; break;

                   }
               } Console.WriteLine();
           }
           else
           {
               Console.WriteLine("invalid number");
           }
       }
   }
}
iodan
iodan
Admin

Брой мнения : 39
Join date : 27.04.2015
Age : 49

https://kiosys.board-directory.net

Върнете се в началото Go down

if constructions Empty Re: if constructions

Писане by Sponsored content


Sponsored content


Върнете се в началото Go down

Върнете се в началото


 
Права за този форум:
Не Можете да отговаряте на темите