The Wayback Machine - https://web.archive.org/web/20241005060152/https://www.geeksforgeeks.org/exponential-notation-decimal-number/
Open In App

Exponential notation of a decimal number

Last Updated : 28 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a positive decimal number, find the simple exponential notation (x = a·10^b) of the given number. Examples:

Input : 100.0
Output : 1E2
Explanation:
The exponential notation of 100.0 is 1E2.

Input :19
Output :1.9E1
Explanation:
The exponential notation of 16 is 1.6E1.

Approach: The simplest way is to find the position of the first non zero digit and the position of the dot. The difference between that positions is the value of b (if the value is positive you should also decrease it by one). Below is the implementation of the above approach: 

C++




// C++ code to find the exponential notation
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate the exponential
// notation
void FindExponent(char s[], int n)
{
    int i, j, b, c;
    for (i = 0; s[i] == '0' || s[i] == '.'; i++)
        ;
    for (j = n - 1; s[j] == '0' || s[j] == '.'; j--)
        ;
  
    c = find(s, s + n, '.') - s;
    putchar(s[i]);
  
    if (i != j)
        putchar('.');
  
    for (int k = i + 1; k <= j; k++)
        if (s[k] != '.')
            putchar(s[k]);
    if (i < c)
        b = c - i - 1;
    else
        b = c - i;
    if (b)
        printf("E%d", b);
}
  
// main function
int main()
{
    char s[] = "100";
    int n = strlen(s);
    FindExponent(s, n);
}


Java




// Java code to find the exponential notation
import java.util.*;
 
class GFG {
 
  // function to calculate the exponential
  // notation
  static void FindExponent(String s, int n)
  {
 
    // Storing the result in an array
 
    int i, j, b, c;
    for (i = 0; s.charAt(i) == '0' || s.charAt(i) == '.'; i++)
      ;
    for (j = n - 1; s.charAt(j) == '0' || s.charAt(j) == '.'; j--)
      ;
 
    // Finding the first index in s which is equal to
    // '.'
    c = s.indexOf('.');
 
    // if '.' not found then put c = n.
    if (c == -1) {
      c = n;
    }
 
    System.out.print(s.charAt(i));
 
    if (i != j) {
      System.out.print('.');
    }
 
    for (int k = i + 1; k <= j; k++) {
      if (s.charAt(k) != '.') {
        System.out.print(s.charAt(k));
      }
    }
 
    if (i < c) {
      b = c - i - 1;
    }
    else {
      b = c - i;
    }
 
    if (b > 0) {
      System.out.print("E");
      System.out.print(b);
    }
  }
 
  // main function
  public static void main(String[] args)
  {
    String s = "100";
    int n = s.length();
    FindExponent(s, n);
  }
}
 
// The code is contributed by phasing17


Python3




# Python3 code to find the exponential notation
  
# function to calculate the exponential
# notation
def FindExponent(s, n):
    # Storing the result in an array
    res = []
    i = 0
    while (s[i] in '.0'):
        i += 1
     
    j = n - 1
    while (s[j] in '.0' ):
        j -= 1
         
    # Finding the first index in s which is equal to '.'
    if '.' in s:
        c = s.index('.')
    # if '.' not found then put c = n.
    else:
        c = n
 
    res.append(s[i]);
      
    if (i != j):
        res.append('.');
          
    for k in range(i + 1, 1 + j):
        if (s[k] != '.'):
            res.append(s[k]);
  
    if (i < c):
        b = c - i - 1;
     
    else:
        b = c - i;
     
      
    if (b != 0):
        res.append("E");
        res.append(str(b));
     
    print("".join(res))
 
# main function
s = "100";
n = len(s)
FindExponent(list(s), n);
 
# The code is contributed by phasing17


C#




// C# code to find the exponential notation
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // function to calculate the exponential
  // notation
  static void FindExponent(string s, int n)
  {
 
    // Storing the result in an array
 
    int i, j, b, c;
    for (i = 0; s[i] == '0' || s[i] == '.'; i++);
    for (j = n - 1; s[j] == '0' || s[j] == '.'; j--);
 
    // Finding the first index in s which is equal to '.'
    c = s.IndexOf('.');
 
    // if '.' not found then put c = n.
    if(c == -1){
      c = n;
    }
 
    Console.Write(s[i]);
 
    if (i != j){
      Console.Write('.');
    }
 
    for (int k = i + 1; k <= j; k++){
      if (s[k] != '.'){
        Console.Write(s[k]);
      }
    }
 
    if (i < c){
      b = c - i - 1;
    }    
    else{
      b = c - i;
    }
 
    if (b > 0){
      Console.Write("E");
      Console.Write(b);
    }
  }
 
  // main function
  public static void Main(string[] args)
  {
    string s = "100";
    int n = s.Length;
    FindExponent(s, n);
  }
}
 
