How do I get a decimal

Joined
Dec 11, 2017
Messages
2
Reaction score
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();
}
}
}
 
Joined
Dec 27, 2017
Messages
1
Reaction score
0
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...
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top