Loop Statement in C#:-
Loop is used to print large statements of data using a single repeatable block, Loop is also called an iterative statement because it will repeat the data according to condition.
Type of Loop:-
1)Entry Control Loop
1.1) While Loop:-
init;
while(condition)
{
Statement;
Increment;
}
When we want to create infinite Loop in C# then we prefer While Loop Otherwise we will use For Loop
class WhileExample
{
public static void Main()
{
int i = 1;
while (true)
{
Console.WriteLine(i);
i++;
if (i == 10)
break;
}
Console.ReadKey();
}
}
1.2) For Loop:-
This Loop will work using a single Line Statement
for(init;condition;iteration)
{
Statement;
}
WAP to check prime number?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class CheckPrime
{
static void Main()
{
int num;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
c++;
}
if (c == 2)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime");
Console.ReadKey();
}
}
}
WAP to check prime number without using third variable?
class CheckPrime
{
static void Main()
{
int num,i;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
for (i = 2; i < num; i++)
{
if (num % i == 0)
{
Console.WriteLine("Not Prime");
}
}
if (num==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
WAP to check prime number with complete program optimization?
class CheckPrime
{
static void Main()
{
int num,i;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
for (i = 2; i < num/2; i++)
{
if (num % i == 0)
{
Console.WriteLine("Not Prime");
}
}
if (num/2==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
2) Exit Control Loop
2.1) do--while:- First Execute Statement then check condition.
init;
do
{
Statement;
Increment;
}while(condition);
WAP to print 1 to 10 and 10 to 1 using do-while Loop?
class DoWhileExample
{
public static void Main()
{
int i = 1;
do
{
if (i <= 10)
Console.WriteLine(i);
else
Console.WriteLine(21-i);
i++;
} while (i <= 20);
Console.ReadKey();
}
}
ASSIGNMENT of LOOP:-
1) WAP to check prime number without using a third variable?
2) WAP to display the table of each Fibonacci series digit?
Loop is used to print large statements of data using a single repeatable block, Loop is also called an iterative statement because it will repeat the data according to condition.
Type of Loop:-
1)Entry Control Loop
1.1) While Loop:-
init;
while(condition)
{
Statement;
Increment;
}
When we want to create infinite Loop in C# then we prefer While Loop Otherwise we will use For Loop
class WhileExample
{
public static void Main()
{
int i = 1;
while (true)
{
Console.WriteLine(i);
i++;
if (i == 10)
break;
}
Console.ReadKey();
}
}
1.2) For Loop:-
This Loop will work using a single Line Statement
for(init;condition;iteration)
{
Statement;
}
WAP to check prime number?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class CheckPrime
{
static void Main()
{
int num;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
c++;
}
if (c == 2)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime");
Console.ReadKey();
}
}
}
WAP to check prime number without using third variable?
class CheckPrime
{
static void Main()
{
int num,i;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
for (i = 2; i < num; i++)
{
if (num % i == 0)
{
Console.WriteLine("Not Prime");
}
}
if (num==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
WAP to check prime number with complete program optimization?
class CheckPrime
{
static void Main()
{
int num,i;
Console.WriteLine("Enter number");
num = int.Parse(Console.ReadLine());
for (i = 2; i < num/2; i++)
{
if (num % i == 0)
{
Console.WriteLine("Not Prime");
}
}
if (num/2==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
2) Exit Control Loop
2.1) do--while:- First Execute Statement then check condition.
init;
do
{
Statement;
Increment;
}while(condition);
WAP to print 1 to 10 and 10 to 1 using do-while Loop?
class DoWhileExample
{
public static void Main()
{
int i = 1;
do
{
if (i <= 10)
Console.WriteLine(i);
else
Console.WriteLine(21-i);
i++;
} while (i <= 20);
Console.ReadKey();
}
}
ASSIGNMENT of LOOP:-
1) WAP to check prime number without using a third variable?
3) WAP to find the first , second, and third max number in pin code?
2) WAP to display the table of each Fibonacci series digit?
WAP to print factorial with expression
ReplyDeleteusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int fact=1, i,num;
Console.WriteLine("Enter a number to find factorial ");
num = int.Parse(Console.ReadLine());
for(i=num;i>=1;i--)
{
fact = fact * i;
Console.Write("*");
Console.Write(i*1 );
}
Console.WriteLine(" = " + fact);
Console.ReadKey();
}
}
}
WAP to display table of each Fibonacci series digit?
ReplyDeleteusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fabonacci
{
class Program
{
static void Main(string[] args)
{
int a = -1 , b = 1 ,c;
for (int i = 1; i <= 6; i++)
{
c = a + b;
if (c > 0)
{
Console.WriteLine("Table of " + c);
for (int j = 1; j <= 10; j++)
{
Console.WriteLine(c * j);
}
}
a = b;
b = c;
}
Console.ReadKey();
}
}
}
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .