Ex 1
Ex 1
Create a program to write the even numbers from 10 to 20, both included,
except 16, in 3 different ways:
Solution:
using System;
public class BreakContinue
{
public static void Main()
{
for (int i = 10; i <= 20; i += 2)
{
if (i == 16)
continue;
Console.WriteLine(i);
}
}
}
}
EX2:
Create a program to ask the user for two numbers and display the numbers
between them (both included), three times: using "for", using "while" and
using "do..while"
Enter first number: 6
Enter last number 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Solution :
Method1:
using System;
public class RepetitiveStructures
{
public static void Main()
{
int number, number2;
int i;
Console.WriteLine();
i = number;
while (i <= number2)
{
Console.Write("{0} ", i);
i++;
}
Console.WriteLine();
i = number;
do
{
Console.Write("{0} ", i);
i++;
}
while (i <= number2);
Console.WriteLine();
}
}
Method2:
….
For(int i=0;i<3;i++)
For(int j=numb1;j<=numb2;j++)
Console.print(j);
Console.writeLn() ;
Ex3:
Create a program to calculate how many digits has a positive integer (hint: it can be
done by dividing by 10 several times). If the user enters a negative integer, the program
must show a warning message, and proceed with the equivalent positive number.
For example:
Number = 32
2 digits
Number = -4000
(Warning: it is a negative number) 4 digits
using System;
public class exercise041
{
public static void Main()
{
int number;
int digit = 0;
Console.Write("Number? ");
number = Convert.ToInt32(Console.ReadLine());
if (number < 0)
{
Console.WriteLine("(Warning: it is a negative
number)");
number = -number;
}
if (digit == 0)
digit = 1;
Ex4:
Write a program to ask the user for his/her login and his/her password (both must be
integer numbers), until the entered login is "12" and the password is "1234". The user
will have 3 attempts maximum.
using System;
public class Exercise034
{
public static void Main()
{
int user, pass;
int counter = 0;
do
{
Console.Write("Enter a user: ");
user = Convert.ToInt32(Console.ReadLine());
}
while (((user != 12) || (pass != 1234)) && (counter !=
3));