Write a Java program that defines the following methods:
input () – To accept a number
EvenPos() - To calculate and display the sum of even positioned digits.
OddPos() - To calculate and display the sum of odd positioned digits.
main () – To create and object and call the above member methods.
Eg: Input: 53620
Output: Sum of even positioned digits – 5
Sum of odd positioned digits –11
whoever answer is correct will be marked brainly
Answers
Answered by
4
Answer:
Program:-
import java.util.*;
public class Main
{
int n;//class variable
public void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number:");
n=in.nextInt();
}
void EvenPos()
{
int a=0,c=0,s=0,num=0;
num=n;
while(num!=0)
{
a=num%10;
c++;
if(c%2==0)
s=s+a;
num=num/10;
}
System.out.println("The sum of even positioned digits="+s);
}
void OddPos()
{
int r=0,d=0,s1=0;
while(n!=0)
{
r=n%10;
d++;
if(d%2!=0)
s1=s1+r;
n=n/10;
}
System.out.println("The sum of odd positioned digits="+s1);
}
public static void main(String args[])
{
Main ob=new Main();
ob.input();
ob.EvenPos();
ob.OddPos();
}
}
Output is attched.
Attachments:
Similar questions