Write one program to input a number and perform the following operations
1) Print the number of digits
2) Print sum of all odd digits
3) Print the smallest digit present in the number
For example if the number is = 8723. then output will be
The number of digits=4
The sum of odd digits= 10
The smallest digit= 2
Answers
The following cσdes have been written using Python.
n = int(input("Enter a number: "))
print()
l = len(str(n))
s = 0
for i in str(n):
if int(i)%2 != 0:
s = s + int(i)
sl = list(str(n))
md = min(sl)
print("The number of digits: ", l)
print("The sum of the odd digits: ", s)
print("The smallest digit: ", md)
Answer:
The following códe is written in JAVA
package Programmer; //optional statement, no need to write it.
import java.util.Scanner;
public class NumDig{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int sum=0,sumodd=0,k=n%10;
for(;n!=0;n/=10){
sum+=n%10;
sumodd=((n%10)%2!=0)? sumodd+(n%10) : sumodd+0;
if(k<(n%10))
k=n%10;
}
System.out.println("Sum of digits:"+sum);
System.out.println("Sum of odd digits:"+sumodd);
System.out.println("Smallest digits:"+k);
}
}