Tutorial 1 Notes

1. Write a program that prints the sum of all integers in the range 0 ≤ i ≤ 1,000,000 such that i is not divisible by any of the numbers {3, 5, 7}.

using static System.Console;

class Sum {
  static void Main() {
    int i = 0;
    int sum = 0;

    while (i <= 1_000_000) {
      if (i % 3 != 0 && i % 5 != 0 && i % 7 != 0)
        sum += i;

      i += 1;
    }
    WriteLine(sum);
  }
}

The sum is 938,304,743.

2. Write a program that reads a string from the console and prints "palindrome" if it is a palindrome, otherwise "not"'.

using static System.Console;

class Palindrome {
  static void Main() {
    string s = ReadLine();
    int i = 0;
    bool palindrome = true;
    
    while (i < s.Length / 2) {
      if (s[i] != s[s.Length - 1 - i])
        palindrome = false;
      i += 1;
    }
    
    if (palindrome)
      WriteLine("palindrome");
    else WriteLine("not");
  }
}

3. Write a program that reads a string and writes it back out, capitalizing the first letter of every word.

using static System.Console;

class Capitalize {
  static void Main() {
    string s = ReadLine();
    int i = 0;
    
    while (i < s.Length) {
      if (i == 0 || s[i - 1] == ' ')
        Write(char.ToUpper(s[i]));
      else
        Write(s[i]);
      i += 1;
    }
    
    WriteLine();
  }
}