java - Factorial sequence -
i first year pupil , having our first case study...
right im working on 1 of problems regarding factorial sequence, , i'm getting errors can't figure out.
the output should this
user input =4
correct output: 4! = 4 x 3 x 2 x 1 = 24
there...
i able output above except in ascending order not descending above...
like
output:
4! = 1 2 x 3 x 4 x = 24
my code above output this
code:
public static void main(string[] args) throws exception {
scanner sc = new scanner(system.in); system.out.println("this programs displays factorial sequence of number"); system.out.println(); system.out.print("enter number display factors: "); int producti = 1; int num = sc.nextint(); system.out.print(num + "!" + " " + "=" + " "); (int = 1; <= num; i++) { if (num % == 0); system.out.print(i + " "); if (i > 1) { system.out.print("x"); } system.out.print(" "); producti *= i; } system.out.print("=" + " " + producti); }
there
so have come code go in descending order made incomplete output plus error
the code is
code:
public static void main(string[] args) throws exception { scanner sc = new scanner(system.in); system.out.println("this programs displays factorial sequence of number"); system.out.println(); system.out.print("enter number display factors: "); int producti = 1; int num = sc.nextint(); system.out.print(num + "!" + " " + "=" + " "); (int = num; num >= i; i--) { if (num % == 0); system.out.print(i + " "); if (i > 1) { system.out.print("x"); } system.out.print(" "); producti *= i; } system.out.print("=" + " " + producti); }
and output of code shows
output: programs displays factorial sequence of number
enter number display factors: 4 exception in thread "main" java.lang.arithmeticexception: / 0 @ javaapplication2.javaapplication2.main(javaapplication2.java:27) 4! = 4 x 3 x 2 x 1 java result: 1
there
so dont know why lastly scheme out print didnt show , instead showed error. , why errors when changed position of num in program...
is there can clear problem out...
what if (num % == 0);
? did seek here?
remove it, , alter for loop
for (int = num; i>=1; i--)
.
another thing, can write: system.out.print(num + "! = ");
instead of system.out.print(num + "!" + " " + "=" + " ");
after changes you'll code, , should work:
for (int = num; i>=1; i--) { system.out.print(i + " "); if (i > 1) { system.out.print("x"); } system.out.print(" "); producti *= i; } system.out.print("=" + " " + producti);
java sequence factorial factors
No comments:
Post a Comment