Venciity

[Jumpstart C#] Exam - 2 задача

Feb 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. /*Задача 2
  2. Напишете програма която подканва потребителя да въведе три цели положителни числа M, N и K ,
  3.  * където M < N и отпечатва всички четни числа в интервала [M, N] които се делят точно на K (всяко на нов ред)
  4.  * и тяхната сума на последния ред.*/
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace exercise2
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             int numberM, numberN, numberK;
  18.             string input;
  19.             int sum = 0;
  20.  
  21.             do
  22.             {
  23.                 Console.Write("Въведете число M: ");
  24.                 input = Console.ReadLine();
  25.             }
  26.             while (!(int.TryParse(input, out numberM) && (numberM >= 0)));
  27.  
  28.             do
  29.             {
  30.                 Console.Write("Въведете число N: ");
  31.                 input = Console.ReadLine();
  32.             }
  33.             while (!(int.TryParse(input, out numberN) && (numberN >= 0)));
  34.  
  35.             do
  36.             {
  37.                 Console.Write("Въведете число K: ");
  38.                 input = Console.ReadLine();
  39.             }
  40.             while (!(int.TryParse(input, out numberK) && (numberK >= 0)));
  41.  
  42.             Console.WriteLine("всички четни числа в интервала [M, N] които се делят точно на K :");
  43.             for (int i = numberM; i <= numberN; i++)
  44.             {
  45.                 if (i % 2 == 0)
  46.                 {
  47.                     if (i % numberK == 0)
  48.                     {
  49.                         Console.WriteLine(i);
  50.                         sum += i;
  51.                     }
  52.                 }
  53.             }
  54.             Console.WriteLine("sum: {0}",sum);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment