0% found this document useful (0 votes)
9 views3 pages

C# Class for Number Operations

The document contains a C# class named Class1 that includes several methods for various functionalities such as printing numbers, checking age eligibility for voting, calculating sums, comparing values, checking positivity, determining evenness, and printing powers of numbers. It utilizes loops and conditionals to perform these tasks and provides console output for user interaction. The class is structured to handle input and output effectively while demonstrating basic programming concepts.

Uploaded by

aizenaizen090
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

C# Class for Number Operations

The document contains a C# class named Class1 that includes several methods for various functionalities such as printing numbers, checking age eligibility for voting, calculating sums, comparing values, checking positivity, determining evenness, and printing powers of numbers. It utilizes loops and conditionals to perform these tasks and provides console output for user interaction. The class is structured to handle input and output effectively while demonstrating basic programming concepts.

Uploaded by

aizenaizen090
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];
using [Link];
using [Link];

namespace ushtrime_25_tetor
{
internal class Class1(int n)
{

private int n = n;
private int sum = 0;
public void printNumbers(int start = 1)
{
for(int i = start; i <= n; i++)
{
[Link](i);
sum += i;
}
}

public void checkAge(int age)


{
if(age >= 18)
{
[Link]("You can vote");
} else
{
[Link]("You cannot vote");
}
}

public void printSumFrom(int end,int start = 1)


{
int s = 0;
if(end > 0)
{
for(int i = start; i <= end; i++)
{
s += i;
}
[Link]("SUM from {0} to {1} = {2}", start,end,s);
}
}
public void compareWith(int b)
{
if(n > b)
{
[Link]("{0} > {1}", n, b);

} else if(b > n)


{
[Link]("{0} > {1}", b, n);
} else
{
[Link]("{0} = {1}", b, n);

}
}
public void checkPositivity()
{
if (n == 0)
{
[Link]("It is just 0", n);

}
else if (n > 0)
{
[Link]("{0} is positive", n);
} else
{
[Link]("{0} is negative", n);

}
}

public Boolean checkIfEven(int number)


{
return number%2 == 0;
}

public void printUntilZero()


{
int input = [Link]([Link]());
while(input != 0)
{
if (checkIfEven(input))
{
[Link]("Number even found!");
break;
} else
{
[Link]("Not an even number! Continue entering:");
input = [Link]([Link]());

}
}
}

public void printPowSumFrom(int end, double pow = 3)


{
int s = 0;
String a= "";
for (double i = 2; i <= end; i+=2)
{

s += (int)[Link](i, pow);
a += [Link]() + "^" + pow;
if(i < end)
{
a += " + ";
}
}
[Link](a + " = " + s);

}
}
}

Common questions

Powered by AI

The 'printPowSumFrom' method calculates the sum of even numbers raised to a specified power, 'pow', from 2 up to 'end'. It iterates through even numbers, computes their power using Math.Pow, and accumulates these values into a sum 's'. The method displays the expression of this power sum calculation. It addresses the problem of computing and expressing mathematical power series of even numbers in a concise format .

The method first checks if 'end' is greater than zero, ensuring a meaningful range for calculation. If positive, it computes the sum from 'start' to 'end' by iterating over the range and adding each integer to 's'. This condition ensures the summation logic only proceeds when there's a valid numerical scope for operation .

Within the 'printNumbers' method, 'sum' accumulates the total of all integers from 'start' to 'n'. It is incremented by the loop variable 'i' at each iteration. This operation holds significance in tracking the cumulative sum of numbers being printed, thereby providing an aggregated result up to the specified limit 'n' .

The 'checkPositivity' method employs a series of conditional checks to classify 'n' as zero, positive, or negative. It uses 'if', 'else if', and 'else' clauses to cover all numerical scenarios: equality to zero, greater than zero, and less than zero, respectively. This structure ensures all potential integer conditions are assessed, providing comprehensive classification .

Using Math.Pow in 'printPowSumFrom' enables efficient calculation of large powers by leveraging optimized mathematical routines. However, for large 'end' values, results could overflow the 'int' storage capacity, leading to incorrect sums. The method's reliance on double to perform pow calculations provides some precision resilience but necessitates caution in managing potential precision loss and overflow risk, especially due to conversion from double to int .

When the age is 16, the output is "You cannot vote" because the input is less than 18. When the age is 20, the output is "You can vote" since the input is equal to or greater than 18. The method uses an if-else structure to check if the age meets the minimum voting requirement .

The 'compareWith' method determines the relationship by comparing the class variable 'n' with the parameter 'b'. It prints "n > b" if the class variable is greater, "b > n" if the parameter is greater, and "b = n" if they are equal. This involves conditional statements to evaluate the integer values .

Setting the default start value to 1 in 'printNumbers' implies that the method is primarily intended to print natural numbers up to 'n'. By assuming a base print range from 1, it simplifies invocations by allowing users to omit the 'start' parameter for common usage scenarios. However, it limits the scope of the function when initiating from arbitrary non-natural numbers without explicit input adjustment .

The 'checkIfEven' method determines if a given number is even by returning true if 'number % 2 == 0'. It is notably used within the 'printUntilZero' method to break the loop upon finding an even number. This modular approach encapsulates the even-check logic, promoting reusability across other methods requiring parity check .

The 'printUntilZero' method reads integers from the user until either a zero is input or an even number is found. If an even number is detected, it prints "Number even found!" and exits the loop. If an odd number is entered, it asks for further input. This drives a while loop that ensures execution halts only when an even number or zero is provided, facilitating responsive and iterative user interaction .

You might also like