adding odd numbers only

This is a discussion on adding odd numbers only within the C++ Programming forums, part of the General Programming Boards category; Hello all, Im new here and was hoping to get a bit of help with a program i am writing ...

  1. #1
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    adding odd numbers only

    Hello all,

    Im new here and was hoping to get a bit of help with a program i am writing for class. The program has the user enter a start number then a ending number. Then displays only the Odd number. Ive been successful up to that point. The next part has been giving me a bit of a headache. Can you help?

    After the odd numbers are displayed the program needs to Add those numbers together and display the sum. So if the user enters in 1 and 10 it will display 1,3,5,7,9 then 25 for the total sum. Any guidance would be much appreciated and I hope to hang around here and help anyone that might need it in the future.


    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <math.h>
    
    void main()
    {
    	char doAgn = 'Y';
    	int startNum= 0;
    	int endNum = 0;
    	int oddCount = 0;
    	int sum = 0;
    	int sumNum = 0;
    	
    
    	while (toupper(doAgn) =='Y'){
    
    		//get inputs
    
    		cout<<"Please enter the beginning number:";
    		cin>>startNum;
    
    		cout<<"Please enter the ending number:";
    		cin>>endNum;
    
    		//display the odd numbers
    		
    		for (int i = startNum; i < endNum; i++){
    			oddCount++;			
    			if(oddCount % 2 !=0)
    			cout<<oddCount<<endl;
    		}//end For
    
    		//display the total sum for the odd numbers displayed
    
                    ????????
    
    		//display the average for all the odd numbers displayed
    
                    //hopefully i will grasp this after understanding the above line of code.
    
    			
    
    		//clear out odd count 
    		oddCount = 0;
    
    	}//end while
    
    
    }//end main

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    keep track of the sum in a variable. Keep on adding to it whenever you find an odd number

  3. #3
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    thats what i figured needs to happen. The problem for me is how do i code after the (oddCount % 2 !=0) finds the odd number once then again "then make those two numbers add" up that I can store in a variable. thanks for the point in the right direction skorman00.

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    if that test comes out to true, you know you have an odd number right? In addition to displaying it, also add it to the running sum.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    29
    You wrote:

    for (int i = startNum; i < endNum; i++){
    oddCount++;
    if(oddCount % 2 !=0)
    cout<<oddCount<<endl;

    Try this:-

    for (int i = startNum; i < endNum; i++)
    {

    if(oddCount % 2 !=0)
    {
    oddCount++;
    cout<<oddCount<<endl;
    }
    }

    Hope this helps,

    BigSteve
    bigSteve

  6. #6
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    thank you for your help. I'll keep thinking this one through

  7. #7
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    oh i see bigSteve let me try that real quick

  8. #8
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    well now that doesnt display my oddCount ... i guess this is one to sleep on or something

  9. #9
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    that threw you into an infinite loop. You only incrimented oddCount if it was odd, so it would just stop incrimenting if it became even. I'll now point out that you are using oddCount to keep track of the odd numbers, which starts at 0, not the number you got from the user. Keep track of that with i instead, which you have set up properly.

  10. #10
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    What is the point of oddCount?

    Here is some pseudo-code/logic to your program. I hope it helps.

    Code:
    while still getting input
    {
            declare total variable
     
            get starting number
            get ending number
    
            for i = from starting-number to ending-number
            {
                    if i is an odd number
                    {
                            print i
                            total += i 
                    }
            }
    
             print total
    }

  11. #11
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    Thanks peoples for the help. My c++ book doesnt have a thing about the += operator in it Thats what was causing me the problems. How would you achieve the same effect without it?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    total = total + i
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    thanks laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 10:59 AM
  2. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  3. adding base n numbers
    By doogle in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:23 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21