Algorithm
Problem Name: beecrowd | 1958
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1958
Scientific Notation
By M.C. Pinto, UNILA Brazil
Timelimit: 1
Floating point numbers can be very long to show. In these cases, it is convenient to use the scientific notation.
You must write a program that, given a floating point number, shows this number in scientific notation: always show the mantissa sign; always show the mantissa with 4 decimal places; use the character 'E' between the mantissa and the exponent; always show the exponent sign; and show the exponent with at least 2 digits.
Input
The input is a double precision floating point number X (according to the IEEE 754-2008 standard). There will never be a number with more than 110 characters long and more than 6 decimal places.
Output
The output is the number X in a single line using the scientific notation detailed above. See the examples below.
Input Samples | Output Samples |
3.141592 |
+3.1416E+00 |
1.618033 |
+1.6180E+00 |
602214085774747474747474 |
+6.0221E+23 |
-0.000027 |
-2.7000E-05 |
-100000000000000000000000000000000000000000000000000000000000000000000000000000000 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
long double n;
char ar[111];
cin >> n;
sprintf(ar,"%LE",n);
if(ar[0]!='-') cout <<"+";
printf("%.4LE\n",n);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
var Nstring = prompt();
var Nfull = parseFloat(Nstring);
N = Nfull.toExponential(4);
N = N.toString().replace(/e/, "E");
if (Nfull >= 0) {
N = "+" + N;
}
var exp = N.slice(9);
if (exp < 10) {
N = N.substring(0, N.length - 1) + "0" + N.substring(N.length - 1, N.length);
}
if (Nstring == "-0") {
console.log("-0.0000E+00");
} else {
console.log(N);
}
Copy The Code &
Try With Live Editor
Input
Output