473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Odd numbers using a for loop help

Hi, I have this assignment that I have almost finished except I have one problem. I'm having trouble making my for loop find the sum of the first n odd numbers ( n being the variable in which a value is read in)
oddnum and oddsum are the variables i came up with
This is what I have so far :

Expand|Select|Wrap|Line Numbers
  1. int sumodd (int n)
  2. {
  3.     int oddnum, oddsum = 0;
  4.  
  5.     for (oddnum = 1; oddnum <= n; oddnum++)
  6.         oddsum += oddnum * oddnum;
  7.     return oddsum;
  8. }
Can anyone help me fix this??? My professor says it's right and that I just have to fix the limit. I don't know what she means. I asked this other guy for help and he came up with this:

Expand|Select|Wrap|Line Numbers
  1. int sumodd (int n)
  2. {
  3.     int oddnum, oddsum = 0;
  4.  
  5.     for ( oddnum = 0; oddnum < n; oddnum++)
  6.         if( (oddnum % 2) != 0 )
  7.             oddsum += oddnum;
  8.  
  9.     return oddsum;
  10. }
But that doesn't work either!
Mar 1 '07 #1
6 6580
Ganon11
3,652 Expert 2GB
The second method you have should be working - what input are you using, and what output are you getting?
Mar 1 '07 #2
lqdeffx
39
Typically, in a situation like this, I would create a quick print function that allows me to iterate through the loop and see how the values are changed. You might be having a problem elsewhere in the code!
Mar 1 '07 #3
The second method you have should be working - what input are you using, and what output are you getting?
This is my output:

Expand|Select|Wrap|Line Numbers
  1. Cassandra Cullen                Assignment 3
  2.  
  3. Enter a value, 0 to stop > 1
  4.  
  5. the sum of the first 1 cubes is 1
  6. the check is 1  CORRECT
  7.  
  8. the sum of the first 1 odd numbers is 0
  9. the check is incorrect
  10.  
  11. Enter a value, 0 to stop > 2
  12. the sum of the first 2 cubes is 9
  13. the check is 9  CORRECT
  14.  
  15. the sum of the first 2 odd numbers is 1
  16. the check is incorrect
  17.  
  18. Enter a value, 0 to stop > 3
  19. the sum of the first 3 cubes is 36
  20. the check is 36 CORRECT
  21.  
  22. the sum of the first 3 odd numbers is 1
  23. the check is incorrect
  24.  
  25. Enter a value, 0 to stop > 4
  26. the sum of the first 4 cubes is 100
  27. the check is 100        CORRECT
  28.  
  29. the sum of the first 4 odd numbers is 4
  30. the check is incorrect
  31.  
  32. Enter a value, 0 to stop > 5
  33. the sum of the first 5 cubes is 225
  34. the check is 225        CORRECT
  35.  
  36. the sum of the first 5 odd numbers is 4
  37. the check is incorrect
  38.  
  39. Enter a value, 0 to stop >
I'll put the whole program up because maybe it's something else:

Expand|Select|Wrap|Line Numbers
  1. // reads in a series of numbers to be stored in the variable n
  2. // sumcubes function finds the sum of first n cubes
  3. // sumodd function finds the sum of the first n odd numbers
  4. // labelit function prints a heading on the output with name and program number
  5. #include <iostream>
  6. using namespace std;
  7. void labelit (void);
  8. int sumcubes (int n);
  9. int sumodd (int n);
  10. int main () {
  11.     int n, valuecube, valueodd, check1, check2;
  12.  
  13.     labelit ();
  14.     cout << "Enter a value, 0 to stop > ";
  15.     cin >> n;
  16.     cout << endl;
  17.     while (n!= 0) {
  18.           valuecube = sumcubes(n); 
  19.  
  20.           cout << "the sum of the first " << n << " cubes is " << valuecube
  21.                    << endl;
  22.  
  23.           check1 = (n * (n + 1) / 2) * (n * (n +1) /2);
  24.  
  25.           if (check1 == valuecube)
  26.              cout << "the check is " << check1 << "\t" << "CORRECT" << endl 
  27.                       << endl;
  28.           else
  29.               cout << "the check is incorrect" << endl << endl;
  30.  
  31.           valueodd = sumodd (n);
  32.  
  33.           cout << "the sum of the first " << n << " odd numbers is " 
  34.                    << valueodd << endl;
  35.  
  36.           check2 = n * n;
  37.           if (check2 == valueodd)
  38.              cout << "the check is " << valueodd << "\t" << "CORRECT" 
  39.                       << endl << endl;
  40.           else
  41.               cout << "the check is incorrect" << endl << endl;
  42.           cout << "Enter a value, 0 to stop > ";
  43.           cin >> n;
  44.     }
  45.     system ("pause");
  46.     return 0;
  47. }
  48.  
  49. void labelit (void) 
  50. {
  51.      cout << "Cassandra Cullen " << "\t\t" << "Assignment 3" << endl 
  52.               << endl;
  53. }
  54.  
  55. int sumcubes (int n)
  56. {
  57.     int item, sum = 0;
  58.  
  59.     for (item = 1; item <= n; item++)
  60.         sum += item * item * item;
  61.     return sum;
  62. }
  63.  
  64. int sumodd (int n)
  65. {
  66.     int oddnum, oddsum = 0;
  67.  
  68.     for ( oddnum = 0; oddnum < n; oddnum++)
  69.         if( (oddnum % 2) != 0 )
  70.             oddsum += oddnum;
  71.  
  72.     return oddsum;
  73. }
  74.  
  75.  
Mar 1 '07 #4
Ganon11
3,652 Expert 2GB
Your sumodd loops from 0 to n - 1 - change the header to

Expand|Select|Wrap|Line Numbers
  1. for (oddnum = 0; oddnum <= n; oddnum++)
to count the last value of n.
Mar 1 '07 #5
#include<stdio.h>
#include<conio.h>
main()
{
int a,n;
printf("\nEnter the no : ");
scanf("%d",&n);
for(a=1;a<=n;a++)
if(a%2!=0)
printf("\n%d",a);
getch();
}
Sep 18 '07 #6
it looks funky in variables but it works .i have tried it .
i an a learner if it is wrong sorry for the time u wasted .

#include<stdio.h>
#include<math.h>
main()
{
int n,b,c,totalsum;
printf("Entre the value of n");
scanf("%d",&n);
for (b=1;b<n;b++)
c=n/2;
totalsum=c*c;
printf("The sum of odd numbers up to %d is %d",n,totalsum);
getch();
}


Sep 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: David Berry | last post by:
Hi All. I'm trying to write an ASP page that shows me the UNIQUE account number for a customer (so I can pass it to another page) based on a search criteria. For example, I want to do a select...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
13
by: Supra | last post by:
how do i get 2 different number in same row? using random example: 4 2 5 6 7 4 1 7 9 8 3 3 <===== i want different number but not same as opposite 8 5
3
by: lawrencec | last post by:
Hi there, I'm trying to add the values of a number of form fields and to get a result at the end. It must loop and be able to dynamically update the result of calculation. I have attached the...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
4
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
4
by: Villanmac | last post by:
Hi i have made the following program to make a fucntion that determines if a number is prime. Than i am supposed to use this function to print all prime numbers between 1 and 10000. What am I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.