0% found this document useful (0 votes)
8 views3 pages

MODULE - 2 Java

The document outlines a procedure for calculating the average of two numbers using Java programming. It provides two examples: one with hardcoded values and another that takes user input. Additionally, it includes a task to write a program for calculating the average of three subjects and displaying total and average marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

MODULE - 2 Java

The document outlines a procedure for calculating the average of two numbers using Java programming. It provides two examples: one with hardcoded values and another that takes user input. Additionally, it includes a task to write a program for calculating the average of three subjects and displaying total and average marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MODULE – 2

JAVA PROGRAMMING
Procedure to find the average of two numbers,
1) Take two numbers
2) Declare a sum variable
3) Calculate the addition of two numbers and assign them to the sum variable
4) Find average as average = sum/2
5) Finally, display the result of the average value
1.Write a Java program to find the average of two numbers.?
• First, we will develop a simple Java program by hardcoding the values
i.e. required values will be initialized inside the program.
Java program to find average of two numbers
public class Average {
public static void main(String[] args) {

// take two numbers


double num1 = 10;
double num2 = 20;

// declare sum variable


// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;

// calculate the sum value


sum = num1 + num2;
// calculate the average value
avg = sum/2;
// display result
System.out.println("Average: " + avg );
}
}
Output:-
Average: 15.0

2. Write the Java program to find average of two numbers by


taking input from end-user?
import java.util.Scanner;
public class Average {
public static void main(String[] args) {

// create Scanner class object


Scanner scan = new Scanner(System.in);

// declare two numbers


double num1 = 0;
double num2 = 0;

// declare sum variable


// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// take two numbers
System.out.print("Enter two numbers: ");
num1 = scan.nextDouble();
num2 = scan.nextDouble();

// calculate the sum value


sum = num1 + num2;
// calculate the average value
avg = sum/2;

// display result
System.out.println("Average: " + avg );
}
}
Output for the different test-cases:-
Enter two numbers: 50 100
Average: 75.0
Enter two numbers: -100 125
Average: 12.5

TASKS TO BE COMPLETED:
1. Write a program in Java to calculate the average of three subjects and
display total and average marks.?

You might also like