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();
}
}
Create Lift based Program using While Loop:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharpfundamental
{
internal class WhileLoopExample
{
static void Main()
{
int i = 1;
int f = 1;
bool p = true;
while(i<=10 && p)
{
if (i <= 5)
{
Console.WriteLine("floor "+f);
Thread.Sleep(2000);
f++;
}
else
{
p = false;
f--;
Console.WriteLine("floor " +f);
Thread.Sleep(2000);
}
i++;
}
}
}
}
While Loop Assignment:-
1) WAP to check prime number?
2) WAP to calculate factorial with expression?
5!
5*4*3*2*1=120
3) WAP to print Fibonacci series with expression
0+1=1
1+0=1
1+1=2
1+2=3
3+2=5
4) WAP to create lift for 10th floor building
5) WAP to calculate sum of two digit positive number?
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");
}
}
}
namespace First_project
ReplyDelete{
internal class WhilePrimeNo
{
public void whileprimeno()
{
Console.WriteLine("Enter any NO.");
int num =int.Parse(Console.ReadLine());
int i=2;
while (i < num)
{
if (num%i == 0)
{
Console.WriteLine("not prime");
break;
}
i++;
if (num == i)
{
Console.WriteLine("prime");
}
}
}
}
}
factorial while loop
ReplyDeletenamespace First_project
{
internal class WhileFactorial
{
public void whilefactorial()
{
Console.WriteLine("Enter any No.");
int num=int.Parse(Console.ReadLine());
int i = 1;
int f = 1;
while (i <=num)
{
f = f * i;
i++;
Console.WriteLine(i-1+"*");
}
Console.WriteLine(f);
}
}
}
fibonacci
ReplyDeletenamespace First_project
{
internal class WhileFabonacci
{
public void whilefabonacci()
{
Console.WriteLine("Enter any no.");
int num=int.Parse(Console.ReadLine());
int i = 1, num1 = 0, num2 = 1, sum=1;
while (i <= num)
{
Console.WriteLine(sum);
sum = num1 + num2;
num1 = num2;
num2 = sum;
i++;
}
}
}
}
Lift
ReplyDeletenamespace First_project
{
internal class LiftFor10Floor
{
public void liftfor10floor()
{
Console.WriteLine("Please enter the current Floor You are");
int cfloor=int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the Floor You want to go");
int floor=int.Parse(Console.ReadLine());
int i = cfloor;
if (floor > cfloor && floor <= 10)
{
while (i <= floor)
{
Console.WriteLine(i++);
Thread.Sleep(1000);
}
}
else if (floor < cfloor && floor >= 0)
{
while (i >= floor)
{
Console.WriteLine(i--);
Thread.Sleep(1000);
}
}
else Console.WriteLine("Wrong Input");
}
}
}
Prime no by for loop
ReplyDelete{
Console.WriteLine("Enter any No.");
int num= int.Parse(Console.ReadLine());
for (int i=2; i<num; i++)
{
if (num % i == 0)
{
Console.WriteLine("Not Prime");
break;
}
Console.WriteLine("prime no.");
break;
}
}
Fibonacci by For loop
ReplyDeletepublic void forfibonacci()
{
Console.WriteLine("Enter any No.");
int num = int.Parse(Console.ReadLine());
int num1 = 0, num2 = 1, sum = 1;
for (int i = 0; i < num; i++)
{
sum = num1 + num2;
Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);
num1 = num2;
num2 = sum;
}
}
Factorial by For loop
ReplyDeletepublic void forfactorial()
{
Console.WriteLine("Enter any No.");
int num = int.Parse(Console.ReadLine());
int f = 1;
string s = "";
for (int i = num; i > 1; i--)
{
s = s + i + "x";
f = f * i;
}
Console.WriteLine(s+"1="+ f);
}
lift for 10th floor building
ReplyDeletenamespace CsharpPrograming
{
internal class lift
{
static void Main()
{
int i = 1;
while (i <= 10)
{
Console.WriteLine("floor"+i);
Thread.Sleep(1000);
i++;
}
}
}
}
Fibonacci series with expression
ReplyDeleteinternal class fibona
{
static void Main()
{
Console.WriteLine("Enter the number");
int num = Convert.ToInt32(Console.ReadLine());
int n1 = 0;
int n2 = 1;
Console.WriteLine(n1+"+"+n1+"="+n1);
int i = 2;
while (i< num)
{
int n3 = n1 + n2;
Console.WriteLine(n1 + "+" + n2 + "=" +n3);
n1 = n2;
n2 = n3;
++i;
}
}
}
}
factorial with expression
ReplyDeleteinternal class fact
{
static void Main()
{
Console.WriteLine("Enter a number to find factorial ");
int num = Convert.ToInt32(Console.ReadLine());
int i = num ;
int f = 1;
while (i >= 1)
{
f = f * i;
Console.Write(i*1);
Console.Write("*");
i--;
}
Console.WriteLine(" = " + f);
}
}
}
Pattern 1
ReplyDeleteinternal class Class4
{
static void Main()
{
for (int i = 5; i >=1; i--)
{
for (int j =5; j>i; j--)
{
Console.Write(" ");
}
for (int k=1; k<=i; k++)
{
Console.Write( k);
}
Console.WriteLine( );
}
}
}
}
Inverted Reverse Right Triangle number
ReplyDeletenamespace First_project
{
internal class PatternOne
{
public void patternone()
{
int m = 5;
for (int i = 1; i <=5; i++)
{
for (int j = 1; j<i; j++)
{
Console.Write(' ');
}
for (int k = 1; k <=m; k++)
{
Console.Write(k);
}
m--;
Console.WriteLine();
}
}
}
}
Alternate Alphabet and number Pattern
ReplyDeletenamespace First_project
{
internal class PatternTwo
{
public void patterntwo()
{
for (int i = 5; i >=1; i--)
{
char al = 'A';
for (int j =1 ; j <=i; j++)
{
if (i % 2 == 0)
{
Console.Write(j);
}
else
{
Console.Write(al);
al++;
}
}
Console.WriteLine();
}
}
}
}