Computer Science, asked by Anonymous, 6 months ago

Write a program in Java to enter 3 numbers and a character. Find and print some of the numbers if the given character is 's' and product of the numbers if the character is 'p'. The program * displays an image invalid character if the user enters and alphabet other than s or p .Write a program to enter 3 numbers and a character. Find and print some of the numbers if the given character is 's' and product of the numbers if the character is 'p'. The program displays an image invalid character if the user enters and alphabet other than s or p.

Answers

Answered by apurwa2shree2
21

Answer:

import java.util.Scanner;

public class homework_2

{

   public static void main(String args[])

   {

       Scanner ab = new Scanner(System.in);

       int a,b,c,d,f;

       char e;

       System.out.println("Enter your first number.");

       a=ab.nextInt();

       System.out.println("Enter your second number.");

       b=ab.nextInt();

       System.out.println("Enter your third number.");

       c=ab.nextInt();

       System.out.println("Enter your desired character.");

       e=ab.next().charAt(0);

       d= a+b+c;

       f= a*b*c;

       if(e == 's')

       {

           System.out.println("The sum of the entered numbers is " + d + ".");

       }

       else if(e == 'p')

       {

           System.out.println("The product of the entered numbers is " + f + ".");

       }

       else

       {

           System.out.println("Invalid character.");

       }

   }

}

Explanation:

This will be the answer.

Answered by qwmillwall
4

Sum and Product.

  • To check the given input we will use the if-else-if ladder.
  • In if-else-if, we first check the condition and if it is false then we check another condition in the else-if clause.
  • And further, we can add more else-if.
  • The ladder ends with an else, which is not compulsory.

JAVA Code:

import java.util.Scanner;

public class sum_product {

  public static void main(String agrs[])   {

      double a, b, c, sum, prod;

      char s, p;

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter your choice (s or p):");

      char ch = sc.nextLine().charAt(0);

      if (ch=='s')  {

          System.out.println("Enter three numbers:");

          a = sc.nextDouble();

          b = sc.nextDouble();

          c = sc.nextDouble();

          sum = a + b + c;

          System.out.println("Sum = "+sum+".");

      }

      else if(ch=='p')  {

          System.out.println("Enter three numbers:");

          a = sc.nextDouble();

          b = sc.nextDouble();

          c = sc.nextDouble();

          prod = a * b * c;

          System.out.println("Product = "+prod+".");

      }

      else {

          System.out.println("Invalid character");

      }

  }

}

#SPJ2

Similar questions