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
Related
I'm currently using this code, and I'm trying to output the total sum of how many prime numbers first and then the prime numbers list
Here is my code:
import java.util.*;
public class PrimeTime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
System.out.print(i+ " ");
s = s +1;
}
}
System.out.println("N =" +s);
}
}
This is the result I get:
Input number: 10
2 3 5 7 N = 4
But what I'm looking for is this result:
Input number: 10
N = 4
2 3 5 7
I don't know how, I tried putting the S.O.P in different places and I can't figure out how.
If you want to display N = 4 before 2 3 5 7, you can append the numbers (i.e. 2 3 5 7) to a StringBuilder and print the same after printing N = 4 e.g.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
StringBuilder numbers = new StringBuilder();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0)
f = 1;
}
if (f == 0) {
numbers.append(i).append(' ');// Append to StringBuilder instead of printing
s = s + 1;
}
}
System.out.println("N =" + s);
System.out.println(numbers);
}
}
A sample run:
Input number: 10
N =4
2 3 5 7
Note: for checking the primality, checking up to Math.sqrt(i) is sufficient i.e. you should replace j < i with j <= Math.sqrt(i).
You can store the numbers in a list and then print them after the loop is over
Don't worry if you have not studied lists yet cause you will have to soon anyways.
here's the code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
LinkedList<Integer> list = new LinkedList<>();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
//System.out.print(i+ " ");
list.add(i);
s = s +1;
}
}
System.out.println("N = " +s);
for(int i = 0; i<list.size(); i++ ) {
System.out.print(list.get(i) + " ");
}
}
I tried to write code for an factorial number program,following code
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i & gt; 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}
Yes your error point is & gt; which is not an operator actual operator is >. > is XML encoded from of >.
Use this:
for (int i = n; i > 0; i--)
Instead of
for (int i = n; i & gt; 0; i--)
This happens usually when you copy past code from somewhere.
Complete code should be:
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i > 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}
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'm trying to print all from 6 to n but when i run the code it gives me all the numbers but not the right perfect numbers. For example when I enter 30 it prints out all numbers but it says only 6 and 7 are perfect numbers only 7 is not a perfect number and 28 is.
import java.util.Scanner;
public class PerfectN {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number that is 6 or above: ");
int n = scanner.nextInt();
int sum = 0;
if(n<6){
System.out.println("Incorrect number.");
}
else
for(int i = 6;i<=n;i++){
for(int j = 1; j<i; j++){
if(i%j==0)
sum += j;
}
if(sum==i){
System.out.println(i + " is a perfect number.");
}
else
System.out.println(i + " is not a perfect number.");
}
}
}
You need to reset the sum variable to zero for each number you go through:
for (int i = 6; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {
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));