Write one program to input a number and perform the following operations
(i) Print the number of digits.
(ii) Print sum of all the odd digits.
(iii) Print the smallest digit present in the number.
Answers
Here Is Your Answer:
import java.util.*;
class Work
{
static Scanner in = new Scanner(System.in);
static int input;
Work()
{
}
void Input()
{
System.out.println("Enter The Number");
input = in.nextInt();
}
int number_of_digits(int inp)
{
int count = 0;
while(inp > 0)
{
inp = inp/10;
count++;
}
return count;
}
int sum_of_all_the_odd_digits(int inp)
{
int digit;
int sum = 0;
while(inp > 0)
{
digit = inp%10;
if(is_odd(digit))
{
sum = sum+digit;
}
inp = inp/10;
}
return sum;
}
Boolean is_odd(int digit)
{
if(digit%2!=0)
return true;
else
return false;
}
int smallest_digit(int inp)
{
int digit;
int min = 9999;
while(inp > 0)
{
digit = inp%10;
if(digit<min)
{
min = digit;
}
inp = inp/10;
}
return min;
}
public static void main(String[]args)
{
Work ob = new Work();
ob.Input();
System.out.println("Number Of Digit(s) In "+input+" Is/Are "+ob.number_of_digits(input));
System.out.println("Sum Of All The Odd Digits Present In "+input+" Is "+ob.sum_of_all_the_odd_digits(input));
System.out.println("The Smallest Digit Present In "+input+" Is "+ob.smallest_digit(input));
}
}
I Have Used Java Language