The Wayback Machine - https://web.archive.org/web/20250116084612/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

Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.

And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg