CODING OF ACTION BUTTON(jButton1)
int num,temp,sum = 0;
num=Integer.parseInt(jTextField1.getText());
while(num>0)
{
temp=num%10;
sum+=temp;
num=num/10;
}
jTextField2.setText(" "+sum);
CODING OF CLEAR BUTTON(jButton2)
jTextField1.setText(" ");
jTextField2.setText(" ");
CODING OF EXIT BUTTON(jButton3)
System.exit(0);
CODING |
RUNNING |
We have used NetBeans IDE 7.2.1. To download it visit www.netbeans.org
For any query or suggestion please comment below...
thanks for the code!
ReplyDeletecan you just explain it to me roughly
Using "temp=num%10;" we got the remainder i.e. last digit of number "num". Then we added it to "sum" ("sum" is 0 at starting) using "sum+=temp;". Now we will remove last digit of number by "num=num/10;" . We will repeat this until "num" is 0.
DeleteHope you understood. If you have any doubt please feel free to ask.