Enter 3 numbers from the user & make a function to print their average.
To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average:
import java.util.*;
public class Solutions {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int average = (a + b + c) / 3;
System.out.println(average);
}
}
This implementation takes three double numbers from the user using the cin function and then calls the average function to calculate their average. The average function calculates the sum of the three numbers and then divides the sum by 3 to get the average.
C:
This implementation is similar to the C++ implementation, but it uses the printf and scanf functions instead of cout and cin for input and output.
Python:
This implementation takes three float numbers from the user using the input function and then calls the average function to calculate their average. The average function calculates the sum of the three numbers and then divides the sum by 3 to get the average.
Java:
This implementation is similar to the C++ implementation, but it uses the Scanner class to take input from the user. The average function calculates the sum of the three numbers and then divides the sum by 3 to get the average.
Others Links:-
>> Package in Java| what is the case used for a package in java| creating packages in java
>> Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.
>> Write a function that calculates the Greatest Common Divisor of 2 numbers
>> Variables & Data Types (Lecture 2) | Primitive Data Types| Non-Primitive Data types
Comments
Post a Comment