store a 2 digit odd number. extract the digits in the units place and calculate the half of the digit. display all details in java
Answers
Answer:
import java.util.Scanner;
public class Sum_Odd_Even
{
public static void main(String[] args)
{
int n, sumE = 0, sumO = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in array:");
n = s.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 == 0)
{
sumE = sumE + a[i];
}
else
{
sumO = sumO + a[i];
}
}
System.out.println("Sum of Even Numbers:"+sumE);
System.out.println("Sum of Odd Numbers:"+sumO);
}
}
INPUT :
Enter the number of elements in array:6
Enter the elements of the array:
1
3
2
6
7
9
OUTPUT : Sum of Even Numbers:8
Sum of Odd Numbers:20
Explanation:
Thanku ❤️
Answer:
//To calculate half of the units digits
import java.util.*;
public class OddDigHalf
{
public static void main (String ar [])
{
Scanner sc=new Scanner (System.in);
int a=sc.nextInt(); //Enter the odd number
int d=a%10; //To store the last digit
int k=d/2; //Calculating half of that digit
System.out.println("Half of unit digit:"+k);
}
}
Sample Input:
24
Desired Output:
Half of unit digit: 2