InterviewSolution
Saved Bookmarks
| 1. |
Write a program that reads n digit number. After reading the number, compute and display the sum of the odd positioned digits, multiply all even positioned digits and add these two numbers. |
|
Answer» import JAVA.util.*;public class Main{public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n =sc.nextInt(); int oddsum=0; int evenproduct=1; while(n>0) { int r=n%10; if(r%2!=0) { oddsum=oddsum+r; // 1+3+5+7=16 } if(r%2==0) { evenproduct=evenproduct*r; // 2*4*6*8= 384 } n=n/10; }System.out.println(oddsum+evenproduct); // 16+384=400}}EXPLANATION:it is a java program |
|