Write a program to input a number and print sum of its even digits and odd digits separately. (In BlueJ)...please answer fast...
harindersaini2pcf8vf:
what ?
Answers
Answered by
14
import java. until. *;
public class check
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System. out. println("Enter the number ");
int n=in.nextInt();
int m=n;
int s,o=0,e=0;
while(m>0)
{
s=m%10;
m=m/10;
if(s%2==0)
e=e+s;
if(s%2!=0)
o=o+s;
}
System. out. println("The sum of odd number is"+o);
System. out. println("The sum of even number is"+e) ;
}
}
*************----------************
DRY RUN:---
n=123=m
s=12%10
s=3..which is an odd number...
so,o=0+3
o=1;
next time it's value will be,
s=2..which is an even. number
e=0+2
e=2
next time it's value will be,
s= 1..which is odd number and o=1 already,
o=3+1
o=4
*************----------************
OUTPUT
The sum of odd number is 4
The sum of even number is 2
Hope it helps (。’▽’。)♡
public class check
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System. out. println("Enter the number ");
int n=in.nextInt();
int m=n;
int s,o=0,e=0;
while(m>0)
{
s=m%10;
m=m/10;
if(s%2==0)
e=e+s;
if(s%2!=0)
o=o+s;
}
System. out. println("The sum of odd number is"+o);
System. out. println("The sum of even number is"+e) ;
}
}
*************----------************
DRY RUN:---
n=123=m
s=12%10
s=3..which is an odd number...
so,o=0+3
o=1;
next time it's value will be,
s=2..which is an even. number
e=0+2
e=2
next time it's value will be,
s= 1..which is odd number and o=1 already,
o=3+1
o=4
*************----------************
OUTPUT
The sum of odd number is 4
The sum of even number is 2
Hope it helps (。’▽’。)♡
Similar questions
Math,
7 months ago
Computer Science,
7 months ago
Math,
1 year ago
English,
1 year ago
Math,
1 year ago