For Loop Programs – JavaScript

Table of Contents

Add all digits in a Number

10

				
					<script>
a="1234";
g=a;
b=a.length;
d=0;
for(i=1;i<=b;i++)
{c=a%10;
a=Math.floor(a/10);
d=d+c;
}
document.write(d);
</script>
				
			

Armstrong

armstrong

				
					<script>
a="153";
g=a;
b=a.length;
d=0;
for(i=1;i<=b;i++)
{c=a%10;
a=Math.floor(a/10);
d=d+(c*c*c);
}
if(g==d)
{document.write("armstrong");
}
else{
    document.write(" not armstrong");
}
</script>
				
			

Break

2

4

6

				
					<script type="text/javascript">
var a=2;

for(i=1;i<10;i++){

    document.write(a*i+"<br>");
    if(i==3){
        break;
    }

}
</script>
				
			

Reverse a Number

4321

				
					<script>
a="1234";
g=a;
b=a.length;
d=0;
for(i=1;i<=b;i++)
{c=a%10;
a=Math.floor(a/10);
d=d*10+c;
}
document.write(d);
</script>
				
			

Take Your Free Live Demo Class Now

You May Also Like

Enter Your DETAILS BELOW

To Get Complete Information or We Will Call You

We Will Send You the Course Fees Details In Below Number

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

Enter Your DETAILS BELOW TO take

free demo class