import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
//your code here
Scanner sc =new Scanner(System.in);
int N=sc.nextInt();
int[] arr=new int[N];
for(int i=0;i<N;i++)
arr[i]=sc.nextInt();
Solution s=new Solution();
s.Answer(arr,N);
}
}
class Solution
{
public void Answer(int [] arr,int N)
{
int p=0,ne=0,z=0,i=0;
while(i<arr.length)
{
if(arr[i]>0)
p++;
else if(arr[i]<0)
ne++;
else z++;
i++;
}
System.out.printf("%1.4f \n", p/N);
System.out.printf("%1.4f \n", ne /N);
System.out.printf("%1.4f\n ", z / N);
}
}
I have written the above code for the question Given an array of integers of size N integers, the task is to find the ratio of positive numbers, negative numbers, and zeros in the array up to four decimal places.
Examples:
Input: a[] = {2, -1, 5, 6, 0, -3}
Output: 0.5000 0.3333 0.1667
There are 3 positive, 2 negative, and 1 zero. Their ratio would be positive: 3/6 = 0.5000, negative: 2/6 = 0.3333, and zero: 1/6 = 0.1667.
I compiled the above code but it showing an Illegalformatexception. But I am not getting why it is showing like that.
I checked all syntaxes which I have used in this code. but I couldn't find that problem.
please help me in solving this plus-minus problem.
package ddd;
import java.util.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
//your code here
Scanner sc =new Scanner(System.in);
int N= sc.nextInt();
int[] arr=new int[N];
for(int i=0;i<N;i++)
arr[i]=sc.nextInt();
Solution s=new Solution();
s.Answer(arr,N);
}
}
class Solution
{
public void Answer(int [] arr,int N)
{
int p=0,ne=0,z=0,i=0;
while(i<arr.length)
{
if(arr[i]>0)
p++;
else if(arr[i]<0)
ne++;
else z++;
i++;
}
System.out.printf("%1.4f \n", p/(float)N);
System.out.printf("%1.4f \n", ne/(float)N);
System.out.printf("%1.4f\n ", z/(float)N);
}
}
I think just converting N to float would solve your problem.
Related
link to problem
import java.util.Arrays;
import java.util.Scanner;
class Codechef { static boolean test(int[]a,int n){
for(int i=0;i<n;i++){
if(a[i]-a[i+1]>0)
System.out.println("not si");
return false;
}
return true;
}
static int[] array(int[]a,int n){
int[] a1=new int[n-1];
if(test(a,n )){
for(int i=0;i<n-1;i++){
a1[i]=a[i+1]=a[i];
}
}
return a1;
}
static int type(int[]a,int n){
int c=0;
int []k=array(a, n);
Arrays.sort(k);
for(int i=0;i<n-2;i++){
if(k[i]!=k[i+1])c++;
}
return c;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Array & no of elements");
int n=in.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
a[i]=in.nextInt();
int c=type(a, n);
System.out.println(c);
}`
the following code gives the output:
Array & no of element
5
0
2
8
10
16
0 (output)
clearly not the desired results. So where am i going wrong? please enlighten me.
Ive tried a set of "nested" methods and was expecting for the value of c to be greater than zero...
//Modify the program from the previous exercise, so that it displays just the sum of all of the numbers from one to the input number. Be sure to test your program with several inputs.
// Example5.java
import java.util.Scanner;
public class Example5 {
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = in.nextInt();
while (count <= number) {
System.out.println(count);
++count;
}
}
Given that code I have to modify the program that it displays the sum from 1 to the input number.
You asked
How do I accumulate a sum of a series of input values?
In the mentioned practice you need to separate your numbers with whitespace, there are many more advance approach like using IntStream api.
public class Example5 {
public static void main(String[] args) {
int sum = 0;
System.out.println("Add your numbers to sum: ");
Scanner in = new Scanner(new Scanner(System.in).nextLine());
while (in.hasNext()) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
This code gives an output based on the statement, "Given that code I have to modify the program that it displays the sum from 1 to the input number."
import java.util.Scanner;
public class Example5 {
public static void main(String[] args) {
int sum = 0, number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
number = in.nextInt();
for (int i = 1; i <= number; i++) sum += i;
System.out.println(sum);
}
}
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
//write your code here
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
// For each Iteration I am adding the total no. of
// 1's place digits, 10's place digits, 100's place digits, and so on.
for(int i=0;i<T;i++){
int Num = sc.nextInt();
int count = 0;
for(int j=1;j<=Num;j*=10){
count += Num-j+1;
}
System.out.println(count);
}
}
}
Input:
3
100
1000000000
222222222
The output I am seeing:
192
298954307
1888888896
Desired Output:
192
8888888899
1888888896
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
//write your code here
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0;i<T;i++){
int Num = sc.nextInt();
long count = 0;
for(int j=1;j<=Num;j*=10){
count += Num-j+1;
}
System.out.println(count);
}
}
}
I just needed to change the count variable from int to long.
I've been working on this program and am currently stuck. The HW prompt is to prompt a user to input numbers, save it as an array, find the number of odd numbers & the percentages then display those values back to the user.
Currently I am trying to write to part of the code that finds the percentage of the odd numbers in the array but the return isn't displaying and i just cant figure it out. Any ideas? Thank you!
import java.util.*; // import java course for Scanner class
public class Integers {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
int inputs = Integer.parseInt(console.next());
int[] arraysize = new int[inputs];
Oddvalues(arraysize);
}
public static int Oddvalues (int[] size) {
int countOdd = 0;
for (int i = 1; i < size.length; i++) {
if(size[i] % 2 != 0) {
i++;
}
}
return countOdd;
}
}
Consider the following code, which appears to be working in IntelliJ locally. My approach is to read in a single line from the scanner as a string, and then to split that input by whitespace into component numbers. This avoids the issue you were facing of trying to directly create an array of integers from the console.
Then, just iterate over each numerical string, using Integer.parseInt(), checking to see if it be odd.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
String nextLine = console.nextLine();
String[] nums = nextLine.split(" ");
int oddCount = 0;
for (String num : nums) {
if (Integer.parseInt(num) % 2 == 1) {
++oddCount;
}
}
double oddPercent = 100.0*oddCount / nums.length;
System.out.println("Total count of numbers: " + nums.length + ", percentage odd: " + oddPercent);
}
In the function Oddvalues you promote i instead of promoting countOdd. And the loop should start from 0 not 1.
Try this
import java.util.*;
import java.lang.*;
import java.io.*;
public class OddVals{
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int[] array = new int[sc.nextInt()]; // Get the value of each element in the array
System.out.println("Please input a series of numbers");
for(int i = 0; i < array.length; i++)
array[i] = sc.nextInt();
System.out.println("Number of Odds:" +Oddvalues(array));
printOdd(array);
}
public static int Oddvalues (int[] size) {
int countOdd = 0;
for (int i=0; i < size.length; i++){
if(size[i]%2 != 0)
++countOdd;
}
return countOdd;
}
public static void printOdd(int[] arr)
{
for(int i=0;i<arr.length;++i)
{
if(arr[i]%2==1)
System.out.print(arr[i]+" ");
}
}
import java.util.*; // import java course for Scanner class
public class Integers {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
while (console.hasNext())
{
String str = console.next();
try
{
if(str.equals("quit")){
break;
}
int inputs = Integer.parseInt(str);
System.out.println("the integer values are" +inputs);
intList.add(inputs);
}
catch (java.util.InputMismatchException|NumberFormatException e)
{
console.nextLine();
}
}
console.close();
double d = Oddvalues(intList);
System.out.println("the percent is" +d);
}
public static double Oddvalues (List<Integer> list) {
int count = 0;
for( Integer i : list)
{
if(!(i%2==0))
{
count++;
}
}
double percentage = (Double.valueOf(String.valueOf(count))/ Double.valueOf(String.valueOf(list.size())))*100;
return percentage;
}
}
If this helps
I am trying to write a java code for getting the total number of combinations of subset for which sum is equal to x where n is the number of elements using recursion ..I wrote the code but it seems it needs to be more optimized since its taking a lot of time to get the result
import java.util.*;
class subset
{
public static void main(String[] args)
{
int n,x,z;
int m[]=new int[30];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
x=sc.nextInt();
for(int i=0;i<n;i++)
{
m[i]=sc.nextInt();
}
z=func(0,m,x);
System.out.println(z);
}
static int func(int i,int m[],int w)
{
if(i==m.length)
{
return 0;
}
if(m[i]==w)
return (1+func(i+1,m,w));
else
{
return((func(i+1,m,w-m[i])+func(i+1,m,w)));
}
well I found out how to optimize it.I simply sorted out the inputs and the put a constraint the if the sum calculated by the func function becomes greater than the subset sum(i.e x) it will discard it.
import java.util.*;
class subset
{
public static void main(String[] args)
{
int n,x,z;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
x=sc.nextInt();
int m[]=new int[n];
long b=System.currentTimeMillis();
for(int i=0;i<n;i++)
{
m[i]=sc.nextInt();
}
Arrays.sort(m);
for(int i=0;i<m.length;i++)
{
System.out.print(" "+m[i]+" " );
}
z=func(0,m,x);
System.out.println(z);
System.out.println(System.currentTimeMillis()-b);
}
static int func(int i,int m[],int w)
{
if(i==m.length||m[i]>w)
{
return 0;
}
if(m[i]==w)
return (1+func(i+1,m,w));
else
{
return((func(i+1,m,w-m[i])+func(i+1,m,w)));
}
}
}