Sei sulla pagina 1di 4

# using System;

namespace CSharp1Exercises.ControlFlow

## public class Loops

/// <summary>

/// Write a program to count how many numbers between 1 and 100 are divisible by 3 with no
remainder.

/// </summary>

var count = 0;

if (i%3 == 0)

count++;

## Console.WriteLine("There are {0} numbers divisible by 3 between 1 and 100.", count);

/// <summary>

/// Write a program and continuously ask the user to enter a number. The loop terminates when
the user

/// enters “ok". Calculate the sum of all the previously entered numbers and display it on the
console.

/// </summary>

{
var sum = 0;

while (true)

## Console.Write("Enter a number (or 'ok' to exit): ");

if (input.ToLower() == "ok")

break;

sum += Convert.ToInt32(input);

## Console.WriteLine("Sum of all numbers is: " + sum);

/// <summary>

/// Write a program which takes a single argument from the console, computes the factorial and
prints the

/// value on the console. For example, if the user enters 5, the program should calculate 5 x 4 x 3 x 2
x1

/// </summary>

## Console.Write("Enter a number: ");

var factorial = 1;

## for (var i = 1; i <= number; i++)

factorial *= i;
Console.WriteLine("{0}! = {1}", number, factorial);

/// <summary>

/// Write a program that picks a random number between 1 and 10. Give the user 4 chances to
guess the number.

/// If the user guesses the number, display “You won". Otherwise, display “You lost".

/// </summary>

## Console.Write("Guess the secret number: ");

if (guess == number)

Console.WriteLine("You won!");

return;

Console.WriteLine("You lost!");

}
/// <summary>

/// Write a program and ask the user to enter a series of numbers separated by comma. Find the
maximum of the

/// numbers and display it on the result. For example, if the user enters “5, 3, 8, 1, 4", the program
should

/// </summary>

max = number;