2 Replies - 274 Views - Last Post: 05 February 2016 - 03:16 PM Rate Topic: -----

#1 Rapmon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1