// The code is contributed by phasing17


Javascript




// JavaScript code to find the exponential notation
  
// function to calculate the exponential
// notation
function FindExponent(s, n){
    // Storing the result in an array
    const res = new Array();
      
    let i, j, b, c;
    for (i = 0; s[i] == '0' || s[i] == '.'; i++);
    for (j = n - 1; s[j] == '0' || s[j] == '.'; j--);
      
    // Finding the first index in s which is equal to '.'
    c = s.findIndex(function (element) {
        return element == '.';
    });
  
    // if '.' not found then put c = n.
    if(c == -1){
        c = n;
    }
      
    res.push(s[i]);
      
    if (i != j){
        res.push('.');
    }
          
    for (let k = i + 1; k <= j; k++){
        if (s[k] != '.'){
            res.push(s[k]);
        }
    }
  
    if (i < c){
        b = c - i - 1;
    }    
    else{
       b = c - i;
    }
      
    if (b){
        res.push("E");
        res.push(b);
    }
    console.log(res.join(''));
}
  
// main function
{
    let s = "100";
    let n = s.length;
    FindExponent(s.split(''), n);
}
  
// The code is contributed by Gautam goel (gautamgoel962)


Output:

1E2


Previous Article
Next Article

Similar Reads

Find the larger exponential among two exponentials
Given four integers A, B, C and D. The task is to find which is greater AB or CD. Examples: Input: A = 2, B = 5, C = 4, D = 2 Output: 2^5 25 = 32 42 = 16 Input: A = 8, B = 29, C = 60, D = 59 Output: 60^59 Naive approach: Calculate the values of AB and CD then compare them. This approach will fail when the values are greater say 562145321457. Effici
4 min read
Count of exponential paths in a Binary Tree
Given a Binary Tree, the task is to count the number of Exponential paths in the given Binary Tree. Exponential Path is a path where root to leaf path contains all nodes being equal to xy, & where x is a minimum possible positive constant & y is a variable positive integer. Example: Input: 27 / \ 9 81 / \ / \ 3 10 70 243 / \ 81 909 Output:
11 min read
Print all Exponential Levels of a Binary Tree
Given a Binary Tree, the task is to print all the Exponential Levels in the given Binary Tree. An Exponential Level is a level whose all nodes of that levels equals to xy, & where x is a minimum possible positive constant & y is a variable positive integer. Examples: Input: 20 / \ 9 81 / \ / \ 3 10 70 243 / \ 81 909 Output: 20 9 81 Explanat
15+ min read
Exponential factorial of N
Given a positive integer N, the task is to print the Exponential factorial of N. Since the output can be very large, print the answer modulus 1000000007. Examples: Input: N = 4 Output: 262144 Input: N = 3 Output: 9 Approach: The given problem can be solved based on the following observations: The exponential factorial is defined by the recurrence r
4 min read
Find all ranges of consecutive numbers from Array | Set -2 (using Exponential BackOff)
Given a sorted array arr[] consisting of N integers without any duplicates, the task is to find the ranges of consecutive numbers from that array. Examples: Input: arr[] = {1, 2, 3, 6, 7} Output: 1->3, 6->7 Explanation: There are two ranges of consecutive number from that array. Range 1 = 1 -> 3 Range 2 = 6 -> 7 Input: arr[] = {-1, 0, 1
11 min read
Exponential Squaring (Fast Modulo Multiplication)
Given two numbers base and exp, we need to compute baseexp under Modulo 10^9+7 Examples: Input : base = 2, exp = 2Output : 4Input : base = 5, exp = 100000Output : 754573817In competitions, for calculating large powers of a number we are given a modulus value(a large prime number) because as the values of [Tex]x^y [/Tex]is being calculated it can ge
12 min read
Exponential Search
The name of this searching algorithm may be misleading as it works in O(Log n) time. The name comes from the way it searches an element. Given a sorted array, and an element x to be searched, find position of x in the array. Input: arr[] = {10, 20, 40, 45, 55} x = 45 Output: Element found at index 3 Input: arr[] = {10, 15, 25, 45, 55} x = 15 Output
15+ min read
Exponential value sort
Given a sorted array arr[] and an integer P. Your task is to raise elements of arr[] to power of P and return them in sorted order in a new array. Examples:Input: arr[] = {-20, -5, 1, 2, 8}, P=2Output: 1 4 25 64 400Explanation: When we square the values of arr we get 400,25,1,4,64 which then sorted will give us the values as [1,4,25,64,400]. We hav
8 min read
Is exponential search faster than binary search?
Exponential search and binary search are two algorithms used to find a target element in a sorted array. While both algorithms have their advantages and disadvantages, exponential search is generally not considered to be faster than binary search. Time Complexity of Exponential Search:The time complexity of exponential search is O(log n), where n i
2 min read
Difference Between Exponential and Polynomial Complexities
Understanding the computational complexity of algorithms is essential in computer science, as it helps determine the feasibility and efficiency of solutions to problems. Two of the most common types of complexities are exponential and polynomial complexities. This article will provided the differences between these two, explaining their characteris
4 min read
Convert Infix To Prefix Notation
Given an infix expression, the task is to convert it to a prefix expression. Infix Expression: The expression of type a 'operator' b (a+b, where + is an operator) i.e., when the operator is between two operands. Prefix Expression: The expression of type 'operator' a b (+ab where + is an operator) i.e., when the operator is placed before the operand
12 min read
Program to convert Infix notation to Expression Tree
Given a string representing infix notation. The task is to convert it to an expression tree.Expression Tree is a binary tree where the operands are represented by leaf nodes and operators are represented by intermediate nodes. No node can have a single child. Construction of Expression tree The algorithm follows a combination of shunting yard along
12 min read
Knuth's Up-Arrow Notation For Exponentiation
Knuth's up-arrow notation, also known as Knuth's arrow notation, is a mathematical notation for exponentiation that was introduced by Donald Knuth in his book "Concrete Mathematics". It uses a sequence of up-arrows (↑) to represent exponentiation with various bases and exponents. Approach: Exponentiation depends on the number of arrows used in the
4 min read
Analysis of Algorithms | Big - Θ (Big Theta) Notation
In the analysis of algorithms, asymptotic notations are used to evaluate the performance of an algorithm, in its best cases and worst cases. This article will discuss Big - Theta notations represented by a Greek letter (Θ). Definition: Let g and f be the function from the set of natural numbers to itself. The function f is said to be Θ(g), if there
6 min read
Print all words matching a pattern in CamelCase Notation Dictionary
Given a dictionary of words where each word follows CamelCase notation, The task is to print all words in the dictionary that match with a given pattern consisting of uppercase characters only. Note: CamelCase is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. Common examples inclu
15+ min read
Top 30 Big-O Notation Interview Questions & Answers 2023
Big O notation plays a role, in computer science and software engineering as it helps us analyze the efficiency and performance of algorithms. Whether you're someone preparing for an interview or an employer evaluating a candidates knowledge having an understanding of Big O notation is vital. In this article, we'll explore the 30 interview question
12 min read
Analysis of Algorithms | Big-Omega Ω Notation
In the analysis of algorithms, asymptotic notations are used to evaluate the performance of an algorithm, in its best cases and worst cases. This article will discuss Big-Omega Notation represented by a Greek letter (Ω). Table of Content What is Big-Omega Ω Notation?Definition of Big-Omega Ω Notation?How to Determine Big-Omega Ω Notation?Example of
9 min read
Asymptotic Notation and Analysis (Based on input size) in Complexity Analysis of Algorithms
Asymptotic Analysis is defined as the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, how the time (or space) taken by an algorithm increases with the input size. Asymptotic notation is
8 min read
Given a number N in decimal base, find number of its digits in any base (base b)
Given A Number n in a base 10, find the number of digits in its base b representation. Constraints : [Tex]N \in \mathbb{W} [/Tex]Whole Examples : Input : Number = 48 Base = 4 Output: 3 Explanation : (48)10 = (300)4 Input : Number = 1446 Base = 7 Output: 4 Explanation : (446)10 = (4134)7 A simple approach: convert the decimal number into the given b
8 min read
Program to Convert BCD number into Decimal number
Given a BCD (Binary Coded Decimal) number, the task is to convert the BCD number into its equivalent Decimal number. Examples: Input: BCD = 100000101000 Output: 828 Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1000. Here, 1000 is equivalent to 8 and 0010 is equivalent to 2. So, the number becomes 828. Input: BCD = 1001000
10 min read
Program to convert a Binary Number to Hexa-Decimal Number
Given a Binary Number, the task is to convert this Binary number to its equivalent Hexa-Decimal Number.Examples: Input: 100000101111 Output: 82F Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1111. Here, 1000 is equivalent to 8, 0010 is equivalent to 2 and 1111 is equivalent to F Therefore, the number becomes 82FInput:10101
10 min read
Count number of digits after decimal on dividing a number
We are given two numbers A and B. We need to calculate the number of digits after decimal. If in case the numbers are irrational then print "INF".Examples: Input : x = 5, y = 3 Output : INF 5/3 = 1.666.... Input : x = 3, y = 6 Output : 1 3/6 = 0.5 Recommended PracticeCount number of digits after decimalTry It! The idea is simple we follow school di
5 min read
Number of substrings with odd decimal value in a binary string
Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Substrings with odd decimal representation are: {1, 1,
6 min read
Number of decimal numbers of length k, that are strict monotone
We call a decimal number strict monotone if: D[i-1] < D[i], 0 < i < |D| Write a program which takes positive number n on input and returns number of decimal numbers of length n that are strict monotone. Number can't start with 0. Examples : Input : 2 Output : 36 Numbers are 12, 13, ... 19, 23 24, ... 29, .... 89. Input : 3 Output : 84 Expl
5 min read
10's Complement of a decimal number
Given a decimal number N. The task is to find 10’s complement of the number N.Example: Input : 25 Output : 10's complement is : 75 Input : 456 Output : 10's complement is : 544 10’s complement of a decimal number can be found by adding 1 to the 9's complement of that decimal number. It is just like 2s complement in binary number representation.Math
4 min read
Sum of decimal equivalent of all possible pairs of Binary representation of a Number
Given a number N. The task is to find the sum of the decimal equivalent of all the pairs formed from the binary representation of the given number. Examples: Input: N = 4 Output: 4 Binary equivalent of 4 is 100. All possible pairs are 10, 10, 00 and their decimal equivalent are 2, 2, 0 respectively. So, 2 + 2+ 0 = 4 Input: N = 11 Output: 13 All pos
5 min read
Check if Decimal representation of an Octal number is divisible by 7
Given an Octal number N. The task is to write a program to check if the Decimal representation of the given octal number N is divisible by 7 or not. Examples: Input: N = 112 Output: NO Equivalent Decimal = 74 7410 = 7 * 10 1 + 4 * 100 1128 = 1 * 82 + 1 * 81 + 2 * 80 Input: N = 25 Output: YES Decimal Equivalent = 21 The idea is to note that, 8 % 7 w
4 min read
Number of digits before the decimal point in the division of two numbers
Given two integers a and b. The task is to find the number of digits before the decimal point in a / b.Examples: Input: a = 100, b = 4 Output: 2 100 / 4 = 25 and number of digits in 25 = 2.Input: a = 100000, b = 10 Output: 5 Naive approach: Divide the two numbers and then find the number of digits in the division. Take the absolute value of the div
6 min read
Find the Nth digit from right in base B of the given number in Decimal base
Given a number A in decimal base, the task is to find the Nth digit from last in base BExamples: Input: A = 100, N = 3, B = 4 Output: 2 Explanation: (100)4 = 1210 3rd digit from last is 2 Input: A = 50, N = 3, B = 5 Output: 2 Explanation: (50)5 = 200 3rd digit from last is 2 Naive Approach: The basic idea is to first convert the decimal number A in
7 min read
Given a number N in decimal base, find the sum of digits in any base B
Given a number N in decimal base, the task is to find the sum of digits of the number in any base B. Examples: Input: N = 100, B = 8 Output: 9 Explanation: (100)8 = 144 Sum(144) = 1 + 4 + 4 = 9 Input: N = 50, B = 2 Output: 3 Explanation: (50)2 = 110010 Sum(110010) = 1 + 1 + 0 + 0 + 1 + 0 = 3 Approach: Find unit digit by performing modulo operation
6 min read
Article Tags :
Practice Tags :