import java.util.Scanner;
public class Rpn_calculator
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double ans = 0;
double n = 0;
double r = 0;
String j;
System.out.println();
System.out.println();
System.out.print("Please enter a value for n ");
n = keyboard.nextDouble();
System.out.print("Please enter a value for r ");
r = keyboard.nextDouble();
System.out.println("Please choose one of these operands:+,-,*,nCr,/ to continue, or q to quit ");
j = keyboard.nextLine();
while (j != "q")
{
if(j == "+")
ans = n + r;
if(j == "-")
ans = n - r;
if(j == "*")
ans = n * r;
if(j == "/")
ans = n / r;
if(j == "nCr")
ans = factorial(n + r -1) / (factorial(r)*factorial(n - 1));
System.out.print(ans);
System.out.print("Please enter a value for n ");
n = keyboard.nextDouble();
System.out.print("Please enter a value for r ");
r = keyboard.nextDouble();
System.out.print("Please choose one of these operands:+,-,*,nCr,/ to continue or q to quit ");
j = keyboard.nextLine();
}
}
public static double factorial(double x);
{
double sum = 1;
int i;
for (i = 0; i<= x; i++);
sum = sum * i;
return sum;
}
}
I am trying to create an RPN calculator for this assignment, I believe I have what I need to make it function, but I have syntax errors with my factorial function. It says "missing method body".
Also, I don't understand why, when I compile it, my string input request is ignored completely - locking the program in the while loop. I hope I'm not missing something really obvious.
There are a couple of semicolons where there shouldn't be:
public static double factorial(double x); // <-- This one
{
double sum = 1;
int i;
for (i = 0; i<= x; i++); // <-- This one
sum = sum * i;
return sum;
}
When you put the semicolon right after the signature, you have no method body, hence the error. The for loop wouldn't be a compiler error, but it would be an empty loop.
Also, I think you need to use String.equals to compare strings in Java, as opposed to the == operator.
Related
This piece of code is supposed to take 2 numbers and find the factorial of every number between and incuding said numbers. I'm not getting the right output however and cant figure out what im doing wrong.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0) //want factorial greater than zero
for(int j = n; j <= m; j++)
{
for(int i = 1; i <= j; i++)
{
result = result * i; //find factorial
}
System.out.println(result);
}
if(n <= 0 || m <= 0) //if value is les than zero
{
System.out.println("Not Valid!");
}
Something like should work:
public class RangeFactorial {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int max = scan.nextInt();
int min = scan.nextInt();
if (max < 0 || min < 0) {
System.out.println("Invalid Params");
}
for (int i = min; i <= max; i++) {
System.out.println("Factorial for " + i + " is: " + factorial(i));
}
scan.close();
}
private static int factorial(int i) {
if (i <= 1) {
return 1;
}
return i * factorial(i-1);
}
}
Note that the code assumes that max/min fall in place, I've omitted the logic to decide the max/min integer from the given inputs. You'll need to add this.
You forgot to reset 'result' to 1.
Also, there is no need to have another if statement if it is only checking for the negation of the first one, just use an else.
I also fixed the code style guidelines to follow the standard Java ones as:
The curly bracket style you used is the one usually used in C/C++.
Even if if-statements or loops contain only one line after them, it is good etiquette to use curly brackets anyway, in Java.
Take a look at the Google Java Style Guide if you want to know more.
Scanner scan = new Scanner(System.in);
long result = 1;
int m = scan.nextInt();
int n = scan.nextInt();
scan.close();
if (n > 0 && m > 0){
for(int j = n; j <= m; j++){
result = 1; //You forgot to reset 'result'
for(int i = 1; i <= j; i++){
result *= i;
}
System.out.println(result);
} else { // No need for another if statement
System.out.println("Not Valid!");
}
I want my code to loop, but reinitialize the variable back to 0. Every time I input a number, it add it to the previous result, but I want it to reset. I attached two images below. One is the actual output and the other is the expected output.
import java.util.Scanner;
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
int sum = 0;
int total = 1;
int n = 0;
while (true) {
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
Actual Output
Desired Output
You can use continue
if(n == 0) {
sum = 0;
total = 1;
continue;
}
You can initialize the variables inside the while loop
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
while (true) {
int sum = 0;
int total = 1;
int n = 0;
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
To zero a variable, simply assign 0 at the appropriate point.
sum = 0;
An appropriate place to insert this statement for your desired output is immediately before the first for loop. Likewise, reset total before the second for.
However, a much better way to write this is to declare the variable where it is needed, as you have done for x and y. This applies to am, sum, total and n
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("enter an integer number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
String am = input.nextLine();
if (am.equals("a")) {
int sum = 0;
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
int total = 1;
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
I don't know if I fully understand your question, but just do sum=0; and total=1; after you print out the final result. You should also consider doing a try/catch statement for robustness on the nextInt so that characters and strings don't break your program...
try {
n = input.nextInt();
}
catch (Exception e) {
System.out.println("Not a Number");
}
Here is my code :-
package javaapplication;
import java.util.Scanner;
public class perfect02 {
public static void main(String args[]) {
int i = 1, sum = 0;
System.out.println("Enter maximum range : ");
Scanner kb = new Scanner(System.in);
int a = kb.nextInt();
System.out.println("Enter minimum range : ");
Scanner kb2 = new Scanner(System.in);
int b = kb2.nextInt();
System.out.println("perfect number in the given range are :");
for (int n = b; n <= a; n++) {
while (i < n) {
if (n % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == n)
System.out.println(+n + " ");
}
}
}
Why program is not printing perfect numbers ?
I have checked code many times but i am unable to find the solution .Please tell me what is going wrong in my code .Thanks in advance
Any help would be appreciated ....
Here, I looked into the perfect number generation and fixed it for you!
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter minimum range : ");
int b = kb.nextInt();
System.out.println("Enter maximum range : ");
int a = kb.nextInt();
kb.close();
System.out.println("Perfect number in the given range are :");
for (int n = b; n <= a; n++) {
int sum = 0;
int i = 1;
while (i < n) {
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
System.out.println(n + " is perfect");
}
}
You should've had the sum and i variables declared inside the for loop, so they would be reset for each number!
I am new at coding Java, but I need to write a program that is from integer 1 to n. The program would ask the user to enter a positive number and if it is not positive then it will ask for another number.
Once the positive integer is entered for n in the program, it shows how the n factorial is computed followed by the result.
So my program will show everything correct except the result, it is not multiplying all the numbers together. If someone can point me in the right direction on how to get this solved that would be great!
CODE:
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 30
As you can see for the result it should be 120 and not 30.
Just change that part
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
for
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
Your last print was wrong as you simply multiplied n with i which is a simple multiplication and have nothing to do with factorial.
Your program is doing exactly one computation ( " is " + n * i) and this computation is not doing a factorial. You probably want to do the multiplication more than once - and with different numbers.
You are not doing the calculation properly. You just show the end result of n*i`.
In the below solution, I've taken an int fact = 1 and I'm multiplying it with the value of i inside the for loop and assigning back the result to factvariable. That's the core part. Thats how you get 1*2*3...*n = n!
import java.util.Scanner;
public class SomeArrayQuestion {
public static void main(String[] args) {
int n, i = 1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int fact = 1;
for (i = 1; i <= n; i++) {
System.out.print(i + "*");
fact = fact * i;
}
System.out.print(" is " + fact);
}
}
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 120
I wanted to convert decimal to binary and the following code doesn't work.
Please help me to correct the code.
package mainpackage;
import java.util.*;
class MainClass {
public MainClass() {
}
public static int binaryfinder(int n) {
int a[] = new int[8];
int i = 0;
int b = 0;
int n1 = n;
while (n1 > 0) {
a[i] = n1 % 2;
i++;
n1 = n1 / 2;
}
System.out.printf("Binary number of %d is = ", n);
for (int j = i - 1; j >= 0; j--) {
b += ((10 ^ j) * a[j]);
}
return b;
}
public static void main(String[] args) {
System.out.println("\nEnter the number to find its binary value:\n");
Scanner k = new Scanner(System.in);
int num = k.nextInt();
int inBin = binaryfinder(num);
System.out.print(inBin);
}
}
After I click RUN, it asks to enter the binary value and when I enter the value, it says, "Binary number of 0 = " No matter what I enter it always outputs "Binary number of 0 = ".
No errors are thrown.
Integer.toBinaryString(i)
this should do the trick in java
You have an endless loop, thats why your program never terminates:
while (j != 0) {
b += ((10 ^ j) * a[j]);
}
After I click RUN, it asks to enter the binary value and when I enter the value, it says, "Binary number of 0 = " No matter what I enter it always outputs "Binary number of 0 = ". No errors are thrown.
Never ending while loop
while (j != 0) {
b += ((10 ^ j) * a[j]);
}
You are changing value of n and finally after while loop :n = 0 and so the output
while (n > 0) {
a[i] = n % 2;
i++;
n = n / 2;
}
System.out.println(output);
Your program (as whole) is incorrect
Other Options:
1) Use Integer.toBinaryString()
2) Use the basic formula to convert decimal --> binary
int n = 10;
String output = "";
do {
output = (n % 2) + output; // reverse string
n = n / 2;
} while (n > 0);