Ex 19. Write a program that allows the user 10 tries to answer the question: “Which U.S. President was born on July 4?” After three incorrect guesses the program should give the hint: “He once said, ‘if you don’t say anything, you won’t be called upon to repeat it.’ “. After seven incorrect guesses, the program should give the hint “His nickname was ‘Silent Cal.’”. Note: Calvin Coolidge was born on July 4, 1872.
{
string answer = "Calvin Coolidge";
string userGuess;
for (int i = 1; i <= 10; i++)
{
Console.Write("Which U.S. President was born on July 4?: ");
userGuess = (Console.ReadLine());
if (userGuess == answer)
{
Console.WriteLine("You are right");
break;
}
else if (i == 3)
{
Console.WriteLine("hint");
}
else if (i == 7)
{
Console.WriteLine("Hint 2");
}
}
}
//=======================Ex 20=====================
Ex 20. Use a loop to add all even integer values between 1000 and 2000. Display the sum.
{
int N = 1000;
int sum = 0;
do
{
if (N % 2 == 0)
{
sum += N;
Console.WriteLine("Number: {0} Sum: {1}", N, sum);
}
N++; //get next value
} while (N <= 2000);
}
//=======================Ex 21=====================
Ex 21. Use a loop to multiply all the odd consecutive values, starting at 100, until the product reaches or exceeds 9,000,000,000.Display all the values involved in the product.
{
long counter = 100;
long oddsum = 0;
do
{
if (counter % 2 != 0)
{
oddsum += counter;
Console.WriteLine("value No:{0}, sum:{1}", counter, oddsum);
}
counter++;
} while (oddsum <= 9000000000);
Console.WriteLine("\nThe final sum is: {0}", oddsum);
Console.WriteLine("\nThe number of values used are: {0}", counter - 100);
}
//=======================Ex 22=====================
Ex 22. Use a single loop to add all the even numbers together and all the odd numbers together for all the integer values between 1 and 100. Display the sum of all even numbers and the sum of all odd numbers.
{
int evensum = 0;
int oddsum = 0;
for (int counter = 1; counter <= 100; counter++)
{
if (counter % 2 == 0)
evensum += counter;
else
oddsum += counter;
Console.Write("Even Sum: {0} ", evensum);
Console.WriteLine("Odd Sum: {0}", oddsum);
}
}
No comments:
Post a Comment