Computer Science, asked by purveshKolhe, 4 months ago

write a program in java to get input from user and get its square - 2.​

Answers

Answered by BrainlyProgrammer
12

Corrected Question:-

  • WAP in JAVA to get input from user and get its square-2

SAMPLE INPUT:

3

SAMPLE OUTPUT:

7

Logic:-

  • Subtract 2 from the Square

Required Answer:-

import java.util.*;

public class Check{

public static void main (String ar []){

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

System.out.println((int)(Math.pow(n,2))-2);

}

}

• Check Attachment for your output.

Attachments:
Answered by simrankerketta007
1

Explanation:

The above statement creates a constructor of the Scanner class having System.inM as an argument. It means it is going to read from the standard input stream of the program. The java.util package should be import while using Scanner class.

It also converts the Bytes (from the input stream) into characters using the platform's default charset.

Methods of Java Scanner Class

Java Scanner class provides the following methods to read different primitives types:

Method Description

int nextInt() It is used to scan the next token of the input as an integer.

float nextFloat() It is used to scan the next token of the input as a float.

double nextDouble() It is used to scan the next token of the input as a double.

byte nextByte() It is used to scan the next token of the input as a byte.

String nextLine() Advances this scanner past the current line.

boolean nextBoolean() It is used to scan the next token of the input into a boolean value.

long nextLong() It is used to scan the next token of the input as a long.

short nextShort() It is used to scan the next token of the input as a Short.

BigInteger nextBigInteger() It is used to scan the next token of the input as a BigInteger.

BigDecimal nextBigDecimal() It is used to .

see attachment ♥️

Attachments:
Similar questions