# someone please be kind enough demonstrate a code like this......

• January 22nd, 2013, 02:51 PM
willc86
someone please be kind enough demonstrate a code like this......
This is the work

- From the main method call a method named sumOfDigits(), without passing any parameters
- Create a method named sumOfDigits()
- In the sumOfDigits() method declare the variable N of type int
- Assign the value 100 to the variable N.
- Using the sum of digits formula: (1 + N)*(N/2)
print the total amount of the sum of digits from 1 to 100

__________________________________________________ __________________________

ive tried using

public void sumOfNumbers() {

formula = (1+100)*(2/100)
}

then I do system print (formula) but it keeps displaying 0

ive been messing around with it also. this is my code so far

__________________________________________________ _________

package Lunix;

public class chaplew {

private String myName;
private double creditsTaken;
private double totalCredits;
private double GPA;
private String major;
private String className;
private int maxValue;
private double N;

public chaplew(String myName, double creditsTaken,
double totalCredits, double GPA, String major, String className, int maxValue, int N )
{
this.myName = myName;
this.GPA = GPA;
this.className = className;
this.major = major;
this.totalCredits = totalCredits;
this.creditsTaken = creditsTaken;
this.maxValue = maxValue;
this.N = N;

}

public String getmyName()
{
return myName;
}
public double getcreditsTaken()
{
return creditsTaken;
}
public double gettotalCredits()
{
}
public double gteGPA()
{
return GPA;
}
public String getmajor()
{
return major;
}
public String getclassName()
{
return className;
}

{

}

public void subtractNumbers(int subtraction)
{
maxValue = subtraction-1;
}

public void multiplyNumbers(int multiply)
{
maxValue = multiply*1;
}

public void divideNumbers(int divide)
{
maxValue = divide/1;
}

public void modNumbers(int mod)
{
maxValue = mod%1;
}

public void sumOfNumbers(int form)
{
N = form;
}

public static void main(String[] args) {
// TODO code application logic here

chaplew p = new chaplew("William", 9, 70, 3.1, "Information Technology", "Operating Systems", 100, 1);
System.out.println("my name is...." + p.myName);
System.out.println("credits I am enrolled is..." + p.creditsTaken);
System.out.println("I have taken this amount of credits throughout college..." + p.totalCredits);
System.out.println("My gpa is..." + p.GPA);
System.out.println("My Major is.." + p.major);
System.out.println("This class is called......." + p.className);
System.out.println();

System.out.println("Max Value 1 + 2 + 3 + 4 + 5");
System.out.println(p.maxValue);
System.out.println(p.maxValue+(1));
System.out.println(p.maxValue+(2));
System.out.println(p.maxValue+(3));
System.out.println(p.maxValue+(4));

p.subtractNumbers(100);
System.out.println();
System.out.println("Max Value 1 - 2 - 3 - 4 - 5");
System.out.println(p.maxValue-1);
System.out.println(p.maxValue-2);
System.out.println(p.maxValue-3);
System.out.println(p.maxValue-4);
System.out.println(p.maxValue-5);

p.multiplyNumbers(100);
System.out.println();
System.out.println("Max Value 1 * 2 * 3 * 4 * 5");
System.out.println(p.maxValue*1);
System.out.println(p.maxValue*2);
System.out.println(p.maxValue*3);
System.out.println(p.maxValue*4);
System.out.println(p.maxValue*5);

p.divideNumbers(100);
System.out.println();
System.out.println("Max Value 1 / 2 / 3 / 4 / 5");
System.out.println(p.maxValue/1);
System.out.println(p.maxValue/2);
System.out.println(p.maxValue/3);
System.out.println(p.maxValue/4);
System.out.println(p.maxValue/5);

p.modNumbers(100);
System.out.println();
System.out.println("Max Value 1 % 2 % 3 % 4 % 5");
System.out.println(p.maxValue%1);
System.out.println(p.maxValue%2);
System.out.println(p.maxValue%3);
System.out.println(p.maxValue%4);
System.out.println(p.maxValue%5);

System.out.println();
System.out.println((1+100)*(2/100));

}
}
• January 22nd, 2013, 03:30 PM
Norm
Re: someone please be kind enough demonstrate a code like this......
What is your question?

Quote:

do system print (formula) but it keeps displaying 0
Where does the code change the value so it would not be 0?

[code=java]
[/code]
to get highlighting and preserve formatting.
• January 22nd, 2013, 05:25 PM
Zaphod_b
Re: someone please be kind enough demonstrate a code like this......
Quote:

Originally Posted by willc86
...
- Using the sum of digits formula: (1 + N)*(N/2)...

First of all, that is not the formula for a sum of digits. It is the formula to calculate the sum of the first N positive integers.

Secondly, and more importantly, I can't see where you have written code that implements the given formula. In fact, I can't see in your post where you have done a single thing that is specified by the assignment.

Why (oh, why) did you post all of the other stuff?

Bottom line: Read the assignment. Follow it exactly. If you don't get the right answer (or if the program doesn't compile for some reason), post your entire code with stuff needed for the part of the assignment that you don't understand.

Explain what you don't understand about what happens when you compile or run the code.

Cheers!

Z
• January 23rd, 2013, 05:32 AM
basha2013
Re: someone please be kind enough demonstrate a code like this......
Hi, the formula is ((N+1)*N)/2. In this first the jvm calculate the (N+1) ,after it multiply the result of (N+1) with N and it perform the division opertation on result (N+1)*N with 2.

hence formula is

formula = ((1+100)*100)/2;