{"id":6206,"date":"2025-05-30T15:26:15","date_gmt":"2025-05-30T09:56:15","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6206"},"modified":"2025-05-30T15:40:24","modified_gmt":"2025-05-30T10:10:24","slug":"function-to-check-voting-eligibility-by-age-in-javascript-c-c-java-and-python","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/function-to-check-voting-eligibility-by-age-in-javascript-c-c-java-and-python\/","title":{"rendered":"Function to Check Voting Eligibility by Age in JavaScript, C++, C, Java, and Python"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n\n  <h2>Question<\/h2>\n  <p>Write a function that accepts a person&#8217;s age and prints whether the person is:<\/p>\n  <ul>\n    <li><strong>&#8220;Invalid input&#8221;<\/strong> if the age is less than 1.<\/li>\n    <li><strong>&#8220;Not eligible to vote&#8221;<\/strong> if the age is less than 18.<\/li>\n    <li><strong>&#8220;Eligible to vote&#8221;<\/strong> if the age is 18 or above.<\/li>\n  <\/ul>\n  <p>Call the function with different test values: <code>18<\/code>, <code>0<\/code>, and <code>8<\/code>.<\/p>\n\n  <h2>Approach<\/h2>\n  <p>To solve this problem, follow these steps:<\/p>\n  <ul>\n    <li>Accept the input age in the function.<\/li>\n    <li>Check if the age is less than 1; if yes, print &#8220;Invalid input&#8221;.<\/li>\n    <li>If the age is valid but less than 18, print &#8220;Not eligible to vote&#8221;.<\/li>\n    <li>If the age is 18 or more, print &#8220;Eligible to vote&#8221;.<\/li>\n    <li>Test the function with various inputs to ensure all cases work correctly.<\/li>\n  <\/ul>\n\n  <h2>Examples<\/h2>\n  <ul>\n    <li><code>eligibleToVote(18)<\/code> prints <em>Eligible to vote<\/em><\/li>\n    <li><code>eligibleToVote(0)<\/code> prints <em>Invalid input<\/em><\/li>\n    <li><code>eligibleToVote(8)<\/code> prints <em>Not eligible to vote<\/em><\/li>\n  <\/ul>\n\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nfunction eligibleToVote(age) {\n  if (age < 1) {\n    console.log(\"Invalid input\");\n  } else if (age < 18) {\n    console.log(\"Not eligible to vote\");\n  } else {\n    console.log(\"Eligible to vote\");\n  }\n}\n\neligibleToVote(18);  \/\/ Eligible to vote\neligibleToVote(0);   \/\/ Invalid input\neligibleToVote(8);   \/\/ Not eligible to vote\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\nusing namespace std;\n\nvoid eligibleToVote(int age) {\n  if (age &lt; 1) {\n    cout &lt;&lt; \"Invalid input\" &lt;&lt; endl;\n  } else if (age &lt; 18) {\n    cout &lt;&lt; \"Not eligible to vote\" &lt;&lt; endl;\n  } else {\n    cout &lt;&lt; \"Eligible to vote\" &lt;&lt; endl;\n  }\n}\n\nint main() {\n  eligibleToVote(18);  \/\/ Eligible to vote\n  eligibleToVote(0);   \/\/ Invalid input\n  eligibleToVote(8);   \/\/ Not eligible to vote\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n\nvoid eligibleToVote(int age) {\n  if (age &lt; 1) {\n    printf(\"Invalid input\\n\");\n  } else if (age &lt; 18) {\n    printf(\"Not eligible to vote\\n\");\n  } else {\n    printf(\"Eligible to vote\\n\");\n  }\n}\n\nint main() {\n  eligibleToVote(18);  \/\/ Eligible to vote\n  eligibleToVote(0);   \/\/ Invalid input\n  eligibleToVote(8);   \/\/ Not eligible to vote\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Main {\n  public static void eligibleToVote(int age) {\n    if (age &lt; 1) {\n      System.out.println(\"Invalid input\");\n    } else if (age &lt; 18) {\n      System.out.println(\"Not eligible to vote\");\n    } else {\n      System.out.println(\"Eligible to vote\");\n    }\n  }\n\n  public static void main(String[] args) {\n    eligibleToVote(18);  \/\/ Eligible to vote\n    eligibleToVote(0);   \/\/ Invalid input\n    eligibleToVote(8);   \/\/ Not eligible to vote\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\ndef eligible_to_vote(age):\n    if age &lt; 1:\n        print(\"Invalid input\")\n    elif age &lt; 18:\n        print(\"Not eligible to vote\")\n    else:\n        print(\"Eligible to vote\")\n\neligible_to_vote(18)  # Eligible to vote\neligible_to_vote(0)   # Invalid input\neligible_to_vote(8)   # Not eligible to vote\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\n  document.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n\n        buttons.forEach(btn => btn.classList.remove('active'));\n        button.classList.add('active');\n\n        contents.forEach(content => {\n          content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n        });\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Question Write a function that accepts a person&#8217;s age and prints whether the person is: &#8220;Invalid input&#8221; if the age is less than 1. &#8220;Not eligible to vote&#8221; if the age is less than 18. &#8220;Eligible to vote&#8221; if the age is 18 or above. Call the function with different test values: 18, 0, and<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":{"0":"post-6206","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-c-plus-plus","7":"category-data-structures-and-algorithms","8":"category-dsa","9":"category-java","10":"category-javascript","11":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6206"}],"version-history":[{"count":3,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6206\/revisions"}],"predecessor-version":[{"id":6211,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6206\/revisions\/6211"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}