Looping Programs

1) Prime Number i)Finding whether the given number is prime or not: let num = 7; let isPrime = true; for (let i = 2; i < num; i++) { if (num % i === 0) { isPrime = false; break; } } if (num >...

By · · 1 min read
Looping Programs

Source: DEV Community

1) Prime Number i)Finding whether the given number is prime or not: let num = 7; let isPrime = true; for (let i = 2; i < num; i++) { if (num % i === 0) { isPrime = false; break; } } if (num > 1 && isPrime) { console.log("Prime"); } else { console.log("Not Prime"); } Output: Prime ii)Printing all the prime numbers within the 2-50: for (let num = 2; num <= 50; num++) { let isPrime = true; for (let i = 2; i < num; i++) { if (num % i === 0) { isPrime = false; break; } } if (isPrime) { console.log(num); } } Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 2) Reverse Printing a Number let num=1234; let reverse=0; while(num>0){ let digit=num%10; reverse=reverse*10+digit; num=Math.floor(num/10); } document.write("Reverse: "+reverse); Output: Reverse: 4321 3) Count of Digits let num=1234; let count=0; while(num>0){ let digit=num%10; num=Math.floor(num/10); count++; } document.write("Count of digits: "+count); Output: Count of digits: 4 4) Sum of Digits let num = 123; l

Related Posts

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#ai (4205)#news (2446)#webdev (1828)#programming (1364)#business (1152)#opensource (1072)#security (1006)#productivity (936)#/business (821)#javascript (800)

Around the Network