prime number program in javascript

  • JavaScript prime number program
  • In this example will learn about how to find the number is prime or not.
  • WHATS IS PRIME NUMBER
  • A number greater than 1 with exactly two factors is called prime number.
  • Number which is divisible by 1 and itself is prime number. If it is divisible by any other number(other than 1 and itself) then it's not a prime number.
    • Consider an example of number 5 ,Which has only two factors  1 and 5. This means 5 is a prime number. Lets take one more example Number 6, which has more than two factors, i.e 1,2,3. This means 6 is not a prime number.
    • The first ten prime numbers are 2,3,5,7,11,13,17,19,23,29.
    • Note: It should be noted that 1 is non-prime number, because in the above defination already mentioned A number greater than 1 with 2 factors called prime number..

  • Here we have simple program to print the prime numbers in java script.
JavaScript program to check a number is prime number or not.

  1. <html>
  2.     <head>
  3. <script>
  4.     // javascript program to check the number is prime or not.
  5.     // prime number program in javascript
  6.     // javascript prime number program
  7.     // take input from the user
  8. const x = parseInt(prompt("Enter a number to check prime or not: "));
  9. let isPrimeNumber=true;

  10. // check if number is equal to 1
  11. if (x  === 1) {
  12.     alert("1 is neither prime nor composite number.");
  13. }

  14. // checking  if  number is greater than one or not

  15. else if (x  > 1) {

  16.     // iterating from 2 to number -1 (leaving 1 and itself )
  17.     for (let i = 2; i < x ; i++) {
  18.         if (x  % i == 0) {
  19.             isPrimeNumber = false;
  20.             break;
  21.         }
  22.     }

  23.     if (isPrimeNumber) {
  24.         alert(`${x } is a prime number`);
  25.     } else {
  26.         alert(`${x } is not a prime number`);
  27.     }
  28. }

  29. // check if number is less than 1
  30. else {
  31.     console.log("The number is not a prime number.");
  32. }
  33. </script>
  34.     </head>
  35.     <body>

  36.     </body>
  37. </html>




Output:
javascript prime number
javascript prime number program


Python range function recursive


