Pages

Tampilkan postingan dengan label which. Tampilkan semua postingan
Tampilkan postingan dengan label which. Tampilkan semua postingan

Sabtu, 28 Mei 2016

C Program which takes n values from user and then sort them using Bubble sort

C# Program which takes n values from user and then sort them using Bubble sort

Program Statement:
Write a program which takes n values from user and then sort them using Bubble sort

Solution:
 public class bubble
{
int n, x, y, z;
public void sort()
{
System.Console.Write(" Enter length of array : ");
n = Convert.ToInt32(System.Console.ReadLine());
int[] array = new int[n + 1];
int[] temp1 = new int[n + 1];
int[] temp2 = new int[n];
System.Console.WriteLine(" Enter {0} numbers : ", n);
for (x = 0; x < n; x++)
{
array[x] = Convert.ToInt32(System.Console.ReadLine());
}
for (y = n; y > 0; y--)
{
for (z = 0; z < y; z++)
{
if (array[z] > array[z + 1])
{
temp1[z] = array[z];
array[z] = array[z + 1];
array[z + 1] = temp1[z];
temp2[z] = array[z + 1];
}
else
{ temp2[z] = array[z]; }
}
}
Console.WriteLine(" >>>Ordered List<<< ");
for (int i = 0; i < n; i++)
{
Console.WriteLine(" {0}", temp2[i]);
}
}
}


Read More..

Rabu, 04 Mei 2016

C Program which takes n values from user and then sort them in ascending order

C# Program which takes n values from user and then sort them in ascending order

Program Statement:
Write a program which takes n values from user and then sort them in ascending order.

Solution:
 public class sort
{
int n, x, y, z;
public void s()
{
Console.Write(" Enter number of values you want to sort : ");
n = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write(" Enter number : ");
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (x = 0; x < n; x++)
{
for (y = x + 1; y < n; y++)
if (arr[x] > arr[y])
{
int temp;
temp = arr[y];
arr[y] = arr[x];
arr[x] = temp;
}
}
Console.WriteLine(" >>>Ascending Order<<< ");
for (z = 0; z < n; z++)
{
Console.WriteLine(" {0}", arr[z]);
}
}
}


Read More..

Jumat, 08 April 2016

C Program which copies the values of one array in second array in reverse order

C# Program which copies the values of one array in second array in reverse order

Program Statement:
Write a program which copies the values of one array in second array in reverse order

Solution:
 public class reverse
{
int n;
public void rev()
{
Console.Write(" Enter length of array : ");
n = Convert.ToInt32(Console.ReadLine());

int[] arr1 = new int[n];
int[] arr2 = new int[n];

Console.WriteLine(" Enter {0} numbers : ", n);
for (int x = 0; x < n; x++)
{ arr1[x] = Convert.ToInt32(Console.ReadLine()); }

Console.Write(" Reversed element : ");
for (int y = n - 1; y >= 0; y--)
{ arr2[(n - 1) - y] = arr1[y]; }
for (int z = 0; z < n; z++)
{ Console.Write(" {0}", arr2[z]); }
Console.WriteLine(" ");
}
}


Read More..

Jumat, 11 Maret 2016

C Program which calculated factorial to prime function

C# Program which calculated factorial to prime function

Program Statement:
Write two functions max(int,int) and prime(int). max function will take two arguments and will return the maximum of two numbers in main. Then main function will pass this calculated factorial to prime function which will show that passed value is prime or not.

Solution:
 public class _max
{
public int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
public void prime(int n)
{
if (n == 1)
Console.WriteLine(" {0} is maximum but not prime number! ", n);
if (n >= 2)
{
if (n % 2 != 0)
{
Console.WriteLine(" {0} is maximum and Prime number! ", n);
}
else
{ Console.WriteLine(" {0} is maximum but not a Prime number! ", n); }
}
}
}


Read More..