0% found this document useful (0 votes)
16 views

GFG Dsa

dsa

Uploaded by

suvejah18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

GFG Dsa

dsa

Uploaded by

suvejah18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Count Digits

Given a number N, the task is to return the count of digits in this number.
Example:

Program to count digits in an integer


Simple Iterative Solution to count digits in an integer
The integer entered by the user is stored in the variable n. Then the while loop is iterated until the test
expression n != 0 is evaluated to 0 (false). We will consider 3456 as the input integer.
1. After the first iteration, the value of n will be updated to 345 and the count is incremented to 1.
2. After the second iteration, the value of n will be updated to 34 and the count is incremented to
2.
3. After the third iteration, the value of n will be updated to 3 and the count is incremented to 3.
4. In the fourth iteration, the value of n will be updated to zero and the count will be incremented
to 4.
5. Then the test expression is evaluated ( n!=0 ) as false and the loop terminates with final count as
4.
Below is the implementation of the above approach:
C++Java
// JAVA Code to count number of
// digits in an integer
import java.util.*;

class GFG {

static int countDigit(long n)


{
if (n/10 == 0)
return 1;
return 1 + countDigit(n / 10);
}

/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}

Output
Number of digits : 9
Time Complexity : O(log10(n)) or θ(num digits)
Auxiliary Space: O(1) or constant

Palindrome Numbers

Given an integer, write a function that returns true if the given number is palindrome, else false. For
example, 12321 is palindrome, but 1451 is not palindrome.
A simple approach to check if a number is Palindrome or not . This approach can be used when the
number of digits in the given number is less than 10^18 because if the number of digits of that number
exceeds 10^18, we can’t take that number as an integer since the range of long long int doesn’t satisfy
the given number.
To check whether the given number is palindrome or not we will just reverse the digits of the given
number and check if the reverse of that number is equal to the original number or not . If reverse of
number is equal to that number than the number will be Palindrome else it will not a Palindrome.
C++JavaPython3C#Javascript
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
// Java program to check if a number is Palindrome

// Function to check Palindrome


static boolean checkPalindrome(int n)
{
int reverse = 0;
int temp = n;
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
return (reverse == n); // if it is true then it will return 1;
// else if false it will return 0;
}

// Driver Code
public static void main(String args[])
{
int n = 7007;
if (checkPalindrome(n) == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}

Output
Yes
Time Complexity : O(log(n)) or O(Number of digits in a given number)
Auxiliary space : O(1) or constant

You might also like