I have a code is written that is supposed to print each of the numbers separately. Heres an example.
printDigits(1362) prints
2
6
3
1
printsDigits(985) prints
5
8
9
You can pull apart a number into its digits using / 10 and % 10.
I have started some code the way I was taught but I am not sure what to do with the other variables.
Please have a look:
public class Main {
public static void main(String[] args) {
System.out.println(printDigits(1362));
System.out.println(printDigits(985));
}
public static int printDigits(int x){
int y = x % 10;
while (x > 0){
x = y;
System.out.println(x);
x = x / 10;
}
return x;
}
}
Why don't you convert the parameter x to String then read each Char in the String since a String is array of Char. If the output must be an int you convert the Char to int.
printDigits method should be like this:
public static void printDigits(int x) {
int y;
while (x > 0) {
y = x % 10;
System.out.println(y);
x = x / 10;
}
}
And the calling of the method will be like this:
printDigits(1362); // without the System.out.println()
There are various ways to achieve such results. You can better understand all solutions in these answers.
public static void printDigits(int num) {
while(num>0) {
int remainder = num%10;
num = num/10;
System.out.println(remainder);
printDigits(num);
}
}
You can use recursion to make it even more efficient.
You need to write int y = x % 10; inside the loop to "pull apart" every digit and print the digit y you have "pulled out".
Remove the System.out.println around your function calls.
You don't need to return a number, so you can change your return type to void:
public class Main {
public static void main(String[] args) {
printDigits(1362);
System.out.println();
printDigits(985);
}
public static void printDigits(int x) {
while (x > 0) {
int y = x % 10;
System.out.println(y);
x = x / 10;
}
}
}
Related
I want to reverse a number without using array.I want to know how would I save the number.I think for this step I need to also know whether the number is one digit or two digit etc.
Below is my code what I did.
#Edit
I have solved this problem by following method
public class ReverseNumber
{
public static void main(String[] args)
{
ReverseNumber obj = new ReverseNumber();
int result = obj.reverse(2199);
System.out.println(result);
}
public int reverse(int num)
{
int rnum1=0;
for(int i=num;i!=0;)
{
rnum1=(rnum1*10)+(i%10);
i=i/10;
// write your code here
}
return rnum1;
}
}
you almost had it! you're only missing the part where you need to multiply num1 with 10, and the choice of the loop is a bit unlucky:
public static int reverse(int num) {
int input = num;
int num1 = 0;
while (input>0) {
num1 = num1 * 10;
num1 = num1 + input%10;
input = input / 10;
}
return num1;
}
EDIT: had a mistake in the implemetation... now it's fixed
#Parker_Halo provided an excellent iterative solution. I am adding here a recursive solution for completeness:
public static int reverse(int number, int n) {
if (number == 0)
return n;
return reverse(number / 10, n * 10 + number % 10);
}
You would call it like this :
int rev = reverse(num, 0);
So. Hello smart ones. What am I doing wrong here? I just can't figure out what is wrong with this code. 10 points for whomever helps me.
I'm trying to use recursion to make a method for e^x. using the e^x = 1 + x + x2/2! + x3/3! + x4/4! + ... equation
public class tester {
public static double power(double x, int n) {
if (n == 0) {
return 1;
} else {
return x * power(x, n - 1);
}
}
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
public static double myexp(double x, int n) {
if (n == 0) {
return 1;
} else {
return (power(x, n) / factorial(n)) + myexp(x, n - 1);
}
}
public static void main(String[] args) {
System.out.println(myexp(x, n)); // unfortunately, increasing n value
// makes it go infinite.
}
}
So x is the x in e^x and n is the total value when up to nth term is added. So
for example, myexp(3,5) is going to be e^3 added up to 5th term. Thus, the higher the n is, the more accurate e^3 is going to be.
Your problem is the use of the "int" data type for the factorial method. More specifically, factorial numbers quickly become huge and the int data type is too small. For example, if you code:
public static void main(String[] args) {
System.out.println(factorial(50));
}
The output is 0 which is obviously wrong, hence your result of Infinity. Simply change the return type of factorial from intto double as follows:
public static double factorial(int n)
And then if you try:
public static void main(String[] args) {
System.out.println(myexp(1., 100));
}
You get 2.7182818284590455
public class Prod {
public static void main(String[] args) {
System.out.println(prod(1, 4));
}
public static int prod(int m, int n) {
if (m == n) {
return n;
} else {
int recurse = prod(m, n-1);
int result = n * recurse;
return result;
}
}
}
This is an exercise in the book I am stumped on. Why would the program not just recurse until the two numbers are equal and then return n ? Also, where it says,
int result = n * recurse;
How does it multiply int n by recurse which would be (int, int)? How can it multiply one integer by a set of two integers?
In what way am I misunderstanding this program?
EDIT: This is a different question because I am not using factorials
prod(x,y) is equivalent to y! when x=1.
If x is different from 1, then its doing recursive multiplication (y * (y- 1) * (y -2) .... ) until y = x.
Assuming y > x.
By the way, if x > y then prod() will crash.
I'm trying to compute the value of 7 factorial and display the answer, but when I tried to look up a way to do this I kept finding code that was written so that a number first had to be put in from the user and then it would factor whatever number the user put in. But I already know what number I need, obviously, so the code is going to be different and I'm having trouble figuring out how to do this.
I tried this at first
public class Ch4_Lab_7
{
public static void main(String[] args)
{
int factorial = 7;
while (factorial <= 7)
{
if (factorial > 0)
System.out.println(factorial*facto…
factorial--;
}
}
}
But all it does is display 7*7, then 6*6, then 5*5, and so on, and this isn't what I'm trying to do.
Does anyone know how to do it correctly?
import java.util.Scanner;
public class factorial {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
//Gives Prompt
System.out.print("Enter a number to find the factorial of it");
//Enter the times you want to run
int number = input.nextInt();
//Declares new int
int factor = 1;
//Runs loop and multiplies factor each time runned
for (int i=1; i<=number; i++) {
factor = factor*i;
}
//Prints out final number
System.out.println(factor);
}
}
Just keep multiplying it and until it reaches the number you inputted. Then print.
Input:5
Output:120
input:7
Output:5040
You need to have two variables, one for the factorial calculation and other for the purpose of counter. Try this, i have not tested it but should work:
public static void main(String[] args)
{
int input = 7;
int factorial = 1;
while (input > 0)
{
factorial = factorial * input
input--;
}
System.out.println("Factorial = " + factorial);
}
int a=7, fact=1, b=1;
do
{
fact=fact*b;//fact has the value 1 as constant and fact into b will be save in fact to multiply again.
System.out.print(fact);
b++;
}
while(a>=b); // a is greater and equals tob.
1st reason:
The methods you seen are probably recursive, which you seem to have edited.
2nd:
You are not storing, ANYWHERE the temporal results of factorial.
Try this
//number->n for n!
int number = 7;
//We'll store here the result of n!
int result = 1;
//we start from 7 and count backwards until 1
while (number > 0) {
//Multiply result and current number, and update result
result = number*result;
//Update the number, counting backwards here
number--;
}
//Print result in Screen
System.out.println(result);
Try this:
public static void main(String args[]) {
int i = 7;
int j = factorial(i); //Call the method
System.out.println(j); //Print result
}
public static int factorial(int i) { //Recursive method
if(i == 1)
return 1;
else
return i * factorial(i - 1);
}
This would print out the factorial of 7.
public class Factorial {
public static void main(String[] args) {
int result = factorial(5); //this is where we do 5!, to test.
System.out.println(result);
}
public static int factorial(int n) {
int x = 1;
int y = 1;
for (int i = 1; i <= n; i++) {
y = x * i;
x = y;
}
return y;
}
}
/*so, for 3! for example, we have:
i=1:
y = x * i, where x = 1, so that means:
y = 1*1 ; y= 1; x = y so x = 1. Then i increments =>
i = 2:
y = x * i. x is 1 and i is 2, so we have y = 2. Next step in code: x=y, means x = 2. Then i increments =>
i = 3:
y = x *i so we have y = 2*3. y=6. */
Hello I'm a beginner in Java and I have a question regarding summing the iterations. The question is: Write a program that computes the following expression to 4 decimal places:
(1/10) + (2/9) + (3/8) + (4/7) + (5/6) + (6/5) + (7/4) + (8/3) + (9/2) + (10/1)
So far I have:
public class Expression
{
public static void main(String[] args)
{
float x;
for ( float m=1, n=10; m<11; m++,n--)
{
x = (m)/(n);
}
How would I go about summing all the iterations the for loop makes?
Thanks everyone :)
public class Expression
{
public static void main(String[] args)
{
float x = 0;
for ( float m=1, n=10; m<11; m++,n--)
{
x += (m)/(n);
}
System.out.println(x);
}
}
Nice code, i like it so far!
You need a variable like: sum = sum + x; or shorter sum += x; within the loop. Define it with float sum = 0; before the loop.
You can also directly use x and define it with 0, but the compiler will optimise it anyway, so there is no speed gain.
The += operator. x += y is equivalent to x = x + y. In your case, you will have x += (m)/(n), resulting in, ultimately:
public class Expression {
public static void main(String[] args) {
float x = 0;
for ( float m=1, n=10; m<11; m++,n--) {
x += (m)/(n);
}
}
}
Change
x = (m)/(n);
to
x += (m)/(n);
Since x is already in scope outside the loop, it is possible to do this, and x will persist between iterations. I recommend changing the loop, however:
float x=0;
for ( float m=1; m<11; m++)
{
x += (m)/(11-m);
}
It may be more straightforward to read in the future.