Write a basic program. to input a four digit number and print all its digits.
Answers
please mark my answer as brainliest answer please
Answer:
Java
We are assuming that the user will enter a proper 4 digit integer.
We can take this user input with the help of Scanner.
After this, we need to extract the digits.
If we take modulo 10, i.e. number % 10, we will get the units digit as remainder.
We can store this. Now, if we divide by 10, i.e. number / 10, we will get the quotient of the division. Essentially, we would get the number with the units digit removed.
We can now again use the % 10 trick to extract the new units digit.
In this way we can get the digits of a number from right to left. We can then find the sum of the first two digits and product of the last two digits.
\rule{300}{1}
import java.util.Scanner; //Importing Scanner
public class DigitOperations //Creating Class
{
public static void main(String[] args) //Main Function
{
//Create Scanner Object
Scanner scr = new Scanner(System.in);
//Take User Input
System.out.print("Enter a four digit number: ");
int num = scr.nextInt(); //Storing Input in num
int digit1 = num % 10; //Extracting Unit's Digit
num /= 10; //Dividing by 10 to remove unit's digit
int digit2 = num%10; //Extracting Ten's Digit
num /= 10; //Dividing by 10 to remove original ten's digit
int digit3 = num%10; //Extracting Hundred's Digit
num /= 10; //Dividing by 10 to remove original hundred's digit
int digit4 = num%10; //Extracting Thousand's Digit
//Now, we have extracted the digits from right to left
//So, digit1 and digit2 are the last two two digits
//digit3 and digit4 are the first two digits
int sum = digit3 + digit4;
int product = digit1 * digit2;
System.out.println("Sum of first two digits = "+sum);
System.out.println("Product of last two digits = "+product);
}
}