Find first and last digits of a number,Given a number and to find first and last digit of a number.Examples: ,Finding sum of digits of a number until sum becomes single digit,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Input: 12345
Output: First digit: 1
last digit: 5
Input: 98562
Output: First digit: 9
last digit: 2
9 2
9 2
lastDigit = number % 10;
To find the last digit of a number, we use modulo operator %. When modulo divided by 10 returns last digit of the input number.,Enter number = 12345 first digit = 1 and last digit = 5,To find the first digit of a number we divide the given number by 10 until the number is greater than 10. In the end, we get the first digit.,In this blog post, we learn how to write a C Program to find the first and last digit of a number?. We will write the C Program to find the first digit and last digit of a number using the Arithmetic Operators. Here we will find first and the last digit of a number using the loop and without using the loop. Let see an example,
In this blog post, we learn how to write a C Program to find the first and last digit of a number?. We will write the C Program to find the first digit and last digit of a number using the Arithmetic Operators. Here we will find first and the last digit of a number using the loop and without using the loop. Let see an example,
Input: 12345
Output:
First digit => 1
last digit => 5
- Ask the user to enter an integer number. Suppose n = 12345, where n is an integer variable.
int n = 12345;
- To find the last digit of a number, we use modulo operator %. When modulo divided by 10 returns last digit of the input number.
lastDigit = num % 10
Count total number of times each alphabet appears in the string java program code with example,Java 8 new features : Lambda expressions , optional class , Defender methods with examples
Input Number: 123456
First Digit: 1, Last Digit: 6
Input Number: 8041989
First Digit: 8, Last Digit: 9
import java.util.Scanner;
public class JavaHungry {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Given number is: " + number);
int firstDigit = 0;
int lastDigit = 0;
/*Below is the way to calculate the
Last Digit of a Number*/
lastDigit = number % 10;
System.out.println("Last Digit is: " + lastDigit);
/*Below is the way to calculate the
First Digit of a Number*/
while (number != 0) {
firstDigit = number % 10;
number = number / 10;
}
System.out.println("First Digit is: " + firstDigit);
}
}
import java.util.Scanner;
public class JavaHungry {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Given number is: " + number);
int firstDigit = 0;
int lastDigit = 0;
/*Below is the way to calculate the
Last Digit of a Number*/
lastDigit = number % 10;
System.out.println("Last Digit is: " + lastDigit);
/*Below is the way to calculate the
First Digit of a Number*/
int totalDigits = (int) Math.log10(number);
firstDigit = (int)(number / (int) Math.pow(10, totalDigits));
System.out.println("First Digit is: " + firstDigit);
}
}
import java.util.Scanner;
public class JavaHungry {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Given number is: " + number);
int firstDigit = 0;
int lastDigit = 0;
int digitCount = 0;
/*Below is the way to calculate the
Last Digit of a Number*/
lastDigit = number % 10;
System.out.println("Last Digit is: " + lastDigit);
/*Below is the way to calculate the
First Digit of a Number*/
int num = number;
while (num != 0) {
digitCount++;
num = num / 10;
}
firstDigit = (int) number / (int) Math.pow(10, digitCount - 1);
System.out.println("First Digit is: " + firstDigit);
}
}