Design an algorithm and also write a program that reads an integer
number and determines whether first digit and last digit are same
Or not
Answers
Explanation:
subscribe on youtube this channel I will follow you on brainly and I will mark u brainliest ( u should send screenshot ) name of channel - cutemellows
Answer:
u have to say that in which language do u want the program.
if it is in c++
// Program to find first and last
// digits of a number
#include <bits/stdc++.h>
using namespace std;
// Find the first digit
int firstDigit(int n)
{
// Remove last digit from number
// till only one digit is left
while (n >= 10)
n /= 10;
// return the first digit
return n;
}
// Find the last digit
int lastDigit(int n)
{
// return the last digit
return (n % 10);
}
// Driver program
int main()
{
int n = 98562;
cout << firstDigit(n) << " "
<< lastDigit(n) << endl;
return 0;
}
Output:
9 2
Explanation:
if it is in java
// Java Program to find first and last
// digits of a number
import java.util.*;
import java.lang.*;
public class GfG{
// Find the first digit
public static int firstDigit(int n)
{
// Remove last digit from number
// till only one digit is left
while (n >= 10)
n /= 10;
// return the first digit
return n;
}
// Find the last digit
public static int lastDigit(int n)
{
// return the last digit
return (n % 10);
}
// driver function
public static void main(String argc[])
{
int n = 98562;
System.out.println(firstDigit(n) + " "
+ lastDigit(n));
}
}
if it is in python
# Python3 program to find first and
# last digits of a number
# Find the first digit
def firstDigit(n) :
# Remove last digit from number
# till only one digit is left
while n >= 10:
n = n / 10;
# return the first digit
return int(n)
# Find the last digit
def lastDigit(n) :
# return the last digit
return (n % 10)
# Driver Code
n = 98562;
print(firstDigit(n), end = " ")
print(lastDigit(n))
if it is in c language
// C# Program to find first and last
// digits of a number
using System;
public class GfG{
// Find the first digit
public static int firstDigit(int n)
{
// Remove last digit from number
// till only one digit is left
while (n >= 10)
n /= 10;
// return the first digit
return n;
}
// Find the last digit
public static int lastDigit(int n)
{
// return the last digit
return (n % 10);
}
// driver function
public static void Main()
{
int n = 98562;
Console.WriteLine(firstDigit(n) + " "
+ lastDigit(n));
}
}
if it is in php language
<?php
// PHP Program to find first
// and last digits of a number
// Find the first digit
function firstDigit($n)
{
// Remove last digit from number
// till only one digit is left
while ($n >= 10)
$n /= 10;
// return the first digit
return (int)$n;
}
// Find the last digit
function lastDigit($n)
{
// return the last digit
return ((int)$n % 10);
}
// Driver Code
$n = 98562;
echo firstDigit($n) . " " .
lastDigit($n) . "\n";