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)
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)
|
|
|
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);
}
}
nicckk said:
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);
}
}
0 members, 1 guests, 0 anonymous users