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++
#include <bits/stdc++.h>
using namespace std;
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);
}
int main()
{
char s[] = "100" ;
int n = strlen (s);
FindExponent(s, n);
}
|
Java
import java.util.*;
class GFG {
static void FindExponent(String s, int n)
{
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--)
;
c = s.indexOf( '.' );
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);
}
}
public static void main(String[] args)
{
String s = "100" ;
int n = s.length();
FindExponent(s, n);
}
}
|
Python3
def FindExponent(s, n):
res = []
i = 0
while (s[i] in '.0' ):
i + = 1
j = n - 1
while (s[j] in '.0' ):
j - = 1
if '.' in s:
c = s.index( '.' )
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))
s = "100" ;
n = len (s)
FindExponent( list (s), n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void FindExponent( string 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 = s.IndexOf( '.' );
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);
}
}
public static void Main( string [] args)
{
string s = "100" ;
int n = s.Length;
FindExponent(s, n);
}
}
|
Javascript
function FindExponent(s, n){
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--);
c = s.findIndex( function (element) {
return element == '.' ;
});
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( '' ));
}
{
let s = "100" ;
let n = s.length;
FindExponent(s.split( '' ), n);
}
|
Output:
1E2
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 geeks!