Loop Statement in C#, Control Structure in C#, For Loop, While Loop and Do While Loop Concept in C#:-
Type of Loop:-
1)Entry Control Loop:-
First check condition then execute loop statements hence it is called entry control.
1.1) While Loop:-
Syntax of While Loop:-
init;
while(condition)
{
Statement;
Increment;
}
Now I am discussing one simple program of while loop, suppose I want to display a range of elements from1 to 100?
Solution:-
int i=1;
while(i<=100)
{
Console.WriteLine(i);
i++;
}
suppose I want to display a range of elements from100 to 1?
Solution:-
int i=100;
while(i>=1)
{
Console.WriteLine(i);
i--;
}
Write a program to calculate a table of any number using a complete expression?
class CalculateTable
{
static void Main()
{
int num,i;
Console.WriteLine("Enter number to print table");
num = int.Parse(Console.ReadLine());
i = 1;
while (i <= 10)
{
Console.WriteLine("{0}*{1}={2}",num,i,num * i);
i++;
}
Console.ReadKey();
}
}
Infinite loop means, loop without any finite condition for example if we pass true under while loop, then it will automatically be converted into the infinite while loop.
When we want to create an infinite loop in C# then we prefer while loop Otherwise we will use for Loop
Syntax of Infinite While Loop:-
while(true)
{
Statements;
}
Example of Infinite While Loop:-
class WhileExample
{
public static void Main()
{
int i = 1;
while (true)
{
Console.WriteLine(i);
i++;
if (i == 10)
break;
}
Console.ReadKey();
}
}
Another Example of Infinite While Loop:-
class WhileExample
{
static void Main()
{
bool flag = true;
int x = 0;
while (flag)
{
Console.WriteLine("hello"+x);
x++;
if (x > 10)
{
flag = false;
}
}
Console.ReadKey();
}
}
1.2) For Loop:- This Loop will contain all statements (initialization, condition, and iteration) using a single statement. that's why for loop provides a simple syntax structure as compared to the while loop. we always prefer for loop to solve range-based problems.
Syntax of for loop:-
1 2 4
for(init;condition;iteration)
{
3
Statement;
}
Statement;
}
WAP to check the prime number?
using System;
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 the 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");
break;
}
}
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==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");
break;
}
}
if (num/2==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
2) Exit Control Loop:-
}
}
if (num/2==i)
Console.WriteLine("Prime");
Console.ReadKey();
}
}
2) Exit Control Loop:-
First, execute the loop statement then check the condition, this loop will not use mostly on application programming.
2.1) do--while:-
First, execute the statement then check the condition.
Syntax of Do--While Loop:-
Syntax of Do--While Loop:-
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();
}
}
Q) Program to implement Fibonacci series?
using System;
// 0 1 1 2 3 5 8 13
class DowhileExample
{
public static void Main()
{
int a=-1,b=1,c=0,i=1;
do
{
c = a+b;
Console.WriteLine(c);
a=b;
b=c;
i=i+1;
}while(i<=10);
}
}
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?
0 1 1 2 3 5 8 13
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();
}
}
}
#Shiva_Patel
ReplyDeleteAns:- Find Prime number with two variables
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");
break;
}
}
if(num==i)
Console.WriteLine("{0} is Prime",num);
program to check if a particular digit present in a phone number
ReplyDeleteusing System;
class digit
{
public static void Main()
{
double i,a,b,c;
Console.WriteLine("enter phone number");
i=double.Parse(Console.ReadLine());
Console.WriteLine("enter digit which you wanna check");
c=int.Parse(Console.ReadLine());
while(i!=0)
{
a=i%10;
b=a;
i/=10;
if(b==c)
{
Console.WriteLine("number is present");
break;
}
}
if (i==0)
{
Console.WriteLine("number is not present");
}
}
}
Post a Comment
POST Answer of Questions and ASK to Doubt