473,386 Members | 1,867 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.

I cannot get my numbers to square

on my assignment I need to ask a user for two numbers

output all the odd # between the two #
output the sum of the even #
*output the # and squares between 1-10*
*output the sum of the square of the odd numbers between #1 and #2*

now I can get the first part but when it comes to the squares between 1-10 my code does not work, and I am not sure why

cout << "1 squared is: " << pow(1, 2) << endl;
cout << "2 squared is: " << pow(2, 2) << endl;
cout << "3 squared is: " << pow(3, 2) << endl;
cout << "4 squared is: " << pow(4, 2) << endl;
cout << "5 squared is: " << pow(5, 2) << endl;
cout << "6 squared is: " << pow(6, 2) << endl;
cout << "7 squared is: " << pow(7, 2) << endl;
cout << "8 squared is: " << pow(8, 2) << endl;
cout << "9 squared is: " << pow(9, 2) << endl;
cout << "10 squared is: " << pow(10, 2) << endl;

Hunderpanzer said
"Well the line of code listed above would work if the user input 1 as a number, but remember that the user can start the firstNumb at 50 and the secondNumb at 70. In your code, it would then print the squares of 1- 10 which isn't the right result."

but all I need it to do is square 1-10, but I keep getting an error.

do I need to put in more variables for the squares?
Jul 24 '07 #1
3 6613
ilikepython
844 Expert 512MB
on my assignment I need to ask a user for two numbers

output all the odd # between the two #
output the sum of the even #
*output the # and squares between 1-10*
*output the sum of the square of the odd numbers between #1 and #2*

now I can get the first part but when it comes to the squares between 1-10 my code does not work, and I am not sure why

cout << "1 squared is: " << pow(1, 2) << endl;
cout << "2 squared is: " << pow(2, 2) << endl;
cout << "3 squared is: " << pow(3, 2) << endl;
cout << "4 squared is: " << pow(4, 2) << endl;
cout << "5 squared is: " << pow(5, 2) << endl;
cout << "6 squared is: " << pow(6, 2) << endl;
cout << "7 squared is: " << pow(7, 2) << endl;
cout << "8 squared is: " << pow(8, 2) << endl;
cout << "9 squared is: " << pow(9, 2) << endl;
cout << "10 squared is: " << pow(10, 2) << endl;

Hunderpanzer said
"Well the line of code listed above would work if the user input 1 as a number, but remember that the user can start the firstNumb at 50 and the secondNumb at 70. In your code, it would then print the squares of 1- 10 which isn't the right result."

but all I need it to do is square 1-10, but I keep getting an error.

do I need to put in more variables for the squares?
I don't under stand why you need all those couts when you can use a simple for loop:
Expand|Select|Wrap|Line Numbers
  1. for (int x = 1; x <= 10; x++)
  2. {
  3.     cout << x << " squared is: " << x*x << endl;
  4. }
  5.  
Jul 24 '07 #2
I don't under stand why you need all those couts when you can use a simple for loop:

Code: ( cpp )
for (int x = 1; x <= 10; x++)
{
cout << x << " squared is: " << x*x << endl;
}


To be honest it is the only way I could think to do it

but I see how you are saying it just loop it so that it goes up to 10, dang that is a lot easier way to do it

how about if I needed to raise it to the third power just use
cout << x << " squared is: " << x*x*x << endl;

Thank you, I am just learning how to write programs in C++ so I don't have my head around all the great short cuts/ good ways to do things.
Jul 24 '07 #3
sicarie
4,677 Expert Mod 4TB
Though the other solution is more elegant, I wanted to post in case there are any searches later with this issue. With the pow() function, you have to be sure to include the math.h library. (With that, your original code will compile and run)
Jul 24 '07 #4

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

Similar topics

2
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
17
by: Mike Meyer | last post by:
In the discussion of equality, the issue that decimal('3.0') == 3.0 is False came up as a reason for changing the behavior of ==. The problem with this is that the proposed change doesn't really...
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...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
2
by: shihaoran | last post by:
I really need help with one my program; it is about arraylist; I do not get it. Can someone please help me with it? Here's the instruction: 1. Your instructor will provide you with a text...
2
by: cmeekeen | last post by:
How do I square the numbers in the even rows? I have generated 100 integer random numbers in the range of 1 to 100 in array x(10,10). Private Sub Form_Activate() Const row As Integer = 10 Const...
1
by: mandanarchi | last post by:
I've hunted for info about this and come up with loads. Jet 4.0 error - fixed in latest service pack. We all have latest service pack. Copy faulty table, delete old, remove fault field etc This...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.