Advertisement
RynkunPokemon

Zadanie 3

Jun 12th, 2022
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static int sumowanie(int[] tab, int p, int k)
  8.         {
  9.             int suma = 0;
  10.             do
  11.             {
  12.                 int i = p;
  13.                 suma += tab[i];
  14.                 p++;
  15.             } while (p <= k);
  16.             return suma;
  17.  
  18.         }
  19.  
  20.  
  21.  
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.             int[] tab = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  26.  
  27.             Console.WriteLine("Ilosc elementow w tablicy: " + tab.Length.ToString());
  28.             Console.WriteLine("Podaj element poczatkowy tablicy");
  29.             int p = Convert.ToInt32(Console.ReadLine());
  30.             Console.WriteLine("Podaj element koncowy tablicy");
  31.             int k = Convert.ToInt32(Console.ReadLine());
  32.  
  33.            
  34.  
  35.             if(p<0 || p > tab.Length) {
  36.                 Console.WriteLine("Blad w wyborze elementów początkowego i końcowego. Wybierz elementy od 0 do " + tab.Length.ToString());
  37.             } else
  38.             {
  39.                 Console.Write("Suma elementow tablicy od " + p.ToString() + " do " + k.ToString());
  40.                 Console.WriteLine();
  41.                 Console.WriteLine("Suma: "+sumowanie(tab, p, k).ToString());
  42.             }
  43.  
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement