# Sum of counting and odd numbers

Page 1 of 1

## 2 Replies - 305 Views - Last Post: 05 February 2016 - 03:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=388383&amp;s=e393d89aaf5f0fb55865d9bcafa16dd0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rapmon

Reputation: 0
• Posts: 1
• Joined: 05-February 16

# Sum of counting and odd numbers

Posted 05 February 2016 - 01:55 PM

Write Java code to do the following: ask the user to enter a number value, n. Your code then uses three separate loops to compute the following:a.sum of the first n counting numbers: 1+2+3+...+nb.sum of the first n odd numbers: 1+3+5+...+(2n-1).c.The number of times the number n can be divided by 2 (using integer division) before reaching 1.

***I am having trouble trying to make count3 to only num 2 instead of 5 for the n. But it keeps on printing out num1 to 5. I only want it to show to num 2 since it is the number of times it can divide before reaching 1. Any solutions?
```import java.util.Scanner;

public class Kid {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

//Declare variables
int sum = 0;

System.out.print("Please enter a number for n: ");

int n = input.nextInt();

System.out.println("Sum of the first counting numbers: ");
for (int count = 1; count <= n; count++){
sum = count + sum;
System.out.println("Num: "+count + " Total: "+ sum);
}
System.out.println("Total sum: "+ sum);

System.out.print("\n");

int sumOdd = 0;
System.out.println("Sum of first n odd numbers: ");
for (int count2 = 1; count2 <= n; count2++){
sumOdd = (2 * count2) - 1;
System.out.println("Num: "+ count2+ "(2 * "+ count2 +")" + " - 1" + " Total: "+ sumOdd);
}
System.out.println("Total sum of odd number: "+ sumOdd);
System.out.print("\n");

int numDivided = 0;
[System.out.println("Number of times n can be divided: ");
for (int count3 = 1; count3 <= n; count3++){
numDivided = n / 2;
System.out.println("Num: "+count3);
}
System.out.println(n + " can be divided by "+ numDivided + " times before reaching 1.");]

}

}
```

This post has been edited by jon.kiparsky: 05 February 2016 - 02:21 PM
Reason for edit:: added [code] tags

Is This A Good Question/Topic? 0

## Replies To: Sum of counting and odd numbers

### #2 NormR

• D.I.C Lover

Reputation: 672
• Posts: 5,076
• Joined: 25-December 13

## Re: Sum of counting and odd numbers

Posted 05 February 2016 - 02:29 PM

Quote

trouble trying to make count3 to only num 2 instead of 5

Can you explain that a little better? And give an example of what the program is supposed to do.

### #3 ChrisNt

• cute bug

Reputation: 262
• Posts: 896
• Joined: 31-July 13

## Re: Sum of counting and odd numbers

Posted 05 February 2016 - 03:16 PM

When you dont know the number of iterations then you should use a While loop .

```for (int count3 = 1; count3 <= n; count3++){
numDivided = n / 2;
System.out.println("Num: "+count3);
}

```

This for loop will be executed based on the value that you assign to the variable n from the scanner.

```int n = input.nextInt();

```

This post has been edited by ChrisNt: 05 February 2016 - 03:19 PM