Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate numbers with loop in javascript

I am working with javascript, i have static value(15000) and i want
15000*1.5/100+15000 with 90 times,i mean after getting result i want to again calculate that value(result/output,for example 225) 90 times,How can i do this ?

<script>
const cars ="15000";
for (let i = 0; i < 90; i++) {
   cars = cars*1.5/100+15000;
   cars --;
 }
 </script>

In c++ i did with following code but now i want to convert to javascript code

#include <iostream>

int main() {
    float a,b,c;
    std::cin>>b;
    for(int i=0;i<90;i++)
    {
        a=b*1.5/100;
        c=a+b;
        std::cout<<c<<"\n";
        b=c;
    }
    std::cout << "Hello world!"<<c;

    return 0;
}

My expected output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

15000
15225
15453.4
15685.2
15920.5
16159.3
16401.6
...

>Solution :

You can do something like this:

let cars = 15000;
for (let i = 0; i < 90; i++) {
  console.log(parseFloat(cars.toFixed(1))); // set 1 decimal and print it
  cars = (cars * 1.5) / 100 + cars; // same function as c++
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading