How do I get a decimal

Discussion in 'C Programming' started by 1236x, Dec 11, 2017.

  1. 1236x

    1236x

    Joined:
    Dec 11, 2017
    Messages:
    2
    Likes Received:
    0
    Hi. I wrote this code below to find the average of three numbers. It works, but the answer is always rounded to a whole number. How can I change my code to make this work?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Assignment_1_mcdaniel
    {
    class Program
    {
    static void Main(string[] args)
    {
    int f, s, t, r;
    Console.WriteLine("Enter Frst Number:");
    f = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter Second Number:");
    s = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter Third Number:");
    t = Convert.ToInt32(Console.ReadLine());
    r = (f + s + t) / 3;
    Console.WriteLine("Answer:" + r);
    Console.ReadKey();
    }
    }
    }
     
    1236x, Dec 11, 2017
    #1
    1. Advertisements

  2. 1236x

    Robert Johnson

    Joined:
    Dec 27, 2017
    Messages:
    1
    Likes Received:
    0
    Location:
    San Fransisco
    You have defined all the numbers as integers here int f, s, t, r;
    So as to get the output in decimals, define float r;
    Hope it helps...
     
    Robert Johnson, Dec 28, 2017
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.