Advertisement
ekostadinov

C# PrintSequenceMembers

Sep 18th, 2012
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _9.PrintSequence
  7. {
  8.     class PrintSequence
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string members = "2, -3, 4, -5, 6, -7";
  13.             Console.WriteLine("If your sequence of numbers is: {0}", members);
  14.            
  15.             Console.WriteLine("The first 10 members are:");
  16.  
  17.         // to find all members of the sequence we can use for loop
  18.  
  19.             for (int index = 2; index <= 11; index++)
  20.             {
  21.                 // Console.Write(index + " ");
  22.         // prints the numbers between 2 and 11 (our first 10 members)
  23.  
  24.                 int result = index % 2;
  25.         // dividing by 2 we can find the odd and even numbers in the sequence
  26.  
  27.                 //Console.WriteLine(result + " ");
  28.         //prints 0 for odd and 1 for even number in the sequence  
  29.  
  30.                 if (result != 0) // we can use if loop to separate odd from even numbers
  31.                 {
  32.                     Console.Write(" -{0}, ", index);
  33.             // prints odd numbers in the sequence (our negative members)
  34.                 }
  35.                 else
  36.                 {
  37.                     Console.Write("{0},", index);
  38.                     // prints even numbers in the sequence (our positive members)
  39.                 }
  40.                                
  41.             }
  42.             Console.WriteLine();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement