In C#, please.
Enter number of elements [Max=50] for Array MyUDA1D: 12
Enter value for MyUDA1D [0]: 1
Enter value for MyUDA1D [1]: 12
Enter value for MyUDA1D [2]: 10
Enter value for MyUDA1D [3]: 3
Enter value for MyUDA1D [4]: 5
Enter value for MyUDA1D [5]: 6
Enter value for MyUDA1D [6]: 2
Enter value for MyUDA1D [7]: 4
Enter value for MyUDA1D [8]: 7
Enter value for MyUDA1D [9]: 8
Enter value for MyUDA1D [10]: 9
Enter value for MyUDA1D [11]: 11
------------------------------------------------
Minimum value = 1
Maximum value = 12
Total number of odd number/s = 6
Total number of even number/s = 6
Total number of deficient number/s = 10
Total number of efficient number/s = 1
Total number of perfect numbers = 1
Total number of prime number/s = 5
Total number of composite number/s = 6
------------------------------------------------
Sorted Values
Ascending Order = 1 2 3 4 5 6 7 8 9 10 11 12
Descending Order = 12 11 10 9 8 7 6 5 4 3 2 1
using System;
namespace projectMath
{
class Program
{
public static int odd(int num)
{
if (num % 2 != 0)
return 1;
else return 0;
}
public static int deficient(int num)
{
int sum = 0;
for (int i = 1; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
if (num / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (num / i);
}
}
}
if (sum < (2 * num))
return 1;
else return 0;
}
public static int efficient(int num)
{
int sum = 0;
for (int i = 1; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
if (num / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (num / i);
}
}
}
if (sum > (2 * num))
return 1;
else return 0;
}
public static int perfect(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
sum += i;
}
if (sum == num)
return 1;
else return 0;
}
public static int prime(int num)
{
if (num == 0 || num == 1)
return 0;
else
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
return 0;
}
return 1;
}
public static int composite(int num)
{
int total = 0;
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
total++;
}
if(total > 2)
return 1;
else return 0;
}
public static void Main(string[] args)
{
int[] MyUDA1D = new int[50];
int total_odd = 0;
int total_deficient = 0;
int total_efficient = 0;
int total_perfect = 0;
int total_prime = 0;
int total_composite = 0;
int temp, max, min;
Console.Write("Enter number of elements [Max=50] for Array MyUDA1D: ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.Write("Enter value for MyUDA1D [{0}]: ", i);
MyUDA1D[i] = Convert.ToInt32(Console.ReadLine());
total_odd += odd(MyUDA1D[i]);
total_deficient += deficient(MyUDA1D[i]);
total_efficient += efficient(MyUDA1D[i]);
total_perfect += perfect(MyUDA1D[i]);
total_prime += prime(MyUDA1D[i]);
total_composite += composite(MyUDA1D[i]);
}
max = MyUDA1D[0];
min = MyUDA1D[0];
for (int i = 0; i < n; i++)
{
if (max < MyUDA1D[i])
max = MyUDA1D[i];
if (min > MyUDA1D[i])
min = MyUDA1D[i];
}
Console.WriteLine("------------------------------------------------");
Console.WriteLine("Minimum value = {0}", min);
Console.WriteLine("Maximum value = {0}", max);
Console.WriteLine("Total number of odd number/s = {0}", total_odd);
Console.WriteLine("Total number of even number/s = {0}", n - total_odd);
Console.WriteLine("Total number of deficient number/s = {0}", total_deficient);
Console.WriteLine("Total number of efficient number/s = {0}", total_efficient);
Console.WriteLine("Total number of perfect numbers = {0}", total_perfect);
Console.WriteLine("Total number of prime number/s = {0}", total_prime);
Console.WriteLine("Total number of composite number/s = {0}", total_composite);
Console.WriteLine("------------------------------------------------");
Console.WriteLine("Sorted Values");
Console.Write("Ascending Order = ");
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (MyUDA1D[i] > MyUDA1D[j])
{
temp = MyUDA1D[i];
MyUDA1D[i] = MyUDA1D[j];
MyUDA1D[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
Console.Write(MyUDA1D[i] + " ");
Console.WriteLine();
Console.Write("Descending Order = ");
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (MyUDA1D[i] < MyUDA1D[j])
{
temp = MyUDA1D[i];
MyUDA1D[i] = MyUDA1D[j];
MyUDA1D[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
Console.Write(MyUDA1D[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.Write("Press any key to exit. . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment