How to take the sum of squares in java? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I am a beginner in java and am just learning the loop concept. One of my assignments is to modify this code below so that it prints out the sum of the squares of the values, rather than just the sum of the values:
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i);
}
System.out.print("Sum from "+ first +" to " + second );
System.out.println(" is " + sum);`
Apparently I only have to modify one line of this code. I tried:
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i^2);
}
where I thought if you just added the i^2 value inside of the parenthesis it would work, but it did not. Any help on how I do this/any help on understanding how a loop works would be greatly appreciated.

As the comments said,
change i^2 to i*i
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i*i);
}

I applied the formula of sum of squares of n terms here and subtracting first one I had used n*(n+1)*(2*n+1)/6 formula here with out no loop performance o(1)
sum = (second*(second+1)*((2*second)+1))/6 - (first*(first+1)*((2*first)+1))/6;

Just change
sum += (i);
to
sum += (i*i);

Related

i want to write a code which accepts int and makes a horizontal bar with the number of asterisks as of int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
import java.util.*;
public class assign {
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
System.out.println("Enter first number between 1 to 30");
int a = scanner.nextInt();
System.out.println("Enter second number between 1 to 30");
int b = scanner.nextInt();
System.out.println("Enter third number between 1 to 30");
int c = scanner.nextInt();
System.out.println("Enter fourth number between 1 to 30");
int d = scanner.nextInt();
System.out.println("Enter fifth number between 1 to 30");
int e = scanner.nextInt();
scanner.close();
for(int i=0;i<a-1;i++);{
System.out.print("*");
}
System.out.println();
for(int i=0;i<b-1;i++);{
System.out.print("*");
}
System.out.println();
for(int i=0;i<c-1;i++);{
System.out.print("*");
}
System.out.println();
for(int i=0;i<d-1;i++);{
System.out.print("*");
}
System.out.println();
for(int i=0;i<e-1;i++);{
System.out.print("*");
}
System.out.println();
}
}
the output gives only one asterisk per line instead of required the number of scanned quantity
Fix
Remove the semicolon, and change the bound from a-1 to a to have the good amount
for (int i = 0; i < a; i++) {
System.out.print("*");
}
Improve
With String.repeat
System.out.println("*".repeat(a));
System.out.println("*".repeat(b));
System.out.println("*".repeat(c));
System.out.println("*".repeat(d));
System.out.println("*".repeat(e));

How to link one with another parts [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have this homework. I'm having a hard time linking the first part with the second. This is what I have. I know I'm missing something at some point to indicate that from the number input it should add the next 100 numbers.
package test;
import java.util.Scanner;
public class Ex216 {
public static void main(String[] args) {
// Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen
Scanner myInput = new Scanner(System.in);
int =a
int sum;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
for (int n = a; n <= 100; n++)
System.out.printf("Sum = %d\n", sum);
}
}
This is what is casing me the trouble.
first of all,int =a is not a valid expression. it should be int a; then as you want to add next 100 number from the given value, you need to add those values into sum, such as sum = sum+number.
Here is a code snippet:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);

Problems with array. Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I started learning java yesterday and this is the first time i'm using array. This is the code
import java.util.Scanner;
public class array
{
public static void main(String[] args)
{
int num[];
num = new int[5];
Scanner input = new Scanner(System.in);
int i;
System.out.println("Insert 5 numbers:");
for(i = 0; i < 5; i = i + 1);
{
System.out.print("Insert the " + i + "° number: ");
num[i] = input.nextInt();
}
System.out.print("The numbers you entered are: ");
for(i = 0; i < 5 ; i = i + 1)
{
System.out.println(num[i] + " ");
}
}
}
When i try to run it i get this problem:
Insert 5 numbers:
Insert the 5° number: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at array.main(array.java:14)
Two things.
You have a semicolon at the end of your loop. That will cause the loop to run until i = 5 and you're kind of stuck with that value now. Remove it.
i will remain 5 after the first loop for the same reason as above. Declare and initialize i inside of your for statement.
for(int i = 0; i < 5; i = i + 1) {
// the rest of your block
}

How to increment input value by two in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My program asks the individual to input 2 numbers (ie; 10 and 20).
I would like the output to be:
Even numbers: 10 12 14 16 18 20
My code:
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
while ((firstNum < secondNum) && mod == 0)
{
firstNum = firstNum + 2;
System.out.print("Even numbers" +firstNum);
}
You are close to the result you are after, just need to rearrange the order of a couple of lines and add one if check.
I have made a complete example that runs as expected and allows for both odd and even numbers. You can replace your code with the following:
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
//If first number is odd, increase by one to make it even.
if (mod != 0)
{
firstNum++;
}
System.out.print("Even Numbers: ");
while (firstNum <= secondNum)
{
System.out.print(firstNum + " ");
firstNum = firstNum + 2;
}
keyboard.close();
}

How to display the sum of the cubes of the digits? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Code:
import java.util.*;
import java.io.*;
class Cubesum {
public static void main(String args[]){
int input=0;
int num1,num2,num3;
//read the number
System.out.println("Enter a Number");
Scanner console = new Scanner(System.in);
input= Integer.parseInt(console.nextLine());
// now let us print the cube of digits
//i.e if number is 123 we will print 1^3, 2^3 and 3^3
//we will also add 1 and 3 to output the sum of first
//and last digits
int number = input; //number is a temp variable
int counter = 0; //counter is used to count no of digits
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
counter = counter+1;
number = number/10;
}
}
}
Output:
Enter a Number
**223**
Cube of 3 is 27
Cube of 2 is 8
Cube of 2 is 8
How do I add the cubes of these numbers? For example, 27+8+8 would be 43
Maybe you want to do something like this:
int number = input;
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += digit * digit * digit;
number /= 10;
}
Try this code.
int sum=0;
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
sum=sum+(t*t*t);
counter = counter+1;
number = number/10;
}
System.out.println(sum);
Here a solution:
int sum = 0;
while(number>0){
int t= number%10;
System.out.println("Cube of "+t +" is "+(t*t*t));
sum += t*t*t;
counter = counter+1;
number = number/10;
}
System.out.println(sum);
You seem new to Java, so here is a more simple (and readable) example for you:
import java.util.*;
import java.io.*;
class Cubesum {
public static void main(String args[]){
int num1,num2,num3;
Scanner console = new Scanner(System.in);
//read the numbers
System.out.println("Enter the first number");
num1 = Integer.parseInt(console.nextLine());
System.out.println("Enter the second number");
num2 = Integer.parseInt(console.nextLine());
System.out.println("Enter the third number");
num3 = Integer.parseInt(console.nextLine());
int output = (int1*int1*int1)+(int2*int2*int2)+(int3*int3*int3)
System.out.println("result is: " + output);
}
}
You want to get each number individually then create the result.
Sample input:
2
2
3
Output should be:
43

Categories