Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
//System.out.println("Enter the numbers ");
for(int i = 1; i<=n ; i++)
{
Scanner scan2 = new Scanner(System.in);
binaryValues = scan2.nextInt();
}
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
//System.out.println("Enter the numbers ");
for(int i = 1; i<=n ; i++)
{
Scanner scan2 = new Scanner(System.in);
binaryValues = scan2.nextInt();
}
Hey @Nitya_Somani
please Send me complete code
Scanner scan2 = new Scanner(System.in); // No need
// Make 1 Scanner obj in a 1 method ya function
Scanner scan = new Scanner(System.in); // use this Scanner
package ChallengePatterns;
import java.util.Scanner;
public class Challenge2 {
public static void main(String[] args)
{
System.out.println("The number of binary numbers to get converted ");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
//System.out.println("Enter the numbers ");
int binaryvalues;
for(int i = 1; i<=n ; i++)
{
Scanner scan2 = new Scanner(System.in);
binaryValues = scan2.nextInt();
}
// To convert into decimal now
int var = 0 ;
int decimalNo=0;
while( binaryValues > 0 )
{
int rem = binaryValues % 10;
decimalNo = decimalNo +rem *var;
var=var*2;
decimalNo = decimalNo /10;
System.out.println(decimalNo);
binaryValues++;
//prep
System.out.println();
}
}
In this code also it is taking values one by one and then converting it but in question all the 4 values are given together by user and is getting converted together .In this also by scanner one at a time we are taking values
Q. all the 4 values are given together by user and is getting converted together
Answer : you can do it this way. but you don’t know arrays concept.
it’s better to use loop
your for loop execute first Time i=0;
Take input and It will be converted binary to decimal
then execute again i=1;
Take input and It will be converted binary to decimal
The loop will continue. As long as i <4