Program to print First and Last Digit of a Number using For loop
- Write a program to print First and Last Digit of a Number using For loop in C
- Write a program to print First and Last Digit of a Number using For loop in C++
- Write a program to print First and Last Digit of a Number using For loop in Python
- Write a program to print First and Last Digit of a Number using For loop in PHP
- Write a program to print First and Last Digit of a Number using For loop in Java
- Write a program to print First and Last Digit of a Number using For loop in Java Script
- Write a program to print First and Last Digit of a Number using For 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 For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int num, lastDigit, firstDigit;
printf("Enter a number: ");
scanf("%d", &num);
// Get last digit
lastDigit = num % 10;
// Loop to get the first digit
for (firstDigit = num; firstDigit >= 10; firstDigit /= 10);
printf("First digit: %d\n", firstDigit);
printf("Last digit: %d\n", lastDigit);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num, lastDigit, firstDigit;
cout << "Enter a number: ";
cin >> num;
// Get last digit
lastDigit = num % 10;
// Loop to get the first digit
for (firstDigit = num; firstDigit >= 10; firstDigit /= 10);
cout << "First digit: " << firstDigit << endl;
cout << "Last digit: " << lastDigit << endl;
return 0;
}
num = int(input("Enter a number: "))
# Get last digit
last_digit = num % 10
# Loop to get the first digit
while num >= 10:
num //= 10
print("First digit:", num)
print("Last digit:", last_digit)
<?php
$num = (int)readline("Enter a number: ");
// Get last digit
$last_digit = $num % 10;
// Loop to get the first digit
while ($num >= 10) {
$num = (int)($num / 10);
}
echo "First digit: $num\n";
echo "Last digit: $last_digit\n";
?>
import java.util.Scanner;
public class FirstLastDigit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
// Get last digit
int lastDigit = num % 10;
// Loop to get the first digit
while (num >= 10) {
num /= 10;
}
System.out.println("First digit: " + num);
System.out.println("Last digit: " + lastDigit);
scanner.close();
}
}
let num = parseInt(prompt("Enter a number:"));
// Get last digit
let lastDigit = num % 10;
// Loop to get the first digit
while (num >= 10) {
num = Math.floor(num / 10);
}
console.log("First digit:", num);
console.log("Last digit:", lastDigit);
using System;
class Program {
static void Main() {
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
// Get last digit
int lastDigit = num % 10;
// Loop to get the first digit
while (num >= 10) {
num /= 10;
}
Console.WriteLine($"First digit: {num}");
Console.WriteLine($"Last digit: {lastDigit}");
}
}