write a program in java to accept a five digit number and find the sum and difference of the first two digits and the last two digits
Answers
Answered by
1
Question:-
Write a program in java to accept a five digit number and find the sum and difference of the first two digits and the last two digits.
Program:-
import java.util.*;
class Sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a 5 digit number: ");
int n=sc.nextInt();
int x=0, y=0, d;
if(n>=10000&&n<100000)
{
for(int i=1;i<=2;i++,n/=10)
y+=n%10;
n/=10;
for(int i=1;i<=2;i++,n/=10)
x+=n%10;
System.out.println("Sum of first two digit: "+x);
System.out.println("Sum of last two digit: "+y);
}
else
System.out.println("Not a 5 digit number.");
}
}
Attachments:
Similar questions
English,
3 months ago
Social Sciences,
3 months ago
Sociology,
3 months ago
English,
6 months ago
Social Sciences,
6 months ago
Physics,
11 months ago