Recursion:
  • The process of calling a function by itself is called recursion and the function which calls itself is called recursive function.
  • Recursion is used to solve various mathematical problems by dividing it into smaller problems. This method of solving a problem is called Divide and Conquer. In programming, it is used to divide complex problem into simpler ones and solving them individually.
  • In order to prevent infinite recursive call,we need to define proper exit condition in a recursive function.



  • Python program to show recursive function

    • n=4
    • def  a(n):
    •  if n==0:
    •     return 0
    •  else:
    • print(n)
    • Return a(n-1)


    • Python range for float numbers
    • Limitation of  python’s range() function
    • The main limitation of Python’s range() is it works only with integers. Python range() doesn’t support the float type i.e., we cannot use floating-point or non-integer number in any of its argument.
    • For example,  Let’s see the following source code
    • for num in range(0, 5.5, 0.1):
    •   print (num)
    • If you try to execute above code Python will raise a TypeError: ‘float’ object cannot be interpreted as an integer.


    • Now, Let see how to generate the range for float numbers? There are various ways to do this Let see one by one with the examples.
    • Use numpy’s arange() function to generate the range for float numbers in Python.
    • We can use the numpy module of Python programming language to get the range of floating-point numbers.

    • NumPy library has various numeric functions and mathematical functions to operate on multi-dimensional arrays and matrices.
    • NumPy has the arange() function to get the range of a floating-point number.
    • arange() function has the same syntax and functionality as python range() function.
    • Additionally, it also supports floating-point numbers in any of its argument.
    • i.e., we can pass float number in the start, stop and step arguments.

    Syntax of numpy’s arange() function: –
    • arange (start, stop, step)
    • Let demonstrate the use with the following example.































    Else statement in python with example program

    Else Statement in Python:


    • The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement.
    • We can use the else statement with if statement to execute a block of code when the condition is false

    if (condition):

        # Executes this block if

        # condition is true

    else:

        # Executes this block if

        # condition is false


    #1: Python program to illustrate If else statement
    1.  i = 2;
    2. if i< 1:
    3.     print ("i is smaller than 1")
    4.     print ("i'm in if Block")
    5. else:
    6.     print ("i is greater than 1")
    7.     print ("i'm in else Block")
    8. print ("i'm not in if and not in else Block")

    Output:
    1. I is greater than 1
    2. I'm in else Block
    3. I'm not in if and not in else Block



    Fibonacci series in c without recursion

    • In Fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. For example Fibonacci series is 0, 1, 1, 2, 3, 5, 8,13, 21
    • C Program to Display Fibonacci Sequence 
    • Fibonacci series means next number will be generated as sum of previous two numbers.

    #1: C example program on Fibonacci series

    1. #include <stdio.h>

    2. void main()
    3. {
    4.     int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;

    5.     printf("Enter the limit to generate the Fibonacci Series \n");
    6.     scanf("%d", &limit);
    7.     printf("Fibonacci Series is ...\n");
    8.     printf("%d\n", fib1);
    9.     printf("%d\n", fib2);
    10.     count = 2;
    11.     while (count < limit)
    12.     {
    13.         fib3 = fib1 + fib2;
    14.         count++;
    15.         printf("%d\n", fib3);
    16.         fib1 = fib2;
    17.         fib2 = fib3;
    18.     }
    19. }
    Output:
    1. Enter the limit to generate the Fibonacci Series
    2. 20
    3. Fibonacci Series is ...
    4. 0
    5. 1
    6. 1
    7. 2
    8. 3
    9. 5
    10. 8
    11. 13
    12. 21
    13. 34
    14. 55
    15. 89
    16. 144
    17. 233
    18. 377
    19. 610
    20. 987
    21. 1597
    22. 2584
    23. 4181


    If statement in python example program

    Decision Making In Python (IF):


    • As we lead our life we have to take decisions. Similarly, as we make progress in programming and try to implement complicated logics, our program has to take decisions. But how? Well the answer is below
    • if statement
    • If statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.


     If condition:
    Statement 1
    Statement 2
    Statement 3
    ………………..
    …………………
    ………………..
    Statement n

    • Here the condition is evaluated in terms of boolean values i.e. either the evaluation of condition results 1 or 0 i.e.. True(1) or False(0) 
    • If the condition evaluates to True(1) then the block of statements below the if statement gets executed 



    #1: Python example program to illustrate if statement

    1. #Python program to illustrate if statement
    2. a=21
    3. If a<21:
    4. print(“if-1 statement is executed successfully”)
    5. If a>18:
    6. print(“if-2 statement is executed successfully”)

    7. Output:
    8. if-2 statement is executed successfully

    Output:
    1. if-2 statement is executed successfully


    Prime number program in c using for loop

    Prime number program in C
    • A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole number that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. 
    • Numbers that have more than two factors are called composite numbers.

    #1:  Prime number program in c using for loop
    1. #include <stdio.h>
    2. void main()
    3. {
    4.   int n1, n2,j, flag;
    5.   long int  i;
    6.   printf("Enter First  numbers(intevals): ");
    7.   scanf("%d", &n1);
    8.   printf("Enter Second  numbers(intevals): ");
    9.   scanf("%d", &n2);
    10.   printf("Prime numbers between %d and %d are: ", n1, n2);
    11.   for(i=n1+1; i<n2; ++i)
    12.   {
    13.       flag=0;
    14.       for(j=2; j<=i/2; ++j)
    15.       {
    16.         if(i%j==0)
    17.         {
    18.           flag=1;
    19.           break;
    20.         }
    21.       }
    22.       if(flag==0)
    23.         printf("%ld ",i);
    24.   }
    25. }


    Output:
    1. Enter First  numbers(intevals): 1
    2. Enter Second  numbers(intevals): 500
    3. Prime numbers between 1 and 500 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
    4. 71 73 79 83 89 97 101 103 107
    5. 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
    6. 233 239 241 251 257 263 269 271
    7. 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
    8. 419 421 431 433 439 443 449 457
    9. 461 463 467 479 487 491 499
    10. Process returned 500 (0x1F4)   execution time : 4.833 s
    11. Press any key to continue.


    prime numbers program


    Select Menu