Pages

Tampilkan postingan dengan label loop. Tampilkan semua postingan
Tampilkan postingan dengan label loop. Tampilkan semua postingan

Senin, 09 Mei 2016

C Simple Loop Program example to print characters

Simple Loop Problem to Print A B D H P

Problem Statement:
Write a program using for loop which prints the following output. (You have to find a pattern to print alphabetics in this order)
A B D H P

Solution:

static void Main(string[] args)
{
int i,j,ch=0,n;
Console.Write("A ");
for (i = 1; i <= 4; i++)
{
n = 1;
for (j = 1; j <= i; j++)
{
n = n * 2;
ch = 64 + n;
}
Console.Write((char)ch + " ");
}

Console.ReadLine();
}


Read More..

Sabtu, 23 April 2016

C Program to Print ASCII values and characters using a while loop

C# Program to Print ASCII values and their equivalent characters using a while loop

Problem Statement:
Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

Solution:
static void Main(string[] args)
{
int i=0;
Console.WriteLine("ASCII Char");
while (i <= 255)
{
Console.WriteLine(i+" "+(char)i);
i++;
}
Console.ReadLine();
}


Read More..

Rabu, 13 April 2016

C Program to Print ASCII Values Using do while Loop

C# Program to Print ASCII Values Using do-while Loop

Program Statement:
Write a program to print all the ASCII values and their equivalent characters using a do-while loop. The ASCII values vary from 10 to 255.

Solution:
 static void Main(string[] args)
{
int i = 0;
Console.WriteLine("ASCII character");
do
{
Console.WriteLine(i+" "+(char)i);
i++;
} while (i <= 255);
Console.ReadLine();
}


Read More..

Minggu, 10 April 2016

C program to prints an identity matrix using for loop

C# program which prints an identity matrix using for loop

Program statement:
Write a program which prints an identity matrix using for loop i.e. takes value n from user and show the identity table of size n * n.

Solution:
static void Main(string[] args)
{
int n,i,j;
Console.WriteLine("Enter value of n:");
n = Convert.ToInt32(Console.ReadLine());
int[,] arr=new int[n,n];

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
arr[i, j] = 1;
else
arr[i, j] = 0;
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}


Read More..