0% found this document useful (0 votes)
9 views5 pages

Ex 1

Uploaded by

ronisabek16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Ex 1

Uploaded by

ronisabek16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex1:

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);
}

for (int i = 10; i <= 20; i++)


{
if (i % 2 == 1)
continue;
if (i == 16)
continue;
Console.WriteLine(i);
}

for (int i = 10; ; i += 2)


{
if (i == 16)
continue;
if (i > 20)
break;
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.Write("Enter first number: ");


number = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter last number: ");


number2 = Convert.ToInt32(Console.ReadLine());

for (i = number; i <= number2; i++)


Console.Write("{0} ", 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;
}

while (number > 0)


{
number = number / 10;
digit++;
}

if (digit == 0)
digit = 1;

Console.WriteLine("{0} digits", digit);


}
}

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());

Console.Write("Enter a password: ");


pass = Convert.ToInt32(Console.ReadLine());

if ((user != 12) || (pass != 1234))


{
Console.WriteLine("Login Error");
counter++;
}

}
while (((user != 12) || (pass != 1234)) && (counter !=
3));

if ((user != 12) || (pass != 1234))


Console.WriteLine("Logged out!");
else
Console.WriteLine("Login successful");
}
}

You might also like