Problem Code: COAD02
Problem link is here
Problem Name: Simple Sum
Contest Name: CodeAdda Practice Contest – 2 (CPC2016)
Author: Yatharth Oza (Username: yatharth100)
Difficulty: Easy
PREREQUISITES: Math
Problem:
Here One String is given as a input in each test case. Each String contains some Float numbers join by one character ‘e’. You just need to extract all floats and do sum of all numbers and print output which is correct up to 4 decimal places.
Explanation:
For this problem we can traverse whole input string by putting one by one character in one temporary string and when ‘e’ comes we will stop and then we can convert string to float and add in ‘Sum’ variable which stores current sum of input. At last we can just return the value of ‘Sum’. After each iteration we make temporary string null and Sum value to 0.
If you are doing your code in C then you can make approach like this. But, If you are using JAVA or any object oriented language then it provides directly “split(“e”)” function from which you can make given approach directly.
Solution:
/*Solution is in JAVA and filename is Main.java */
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
float sum=0,temp;
long t;
Scanner in=new Scanner(System.in);
t=in.nextLong();
while(--t>=0)
{
sum=0;
String str = in.next();
String[] no = str.split("e");
for(int i=0;i<no.length;i++)
{
temp=Float.parseFloat(no[i]);
sum=sum+temp;
}
System.out.printf("%.4f\n", sum);
}
}
}