Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I hope this thread isnt against the rules
my problem is
im using macos and i couldnt find a calculator that Divides number and show the reminder and quotient
so i thought there might be a java code that do that
i could program such app but im really on a hurry
couze im studing multiplicate cipher that uses gcd (greatest common diviser )
and i need to culaculate alot of number using the reminder of that calculation
so i thought i bring this here cause there is alot of experienced programs in this form
thanks in advance
I have found the code
//Author : Mayank Rajoria
import java.util.*;
class remainderAndQuotient {
public static void main(String[] args) {
int n, q, a, k = 1, r;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers");
n = sc.nextInt();
a = sc.nextInt();
r = n % a;
q = n / a;
System.out.println("Quotient : " + q);
System.out.println("Remainder : " + r);
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to write a Java program that can take values and put them into a formula involving electron. How can I calculate e^x in Java?
you can use java.lang.Math.exp() method to calculate e^x.
here is an example
`
public static void main(String[] args)
{
double a = 2.0;
System.out.println(Math.exp(a));
}
//output = 7.38905609893065
`
you can read more about this using this link.
https://www.javatpoint.com/java-math-exp-method
You could use Math.exp().
Example:
import java.lang.Math;
// inside main
Math.exp(1.0); // e^1
Math.exp(1.6); // e^1.6
import java.lang.Math;
class Answer {
// driver code
public static void main(String args[])
{
double a = 30;
double b = 2;
System.out.println(Math.pow(a, b));
a = 3;
b = 4;
System.out.println(Math.pow(a, b));
a = 2;
b = 6;
System.out.println(Math.pow(a, b));
}
//you can use Math.pow(e,x);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I needed to rewrite a java program I had already completed to now include a class I would be able to test with using JUnit4. Unfortunately I can't even get to that point because I'm receiving an error. It's a really simple program where I ask the user for 3 numbers, pass those values into a function that does some calculations and should return a print statement with the value back. I made it work without the function and I'm having trouble with the syntax and fully understanding what I can and can't do with methods.
Here it is:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Given ax^2 + bx^2 + c = 0");
System.out.println("Please enter 'a', 'b', and 'c' to determine if there are any roots: ");
float numA = kybd.nextFloat();
float numB = kybd.nextFloat();
float numC = kybd.nextFloat();
quadraticAnswer(numA, numB, numC);
}
public static void float quadraticAnswer (float numA, float numB, float numC){
float discriminant = ((numB*numB)-(4*numA*numC));
if (discriminant < 0){
System.out.println("The Equation has no roots!");
}
else if (discriminant ==0) {
float root = (-numB + Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has one root: "+ root);
}
else {
float root1 = (-numB + Math.sqrt(discriminant))/(2*numA);
float root2 = (-numB - Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has two roots: " + root1 + " and " + root2 + ".");
}
}
}
Change the invalid syntax of
public static void float quadraticAnswer
to
public static void quadraticAnswer
as you are not returning anything.
If you use an IDE like Eclipse it will quickly highlight such errors
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
https://projecteuler.net/problem=8 link to problem
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.(number is in the code as String num)
The answer i am getting is "9205903071867879424" (wrong answer)
Please point out the mistakes in my code and also suggest your solution to solve the problem efficiently as possible.
I read the other threads about this problem but couldn't understand them and also there weren't enough efficient solutions.
public static void main(String[] args){
String num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long product=1;
long greatestp=0;
long limit=13;
for (int i=0; i<num.length()-13; i++){
product = 1;
for (int j=0; j<limit; j++ ){
product = (long) (product * (int) (num.charAt(j+i)));
}
if (greatestp<product){
greatestp = product;
}
}
System.out.println(greatestp);
}
P.S. I'm a beginner at JAVA, I would appreciate if you explain your solution in detail.
charAt gives you the ascii charcode, not the actual digit. Therefore the fastest way is to subtract the value of '0' from it:
product = product * (num.charAt(i+j) - '0');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I done a coding to get the output 20,40,60,80 and 100.
Multiple.java:
public class Multiple {
static int n=20;
static int m;
public static void main(String[] args) {
for(int i=1;i<=5;i++){
m=n*i;
System.out.println(m);
}
}
}
Output:
20
40
60
80
100
I didn't have any issue in this coding.I got an output successfully.But loop running 5 times.Let me know is there any other way to get these output(20,40,60,80 and 100) in simplest way.
very interesting. if you know the output result (it's fixed), why don't you write them out directly:
System.out.println(20);
System.out.println(40);
System.out.println(60);
System.out.println(80);
System.out.println(100);`
oh i see, you think that's too long. Then probably you can use the below:
int a = 0;
while(a<100){ a += 20; System.out.println(a);}
Enjoy it!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a Java application and want to press a button to generate a 7 digit random number and put it into a text area.
Here is what I have so far:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText();
}
How do I do this?
i don't know what to write inside the brackets to get this random number of seven digits on the text area.
You can use a StringBuilder and append 7 random numbers between 0 and 9 using the nextInt(int n) method :
Random r = new Random();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 7; i++)
sb.append(r.nextInt(10));
jTextArea1.setText(sb.toString());
first you'll need to inport the java random number library at the top of your code, like this:
import java.util.Random;
this code will get you a random number from 1000000 to 9999999, it is a little weird, but take some time trying to figure it out
1000000 + (int)(Math.random() * ((8999999) + 1))
Try putting that between the parenthasis after setText, like this:
jTextArea1.setText(1000000 + (int)(Math.random() * ((8999999) + 1)));
For more information about the random function and how it works, looks here