Advertisement
walec91

Ćwiczenia #5

Dec 12th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp14
  8. {
  9.     internal class Program
  10.     {
  11.         private static void Main(string[] args)
  12.         {
  13.             #region silnia
  14.             //int silnia = 1;
  15.             //Console.Write("Podaj n: ");
  16.             //int n = int.Parse(Console.ReadLine());
  17.  
  18.             //for (int i = 1; i <= n; i++)
  19.             //{
  20.             //    silnia *= i;
  21.             //    Console.WriteLine("Silnia ({0}) = {1}", i, silnia);
  22.             //}
  23.  
  24.             //Console.ReadKey();
  25.             #endregion
  26.             #region S(n)
  27.             //int suma = 0, i;
  28.             //Console.Write("Podaj n: ");
  29.             //int n = int.Parse(Console.ReadLine());
  30.  
  31.             //for (i = 1; i <= n; i++)
  32.             //{
  33.             //    if (i % 2 == 0)
  34.             //    {
  35.             //        suma -= i;
  36.             //    }
  37.             //    else
  38.             //    {
  39.             //        suma += i;
  40.             //    }
  41.             //}
  42.             //Console.WriteLine("Suma ({0}) = {1}", n, suma);
  43.  
  44.             //Console.ReadKey();
  45.             #endregion
  46.             #region S(n).1
  47.             //int suma = 0, i, składnik, mnożnik = 1;
  48.             //Console.Write("Podaj n: ");
  49.             //int n = int.Parse(Console.ReadLine());
  50.  
  51.             //for (i = 1; i <= n; i++)
  52.             //{
  53.             //    składnik = mnożnik * i;
  54.             //    suma += składnik;
  55.             //    mnożnik -= mnożnik;
  56.             //}
  57.             //Console.WriteLine("Suma ({0}) = {1}", n, suma);
  58.  
  59.             //Console.ReadKey();
  60.             #endregion
  61.             #region tablica #1
  62.             //int suma = 0;
  63.             //int[] a = new int[4];
  64.             //a[0] = 1;
  65.             //a[1] = 3;
  66.             //a[2] = 8;
  67.             //a[3] = 5;
  68.             //for (int i = 0; i < 4; i++)
  69.             //{
  70.             //    suma += a[i];
  71.             //}
  72.             //Console.WriteLine("Suma wszytskich wyrazów wynosi: {0}", suma);
  73.  
  74.             //Console.ReadKey();
  75.             #endregion
  76.             #region tablica #2
  77.             //int suma = 0,n;
  78.             //Console.Write("Podaj długość tablicy: ");
  79.             //n = int.Parse(Console.ReadLine());
  80.  
  81.             //int[] a = new int[n];
  82.             //for (int i = 0; i < n; i++)
  83.             //{
  84.             //    Console.Write("Podaj a[{0}]: ", i);
  85.             //    a[i] = int.Parse(Console.ReadLine());
  86.             //}
  87.             //for (int i = 0; i < n; i++)
  88.             //{
  89.             //    suma += a[i];
  90.             //}
  91.             //Console.WriteLine("Suma wszytskich wyrazów wynosi: {0}", suma);
  92.  
  93.             //Console.ReadKey();
  94.             #endregion
  95.             #region Random #1
  96.             //int suma = 0, n;
  97.             //Console.Write("Podaj długość tablicy: ");
  98.             //n = int.Parse(Console.ReadLine());
  99.  
  100.             //int[] a = new int[n];
  101.             //Random r = new Random();
  102.             //for (int i = 0; i < n; i++)
  103.             //{
  104.             //    a[i] = r.Next(-5, 11);
  105.             //    Console.WriteLine("a[{0}] = {1}", i, a[i]);
  106.             //}
  107.             //foreach (int x in a)
  108.             //{
  109.             //    suma += x;
  110.             //}
  111.             //Console.WriteLine("Suma wszytskich wyrazów wynosi: {0}", suma);
  112.  
  113.             //Console.ReadKey();
  114.             #endregion
  115.             #region Random #2
  116.             int n, sumaDodatnich = 0, sumaUjemnych = 0;
  117.             Console.Write("Podaj długość tablicy: ");
  118.             n = int.Parse(Console.ReadLine());
  119.  
  120.             int[] a = new int[n];
  121.             Random r = new Random();
  122.             for (int i = 0; i < n; i++)
  123.             {
  124.                 a[i] = r.Next(-5, 11);
  125.                 Console.WriteLine("a[{0}] = {1}", i, a[i]);
  126.             }
  127.             foreach (int x in a)
  128.             {
  129.                 if (x > 0)
  130.                 {
  131.                     sumaDodatnich += x;
  132.                 }
  133.                 else
  134.                 {
  135.                     sumaUjemnych += x;
  136.                 }
  137.             }
  138.             Console.WriteLine("Suma liczb dodatnich  wynosi: {0}", sumaDodatnich);
  139.             Console.WriteLine("Suma liczb ujemnych  wynosi: {0}", sumaUjemnych);
  140.  
  141.  
  142.  
  143.             Console.ReadKey();
  144.             #endregion
  145.             #region Random #3
  146.             int n, sumaDodatnich = 0, sumaUjemnych = 0, suma = 0, ld = 0, lu = 0, lz = 0;
  147.             Console.Write("Podaj długość tablicy: ");
  148.             n = int.Parse(Console.ReadLine());
  149.  
  150.             int[] a = new int[n];
  151.             Random r = new Random();
  152.             for (int i = 0; i < n; i++)
  153.             {
  154.                 a[i] = r.Next(-5, 6);
  155.                 Console.WriteLine("a[{0}] = {1}", i, a[i]);
  156.             }
  157.             for (int i = 0; i < n; i++)
  158.             {
  159.                 suma += a[i];
  160.             }
  161.             Console.WriteLine("Suma = " + suma);
  162.             foreach (int x in a)
  163.             {
  164.                 if (x > 0)
  165.                 {
  166.                     sumaDodatnich += x;
  167.                     ld++;
  168.                 }
  169.                 else if (x < 0)
  170.                 {
  171.                     sumaUjemnych += x;
  172.                     lu++;
  173.                 }
  174.                 else
  175.                 {
  176.                     lz++;
  177.                 }
  178.             }
  179.             Console.WriteLine("Suma liczb dodatnich  wynosi: {0}, a jest ich {1}", sumaDodatnich, ld);
  180.             Console.WriteLine("Suma liczb ujemnych  wynosi: {0}, a jest ich {1}", sumaUjemnych, lu);
  181.             Console.WriteLine("Liczba zer wynosi {0}", lz);
  182.  
  183.             Console.ReadKey();
  184.             #endregion
  185.             #region Lotto
  186.             int[] t = new int[6];
  187.             Random r = new Random();
  188.             int i = 0;
  189.             while (i < 6)
  190.             {
  191.                 t[i] = r.Next(1, 50);
  192.                 int popr = 1;
  193.                 for (int j = 0; j < i; j++)
  194.                 {
  195.                     if (t[i] == t[j])
  196.                     {
  197.                         popr = 0;
  198.                     }
  199.  
  200.                 }
  201.                 if (popr == 1)
  202.                 {
  203.                     i++;
  204.                 }
  205.             }
  206.             for (int k = 0; k < 6; k++)
  207.             {
  208.                 Console.Write(t[k] + " ");
  209.             }
  210.  
  211.             Console.ReadKey();
  212.             #endregion
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement