0% found this document useful (0 votes)
7 views2 pages

05_Using_Multiple_Functions

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

05_Using_Multiple_Functions

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q> Write a program to input two numbers and perform addition,

subtraction, multiplication and division on them using four different


functions.

import java.util.*;

class RNC
{
double add(double a, double b)
{
return a + b;
}

double sub(double a, double b)


{
return a – b;
}

double mul(double a, double b)


{
return a * b;
}

double div(double a, double b)


{
return a / b;
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);

double x, y, rs ;

System.out.println("Enter the two numbers : ") ;


x = sc.nextDouble();
y = sc.nextDouble();

RNC ob = new RNC() ;

rs = ob.add(x,y);
System.out.println("Sum = " + rs) ;

rs = ob.sub(x,y);
System.out.println("Difference = " + rs) ;
rs = ob.mul(x,y);
System.out.println("Product = " + rs) ;

rs = ob.div(x,y);
System.out.println("Quotient = " + rs) ;

}}

Note : (i) This program uses four functions. Since all the four functions have
different names, there is NO overloading.

(ii) All four functions have parameters with the same names – ‘a’ and ‘b’.
This is perfectly acceptable. You may use different variable names (instead
of ‘a’ and ‘b’) for each function.

Q> Re-write the above program using switch-case (based on the user’s
choice).

You might also like