Computer Science, asked by SuryanshRam, 6 hours ago

Accept 3 numbers and check all are same or not.If all are same then display the sum of the last digits of the first and third number.Otherwise display the product of the second and third number in java.​

Answers

Answered by anindyaadhikari13
13

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

The given problem is solved using language - Java.

import java.util.Scanner;

public class NumberOperation{

   public static void main(String args[]){

       Scanner sc=new Scanner(System.in);

       int a,b,c;

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

       a=sc.nextInt();

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

       b=sc.nextInt();

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

       c=sc.nextInt();

       sc.close();

       if(a==b&&b==c){

           System.out.println("All the numbers are equal.");

           int x=(a%10)+(c%10);

           System.out.println("Sum of the last digit of first and third number is: "+x);

       }

       else{

           System.out.println("All the numbers are not equal.");

           System.out.println("Product of second and third number is: "+b*c);

       }    

   }

}

\textsf{\large{\underline{Logic}:}}

  1. Accept the numbers from the user, say a, b and c.
  2. Check if a == b and b == c.
  3. If true, extract the last digit of a and c by using modulo operator.
  4. Add the digits and display the result.
  5. If false, multiply b and c and display the product.

See the attachment for output.

Attachments:

anindyaadhikari13: Thanks for the brainliest :)
Answered by kamalrajatjoshi94
1

Answer:

Program:-

import java.util.*;

public class Program

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int a,b,c,p=0,ld1=0,ld2=0,s=0;

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

a=in.nextInt();

b=in.nextInt();

c=in.nextInt();

if(a!=b||b!=c||c!=a)

{

p=b*c;

System.out.println("All numbers are not equal");

System.out.println("The product of second and third number="+p);

}

else if(a==b&&b==c&&c==a)

{

System.out.println("All numbers are equal");

ld1=a%10; //For first number

ld2=c%10; //For third number

s=ld1+ld2;

System.out.println("The sum of last digits of first and third number="+s);

}

}

}

Logic:-

  • Accept 3 numbers either by using scanner or BufferedReader class.
  • If numbers are not equal the product of second and third number is displayed.
  • If numbers are equal then find the remainder by using '%' operator.

Ex if number is 56

Then 56%10=6 which is the last digit.

  • Finally display a suitable message for the question.

I hope you understand.

Attachments:
Similar questions