Program to print First and Last Digit of a Number using While loop
- Write a program to print First and Last Digit of a Number using While loop in C
- Write a program to print First and Last Digit of a Number using While loop in C++
- Write a program to print First and Last Digit of a Number using While loop in Python
- Write a program to print First and Last Digit of a Number using While loop in PHP
- Write a program to print First and Last Digit of a Number using While loop in Java
- Write a program to print First and Last Digit of a Number using While loop in Java Script
- Write a program to print First and Last Digit of a Number using While loop in C#
Explanation:
Use the following reasoning to use a for loop to print a number’s first and last digits:
Last Digit: The modulo operation (n % 10) can be used to obtain the number’s last digit.
First Digit: To obtain the first digit, divide the number by 10 several times until there is just one digit left.
Steps:
- Take input for the number (n).
- Last digit can be obtained by n % 10.
- First digit is obtained by dividing the number by 10 repeatedly until it is less than 10.
- Print both digits.
Program to print First and Last Digit of a Number using While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int num, first_digit, last_digit;
printf("Enter a number: ");
scanf("%d", &num);
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Find last digit
last_digit = num % 10;
// Find first digit
first_digit = num;
while (first_digit >= 10) {
first_digit /= 10;
}
printf("First digit: %d, Last digit: %d\n", first_digit, last_digit);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num, first_digit, last_digit;
cout << "Enter a number: ";
cin >> num;
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Find last digit
last_digit = num % 10;
// Find first digit
first_digit = num;
while (first_digit >= 10) {
first_digit /= 10;
}
cout << "First digit: " << first_digit << ", Last digit: " << last_digit << endl;
return 0;
}
num = int(input("Enter a number: "))
# Handle negative numbers
if num < 0:
num = -num
# Find last digit
last_digit = num % 10
# Find first digit
first_digit = num
while first_digit >= 10:
first_digit //= 10
print(f"First digit: {first_digit}, Last digit: {last_digit}")
<?php
$num = (int)readline("Enter a number: ");
// Handle negative numbers
if ($num < 0) {
$num = -$num;
}
// Find last digit
$last_digit = $num % 10;
// Find first digit
$first_digit = $num;
while ($first_digit >= 10) {
$first_digit = (int)($first_digit / 10);
}
echo "First digit: $first_digit, Last digit: $last_digit\n";
?>
import java.util.Scanner;
public class FirstLastDigit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Find last digit
int last_digit = num % 10;
// Find first digit
int first_digit = num;
while (first_digit >= 10) {
first_digit /= 10;
}
System.out.println("First digit: " + first_digit + ", Last digit: " + last_digit);
sc.close();
}
}
let num = parseInt(prompt("Enter a number: "));
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Find last digit
let last_digit = num % 10;
// Find first digit
let first_digit = num;
while (first_digit >= 10) {
first_digit = Math.floor(first_digit / 10);
}
console.log(`First digit: ${first_digit}, Last digit: ${last_digit}`);
using System;
class Program {
static void Main() {
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Find last digit
int last_digit = num % 10;
// Find first digit
int first_digit = num;
while (first_digit >= 10) {
first_digit /= 10;
}
Console.WriteLine($"First digit: {first_digit}, Last digit: {last_digit}");
}
}