## Posts

Showing posts from May, 2022

### Different way to calculate prime number in c++

Three different ways to calculate the prime number in c++=> In this blog, we will see how to calculate given number is prime or not in o(N) time.  here we will see three different methods to check whether the given number is prime or not. Method 1=>  Run loop from 2 to n-1  we will run a loop from 2 to n-1 and if the number is divided by any number from 2 to n-1 we will return false that number is not a prime number otherwise we will return true. Time complexity => O(N) Method 2=> Run loop from 2 to n/2  we will run a loop from 2 to n/2 and if the number is divided by any number from 2 to n/2 we will return false that number is not a prime number otherwise we will return true. Time complexity => O(N/2) => O(N). This program will be faster than method 1 Method 3=>  Run a loop from 2 to root  √n   we will run a loop from 2 to  √n  and if the number is divided by any number from 2 to  √n   we will return false that number is not a prime number otherwise we will return

### Reverse a number in c++ || C++ Program to Reverse a Number

Reverse a number in c++ || C++ Program to Reverse a Number->    Write a program to reverse the digits of an integer. Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10 (3) Return rev_num