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
Related
I'm new to java/programming in general and this is a homework assignment. This is what I have so far: When I run it I get the powers of 2 below the n input. example if n = 50, output is 2 + 4 + 8 + 16 + 32 + = -2
I would like the + after 32 to be gone and I don't know how to properly sum it. I would want the sum to = 62 in this case. I tried using string builder to take off the last two characters but that isn't working for me.
import java.util.Scanner;
public class Powers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n;
System.out.print("Enter the upper limit: ");
n = scan.nextInt();
int sum = 0;
int power = 1;
for (int i = 0; i <= n; i++) {
power = 2 * power;
if (power < n && 0 < power) {
System.out.print(power + " + ");
}
sum = sum + power;
}
System.out.println(" = " + sum);
}
}
There are multiple issues here:
When reaching the upper limit you simply stop doing the output but continue doing the summation.
You use the upper limit as the number of iterations, so in case of 50 in your example, you do a sum of all values between 1 and 2^50, which is the reason why the result is negative, because the sum became larger than the maximum number an int can keep.
Concerning your question how to break a loop, there is break ;-)
Your print is always outputting a + which is why you have the + = in your output. Change the output to something like this:
if (power < n && 0 < power) {
if (i != 0) {
System.out.print(" + ");
}
System.out.print(power);
}
I've added some functionality to your code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Type number:");
Scanner scanner = new Scanner(System.in);
int n = 0;
while (n == 0) { // to ask again if "n" is zero.
n = scanner.nextInt();
}
if (n != 0) {
scanner.close(); // to prevent resource leak
int sum = 0;
int power = 1;
for (int i = 0; i < n; i++) {
power *= 2;
sum += power;
System.out.print(power + " ");
if (sum + power * 2 < 0 | i == n - 1) {
// Should we step to the next iteration?
// If next "sum" will be bigger than the max value for
// integers
// or if this iteration is the last - it will type "sum",
// break "for" cycle and go the next line of code after
// "for" cycle.
// So in this case System.out.print("+ "); won't be
// executed.
System.out.print("= " + sum);
break;
}
System.out.print("+ ");
}
}
}
}
I have a homework problem that I've almost finished, but I'm just stuck on how to output it correctly.
Write a program that reads a positive integer n and prints the sum of all integers from 1 to n as follows:
1+2+…+n=n(n+1)/2
The output does not contain any spaces.Example of input: 5 Corresponding output: 1+2+3+4+5=15
Here's my code:
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 0;i <= n; i++) {
sum = sum + i;
}
System.out.printf("the sum of %d is %d%n", n, sum);
}
}
What I have in the printf command is just a placeholder until I can figure out the correct output.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1;i <= n; i++) {
if (i != n) {
System.out.print(i + "+");
} else {
System.out.print(i + "=");
}
}
System.out.print(n*(n+1)/2);
}
the above will work. You must be strict to the expected output.
You need to print within the loop.
And you should start at 1, like the instructions say
for (int i = 1;i < n; i++) {
sum = sum + i;
System.out.printf("%d+", i);
}
sum += n;
System.out.printf("%d=%d\n", n, sum);
You can deal with the case of printing n and = separately like so:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Print the sum of all integers from 1 to n program");
System.out.println("=================================================");
System.out.print("Please enter n: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1; i < n; i++) {
System.out.print(i + "+");
sum += i;
}
sum += n;
System.out.print(n + "=" + sum);
}
}
Try it here!
Example output:
Print the sum of all integers from 1 to n program
=================================================
Please enter n: 5
1+2+3+4+5=15
I'm trying to Write the main method of a Java program that has the user enter two integers, i and n. If either integer is less than 2, output “Please enter numbers above 1.” Otherwise, output the n positive multiples of i, separated by spaces.
I'm close but can't figure out how to do display the multiples.
Here's what a sample run should look like:
Enter i: 4
Enter n: 6
6 multiples of 4 are: 8 12 16 20 24 28
import java.util.*;
public class HW5Problem3 {
public static void main (String [] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
}
System.out.print(n + " multiples of " + i + " are: ");
}
}
import java.util.Scanner;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int i = 0;
int n = 0;
//It's use to verify the inputs (i and n).
do{
System.out.print("Enter i :");
i = input.nextInt();
System.out.print("\nEnter n :");
n = input.nextInt();
if(i >= 1 || n <= 1){
System.out.println("Please enter numbers above 1 \n");
}
}while(i <= 1 || n <= 1);
System.out.print(n + " multiples of " + i + " are: ");
for (int counter = 0 ; counter < n ; counter++) {
System.out.print(i*(2 + counter) + " ");
}
}
You'll need to create a loop (for loop or while loop) to iterate from 2 to n+1, and multiply i by your loop variable, outputting each value inside the loop
U can use following method in that class
public static void mult(int i,int n){
int[] arr=new int[n];
int count=2;
for(int x=0;x<n;x++){
arr[x]=i*count++;
}
for(int y=0;y<arr.length;y++){
System.out.print(arr[y]+" ");
}
and now your final code looks like
import java.util.*;
public class HW5Problem3 {
private int i = 0;
private int n = 0;
public static void main(String[] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
} else {
System.out.print(n + " multiples of " + i + " are: ");
mult(i, n);
}
}
public static void mult(int i, int n) {
int[] arr = new int[n];
int count = 2;
for (int x = 0; x < n; x++) {
arr[x] = i * count++;
}
for (int y = 0; y < arr.length; y++) {
System.out.print(arr[y] + " ");
}
}
}
n is the multiplier, i is the factor, right? In programming the multiplier is the loop maximum:
System.out.print(n + " multiples of " + i + " are: ");
for (int inc=1; inc<=n; inc++) {
System.out.print(" " + i*inc);
}
This prints out: 4 8 12 16 20 24
If you really want to have this as output: 8 12 16 20 24 28 copy/paste this line:
for (int inc=2; inc<=(n+1); inc++)
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!
My task is to ask the user how many Fibonacci numbers they want and to print that, then also show the average of those numbers. I made a method to show the Fibonacci numbers in my code below, but I'm having a hard time figuring out how to incorporate the average part of the program. Not asking for you to do it for me because this is homework for class, but it would be nice to know where I am supposed to write the average part of the program.
import java.util.Scanner;
public class Clancy_Hw_03_04{
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int number = 0;
System.out.println ("Enter N: ");
number = input.nextInt();
System.out.println("\n\nFibonacci series for " + number +" numbers : ");
for(int i=1; i<=number; i++){
System.out.print(fibonacciLoop(i) +" ");
}
}
public static int fibonacciLoop(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci;
}
}
Your problem might be easier to solve if you printed the fibonacci numbers from within your function, fibonacciLoop(), and not try to return each successive number and print it from the main loop. Then the sum and average are right at your fingertips.
For instance:
public static void main(...) {
...
System.out.println("\n\nFibonacci series for " + number +" numbers : ");
fibonacciLoop(i);
}
static void fibonacciLoop(int number) {
int fibo1 = 1, fibo2 = 1, sum = 0, average = 0, fibonacci;
for (int i = 1; i <= 2 && i <= number; i++) {
System.out.print("1 ");
sum += 1;
}
for (i = 3; i <= number; i++) {
fibonacci = fibo1 + fibo2;
System.out.print(fibonacci + " ");
fibo1 = fibo2; fibo2 = fibonacci;
sum += fibonacci;
}
System.out.println(" average = " + (float)sum/number)
}
Edit: Made sure to convert sum to a float before calculating average
Keep computing the sum and then divide it by number.
You can do something like...
int sum = 0;
for(int i=1; i<=number; i++){
int fibo = fibonacciLoop(i);
System.out.print(fibo +" ");
sum += fibo;
}
System.out.println("Average = " + sum/number);
I hope this time I got your question right.
Put another integer in your main function, to store the total value:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number = 0;
int sum = 0; // Add this here
}
Then, inside your fibonacci function, add to that variable:
public static int fibonacciLoop(int number)
{
if(number == 1 || number == 2)
{
sum += 1;
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++)
{
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
sum += fibonacci; //Put this here, outisde the loop
return fibonacci;
}
When you want to output the average, simply divide sum by number:
System.out.println("Average is: " + (sum / number));