Worksheet on selection statements and Loop statements
using System; using System;
namespace ConsoleApplication10
{ namespace ConsoleApplication10
class Program
{
{
static void Main(string[] args) class Program
{ {
int x, y, z; static void Main(string[]
Console.WriteLine("Enter the first number"); args)
x = Convert.ToInt32(Console.ReadLine()); {
Console.WriteLine("Enter the second number"); int n=1,sum=0,r;
y = Convert.ToInt32(Console.ReadLine()); while(n<=20)
Console.WriteLine("Enter the third number"); {
z = Convert.ToInt32(Console.ReadLine());
if (x > y)
r = n * n;
{ sum = sum + r;
if (y > z) n += 1;
Console.WriteLine("The largest number is {0}", x); }
else
{ Console.WriteLine("Sum={0}", sum);
if(x > z) }
Console.WriteLine("The largest number is {0}", x); }
else
}
Console.WriteLine("The largest number is {0}", z);
}
}
else
{
if (x > z)
Console.WriteLine("The largest number is {0}", y);
else
{
if (y > z)
Console.WriteLine("The largest number is {0}", y);
else
Console.WriteLine("The largest number is {0}", z);
}
}}}}
1. Write an if-statement that takes two integer variables and exchanges their values
if the first one is greater than the second one.
2. Sort 3 real numbers in descending order. Use nested if statements.
3. Write a program that asks for a digit (0-9), and depending on the input, shows the
digit as a word (in English). Use a switch statement.
4. Write a program that applies bonus points to given scores in the range [1…9] by the
following rules:
a. - If the score is between 1 and 3, the program multiplies it by 10.
b. - If the score is between 4 and 6, the program multiplies it by 100.
c. - If the score is between 7 and 9, the program multiplies it by 1000.
d. - If the score is 0 or more than 9, the program prints an error message.
5. Write a program which sums the integers from 1 to 10 using a for loop (and prints the
total at the end).
6. Write a program which finds the factorial of a given number. E.g. 3 factorial, or 3! is
equal to 3 x 2 x 1; 5! is equal to 5 x 4 x 3 x 2 x 1, etc.. Your program should only contain
a single loop.
7. Write a program that asks the user to type an integer N and computes the sum of the
cubes from 53 to N3.
8. Write a program to print all odd number from 1 up to the maximum numbered entered
from a user.
9. Write a program that calculates N!/K! for N and K (1<K<N);
Worksheet on selection statements and Loop statements
using System; using System;
namespace ConsoleApplication10
{ namespace ConsoleApplication10
class Program
{
{
static void Main(string[] args) class Program
{ {
int x, y, z; static void Main(string[]
Console.WriteLine("Enter the first number"); args)
x = Convert.ToInt32(Console.ReadLine()); {
Console.WriteLine("Enter the second number"); int n=1,sum=0,r;
y = Convert.ToInt32(Console.ReadLine()); while(n<=20)
Console.WriteLine("Enter the third number"); {
z = Convert.ToInt32(Console.ReadLine());
if (x > y)
r = n * n;
{ sum = sum + r;
if (y > z) n += 1;
Console.WriteLine("The largest number is {0}", x); }
else
{ Console.WriteLine("Sum={0}", sum);
if(x > z) }
Console.WriteLine("The largest number is {0}", x); }
else
}
Console.WriteLine("The largest number is {0}", z);
}
}
else
{
if (x > z)
Console.WriteLine("The largest number is {0}", y);
else
{
if (y > z)
Console.WriteLine("The largest number is {0}", y);
else
Console.WriteLine("The largest number is {0}", z);
}
}}}}
1. Write an if-statement that takes two integer variables and exchanges their values
if the first one is greater than the second one.
2. Sort 3 real numbers in descending order. Use nested if statements.
3. Write a program that asks for a digit (0-9), and depending on the input, shows the
digit as a word (in English). Use a switch statement.
4. Write a program that applies bonus points to given scores in the range [1…9] by the
following rules:
a. - If the score is between 1 and 3, the program multiplies it by 10.
b. - If the score is between 4 and 6, the program multiplies it by 100.
c. - If the score is between 7 and 9, the program multiplies it by 1000.
d. - If the score is 0 or more than 9, the program prints an error message.
5. Write a program which sums the integers from 1 to 10 using a for loop (and prints the
total at the end).
6. Write a program which finds the factorial of a given number. E.g. 3 factorial, or 3! is
equal to 3 x 2 x 1; 5! is equal to 5 x 4 x 3 x 2 x 1, etc.. Your program should only contain
a single loop.
7. Write a program that asks the user to type an integer N and computes the sum of the
cubes from 53 to N3.
8. Write a program to print all odd number from 1 up to the maximum numbered entered
from a user.
9. Write a program that calculates N!/K! for N and K (1<K<N);