Syntax :-
while(condition){ // while Statement(s); };
Q :- Write a JavaScript program to print natural numbers using while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>Natural Numbers</title> </head> <body> <script type="text/javascript"> var c=0, n=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(c<n){ c=c+1; document.write(c+" "); } </script> </body> </html>
Output :-
N : 5 1 2 3 4 5
Q :- Write a javascript program to print even number with the help of while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>Even Numbers</title> </head> <body> <script type="text/javascript"> var c=0, n=0 , e=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(c<n){ c=c+1; e=2*c; document.write(e+" "); } </script> </body> </html>
Output :-
N : 5 2 4 6 8 10
Q :- Write a javascript program to print odd number with the help of while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>Odd Numbers</title> </head> <body> <script type="text/javascript"> var c=0, n=0 , o=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(c<n){ c=c+1; o=(2*c)-1; document.write(o+" "); } </script> </body> </html>
Output :-
N : 5 1 3 5 7 9
Q :- Write a JavaScript program to print the natural numbers in reverse order using while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>Natural Numbers in reverse order</title> </head> <body> <script type="text/javascript"> var n=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(n>0){ document.write(n+" "); n=n-1; } </script> </body> </html>
Output :-
N : 5 5 4 3 2 1
Q : Write a JavaScript program to print the even numbers in reverse order using while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>even numbers in reverse order</title> </head> <body> <script type="text/javascript"> var n=0,e=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(n>0){ e=2*n; document.write(e+" "); n=n-1; } </script> </body> </html>
Output :-
N : 5 10 8 6 4 2
Q : Write a JavaScript program to print the odd numbers in reverse order using while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>odd numbers in reverse order</title> </head> <body> <script type="text/javascript"> var n=0,o=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(n>0){ o=(2*n)-1; document.write(o+" "); n=n-1; } </script> </body> </html>
Output :-
N : 5 9 7 5 3 1
Q :- Write a JavaScript program to print and sum a square series of natural numbers using while loop.
Source Code :
<!DOCTYPE html> <html> <head> <title>square series of natural numbers</title> </head> <body> <script type="text/javascript"> var c=0, n=0, p=0, s=0; n=window.prompt("Enter a Value",5); n=parseInt(n); document.write("N : "+n+"<br>"); while(c<n){ c=c+1; p=c*c; s=s+p; document.write(p+" "); } document.write("<br>"+"Sum = "+s); </script> </body> </html>
Output :-
N : 5 1 4 9 16 25 Sum = 55
Post a Comment
If you have any doubts,Please let me know