Search........

Google

Friday, March 7, 2008

While Loop

//Now let us see how the while loop works
using System;

namespace whileloop

{

class cl1

{

static void Main()

{

int i=0,j,sum=0;

Console.WriteLine("Enter 10 No.s");

while(i<10)

{

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

sum=sum+j;

i++;

}

Console.WriteLine("Sum="+sum);

}

}

}

Looping Statements

Let us consider acondition where we have to get a 100 numbers.These codes can sill be length like
inum1=Convert.ToInt32(Console.ReadLine());
inum2=Convert.ToInt32(Console.ReadLine());
inum3=Convert.ToInt32(Console.ReadLine());
inum4=Convert.ToInt32(Console.ReadLine());
inum5=Convert.ToInt32(Console.ReadLine());
inum6=Convert.ToInt32(Console.ReadLine());
.
.
.
.
inum100=Convert.ToInt32(Console.ReadLine());
so to over come these problem we are using the loop sattements....Here we will use a loop statement the body will be executed until the condition turns false

Arithmetic operators with switch

// Again one more switch condition statement....

using System;
namespace symbols
{
class class1
{
static void Main()
{
char ch;
int inum1,inum2,inum3;
double dnum;
Console.WriteLine("Enter first nmber");
inum1=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second nmber");
inum2=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your choice + , - , * or /");
ch=Convert.ToChar(Console.ReadLine());
switch(ch)
{
case '+' :
inum3=inum1+inum2;
Console.WriteLine("Sum is "+inum3);
break;
case '-' :
inum3=inum1-inum2;
Console.WriteLine("Difference is "+inum3);
break;
case '*' :
inum3=inum1*inum2;
Console.WriteLine("Product is "+inum3);
break;
case '/' :
dnum=(Double)(inum2/inum1);
Console.WriteLine(" Quotient is "+dnum.ToString());
break;
default :Console.WriteLine("entered a wrong choice");
break;
}
}
}
}

switch statements

/*Now let us see the multiple selection statements ,for these pourpose we can use switch statements

syntax :- switch(expression)

{

case value1:

body;

break;

case value2:

body;

break;

case value3:

body;

break;

default :

body;

break; //executes if all other conditions are wrong....

*/

using System;

namespace switchdemo

{

class class1

{

static void Main()

{

char ch;

Console.WriteLine("enter your choice (a,b,c,d)");

ch=Convert.ToChar(Console.ReadLine());

switch(ch)

{

case 'a' :

Console.WriteLine("This is from case a");

break;

case 'b' :

Console.WriteLine("This is from case b");

break;

case 'c' :

Console.WriteLine("This is from case c")

;

break;

case 'd' :

Console.WriteLine("This is from case d");

break;

default :

Console.WriteLine("This not from the specified choice");

break;

}

}

}

}

Conditional Statements

/ *Now we are going to see the conditional statements in this we canspecify a particular condition for executing one or more statements

Syntax:-

if(condition)

{

body

}

else if(condition)

{

body

}

else{

body

}
*/
using System;

namespace condition{

class class1

{

static void Main()

{

int inum1;

Console.WriteLine("enter a no between 1 to 100");

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

if (inum1<=10)

{

Console.WriteLine("the number you have entered is less than 10");

}

else if (inum1>10&&inum1<50)

{

Console.WriteLine("the number you have entered is less than 50 but more than 10");

}

else

{

Console.WriteLine("the number you have entered is more than 10");

}

}

}

}

Double...

In this program lets see how to convert to double

using System;

namespace doubledemo

{

class class1

{

static void Main()

{

double inum1,inum2,add;

Console.WriteLine("enter a no");

inum1=Convert.ToDouble(Console.ReadLine());

Console.WriteLine("enter a second no.");

inum2=Convert.ToDouble(Console.ReadLine());

add=inum1+inum2;

Console.WriteLine("Result ="+add.ToString());

}

}

}

Thursday, March 6, 2008

converting to character

/ /converting to character .......

using System;

namespace nm1

{

class cl1

{

static void Main()

{

char ch;
Console.WriteLine("enter a character");

ch=Convert.ToChar(Console.ReadLine());

Console.WriteLine("The character entered is "+ ch);
}

}

}