1,105,295 Community Members

Square Root and cube Calculation

Member Avatar
mrbrightside10
Newbie Poster
2 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm so confused on this, can anyone please give me a hand. Thank you in advance.

A program that uses the numbers from 1 - 12 and output the number, the square of the number, the cube of the number and finally the square root of the number. At the end give me a grand total of the square roots.

Member Avatar
mrbrightside10
Newbie Poster
2 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
//**Forgot to post the code before:
//**Having problems with the grad square root. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace squarerootsys

{  

class Program  

{  

static void Main(string[] args)  

{  

int 
Square,  
Cube;
Squareroot;


Console.WriteLine("\tNumber\tSquare\tCube\tSquareroot");  

//loop 12 times calculating and displaying values  

    for (int Number = 1; Number <= 12;  

Number++)  

{  
Square = Number * Number;  

Cube = Square * Number;

Squareroot = Math.Sqrt(Number);

         

        Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", Number, Square, Cube, Squareroot);  

}// end of calculate and display  

  

Console.ReadLine();  

}

}  

}
Member Avatar
kplcjl
Junior Poster
188 posts since Sep 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

I'm confused. This is obviously a homework problem. For that, you don't deserve a code answer. You are supposed to produce something like:
Value: 1, Square: 1, Cube: 1, Sqrt: 1
Value: 2, Square: 4, Cube: 8, Sqrt: 1.4142135623731
Value: 3, Square: 9, Cube: 27, Sqrt: 1.73205080756888
Value: 4, Square: 16, Cube: 64, Sqrt: 2
Value: 5, Square: 25, Cube: 125, Sqrt: 2.23606797749979
Value: 6, Square: 36, Cube: 216, Sqrt: 2.44948974278318
Value: 7, Square: 49, Cube: 343, Sqrt: 2.64575131106459
Value: 8, Square: 64, Cube: 512, Sqrt: 2.82842712474619
Value: 9, Square: 81, Cube: 729, Sqrt: 3
Value: 10, Square: 100, Cube: 1000, Sqrt: 3.16227766016838
Value: 11, Square: 121, Cube: 1331, Sqrt: 3.3166247903554
Value: 12, Square: 144, Cube: 1728, Sqrt: 3.46410161513775
Sum of all Sqrt values: 29.2490045916973

I wrote the code to do that in 11 lines. The square and cubes are simple math statements, you just need to know how to calculate them. That's obviously part of your homework. There are equations for finding the N'th root of numbers, but you don't even have to know that fairly advanced mathmatics. The square root is a built-in function of the Math tool. (A built-in part of the system namespace.)

I've probably given you more help than your teacher wanted you to have in order to solve this.

Member Avatar
kplcjl
Junior Poster
188 posts since Sep 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 3 [?]
 
0
 

My reply was prior to seeing your new post. OK, you know about Math.Sqrt. You didn't define the type of your Sqrt variable. The type you supplied to Sqrt does not match the type it expects. That should be fine, it should automatically convert int to the right type but this is the type of thing that can bite you in the @ss ao you need to know when you are taking the shortcut.

It IS critical you fix your Sqrt variable's type correctly, setting it to int won't cut it. Your logic is missing the variable that caclulates the sum.

Personally, I wouldn't put Console.ReadLine() inside the loop, I would put it right after writing out the sum AFTER the loop has finished.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: