0

Please kindly assist to look at the reason why the value of the triangle is not 15 but it is 0 in this my codes. Thank you. Gbenga Olanipekun

class Program
{
    static void Main(string[] args)
    {
        // int a =3 + 6;
        // int b = 5 -7;
        // int a;
        // a =3 + 6;
        // b =5 + 7;
        // int a = 6
        // b =c +5;
        // v = b -j ;
        // double moneymadefromgames = 100000;
        // double totalprogrammers = 4;
        // double moneyperperson = moneymadefromgames / totalprogrammers; 
        // the formular for area of circle is pi * r ^ 2;
        // the formular for area of triagle is 1/2 b * h;
        float b = 5;
        float height = 6;
        double area = 1/2 *b *  height;
        Console.WriteLine("the areas of triagle is " + area);
        int score;
        int age;
        age = -89;
        score = 33;
        int loan;
        int credit;
        float radius = 4;
        float pi = 3.1415926536f; // the 'f' makes it a float literal instead of a double literal.
        float newarea = pi * radius * radius;
        // using the + operator with strings results in "concatenation".
        Console.WriteLine("The area of the circle is " + newarea + ".");
       loan = 2000;
        credit = 5000;
        float totalcost = 22.54F;
        float tipPercent = 0.18F;
        float tipAmount =  totalcost * tipPercent;
        double moneymadefromgames = 100000;
        double totalprogrammers = 4;
        double moneyperperson = moneymadefromgames / totalprogrammers;
        System.Console.WriteLine(moneyperperson);
        System.Console.WriteLine(score - age);
        System.Console.WriteLine("PRESS ANY key to exit");
        System.Console.WriteLine(loan + credit);
        System.Console.WriteLine(tipAmount);
        System.Console.Read();
        
    }
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    Since you hava specific query about one calculation, you should remove the irrelevant code from your question that is doing the other unrelated calculations – moreON Oct 08 '20 at 04:46

2 Answers2

1

The issue here is the division. You want to calculate 0.5 * b * height. So if you were to enter 0.5 instead of 1/2, you would get 15.

What's happening here is integer division.

For the operands of integer types, the result of the / operator is of an integer type and equals the quotient of the two operands rounded towards zero

From the Microsoft documentation

When you divide two integer numbers, the program will ignore the remainder. As 1/2 is 0.5, all that the integer division will return is 0, ignoring the .5 remainder.
To solve this issue, you need to make at least one of your numbers a float/double value by adding a remainder. 1/2.0 or 1.0/2 or 1.0/2.0. Thus the result will be 0.5.

Console.WriteLine(1   / 2  );  // output: 0
Console.WriteLine(1   / 2.0);  // output: 0.5
Console.WriteLine(1.0 / 2  );  // output: 0.5
Console.WriteLine(1.0 / 2.0);  // output: 0.5
Custos
  • 68
  • 6
0

You need to change the 1/2 value to 0.5 for area calculation. So it should double area = 0.5 *b * height;. This is the reason why you are getting zero instead of actual value.


using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            float b = 5;
            float height = 6;
            double area = 0.5 *b *  height;
            Console.WriteLine("the areas of triagle is " + area);
        }
    }
}

Output:

The areas of triangle is 15.

Here is the screenshot.

enter image description here

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
  • 1
    It may be helpful to explain why as well. `1` and `2` are integer literals. the `/` operator with with integer left and right operands produces an integer result that is the floor of the division ... Maybe also mention the other options: keep formula as is, but use float literals i.e. `1f / 2f * ...` or just divide by `2` the integer value at the end of the expression instead. – moreON Oct 08 '20 at 04:43