Jump to content

what will the code be like

- - - - -

  • Please log in to reply
6 replies to this topic

#1
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
i need help with a program i am writing a in Java. a program that when i input a figure like 2345 it will sum the figure up by adding 2+3+4+5 can any one help some one give me some code
int a = 0 
int i = atoi(argv[1]); 
while (i) { 
    a += i%10; 
    i /= 10; 
} 
printf("%d\n", a);
but it does not run correctly.

Edited by Alexander, 10 November 2010 - 03:39 PM.
(code tags)


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
First line's missing a ';'
while loop must contain a condition which is true or false. Even if i becomes 0, 0 is no true or false for java.

I assume you know as well as I that the code you've given is c-code.
If you just google for the stuff you don't know you find the answer on the first link. Like google for "atoi in java", "printf in java"

#3
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Well, as Oxano said, you gave us C code. The code you have is close. You have to mod the total number by 10, and add it to a integer, then divide the whole number by ten. I made a code-snippit for you to edit to what you need it to do.


import javax.swing.JOptionPane;

public class StringAdd

{

	public static void main(String[] args)

	{

		int total = 0; int totalNum = 0;

		String input = JOptionPane.showInputDialog("Enter a number");

		totalNum = Integer.parseInt(input);

		

		for(int i = 0; i <= input.length(); i++)

		{

			total += totalNum % 10;

			totalNum /= 10;

		}

		System.out.println(total);

	}

}



#4
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
thankz very much nicckk i am very happy

#5
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts

nicckk said:

Well, as Oxano said, you gave us C code. The code you have is close. You have to mod the total number by 10, and add it to a integer, then divide the whole number by ten. I made a code-snippit for you to edit to what you need it to do.


import javax.swing.JOptionPane;

public class StringAdd

{

	public static void main(String[] args)

	{

		int total = 0; int totalNum = 0;

		String input = JOptionPane.showInputDialog("Enter a number");

		totalNum = Integer.parseInt(input);

		

		for(int i = 0; i <= input.length(); i++)

		{

			total += totalNum % 10;

			totalNum /= 10;

		}

		System.out.println(total);

	}

}


can u pls explain the formula to me

#6
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
First, I initialized two variables, total and totalNum. totalNum holds the whole number(eg. 12435) and is assigned to an integer through JOptionPane. Then I ran a for loop until it has run though the entire length of the inputed total number. Inside the loop, the variable total increases by totalNum % 10, which gives you the last digit. Then totalNum is divided by 10 to get rid of the last digit that you added. After the loop finishes total is printed.

For example, if you enter 1234: totalNum's value will be assigned to 1234. When the loop starts it adds 4(1234 % 10) to total. Then 1234 is divided by 10 so the whole number will be 123 so when the loop runs again 3 will be added, an so on.

#7
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
nice one nicckk thankz again i want to make it recursive can u help me wit it pls




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users