JAVA_CODINGS Telegram 72
52. Find closest value of a number in an Array.

import java.util.*;

class ClosestValue
{
public static void main(String[] args)
{
int a[];
int find;
int closest = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");
int size = sc.nextInt();

a = new int[size];

System.out.println("Enter numbers in array");
for (int i = 0; i < size; i++)
{
a[i] = sc.nextInt();
}

System.out.println("Numbers are : ");
for (int i = 0; i < size; i++)
{
System.out.print(a[i] + " ");
}

System.out.println();
System.out.println("Enter Number to find closest value");
find = sc.nextInt();

int distance = Math.abs(closest - find);

for (int i : a)
{
int distanceI = Math.abs(i - find);
if (distance > distanceI)
{
closest = i;
distance = distanceI;
}
}

System.out.println("Closest Value is : " + closest);
}

}

@java_codings



tgoop.com/java_codings/72
Create:
Last Update:

52. Find closest value of a number in an Array.

import java.util.*;

class ClosestValue
{
public static void main(String[] args)
{
int a[];
int find;
int closest = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter size of array");
int size = sc.nextInt();

a = new int[size];

System.out.println("Enter numbers in array");
for (int i = 0; i < size; i++)
{
a[i] = sc.nextInt();
}

System.out.println("Numbers are : ");
for (int i = 0; i < size; i++)
{
System.out.print(a[i] + " ");
}

System.out.println();
System.out.println("Enter Number to find closest value");
find = sc.nextInt();

int distance = Math.abs(closest - find);

for (int i : a)
{
int distanceI = Math.abs(i - find);
if (distance > distanceI)
{
closest = i;
distance = distanceI;
}
}

System.out.println("Closest Value is : " + closest);
}

}

@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Share with your friend now:
tgoop.com/java_codings/72

View MORE
Open in Telegram


Telegram News

Date: |

Matt Hussey, editorial director of NEAR Protocol (and former editor-in-chief of Decrypt) responded to the news of the Telegram group with β€œ#meIRL.” 3How to create a Telegram channel? Your posting frequency depends on the topic of your channel. If you have a news channel, it’s OK to publish new content every day (or even every hour). For other industries, stick with 2-3 large posts a week. How to create a business channel on Telegram? (Tutorial) It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart.
from us


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM American