Given a number N, find the remainder when the first digit of N is divided by its last digit.
Examples:
Input: N = 1234
Output: 1
First digit = 1
Last digit = 4
Remainder = 1 % 4 = 1
Input: N = 5223
Output: 2
First digit = 5
Last digit = 3
Remainder = 5 % 3 = 2
Approach: Find the first digit and the last digit of the number. Find then the remainder when the first digit is divided by the last digit.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findRemainder( int n)
{
int l = n % 10;
while (n >= 10)
n /= 10;
int f = n;
int remainder = f % l;
cout << remainder << endl;
}
int main()
{
int n = 5223;
findRemainder(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findRemainder( int n)
{
int l = n % 10 ;
while (n >= 10 )
n /= 10 ;
int f = n;
int remainder = f % l;
System.out.println(remainder);
}
public static void main(String[] args)
{
int n = 5223 ;
findRemainder(n);
}
}
|
Python3
def findRemainder(n):
l = n % 10
while (n > = 10 ):
n / / = 10
f = n
remainder = f % l
print (remainder)
n = 5223
findRemainder(n)
|
C#
using System;
class GFG
{
static void findRemainder( int n)
{
int l = n % 10;
while (n >= 10)
n /= 10;
int f = n;
int remainder = f % l;
Console.WriteLine(remainder);
}
public static void Main()
{
int n = 5223;
findRemainder(n);
}
}
|
Javascript
<script>
function findRemainder( n)
{
let l = n % 10;
while (n >= 10)
n /= 10;
let f = n;
let remainder = f % l;
document.write(Math.floor(remainder));
}
let n = 5223;
findRemainder(n);
</script>
|
Time Complexity: O(L ) where L is length of number in decimal representation
Auxiliary Space: O(1)
Approach:
- Convert the input number to a string so that we can easily access its first and last digits.
- Extract the first and last digits of the number by accessing the first and last characters of the string.
- Convert the first digit to an integer using the ASCII code of ‘0’ and the subtraction operator.
- Convert the last digit to an integer using the same method.
- Find the remainder when the first digit is divided by the last digit using the modulo operator (%).
- Output the remainder.
Implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
void findRemainder( int n)
{
string s = to_string(n);
char first = s[0];
char last = s[s.length()-1];
int remainder = (first - '0' ) % (last - '0' );
cout << remainder << endl;
}
int main()
{
int n = 5223;
findRemainder(n);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void findRemainder( int n) {
String s = Integer.toString(n);
char first = s.charAt( 0 );
char last = s.charAt(s.length()- 1 );
int remainder = (first - '0' ) % (last - '0' );
System.out.println(remainder);
}
public static void main(String[] args) {
int n = 5223 ;
findRemainder(n);
}
}
|
Python3
def find_remainder(n):
s = str (n)
first = s[ 0 ]
last = s[ - 1 ]
remainder = int (first) % int (last)
print (remainder)
n = 5223
find_remainder(n)
|
C#
using System;
namespace RemainderFinder {
class Program {
static void FindRemainder( int n)
{
string s = n.ToString();
char first = s[0];
char last = s[s.Length - 1];
int remainder = (first - '0' ) % (last - '0' );
Console.WriteLine(remainder);
}
static void Main( string [] args)
{
int n = 5223;
FindRemainder(n);
Console.ReadLine();
}
}
}
|
Javascript
function findRemainder(n) {
let s = n.toString();
let first = s.charAt(0);
let last = s.charAt(s.length-1);
let remainder = parseInt(first) % parseInt(last);
console.log(remainder);
}
let n = 5223;
findRemainder(n);
|
Time Complexity: O(1)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!