Advertisements
Advertisements
Question
Define a class to accept a number. Check if the sum of the largest digit and the smallest digit is an even number or an odd number. Print appropriate messages.
| Sample Input: | 6425 | 3748 |
| Largest digit: | 6 | 8 |
| Smallest digit: | 2 | 3 |
| Sample Output: | Sum is even | Sum is odd |
Code Writing
Advertisements
Solution
import java.util.*;
public class SumLargeSmall
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number :");
int d,ld=0,sd=0,num;
num=sc.nextlnt();
ld=num%10;
sd=num%10;
while (num>0)
{
d=num%10;
if(d>ld)
ld=d;
if(d<sd)
sd=d;
num/=10;
}
System.out.println("Largest digit :"+ld);
System.out.println("Smallest digit :"+sd);
if ((sd+ld)%2=0)
System.out.println("Sum of largest and smallest
digts is even");
else
System.out.println("Sum of largest and smallest
digts is odd");
}
}shaalaa.com
Is there an error in this question or solution?
