Computer Science, asked by karoleena, 1 month ago

write a program which uses a function to accept two numbers from the users and another function to print their sum ​

Answers

Answered by anindyaadhikari13
3

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Java.

import java.util.Scanner;

class Program{

   int a,b;

   void acceptNumber(){

       Scanner sc=new Scanner(System.in);

       System.out.print("Enter a number: ");

       a=sc.nextInt();

       System.out.print("Enter another number: ");

       b=sc.nextInt();

   }

   void displaySum(){

       System.out.println("The sum of the two numbers is: "+(a+b));

   }

   public static void main(String args[]){

       Program obj=new Program();

       obj.acceptNumber();

       obj.displaySum();

   }

}

\textsf{\large{\underline{Explanation}:}}

  • Here, the Scanner class is imported so as to take the numbers as input.
  • Then a function named acceptNumber() is created which accepts two number.
  • The function displaySum() displays the sum of the entered numbers.

See attachment for output.

Attachments:
Similar questions