import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner s= new Scanner(System.in);
int t1=s.nextInt();
int t2= s.nextInt();
int n= s.nextInt();
double arr[]= new double[20];
for(int i=0;i<20;i++){
arr[i]=-1;
}
arr[1]= t1;
arr[2]=t2;
if(arr[n]!=-1){
System.out.println((long)arr[n]);
}
else{
for(int i=3;i<=n;i++){
arr[i]= arr[i-2] + Math.pow(arr[i-1],2);
}
System.out.println((long)arr[n]);
}
}
}
This code is a modified Fibonacci series. I want to calculate the tenth digit in this sequence. But the result is very big I want to ask in which type I should cast the answer ? I have used long but it failed...please suggest any other type ...
There's a special type for this kind of computation called BigInteger. Find more infos here.
Edit:
You can create a BigInteger from a String
BigInteger bigInt = new BigInteger("24");
or from an integer type
BigInteger fromInt = BigInteger.valueOf(24);
Related
import java.util.Scanner;
import java.lang.Math;
public class TestMax {
int minNum = 1, maxNum = 5;
public int inputNum() {
Scanner num = new Scanner(System.in);
return inputNum();
}
public void displayNum(int userNum) {
System.out.printf(Math.min(userNum, maxNum));
System.out.printf(Math.max(userNum, minNum));
}
public static void main(String[] args) {
TestMax input;
input = new TestMax();
int userNum = input.inputNum();
input.displayNum(userNum);
}
}
Sorry about any incorrect formatting, it's my first time posting. I'm creating a program for a school in which a user enters a number and the program then outputs the 'maxNum' or 'minNum' depending on whether the number entered is less than or greater than the min/maxNum. I think I have everything right except for when it comes to the math function. I want it to compare the userNum to the min/maxNum but its saying I'm using an int and need a string. Any help would be greatly appreciated.
You need to return a number from Scanner like this:
public int inputNum() {
Scanner s = new Scanner(System.in);
int number = s.nextInt();
return number;
}
I have written a small program to take number of elements from user and the price of those elements and then print the price.
But the 12th line of this code giving error "float cannot be converted into float[]", and I am not able to figure out how to resolve this. Please provide some help or any modification in the code if needed.
import java.util.Scanner;
public class Main{
public static void main( String args[]){
System.out.println("enter the number of elements :" );
Scanner s1= new Scanner(System.in);
int N = s1.nextInt();
System.out.println("enter the price of all the elements in ascending
order: ");
float[] price =new float[N];
for(int i=0; i<N;i++){
price=s1.nextFloat();
System.out.println(price);
}
}
}
In your code, price is an array
float[] price = ...
And an array of type T consists of elements, each of type T. So you need to assign an element of the array to a float e.g.
price[i] = myFloat;
Formatting and nomenclature are a must when you write code as it makes your code more readable and easy to understand. I have formatted your code and also updated for proper inputs.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
System.out.println("enter the number of elements :");
Scanner s1 = null;
try {
s1 = new Scanner(System.in);
int totalElements = s1.nextInt();
System.out.println("enter the price of all the elements in ascending order :");
float[] price = new float[totalElements];
for (int i = 0; i < totalElements; i++) {
price[i] = s1.nextFloat();
System.out.println(Arrays.toString(price));
}
} finally {
if (s1 != null) {
s1.close();
}
}
}
}
Please take care of below things:
Variable names should start with small case and follow camel casing.
Scanner whenever used needs to be closed.
Your error is resolved by assigning float to one position in array and not to the array. E.g. price[i] = s1.nextFloat();
To print an array, use the Arrays.toString() function.
Happy Coding
import java.util.Scanner;
public class Main{
public static void main( String args[]){
System.out.println("enter the number of elements :" );
Scanner s1= new Scanner(System.in);
int N = s1.nextInt();
System.out.println("enter the price of all the elements in ascending
order :");
float[] price =new float[N];
for(int i=0; i<N;i++){
price[i]=s1.nextFloat();
System.out.println(price[i]);
}
}
}
It should be like this because in the printing statement you've called the whole array that won't give you the element but the some numbers and alphabets thing and also the statement where you're reading the prices by scanner you are not telling the computer to put your price at which index of the array that's why it's giving this error.
My question is based on a number manipulation in java. please, give any example for calculating the sum of any numbers and its reverse in java.for example, 123+321.
Public int sumReverse(int num){
int orignal=num;
int reverse=0;
While(orignal>0){
int remainder=orignal%10;
reverse=reverse*10+remainder;
orignal=orignal/10;
}
return num+reverse;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String n2s="";
int n1= sc.nextInt();
String n1s= String.valueOf(n1); //let n1s be the String of n1
for (int i=1;i<=n1s.length();i++){
n2s+=n1s.charAt(n1s.length()-i);
}
System.out.println(n2s);
int n2=Integer.valueOf(n2s);
int adit=n1+n2;
System.out.println(n1s+" + "+n2s +" = "+ adit);
}
In order to manipulate numbers they way you're looking for, it is easier to work that answer with the String of that number.
Scanner allow the user to type any number he/she wants to manipulate.
Hope this is usefull for you.
You could use StringBuilder also
int num = 123;
StringBuilder ob = new StringBuilder(Integer.toString(num));
ob.reverse();
System.out.println(num + Integer.valueOf(ob.toString()));
Store the data in StringBuilder and then add it to the original after reversing it.
I was trying to write a recursive function which gets a string and returns the integer form of it. I know that this can be done easily in Java using Integer.parseInt. This is a part of an exercise I am doing.
For the following code, when I enter "963" as the input, it returns the output as "6291". So, i checked if the array was getting passed properly and it was.
To check the multiplication, I split and printed the product each time, which gave 5700, 540 and 51, which correctly sum up to 6291. (which actually should have been 900 + 60 + 3 = 963). I'm unable to figure out what is wrong in my program.
import java.io.*;
import java.lang.*;
class RecConvert
{
static double i=0;
static double sum=0;
public static void main(String[] args) throws IOException
{
BufferedReader B = new BufferedReader(new InputStreamReader(System.in));
String input = B.readLine();
int number = RecursiveConvert(input);
System.out.println("The number is "+number);
}
static int RecursiveConvert(String input)
{
char[] charArray = input.toCharArray();
if(i < charArray.length)
{
sum = sum + (charArray[(int)i]) * ( (double)Math.pow(10.0,charArray.length-i-1) );
i++;
return RecursiveConvert(input);
}
else
return (int)sum;
}
}
This is your problem (charArray[(int)i]). You're multiplying a char with a double. Character.getNumericValue(charArray[(int)i]) will solve it.
I am new to programming and I am trying to figure out a simple average function. I have written a function to average 3 integers. Now I would like to be able to use any collection class and any number class and get the average. The problem is with these classes you can't use + or / to get the average.
So I am wondering if there are any work-arounds to be able to use these two classes?
here is my first function that works:
package refresh;
import java.util.*;
public class Average {
public static void main( String args[]){
int a, b, c;
float average;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number");
a = scanner.nextInt();
System.out.println("Enter Second Number");
b = scanner.nextInt();
System.out.println("Enter Third Number");
c = scanner.nextInt();
average = (float)(a+b+c)/3;
System.out.println("The Average Is "+average );
}
}
Here what I have so far for my problem:
package refresh;
import java.util.*;
public class Average {
public static void main( String args[]){
Collection numbers;
Number count;
Number average;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Numbers to be averaged");
numbers = (Collection) scanner.match();
count++;
average = numbers/count;
System.out.println("The Average Is "+average);
}
}
Any help would be great! Thanks for your time!
It makes no sense to convert a String into a number (which is what Scanner does) if you don't know what kind of number you're trying to convert it into.
You can represent pretty much any number as a Double. If you need arbitrary precision, use BigDecimal.
The averaging code can be fully generic over the Number interface; just use the doubleValue method. If you need BigDecimal, then it's more complicated and you probably need to do things involving instanceof.