Given a pattern... (1 / 2)^2 + (2 / 3)^2 + (3 / 4)^2 + ...
Input is n; which becomes the highest denominator used in the summation of these fractions.
When a user enters n = 2; the sum is simply (1 / 2)^2.
When a user enters n = 3; the sum is (1 / 2)^2 + (2 / 3)^2
I believe I've solved this problem (we assume the user enters an integer greater than or equal to 2)...
int n;
double sum = 0, counter = 2;
n = console.nextInt();
while(counter <= n) {
sum = sum + Math.pow(--counter/++counter, 2);
counter++;
}
System.out.println("Sum is " + sum);
Is there any "obvious" way to simplify my solution to this problem (whilst remaining as a while-loop)? This is one of the first loops I've ever written. I'm not sure if less variables can be used to solve this problem.
Try
int n;
double sum = 0, counter=1;
n = console.nextInt();
while((counter+1) <= n) {
sum = sum + Math.pow(counter/(counter+1), 2);
counter++;
}
System.out.println("Sum is " + sum);
Why dont simplify it and write this:
int n;
double sum = 0, counter = 2;
n = console.nextInt();
while(counter <= n) {
sum = sum + Math.pow((counter - 1)/counter, 2);
counter++;
}
System.out.println("Sum is " + sum);
i dont know who is computed first (++counter) or (--counter), if (--counter) your response is correct, but dont pollute your code with ++ or --, use them only if you need it.
Related
Just wondering where exactly is the bug? Is the for statement formatted incorrectly?
int n;
sum = 0;
for (n = 1; n < 10; n++) {
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You need to close your for loop with a } and declare the sum variable as an int. So the code should look like:
int n;
int sum = 0;
for (n = 1; n < 10; n++){
sum = sum + n;
}
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You should research using an IDE like Eclipse. Its a text editor for your code that will point out where the compilation errors are.
My goal is to calculate the mean and standard deviation of 10 numbers obtained from user input.
I learned only while loops in my class.
Question:
How can I assign each user input (inside the loop) to a separate variable, to later compare to the mean, to find the standard deviation?
(I haven't taught about arrays in my class. So, I have to solve this without using arrays)
I did the program a very long a tedious way but can provide more code if needed to show I can do while loops to find mean.
import java.util.Scanner;
public class StandardDev
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
double num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0, num10 = 0, total = 0, mean = 0, stDev1 = 0, stDev2 = 0, sum = 0;
System.out.println("Enter 10 numbers: ");
num1 = input.nextDouble();
num2 = input.nextDouble();
num3 = input.nextDouble();
num4 = input.nextDouble();
num5 = input.nextDouble();
num6 = input.nextDouble();
num7 = input.nextDouble();
num8 = input.nextDouble();
num9 = input.nextDouble();
num10 = input.nextDouble();
sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
mean = sum / (double) 10;
stDev1 = Math.pow(num1 - mean, 2) + Math.pow(num2 - mean, 2) + Math.pow(num3 - mean, 2) + Math.pow(num4 - mean, 2) + Math.pow(num5 - mean, 2) +
Math.pow(num6 - mean, 2) + Math.pow(num7 - mean, 2) + Math.pow(num8 - mean, 2) + Math.pow(num9 - mean, 2) + Math.pow(num10 - mean, 2);
stDev2 = Math.sqrt(stDev1 / 10);
System.out.println ("The mean is " + mean + ".");
System.out.println ("The standard deviation is " + stDev2 + ".");
}
}
Here is what I have for finding mean. My logic tells me I can only find standard deviation if I can access those inputs individually to subtract from mean and square. Not sure how though..
import java.util.Scanner;
public class JTillman03_45
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int count = 0, total = 0;
double mean, stDev = 0;
System.out.println("Enter a digit: ");
int number = input.nextInt();
while (count < 9){
total += number;
count++;
System.out.println("Enter a digit: ");
number = input.nextInt();
}
mean = total / (double) count;
System.out.println("The mean of those numbers is: " + mean);
System.out.println("The standard deviation of those numbers is: " + stDev);
}
}
Do you really need to store all the integers? If not, you can compute the average and standard deviations with the three following values:
How many numbers were entered (in your problem, it is a fixed value 10).
The sum of all numbers.
The sum of the squares of the numbers.
Which gives
double count = 10.0; // is 10.0 for your problem
double sum1 = 0.0; // sum of the numbers
double sum2 = 0.0; // sum of the squares
int i;
for (i=0; i < 10; i++) {
System.out.println("Enter 10 numbers: ");
double n = input.nextDouble();
sum1 += n;
sum2 += n * n;
}
double average = sum1 / count;
double variance = (count * sum2 - sum1 * sum1) / (count * count);
double stdev = Math.sqrt(variance);
For summing up 10 user-inputted numbers, you can keep track of the sum using just one variable and the += operator. No need to have so many variables, that's what the loop is for! For example,
double sum = 0;
Scanner input = new Scanner (System.in);
int counter = 1;
System.out.println("Enter 10 numbers: ");
//adds up 10 user-inputted numbers
while(counter <= 10){
sum += input.nextDouble();
//how the loop will end after 10th iteration
counter++;
}
double mean = sum/10; //no need for double cast here since sum is a double
Without arrays, it would be hard to calculate the standard deviation using loops since we need access to the individual numbers to find the standard deviation. Therefore, your solution is really the only way unless we have the user input the same 10 numbers again using a while loop similar to above to find the variance. For example,
//reset counter
counter = 1;
double variance = 0;
System.out.println("Enter 10 numbers: ");
while(counter <= 10){
variance += Math.pow(input.nextDouble()-mean,2);
counter++;
}
double stdDev = Math.sqrt(variance/10);
System.out.println ("The mean is " + mean + ".");
System.out.println ("The standard deviation is " + stdDev + ".");
Using a while loop, iterate the values of input.nextDouble() as the condition of the loop. as long as there is input, the loop will keep going. At the same time, keep a count of the number of inputs and the sum of the numbers. Just with those 2 numbers, you can calculate the mean and the standard deviation. If you look at stDev1 you can see it is just the sum of the numbers minus the mean times 10 (or the count of inputs). The mean is just the sum divided by the count of inputs. If you need more explanations, let me know. I will be adding some example code shortly.
public class StandardDev {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
sum += input.nextDouble();
count++;
}
mean = sum / count;
stdDev = sum - (count * mean);
System.out.println("The mean is " + mean + ".");
System.out.println("The standard deviation is " + stdDev + ".");
}
}
Here is updated code with loops.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sum = 0;
double sqSum = 0;
System.out.println("Enter 10 numbers: separte by space");
for ( int i = 0 ; i < 10 ; i++){
double val = input.nextDouble();
sum += val;
sqSum += val * val;
}
double mean = sum / 10;
double stdDev = sqSum / 10 - mean * mean;
System.out.println("The mean is " + mean + ".");
System.out.println("The standard deviation is " + stdDev + ".");
}
import java.util.Scanner;
public class While
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Choose a number to find various things for: ");
int n = in.nextInt();
// Extraneous variables to keep myself organized.
int i = 0;
int b = 1;
int t = 0;
int sum1 = 0;
int sum2 = 0;
double sum3 = 0;
double total = 0;
// Extraneous code to keep simple.
System.out.println("Squares up until your number");
while ( (i * i) < n ){
System.out.println(i * i);
sum1 = sum1 + (i * i);
i++;
}
System.out.println("Now positives divisible by 10");
while ( (b * 10) < n){
System.out.println(b * 10);
sum2 = sum2 + (b * 10);
b++;
}
System.out.println("Now squares of 2!");
while ( Math.pow(2,t) < n ){
System.out.println(Math.pow(2,t));
sum3 = sum3 + (Math.pow(2,t));
t++;
}
// Second part of assignment.
total = total + (sum1 + sum2 + sum3);
System.out.println("The sum of all numbers: " + total);
System.out.println();
}
}
Now, the total needs to be separated into its own numbers.
If the user puts in the number 50, the program's total will be 303.
How can I get that 303 to be, 3, 0, 3, then add only the odd numbers
then use a simple System.out. to print a 6 for this example?
Use modulus to get digit by digit. Check this answer to further explanation.
int sum = 0;
while (total > 0) {
int digit = (int) total % 10;
total = total / 10;
if (digit % 2 != 0) sum += digit;
}
System.out.println("The sum of all odd digits: " + sum);
System.out.println();
OUTPUT for 50:
The sum of all numbers: 303.0
The sum of all odd digits: 6
OUTPUT for 200:
The sum of all numbers: 3170.0
The sum of all odd digits: 11
int mod = 0, sum = 0;
while (total > 1) {
mod = (int) (total % 10);
total = total / 100;
sum += mod;
}
System.out.println("The sum of odd numbers is " + sum);
I have used the modulus operator to get the mod. But as you need odd numbers, I divided total by 100 till it is greater than 1. Lower number of loops will run in this case.
I need to do something like this - user types in a number (e.g. 5) and the program shows the result of the following action: 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 + 5*5*5.
My code is following:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your number:");
int n = input.nextInt();
int a[] = new int[n];
int power = 0;
int sum = 0;
for (int i = 1; i <= a.length; i++)
{
power = (int) pow(i, 3);
// Maybe here'a the problem, a[n] has not enough space for the power (???)
sum += a[power];
}
System.out.println(Result: " + sum);
}
I think that I understand why this code doesn't work but I will appreciate any ideas about how to do it properly and runnable.
sum += power;, no need for a.
Or simply use math :
int n = input.nextInt();
int result = (int)(0.25 * Math.pow(n, 2)*Math.pow(1+n,2));
Or even more simple, as #ajb said :
int result = n*n*(n+1)*(n+1)/4;
Change:
sum += a[power];
to
a[i-1] = power;
sum += a[i-1];
or forget the array altogether
sum += power;
sum += a[power]; doesn't exist.
You need to :
store the power value in the ith array item.
add the ith array value to the sum
Change your loop body to:
power = (int) pow(i, 3);
a[i - 1] = power;
sum += a[i - 1];
You're doing way too much work.
Scanner input = new Scanner(System.in);
System.out.println("Enter your number:");
int n = input.nextInt(),
sum = 1;
for (int i=2; i<=n; i++) {
sum += (int) Math.pow(i,3);
}
done. sum now contains the sum 1³ + 2³ + 3³ + ...
Or, more efficiently with exactly the same functionality:
Scanner input = new Scanner(System.in);
System.out.println("Enter your number:");
int n = input.nextInt(),
sum = 1;
while(n > 1) {
sum += (int) Math.pow(n--,3);
}
Why? Because 1 + 5³ + 4³ + 3³ + 2³ is the same as 1³ + 2³ + 3³ + 4³ + 5³
Of course you could also just directly compute that value, using actual maths, cutting all the entire algorithm with a simple arithmetic oneliner.
So the question I'm trying to solve the user is supposed to enter any positive number. Then I'm trying to write a program that adds only the odd numbers up to the number the user enters and displays the total. So for example if the user enters 4 my program should add four odd numbers. 1 + 3 + 5 + 7 = 16. The only tools I have available are for statement, if, if/else if,while loop and println.
I can only figure out how to print out the odd numbers. I know I want to create a variable named total to store the value of adding up all the odd numbers but I don't know how that fits into the program.
import acm.program.*;
public class AddingOddNumbers extends ConsoleProgram {
public void run() {
int n = readInt("enter a positive nunber: ");
int total = 0;
for (int i = 0; i < n; i++) {
if (n == 1) {
println(1);
} else {
println((i * 2) + 1);
}
}
}
}
import acm.program.*;
public class AddingOddNumbers extends ConsoleProgram {
public void run() {
int n = readInt("enter a positive nunber: ");
int total = 0;
for (int i = 0; i < n; i++) {
if (n == 1) {
println(1);
} else {
println((i * 2) + 1);
total += (i * 2) + 1;
}
}
println("total : " + total);
}
}
sum = 0;
for (i = 1; i < n*2; i=i+2)
sum = sum + i;
This will give you the odd number sum.
if (n>0)
{
total=0;
for (int i = 1; i < n; i ++){
if (i%2 == 1)
total+=i;
}
}
If you want to inclusive of n, then change the condition to i<=n.
Maybe you know how to compute the sum of all numbers up to a given number n? The formula is quite simple: (n * (n+1))/2. Now getting the sum of only the odd numbers is a bit trickier but - no worries you can make use only of the previous formula for that. First notice that the sum of all even numbers up to a given number n is:
(((n/2)* (n/2+1))/2) * 2 if N is even(i.e. the sum of all numbers up to n/2 times two that is because you have 2+4+6+8+...N = 2*(1+2+3+...n/2))
((((n-1)/2)* ((n-1)/2+1))/2) * 2 if N is odd
In fact if you have integer division the formula is always: (((n/2)* (n/2+1))/2) * 2 = (n/2)* (n/2+1)
So to compute the sum of all the odd numbers up to n you simply subtract the sum of the even numbers from the sum of the all numbers:
(n * (n+1))/2 - (n/2)*(n/2+1)
In fact if you observe closely you will notice that the sum 1+3+...(2*n-1) always equals to n^2.
This answer should help you solve your problem in all languages and I am leaving the code to you. It is literally one line.
I would use a loop for the odd numbers as well.
for (int i = 0, j = 1; i < n; i++, j += 2) {
println(j);
total += j;
}
println(total);
int oddSum = 0;
for (int i = 0; i < n; i++){
oddSum = oddSum + (i*2) + 1;
}