Skip to main content

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.


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:



JAVA

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);

   }   

}

 





C++:

// c++

#include <iostream>

using namespace std;

double average(double num1, double num2, double num3) {
    double sum = num1 + num2 + num3;
    double average = sum / 3;
    return average;
}

int main() {
    double num1, num2, num3;
    cout << "Enter three numbers: ";
    cin >> num1 >> num2 >> num3;
    double avg = average(num1, num2, num3);
    cout << "The average is: " << avg << endl;
    return 0;
}


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:

// c

#include <stdio.h>

double average(double num1, double num2, double num3) {
    double sum = num1 + num2 + num3;
    double average = sum / 3;
    return average;
}

int main() {
    double num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &num1, &num2, &num3);
    double avg = average(num1, num2, num3);
    printf("The average is: %lf\n", avg);
    return 0;
}


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:

//python
Copy code
def average(num1, num2, num3):
    sum = num1 + num2 + num3
    avg = sum / 3
    return avg

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
avg = average(num1, num2, num3)
print("The average is:", avg)



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:



Java:

// java

import java.util.Scanner;

public class Average {
    public static double average(double num1, double num2, double num3) {
        double sum = num1 + num2 + num3;
        double average = sum / 3;
        return average;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter three numbers: ");
        double num1 = scanner.nextDouble();
        double num2 = scanner.nextDouble();
        double num3 = scanner.nextDouble();
        double avg = average(num1, num2, num3);
        System.out.println("The average is: " + avg);
        scanner.close();
    }
}



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

>> Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

>> 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

Popular posts from this blog

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...